Debian Codegraph @

src/recordings.cpp

raw
1 /*
2 * Copyright (C) 2005-2021 Team Kodi (https://kodi.tv)
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 * See LICENSE.md for more information.
6 */
7
8 #include <vector>
9 #include <stdio.h>
10
11 using namespace std;
12
13 #include "recordings.h"
14 #include "utils.h"
15 #include "timers.h"
16 #include "DateTime.h"
17
18 cRecording::cRecording() :
19 m_channelType(TvDatabase::ChannelType::Unknown)
20 {
21 m_duration = 0;
22 m_Index = -1;
23 m_cardSettings = NULL;
24 m_channelID = PVR_CHANNEL_INVALID_UID;
25 m_isRecording = false;
26 m_genre_type = 0;
27 m_genre_subtype = 0;
28 m_genretable = NULL;
29 m_scheduleID = 0;
30 m_keepUntil = 0;
31 m_timesWatched = 0;
32 m_lastPlayedPosition = 0;
33 }
34
35
36 cRecording::~cRecording()
37 {
38 }
39
40 void cRecording::SetCardSettings(CCards* cardSettings)
41 {
42 m_cardSettings = cardSettings;
43 }
44
45 bool cRecording::ParseLine(const std::string& data)
46 {
47 vector<string> fields;
48
49 Tokenize(data, fields, "|");
50
51 if( fields.size() >= 9 )
52 {
53 //[0] index / mediaportal recording id
54 //[1] start time
55 //[2] end time
56 //[3] channel name
57 //[4] title
58 //[5] description
59 //[6] stream_url (resolved hostname if requested)
60 //[7] filename (we can bypass rtsp streaming when XBMC and the TV server are on the same machine)
61 //[8] keepUntilDate (DateTime)
62 //[9] (optional) original stream_url when resolve hostnames is enabled
63 //[10] keepUntil (int)
64 //[11] episodeName (string)
65 //[12] episodeNumber (string)
66 //[13] episodePart (string)
67 //[14] seriesNumber (string)
68 //[15] scheduleID (int)
69 //[16] genre (string)
70 //[17] idchannel (int)
71 //[18] isrecording (bool)
72 //[19] timesWatched (int)
73 //[20] stopTime (int)
74 //[21] channelType (int)
75
76 m_Index = atoi(fields[0].c_str());
77
78 if ( m_startTime.SetFromDateTime(fields[1]) == false )
79 {
80 kodi::Log(ADDON_LOG_ERROR, "%s: Unable to convert start time '%s' into date+time", __FUNCTION__, fields[1].c_str());
81 return false;
82 }
83
84 if ( m_endTime.SetFromDateTime(fields[2]) == false )
85 {
86 kodi::Log(ADDON_LOG_ERROR, "%s: Unable to convert end time '%s' into date+time", __FUNCTION__, fields[2].c_str());
87 return false;
88 }
89
90 m_duration = m_endTime - m_startTime;
91
92 m_channelName = fields[3];
93 m_title = fields[4];
94 m_description = fields[5];
95 m_stream = fields[6];
96 m_filePath = fields[7];
97
98 // TODO: fill lifetime with data from MP TV Server
99 // From the VDR documentation (VDR is used by Alwinus as basis for the XBMC
100 // PVR framework:
101 // "The lifetime (int) value corresponds to the the number of days (0..99)
102 // a recording made through this timer is guaranteed to remain on disk
103 // before it is automatically removed to free up space for a new recording.
104 // Note that setting this parameter to very high values for all recordings
105 // may soon fill up the entire disk and cause new recordings to fail due to
106 // low disk space. The special value 99 means that this recording will live
107 // forever, and a value of 0 means that this recording can be deleted any
108 // time if a recording with a higher priority needs disk space."
109 if ( m_keepUntilDate.SetFromDateTime(fields[8]) == false )
110 {
111 // invalid date (or outside time_t boundaries)
112 m_keepUntilDate = MPTV::cUndefinedDate;
113 }
114
115 if( m_filePath.length() > 0 )
116 {
117 SplitFilePath();
118 }
119 else
120 {
121 m_basePath = "";
122 m_fileName = "";
123 m_directory = "";
124 }
125
126
127 if (fields.size() >= 10) // Since TVServerKodi 1.0.8.0
128 {
129 m_originalurl = fields[9];
130 }
131 else
132 {
133 m_originalurl = fields[6];
134 }
135
136 if (fields.size() >= 16) // Since TVServerKodi 1.1.x.105
137 {
138 m_keepUntil = atoi( fields[10].c_str() );
139 m_episodeName = fields[11];
140 m_episodeNumber = fields[12];
141 m_episodePart = fields[13];
142 m_seriesNumber = fields[14];
143 m_scheduleID = atoi( fields[15].c_str() );
144 }
145
146 if (fields.size() >= 19) // Since TVServerKodi 1.2.x.111
147 {
148 m_genre = fields[16];
149 m_channelID = atoi( fields[17].c_str() );
150 m_isRecording = stringtobool( fields[18] );
151
152 if (m_genretable) m_genretable->GenreToTypes(m_genre, m_genre_type, m_genre_subtype);
153
154 if (fields.size() >= 20) // Since TVServerKodi 1.2.x.117
155 {
156 m_timesWatched = atoi( fields[19].c_str() );
157 if (fields.size() >= 21) // Since TVServerKodi 1.2.x.121
158 {
159 m_lastPlayedPosition = atoi( fields[20].c_str() );
160 if (fields.size() >= 22) // Since TVServerKodi 1.15.136
161 {
162 m_channelType = atoi(fields[21].c_str());
163 }
164 else
165 {
166 m_channelType = TvDatabase::ChannelType::Unknown;
167 }
168 }
169 }
170 }
171
172 return true;
173 }
174 else
175 {
176 kodi::Log(ADDON_LOG_ERROR, "Recording information has not enough fields. At least 9 fields expected, got only %d fields.", fields.size());
177 return false;
178 }
179 }
180
181 int cRecording::Lifetime(void) const
182 {
183 // margro: the meaning of the XBMC-PVR Lifetime field is undocumented.
184 // Assuming that VDR is the source for this field:
185 // The guaranteed lifetime (in days) of a recording created by this
186 // timer. 0 means that this recording may be automatically deleted
187 // at any time by a new recording with higher priority. 99 means
188 // that this recording will never be automatically deleted. Any
189 // number in the range 1...98 means that this recording may not be
190 // automatically deleted in favour of a new recording, until the
191 // given number of days since the start time of the recording has
192 // passed by
193 TvDatabase::KeepMethodType m_keepmethod = (TvDatabase::KeepMethodType) m_keepUntil;
194
195 switch (m_keepmethod)
196 {
197 case TvDatabase::UntilSpaceNeeded: //until space needed
198 case TvDatabase::UntilWatched: //until watched
199 return 0;
200 break;
201 case TvDatabase::TillDate: //until keepdate
202 {
203 int diffseconds = m_keepUntilDate - m_startTime;
204 int daysremaining = (int)(diffseconds / cSecsInDay);
205 // Calculate value in the range 1...98, based on m_keepdate
206 if ((daysremaining < MAXLIFETIME) && (daysremaining >= 0))
207 {
208 return daysremaining;
209 }
210 else
211 {
212 // > 98 days => return forever
213 return MAXLIFETIME;
214 }
215 }
216 break;
217 case TvDatabase::Always: //forever
218 return MAXLIFETIME;
219 default:
220 return MAXLIFETIME;
221 }
222 }
223
224 void cRecording::SplitFilePath(void)
225 {
226 size_t found = string::npos;
227
228 // Try to find the base path used for this recording by searching for the
229 // card recording folder name in the the recording file name.
230 if ((m_cardSettings) && (m_cardSettings->size() > 0))
231 {
232 for (CCards::iterator it = m_cardSettings->begin(); it < m_cardSettings->end(); ++it)
233 {
234 // Determine whether the first part of the recording file name is shared with this card
235 // Minimal name length of the RecordingFolder should be 3 (drive letter + :\)
236 if (it->RecordingFolder.length() >= 3)
237 {
238 found = m_filePath.find(it->RecordingFolder);
239 if (found != string::npos)
240 {
241 m_basePath = it->RecordingFolder;
242 if (m_basePath.at(m_basePath.length() - 1) != '\\')
243 m_basePath += "\\";
244
245 // Remove the base path
246 m_fileName = m_filePath.substr(it->RecordingFolder.length()+1);
247
248 // Extract subdirectories below the base path
249 size_t found2 = m_fileName.find_last_of("/\\");
250 if (found2 != string::npos)
251 {
252 m_directory = m_fileName.substr(0, found2);
253 m_fileName = m_fileName.substr(found2+1);
254 }
255 else
256 {
257 m_directory = "";
258 }
259
260 break;
261 }
262 }
263 }
264 }
265
266 if (found == string::npos)
267 {
268 m_fileName = m_filePath;
269 m_directory = "";
270 m_basePath = "";
271 }
272 }
273
274 void cRecording::SetGenreTable(CGenreTable* genretable)
275 {
276 m_genretable = genretable;
277 }
278
279 time_t cRecording::StartTime(void) const
280 {
281 time_t time = m_startTime.GetAsTime();
282 return time;
283 }
284
285 int cRecording::Duration(void) const
286 {
287 if (m_isRecording)
288 {
289 MPTV::CDateTime endTime = MPTV::CDateTime::Now();
290 int diff = endTime - m_startTime - 10;
291 if (diff > 0)
292 {
293 return diff;
294 }
295 else
296 {
297 return 0;
298 }
299 }
300 else
301 {
302 return m_duration;
303 }
304 }
305
306 int cRecording::GetSeriesNumber(void) const
307 {
308 // From xbmc_pvr_types.h:
309 // series number (usually called season).
310 // Set to "0" for specials/pilot.
311 // For 'invalid' see iEpisodeNumber or set to -1
312 if (m_seriesNumber.empty())
313 return -1;
314
315 return atoi(m_seriesNumber.c_str());
316 }
317
318 int cRecording::GetEpisodeNumber(void) const
319 {
320 // From xbmc_pvr_types.h:
321 // episode number within the "iSeriesNumber" season.
322 // For 'invalid' set to -1 or iSeriesNumber=iEpisodeNumber=0
323 // to show both are invalid */
324 if (m_episodeNumber.empty())
325 return -1;
326
327 return atoi(m_episodeNumber.c_str());
328 }
329
330 PVR_RECORDING_CHANNEL_TYPE cRecording::GetChannelType(void) const
331 {
332 switch (m_channelType)
333 {
334 case TvDatabase::ChannelType::Tv:
335 return PVR_RECORDING_CHANNEL_TYPE_TV;
336 break;
337 case TvDatabase::ChannelType::Radio:
338 return PVR_RECORDING_CHANNEL_TYPE_RADIO;
339 break;
340 default:
341 return PVR_RECORDING_CHANNEL_TYPE_UNKNOWN;
342 }
343 }