00001 #include "dtvchannel.h"
00002
00003
00004 #include <qmap.h>
00005 #include <qstring.h>
00006 #include <qsqldatabase.h>
00007
00008
00009 #include "mythcontext.h"
00010 #include "mythdbcon.h"
00011 #include "cardutil.h"
00012
00013 #define LOC QString("DTVChan(%1): ").arg(GetDevice())
00014 #define LOC_WARN QString("DTVChan(%1) Warning: ").arg(GetDevice())
00015 #define LOC_ERR QString("DTVChan(%1) Error: ").arg(GetDevice())
00016
00017 QMutex DTVChannel::master_map_lock;
00018 QMap<QString,DTVChannel*> DTVChannel::master_map;
00019
00020 DTVChannel::DTVChannel(TVRec *parent)
00021 : ChannelBase(parent),
00022 sistandard("mpeg"), tuningMode(QString::null),
00023 currentProgramNum(-1),
00024 currentATSCMajorChannel(0), currentATSCMinorChannel(0),
00025 currentTransportID(0), currentOriginalNetworkID(0)
00026 {
00027 }
00028
00029 DTVChannel::~DTVChannel()
00030 {
00031 QMutexLocker locker(&master_map_lock);
00032 QMap<QString,DTVChannel*>::iterator it = master_map.begin();
00033 for (; it != master_map.end(); ++it)
00034 {
00035 if (*it == this)
00036 {
00037 master_map.erase(it);
00038 break;
00039 }
00040 }
00041 }
00042
00050 void DTVChannel::GetCachedPids(int chanid, pid_cache_t &pid_cache)
00051 {
00052 MSqlQuery query(MSqlQuery::InitCon());
00053 QString thequery = QString("SELECT pid, tableid FROM pidcache "
00054 "WHERE chanid='%1'").arg(chanid);
00055 query.prepare(thequery);
00056
00057 if (!query.exec() || !query.isActive())
00058 {
00059 MythContext::DBError("GetCachedPids: fetching pids", query);
00060 return;
00061 }
00062
00063 while (query.next())
00064 {
00065 int pid = query.value(0).toInt(), tid = query.value(1).toInt();
00066 if ((pid >= 0) && (tid >= 0))
00067 pid_cache.push_back(pid_cache_item_t(pid, tid));
00068 }
00069 }
00070
00077 void DTVChannel::SaveCachedPids(int chanid, const pid_cache_t &pid_cache)
00078 {
00079 MSqlQuery query(MSqlQuery::InitCon());
00080
00082 QString thequery =
00083 QString("DELETE FROM pidcache WHERE chanid='%1'").arg(chanid);
00084 query.prepare(thequery);
00085 if (!query.exec() || !query.isActive())
00086 {
00087 MythContext::DBError("GetCachedPids -- delete", query);
00088 return;
00089 }
00090
00092 pid_cache_t::const_iterator it = pid_cache.begin();
00093 for (; it != pid_cache.end(); ++it)
00094 {
00095 thequery = QString("INSERT INTO pidcache "
00096 "SET chanid='%1', pid='%2', tableid='%3'")
00097 .arg(chanid).arg(it->first).arg(it->second);
00098
00099 query.prepare(thequery);
00100
00101 if (!query.exec() || !query.isActive())
00102 {
00103 MythContext::DBError("GetCachedPids -- insert", query);
00104 return;
00105 }
00106 }
00107 }
00108
00109 void DTVChannel::SetDTVInfo(uint atsc_major, uint atsc_minor,
00110 uint dvb_orig_netid,
00111 uint mpeg_tsid, int mpeg_pnum)
00112 {
00113 QMutexLocker locker(&dtvinfo_lock);
00114 currentProgramNum = mpeg_pnum;
00115 currentATSCMajorChannel = atsc_major;
00116 currentATSCMinorChannel = atsc_minor;
00117 currentTransportID = mpeg_tsid;
00118 currentOriginalNetworkID = dvb_orig_netid;
00119 }
00120
00121 QString DTVChannel::GetSIStandard(void) const
00122 {
00123 QMutexLocker locker(&dtvinfo_lock);
00124 return QDeepCopy<QString>(sistandard);
00125 }
00126
00127 void DTVChannel::SetSIStandard(const QString &si_std)
00128 {
00129 QMutexLocker locker(&dtvinfo_lock);
00130 sistandard = QDeepCopy<QString>(si_std.lower());
00131 }
00132
00133 QString DTVChannel::GetSuggestedTuningMode(bool is_live_tv) const
00134 {
00135 uint cardid = GetCardID();
00136 QString input = GetCurrentInput();
00137
00138 uint quickTuning = 0;
00139 if (cardid && !input.isEmpty())
00140 quickTuning = CardUtil::GetQuickTuning(cardid, input);
00141
00142 bool useQuickTuning = (quickTuning && is_live_tv) || (quickTuning > 1);
00143
00144 QMutexLocker locker(&dtvinfo_lock);
00145 if (!useQuickTuning && ((sistandard == "atsc") || (sistandard == "dvb")))
00146 return QDeepCopy<QString>(sistandard);
00147
00148 return "mpeg";
00149 }
00150
00151 QString DTVChannel::GetTuningMode(void) const
00152 {
00153 QMutexLocker locker(&dtvinfo_lock);
00154 return QDeepCopy<QString>(tuningMode);
00155 }
00156
00157 void DTVChannel::SetTuningMode(const QString &tuning_mode)
00158 {
00159 QMutexLocker locker(&dtvinfo_lock);
00160 tuningMode = QDeepCopy<QString>(tuning_mode.lower());
00161 }
00162
00163 DTVChannel *DTVChannel::GetMaster(const QString &videodevice)
00164 {
00165 QMutexLocker locker(&master_map_lock);
00166
00167 QMap<QString,DTVChannel*>::iterator it = master_map.find(videodevice);
00168 if (it != master_map.end())
00169 return *it;
00170
00171 master_map[QDeepCopy<QString>(videodevice)] = this;
00172
00173 return this;
00174 }
00175
00176 const DTVChannel *DTVChannel::GetMaster(const QString &videodevice) const
00177 {
00178 QMutexLocker locker(&master_map_lock);
00179
00180 QMap<QString,DTVChannel*>::iterator it = master_map.find(videodevice);
00181 if (it != master_map.end())
00182 return *it;
00183
00184 master_map[QDeepCopy<QString>(videodevice)] = (DTVChannel*) this;
00185
00186 return this;
00187 }