MythTV master
ExternalChannel.cpp
Go to the documentation of this file.
1
5// MythTV includes
7
8#include "ExternalChannel.h"
9#include "mpeg/mpegtables.h"
10#include "tv_rec.h"
11
12#define LOC QString("ExternChan[%1](%2): ").arg(m_inputId).arg(m_loc)
13
15{
18}
19
21{
22 LOG(VB_CHANNEL, LOG_INFO, LOC + "Open()");
23
24 if (m_device.isEmpty())
25 return false;
26
27 if (IsOpen())
28 {
30 return true;
31
32 LOG(VB_GENERAL, LOG_ERR, LOC +
33 QString("Valid stream handler, but app is not open! Resetting."));
34 Close();
35 }
36
37
38 if (!InitializeInput())
39 return false;
40
41 if (!m_inputId)
42 return false;
43
45 GetMajorID());
47 {
48 LOG(VB_GENERAL, LOG_ERR, LOC + "Open failed");
49 Close();
50 return false;
51 }
52
54 LOG(VB_RECORD, LOG_INFO, LOC + "Opened");
55 return true;
56}
57
59{
60 LOG(VB_CHANNEL, LOG_INFO, LOC + "Close()");
61
63 {
65 m_streamHandler = nullptr;
66 }
67}
68
70{
73 else
74 m_loc = GetDevice();
75
76 return m_loc;
77}
78
80{
83 else
84 m_loc = GetDevice();
85
86 return m_loc;
87}
88
89bool ExternalChannel::Tune(const QString &channum)
90{
91 LOG(VB_CHANNEL, LOG_INFO, LOC + QString("Tune(%1)").arg(channum));
92
93 if (!IsOpen())
94 {
95 LOG(VB_CHANNEL, LOG_ERR, LOC + "Tune failed, not open");
96 return false;
97 }
98
100 return true;
101
102 QString result;
103 if (m_tuneTimeout < 0ms)
104 {
105 if (!m_streamHandler->ProcessCommand("LockTimeout?", result))
106 {
107 LOG(VB_CHANNEL, LOG_ERR, LOC + QString
108 ("Failed to retrieve LockTimeout: %1").arg(result));
109 m_tuneTimeout = 60s;
110 }
111 else
112 {
113 m_tuneTimeout = std::chrono::milliseconds(result.split(":")[1].toInt());
114 }
115
116 LOG(VB_CHANNEL, LOG_INFO, LOC + QString("Using Tune timeout of %1ms")
117 .arg(m_tuneTimeout.count()));
118 }
119
120 LOG(VB_CHANNEL, LOG_INFO, LOC + "Tuning to " + channum);
121
122 if (m_streamHandler->APIVersion() < 3)
123 {
124 if (!m_streamHandler->ProcessCommand("TuneChannel:" + channum,
125 result, m_tuneTimeout))
126 {
127 LOG(VB_CHANNEL, LOG_ERR, LOC + QString
128 ("Failed to Tune %1: %2").arg(channum, result));
129 return false;
130 }
131 m_backgroundTuning = result.startsWith("OK:InProgress");
132 }
133 else
134 {
135 QVariantMap cmd;
136 QVariantMap vresult;
137 QByteArray response;
138
139 cmd["command"] = "TuneChannel";
140 cmd["channum"] = channum;
141 cmd["inputid"] = GetInputID();
142 cmd["sourceid"] = m_sourceId;
143
144 if (m_pParent)
145 {
147 if (prog)
148 {
149 uint recordid = prog->GetRecordingRuleID();
150 cmd["recordid"] = recordid;
151 cmd["title"] = prog->GetTitle();
152 cmd["subtitle"] = prog->GetSubtitle();
153 cmd["description"] = prog->GetDescription();
154 cmd["duration"] = prog->GetRecordingStartTime()
155 .secsTo(prog->GetRecordingEndTime());
156 cmd["programid"] = prog->GetProgramID();
157 cmd["seriesid"] = prog->GetSeriesID();
158 }
159 delete prog;
160 }
161
162 uint chanid = 0;
163 QString name;
164 QString callsign;
165 QString tvformat;
166 QString modulation;
167 QString freqtable;
168 QString freqid;
169 int finetune = 0;
170 uint64_t frequency = 0;
171 QString dtv_si_std;
172 int mpeg_prog_num = 0;
173 uint atsc_major = 0;
174 uint atsc_minor = 0;
175 uint dvb_transportid = 0;
176 uint dvb_networkid = 0;
177 uint mplexid = 0;
178 bool commfree = false;
179
180 if (!ChannelUtil::GetChannelData(m_sourceId, chanid, channum,
181 name, callsign,
182 tvformat, modulation,
183 freqtable, freqid,
184 finetune, frequency, dtv_si_std,
185 mpeg_prog_num, atsc_major, atsc_minor,
186 dvb_transportid, dvb_networkid,
187 mplexid, commfree))
188 {
189 LOG(VB_GENERAL, LOG_ERR, LOC + " " +
190 QString("Failed to find channel in DB on input '%1' ")
191 .arg(m_inputId));
192 }
193 else
194 {
195 cmd["name"] = name;
196 cmd["callsign"] = callsign;
197 cmd["chanid"] = chanid;
198 cmd["freqid"] = freqid;
199 cmd["atsc_major"] = atsc_major;
200 cmd["atsc_minor"] = atsc_minor;
201 cmd["mplexid"] = mplexid;
202 }
203
204 if (!m_streamHandler->ProcessJson(cmd, vresult, response))
205 {
206 LOG(VB_CHANNEL, LOG_ERR, LOC + QString
207 ("Failed to Tune %1: %2").arg(channum, QString(response)));
208 return false;
209 }
210 m_backgroundTuning = vresult["message"]
211 .toString().startsWith("InProgress");
212 }
213
215
216 return true;
217}
218
219bool ExternalChannel::Tune(const QString &freqid, int /*finetune*/)
220{
221 return ExternalChannel::Tune(freqid);
222}
223
225{
226 Close();
227 return true;
228}
229
231{
233 return 3;
234
235 LOG(VB_CHANNEL, LOG_DEBUG, LOC + QString("GetScriptStatus() %1")
236 .arg(m_systemStatus));
237
238 QString result;
239 int ret = 0;
240
241 if (!m_streamHandler->ProcessCommand("TuneStatus?", result))
242 {
243 LOG(VB_CHANNEL, LOG_ERR, LOC + QString
244 ("Failed to Tune: %1").arg(result));
245 ret = 2;
246 m_backgroundTuning = false;
247 }
248 else
249 {
250 if (result.startsWith("OK:InProgress"))
251 ret = 1;
252 else
253 {
254 ret = 3;
255 m_backgroundTuning = false;
257 }
258 }
259
260 LOG(VB_CHANNEL, LOG_DEBUG, LOC + QString("GetScriptStatus() %1 -> %2")
261 .arg(m_systemStatus). arg(ret));
262
263 return ret;
264}
#define LOC
-*- Mode: c++ -*- Class ExternalChannel
uint m_systemStatus
These get mapped from the GENERIC_EXIT_* to these values for use with the signalmonitor code.
Definition: channelbase.h:150
uint m_sourceId
Definition: channelbase.h:138
TVRec * m_pParent
Definition: channelbase.h:134
virtual int GetInputID(void) const
Definition: channelbase.h:67
uint m_inputId
Definition: channelbase.h:137
virtual bool InitializeInput(void)
Fills in input map from DB.
int GetMajorID(void)
static bool GetChannelData(uint sourceid, uint &chanid, const QString &channum, QString &name, QString &callsign, QString &tvformat, QString &modulation, QString &freqtable, QString &freqid, int &finetune, uint64_t &frequency, QString &dtv_si_std, int &mpeg_prog_num, uint &atsc_major, uint &atsc_minor, uint &dvb_transportid, uint &dvb_networkid, uint &mplexid, bool &commfree)
ExternalStreamHandler * m_streamHandler
~ExternalChannel(void) override
QString GetDevice(void) const override
Returns String representing device, useful for debugging.
bool Tune(const DTVMultiplex &) override
This performs the actual frequency tuning and in some cases input switching.
bool EnterPowerSavingMode(void) override
Enters power saving mode if the card supports it.
bool Open(void) override
Opens the channel changing hardware for use.
void Close(void) override
Closes the channel changing hardware to use.
bool IsOpen(void) const override
Reports whether channel is already open.
std::chrono::milliseconds m_tuneTimeout
QString UpdateDescription(void)
QString GetDescription(void)
uint GetTuneStatus(void)
static ExternalStreamHandler * Get(const QString &devname, int inputid, int majorid)
bool ProcessCommand(const QString &cmd, QString &result, std::chrono::milliseconds timeout=4s, uint retry_cnt=3)
bool ProcessJson(const QVariantMap &vmsg, QVariantMap &elements, QByteArray &response, std::chrono::milliseconds timeout=4s, uint retry_cnt=3)
static void Return(ExternalStreamHandler *&ref, int inputid)
Holds information on recordings and videos.
Definition: programinfo.h:74
uint GetRecordingRuleID(void) const
Definition: programinfo.h:460
QString GetSeriesID(void) const
Definition: programinfo.h:446
QString GetProgramID(void) const
Definition: programinfo.h:447
QString GetDescription(void) const
Definition: programinfo.h:372
QString GetTitle(void) const
Definition: programinfo.h:368
QDateTime GetRecordingStartTime(void) const
Approximate time the recording started.
Definition: programinfo.h:412
QDateTime GetRecordingEndTime(void) const
Approximate time the recording should have ended, did end, or is intended to end.
Definition: programinfo.h:420
QString GetSubtitle(void) const
Definition: programinfo.h:370
bool HasError(void) const
Definition: streamhandler.h:66
ProgramInfo * GetRecording(void)
Allocates and returns a ProgramInfo for the current recording.
Definition: tv_rec.cpp:277
unsigned int uint
Definition: compat.h:60
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39