2 * Copyright (C) 2005-2008 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "DllLibCMyth.h"
23 #include "MythSession.h"
24 #include "video/VideoInfoTag.h"
25 #include "settings/AdvancedSettings.h"
29 #include "utils/URIUtils.h"
30 #include "utils/StringUtils.h"
31 #include "threads/SingleLock.h"
32 #include "utils/log.h"
33 #include "utils/TimeUtils.h"
37 #include "cmyth/include/cmyth/cmyth.h"
38 #include "cmyth/include/refmem/refmem.h"
41 using namespace XFILE;
44 #define MYTH_DEFAULT_PORT 6543
45 #define MYTH_DEFAULT_USERNAME "mythtv"
46 #define MYTH_DEFAULT_PASSWORD "mythtv"
47 #define MYTH_DEFAULT_DATABASE "mythconverg"
49 #define MYTH_IDLE_TIMEOUT 5 * 60 // 5 minutes in seconds
51 CCriticalSection CMythSession::m_section_session;
52 vector<CMythSession*> CMythSession::m_sessions;
54 void CMythSession::CheckIdle()
56 CSingleLock lock(m_section_session);
58 vector<CMythSession*>::iterator it;
59 for (it = m_sessions.begin(); it != m_sessions.end(); )
61 CMythSession* session = *it;
62 if (session->m_timestamp + (MYTH_IDLE_TIMEOUT * 1000) < CTimeUtils::GetTimeMS())
64 CLog::Log(LOGINFO, "%s - closing idle connection to MythTV backend: %s", __FUNCTION__, session->m_hostname.c_str());
66 it = m_sessions.erase(it);
75 CMythSession* CMythSession::AquireSession(const CURL& url)
77 CSingleLock lock(m_section_session);
79 vector<CMythSession*>::iterator it;
80 for (it = m_sessions.begin(); it != m_sessions.end(); it++)
82 CMythSession* session = *it;
83 if (session->CanSupport(url))
86 CLog::Log(LOGDEBUG, "%s - Aquired existing MythTV session: %p", __FUNCTION__, session);
90 CMythSession* session = new CMythSession(url);
91 CLog::Log(LOGINFO, "%s - Aquired new MythTV session for %s: %p", __FUNCTION__,
92 url.GetWithoutUserDetails().c_str(), session);
96 void CMythSession::ReleaseSession(CMythSession* session)
98 CLog::Log(LOGDEBUG, "%s - Releasing MythTV session: %p", __FUNCTION__, session);
99 session->SetListener(NULL);
100 session->m_timestamp = CTimeUtils::GetTimeMS();
101 CSingleLock lock(m_section_session);
102 m_sessions.push_back(session);
105 CDateTime CMythSession::GetValue(cmyth_timestamp_t t)
110 time_t time = m_dll->timestamp_to_unixtime(t); // Returns NULL if error
112 result = CTimeUtils::GetLocalTime(time);
114 result = CTimeUtils::GetLocalTime(0);
115 m_dll->ref_release(t);
117 else // Return epoch so 0 and NULL behave the same.
118 result = CTimeUtils::GetLocalTime(0);
123 CStdString CMythSession::GetValue(char *str)
129 m_dll->ref_release(str);
135 void CMythSession::SetFileItemMetaData(CFileItem &item, cmyth_proginfo_t program)
141 * Set the FileItem meta-data.
143 CStdString title = GetValue(m_dll->proginfo_title(program)); // e.g. Mythbusters
144 CStdString subtitle = GetValue(m_dll->proginfo_subtitle(program)); // e.g. The Pirate Special
145 item.m_strTitle = title;
146 if (!subtitle.IsEmpty())
147 item.m_strTitle += " - \"" + subtitle + "\""; // e.g. Mythbusters - "The Pirate Special"
148 item.m_dateTime = GetValue(m_dll->proginfo_rec_start(program));
149 item.m_dwSize = m_dll->proginfo_length(program); // size in bytes
152 * Set the VideoInfoTag meta-data so it matches the FileItem meta-data where possible.
154 CVideoInfoTag* tag = item.GetVideoInfoTag();
155 tag->m_strTitle = subtitle; // The title is just supposed to be the episode title.
156 tag->m_strShowTitle = title;
157 tag->m_strOriginalTitle = title;
158 tag->m_strPlotOutline = subtitle;
159 tag->m_strPlot = GetValue(m_dll->proginfo_description(program));
161 * TODO: Strip out the subtitle from the description if it is present at the start? OR add the
162 * subtitle to the start of the plot if not already as it used to? Seems strange, should be
165 if (tag->m_strPlot.Left(tag->m_strPlotOutline.length()) != tag->m_strPlotOutline && !tag->m_strPlotOutline.IsEmpty())
166 tag->m_strPlot = tag->m_strPlotOutline + '\n' + tag->m_strPlot;
168 tag->m_strGenre = GetValue(m_dll->proginfo_category(program)); // e.g. Sports
169 tag->m_strAlbum = GetValue(m_dll->proginfo_chansign(program)); // e.g. TV3
170 tag->m_strRuntime = StringUtils::SecondsToTimeString(m_dll->proginfo_length_sec(program));
172 SetSeasonAndEpisode(program, &tag->m_iSeason, &tag->m_iEpisode);
175 * Original air date is used by the VideoInfoScanner to scrape the TV Show information into the
176 * Video Library. If the original air date is empty the date returned will be the epoch.
178 CStdString originalairdate = GetValue(m_dll->proginfo_originalairdate(program)).GetAsDBDate();
179 if (originalairdate != "1970-01-01"
180 && originalairdate != "1969-12-31")
181 tag->m_strFirstAired = originalairdate;
184 * Video sort title is the raw title with the date appended on the end in a sortable format so
185 * when the "All Recordings" listing is sorted by "Name" rather than "Date", all of the episodes
186 * for a given show are still presented in date order (even though some may have a subtitle that
187 * would cause it to be shown in a different position if it was indeed strictly sorting by
188 * what is displayed in the list).
190 tag->m_strSortTitle = title + " " + item.m_dateTime.GetAsDBDateTime(); // e.g. Mythbusters 2009-12-13 12:23:14
193 * Set further FileItem and VideoInfoTag meta-data based on whether it is LiveTV or not.
195 CURL url(item.m_strPath);
196 if (url.GetFileName().Left(9) == "channels/")
199 * Prepend the channel number onto the FileItem title for the listing so it's clear what is
200 * playing on each channel without using up as much room as the channel name.
202 CStdString number = GetValue(m_dll->proginfo_chanstr(program));
203 item.m_strTitle = number + " - " + item.m_strTitle;
206 * Append the channel name onto the end of the tag title for the OSD so it's clear what LiveTV
207 * channel is currently being watched to give some context for Next or Previous channel. Added
208 * to the end so sorting by title will work, and it's not really as important as the title
211 CStdString name = GetValue(m_dll->proginfo_chansign(program));
213 tag->m_strTitle += " - " + name;
216 * Set the sort title to be the channel number.
218 tag->m_strSortTitle = number;
221 * Set the status so XBMC treats the content as LiveTV.
223 tag->m_strStatus = "livetv";
226 * Update the path and channel icon for LiveTV in case the channel has changed through
227 * NextChannel(), PreviousChannel() or SetChannel().
229 if (!number.IsEmpty())
231 url.SetFileName("channels/" + number + ".ts"); // e.g. channels/3.ts
232 item.m_strPath = url.Get();
234 CStdString chanicon = GetValue(m_dll->proginfo_chanicon(program));
235 if (!chanicon.IsEmpty())
237 url.SetFileName("files/channels/" + URIUtils::GetFileName(chanicon)); // e.g. files/channels/tv3.jpg
238 item.SetThumbnailImage(url.Get());
244 * MythTV thumbnails aren't generated until a program has finished recording.
246 if (m_dll->proginfo_rec_status(program) == RS_RECORDED)
248 url.SetFileName("files/" + URIUtils::GetFileName(GetValue(m_dll->proginfo_pathname(program))) + ".png");
249 item.SetThumbnailImage(url.Get());
254 void CMythSession::SetSeasonAndEpisode(const cmyth_proginfo_t &program, int *season, int *episode) {
256 * A valid programid generated from an XMLTV source should look like:
257 * [EP|MV|SH|SP][seriesid][episode][season]([partnumber][parttotal])
258 * mythtv/trunk/programs/mytfilldatabaseline/xmltvparser.cpp - Line 522 onwards.
260 * Season changed to a base36 character for XMLTV in Myth 0.24. http://svn.mythtv.org/trac/changeset/24724
262 * A valid SchedulesDirect programid appears to have a similar format to the XMLTV programid but
263 * doesn't have any obvious way to parse out the season and episode information. The number at the
264 * end of the programid could possibly be the completely sequential number for the episode, but
265 * even that doesn't seem to match up with TVDB. SchedulesDirect data does seem to have a valid
266 * original air date though, so if we identify a SchedulesDirect programid, leave the season and
269 CStdString programid = GetValue(m_dll->proginfo_programid(program));
270 CStdString seriesid = GetValue(m_dll->proginfo_seriesid(program));
273 * Default the season and episode to 0 so XBMC treats the content as an episode and displays tag
274 * information. If the season and episode can be parsed from the programid these will be
280 if (programid.IsEmpty() // Can't do anything if the program ID is empty
281 || seriesid.IsEmpty()) // Can't figure out the end parsing if the series ID is empty {
284 CStdString category = programid.Left(2); // Valid for both XMLTV and SchedulesDirect sources
285 if (category != "MV" // Movie
286 && category != "EP" // Series
287 && category != "SH" // TV Show
288 && category != "SP") // Sports
291 if (programid.Mid(category.length(), seriesid.length()) != seriesid) // Series ID does not follow the category
294 CStdString remainder = programid.Mid(category.length() + seriesid.length()); // Whatever is after series ID
297 * All SchedulesDirect remainders appear to be 4 characters and start with a 0. If the assumption
298 * is correct that the number somehow relates to the sequential episode number across all seasons
299 * then we can ignore remainders that start with 0. It will be very unlikely for a sequential
300 * episode number for a series to be > 999.
302 if (remainder.length() == 4 // All SchedulesDirect codes seem to be 4 characters
303 && remainder.Left(0) == "0") // Padded with 0's for low number. No valid XMLTV remainder will start with 0.
307 * If the remainder is more than 5 characters, it must include the optional part number and total
308 * number of parts. Strip off the last 2 characters assuming that there are ridiculously few
309 * cases where the number of parts for a single episode is > 9.
311 if (remainder.length() >= 5) // Must include optional part number and total number of parts
312 remainder = remainder.Left(remainder.length() - 2); // Assumes part number and total are both < 10
315 * Now for some heuristic black magic.
317 if (remainder.length() == 2) // Single character season and episode.
319 *season = atoi(remainder.Right(1)); // TODO: Fix for base 36 in Myth 0.24. Assume season < 10
320 *episode = atoi(remainder.Left(1));
322 else if (remainder.length() == 3) // Ambiguous in Myth 0.23. Single character season in Myth 0.24
325 * Following heuristics are intended to work with largest possible number of cases. It won't be
326 * perfect, but way better than just assuming the season is < 10.
328 if (remainder.Right(1) == "0") // e.g. 610. Unlikely to have a season of 0 (specials) with more than 9 special episodes.
330 *season = atoi(remainder.Right(2));
331 *episode = atoi(remainder.Left(1));
333 else if (remainder.Mid(2, 1) == "0") // e.g. 203. Can't have a season start with 0. Must be end of episode.
335 *season = atoi(remainder.Right(1)); // TODO: Fix for base 36 in Myth 0.24. Assume season < 10
336 *episode = atoi(remainder.Left(2));
338 else if (atoi(remainder.Left(1)) > 3) // e.g. 412. Very unlikely to have more than 39 episodes per season if season > 9.
341 * TODO: See if a check for > 2 is better, e.g. is it still unlike to have more than 29 episodes
342 * per season if season > 9?
344 *season = atoi(remainder.Right(2));
345 *episode = atoi(remainder.Left(1));
347 else // e.g. 129. Assume season is < 10 or Myth 0.24 Base 36 season.
349 *season = atoi(remainder.Right(1)); // TODO: Fix for base 36 in Myth 0.24. Assume season < 10
350 *episode = atoi(remainder.Left(2));
353 else if (remainder.length() == 4) // Double digit season and episode in Myth 0.23 OR TODO: has part number and total number of parts
355 *season = atoi(remainder.Right(2));
356 *episode = atoi(remainder.Left(2));
361 CMythSession::CMythSession(const CURL& url)
366 m_hostname = url.GetHostName();
367 m_username = url.GetUserName() == "" ? MYTH_DEFAULT_USERNAME : url.GetUserName();
368 m_password = url.GetPassWord() == "" ? MYTH_DEFAULT_PASSWORD : url.GetPassWord();
369 m_port = url.HasPort() ? url.GetPort() : MYTH_DEFAULT_PORT;
370 m_timestamp = CTimeUtils::GetTimeMS();
371 m_dll = new DllLibCMyth;
373 if (m_dll->IsLoaded())
375 m_dll->set_dbg_msgcallback(&CMythSession::LogCMyth);
376 if (g_advancedSettings.m_logLevel >= LOG_LEVEL_DEBUG_SAMBA)
377 m_dll->dbg_level(CMYTH_DBG_ALL);
378 else if (g_advancedSettings.m_logLevel >= LOG_LEVEL_DEBUG)
379 m_dll->dbg_level(CMYTH_DBG_DETAIL);
381 m_dll->dbg_level(CMYTH_DBG_ERROR);
383 m_all_recorded = NULL;
386 CMythSession::~CMythSession()
392 bool CMythSession::CanSupport(const CURL& url)
394 if (m_hostname != url.GetHostName())
397 if (m_port != (url.HasPort() ? url.GetPort() : MYTH_DEFAULT_PORT))
400 if (m_username != (url.GetUserName() == "" ? MYTH_DEFAULT_USERNAME : url.GetUserName()))
403 if (m_password != (url.GetPassWord() == "" ? MYTH_DEFAULT_PASSWORD : url.GetPassWord()))
409 void CMythSession::Process()
418 /* check if there are any new events */
421 if (m_dll->event_select(m_event, &to) <= 0)
424 next = m_dll->event_get(m_event, buf, sizeof(buf));
425 buf[sizeof(buf) - 1] = 0;
429 case CMYTH_EVENT_UNKNOWN:
430 CLog::Log(LOGDEBUG, "%s - MythTV event UNKNOWN (error?)", __FUNCTION__);
432 case CMYTH_EVENT_CLOSE:
433 CLog::Log(LOGDEBUG, "%s - MythTV event EVENT_CLOSE", __FUNCTION__);
435 case CMYTH_EVENT_RECORDING_LIST_CHANGE:
436 CLog::Log(LOGDEBUG, "%s - MythTV event RECORDING_LIST_CHANGE", __FUNCTION__);
437 GetAllRecordedPrograms(true);
439 case CMYTH_EVENT_RECORDING_LIST_CHANGE_ADD:
440 CLog::Log(LOGDEBUG, "%s - MythTV event RECORDING_LIST_CHANGE_ADD: %s", __FUNCTION__, buf);
441 GetAllRecordedPrograms(true);
443 case CMYTH_EVENT_RECORDING_LIST_CHANGE_UPDATE:
444 CLog::Log(LOGDEBUG, "%s - MythTV event RECORDING_LIST_CHANGE_UPDATE", __FUNCTION__);
445 GetAllRecordedPrograms(true);
447 case CMYTH_EVENT_RECORDING_LIST_CHANGE_DELETE:
448 CLog::Log(LOGDEBUG, "%s - MythTV event RECORDING_LIST_CHANGE_DELETE: %s", __FUNCTION__, buf);
449 GetAllRecordedPrograms(true);
451 case CMYTH_EVENT_SCHEDULE_CHANGE:
452 CLog::Log(LOGDEBUG, "%s - MythTV event SCHEDULE_CHANGE", __FUNCTION__);
454 case CMYTH_EVENT_DONE_RECORDING:
455 CLog::Log(LOGDEBUG, "%s - MythTV event DONE_RECORDING", __FUNCTION__);
457 case CMYTH_EVENT_QUIT_LIVETV:
458 CLog::Log(LOGDEBUG, "%s - MythTV event QUIT_LIVETV", __FUNCTION__);
460 case CMYTH_EVENT_WATCH_LIVETV:
461 CLog::Log(LOGDEBUG, "%s - MythTV event LIVETV_WATCH", __FUNCTION__);
463 case CMYTH_EVENT_LIVETV_CHAIN_UPDATE:
464 CLog::Log(LOGDEBUG, "%s - MythTV event LIVETV_CHAIN_UPDATE: %s", __FUNCTION__, buf);
466 case CMYTH_EVENT_SIGNAL:
467 CLog::Log(LOGDEBUG, "%s - MythTV event SIGNAL", __FUNCTION__);
469 case CMYTH_EVENT_ASK_RECORDING:
470 CLog::Log(LOGDEBUG, "%s - MythTV event ASK_RECORDING", __FUNCTION__);
472 case CMYTH_EVENT_SYSTEM_EVENT:
473 CLog::Log(LOGDEBUG, "%s - MythTV event SYSTEM_EVENT: %s", __FUNCTION__, buf);
475 case CMYTH_EVENT_UPDATE_FILE_SIZE:
476 CLog::Log(LOGDEBUG, "%s - MythTV event UPDATE_FILE_SIZE: %s", __FUNCTION__, buf);
478 case CMYTH_EVENT_GENERATED_PIXMAP:
479 CLog::Log(LOGDEBUG, "%s - MythTV event GENERATED_PIXMAP: %s", __FUNCTION__, buf);
481 case CMYTH_EVENT_CLEAR_SETTINGS_CACHE:
482 CLog::Log(LOGDEBUG, "%s - MythTV event CLEAR_SETTINGS_CACHE", __FUNCTION__);
487 CSingleLock lock(m_section);
489 m_listener->OnEvent(next, buf);
494 void CMythSession::Disconnect()
496 if (!m_dll || !m_dll->IsLoaded())
502 m_dll->ref_release(m_control);
504 m_dll->ref_release(m_event);
506 m_dll->ref_release(m_database);
508 m_dll->ref_release(m_all_recorded);
511 cmyth_conn_t CMythSession::GetControl()
515 if (!m_dll->IsLoaded())
518 m_control = m_dll->conn_connect_ctrl((char*)m_hostname.c_str(), m_port, 16*1024, 4096);
520 CLog::Log(LOGERROR, "%s - unable to connect to server on %s:%d", __FUNCTION__, m_hostname.c_str(), m_port);
525 cmyth_database_t CMythSession::GetDatabase()
529 if (!m_dll->IsLoaded())
532 m_database = m_dll->database_init((char*)m_hostname.c_str(), (char*)MYTH_DEFAULT_DATABASE,
533 (char*)m_username.c_str(), (char*)m_password.c_str());
535 CLog::Log(LOGERROR, "%s - unable to connect to database on %s:%d", __FUNCTION__, m_hostname.c_str(), m_port);
540 bool CMythSession::SetListener(IEventListener *listener)
542 if (!m_event && listener)
544 if (!m_dll->IsLoaded())
547 m_event = m_dll->conn_connect_event((char*)m_hostname.c_str(), m_port, 16*1024 , 4096);
550 CLog::Log(LOGERROR, "%s - unable to connect to server on %s:%d", __FUNCTION__, m_hostname.c_str(), m_port);
553 /* start event handler thread */
554 CThread::Create(false, THREAD_MINSTACKSIZE);
556 CSingleLock lock(m_section);
557 m_listener = listener;
561 DllLibCMyth* CMythSession::GetLibrary()
563 if (m_dll->IsLoaded())
568 cmyth_proglist_t CMythSession::GetAllRecordedPrograms(bool force)
570 if (!m_all_recorded || force)
572 CSingleLock lock(m_section);
575 m_dll->ref_release(m_all_recorded);
576 m_all_recorded = NULL;
578 cmyth_conn_t control = GetControl();
582 m_all_recorded = m_dll->proglist_get_all_recorded(control);
584 return m_all_recorded;
587 void CMythSession::LogCMyth(int level, char *msg)
592 case CMYTH_DBG_NONE: break;
593 case CMYTH_DBG_ERROR: xbmc_lvl = LOGERROR; break;
594 case CMYTH_DBG_WARN: xbmc_lvl = LOGWARNING; break;
595 case CMYTH_DBG_INFO: xbmc_lvl = LOGINFO; break;
596 default: xbmc_lvl = LOGDEBUG; break;
599 CLog::Log(xbmc_lvl, "%s", msg);