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 }
152 }
153
154 uint chanid = 0;
155 QString tvformat;
156 QString modulation;
157 QString freqtable;
158 QString freqid;
159 int finetune = 0;
160 uint64_t frequency = 0;
161 QString dtv_si_std;
162 int mpeg_prog_num = 0;
163 uint atsc_major = 0;
164 uint atsc_minor = 0;
165 uint dvb_transportid = 0;
166 uint dvb_networkid = 0;
167 uint mplexid = 0;
168 bool commfree = false;
169
170 if (!ChannelUtil::GetChannelData(m_sourceId, chanid, channum,
171 tvformat, modulation, freqtable, freqid,
172 finetune, frequency, dtv_si_std,
173 mpeg_prog_num, atsc_major, atsc_minor,
174 dvb_transportid, dvb_networkid,
175 mplexid, commfree))
176 {
177 LOG(VB_GENERAL, LOG_ERR, LOC + " " +
178 QString("Failed to find channel in DB on input '%1' ")
179 .arg(m_inputId));
180 }
181 else
182 {
183 cmd["chanid"] = chanid;
184 cmd["freqid"] = freqid;
185 cmd["atsc_major"] = atsc_major;
186 cmd["atsc_minor"] = atsc_minor;
187 cmd["mplexid"] = mplexid;
188 }
189
190 if (!m_streamHandler->ProcessJson(cmd, vresult, response))
191 {
192 LOG(VB_CHANNEL, LOG_ERR, LOC + QString
193 ("Failed to Tune %1: %2").arg(channum, QString(response)));
194 return false;
195 }
196 m_backgroundTuning = vresult["message"]
197 .toString().startsWith("InProgress");
198 }
199
201
202 return true;
203}
204
205bool ExternalChannel::Tune(const QString &freqid, int /*finetune*/)
206{
207 return ExternalChannel::Tune(freqid);
208}
209
211{
212 Close();
213 return true;
214}
215
217{
219 return 3;
220
221 LOG(VB_CHANNEL, LOG_DEBUG, LOC + QString("GetScriptStatus() %1")
222 .arg(m_systemStatus));
223
224 QString result;
225 int ret = 0;
226
227 if (!m_streamHandler->ProcessCommand("TuneStatus?", result))
228 {
229 LOG(VB_CHANNEL, LOG_ERR, LOC + QString
230 ("Failed to Tune: %1").arg(result));
231 ret = 2;
232 m_backgroundTuning = false;
233 }
234 else
235 {
236 if (result.startsWith("OK:InProgress"))
237 ret = 1;
238 else
239 {
240 ret = 3;
241 m_backgroundTuning = false;
243 }
244 }
245
246 LOG(VB_CHANNEL, LOG_DEBUG, LOC + QString("GetScriptStatus() %1 -> %2")
247 .arg(m_systemStatus). arg(ret));
248
249 return ret;
250}
#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 &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:68
uint GetRecordingRuleID(void) const
Definition: programinfo.h:453
bool HasError(void) const
Definition: streamhandler.h:66
ProgramInfo * GetRecording(void)
Allocates and returns a ProgramInfo for the current recording.
Definition: tv_rec.cpp:278
unsigned int uint
Definition: freesurround.h:24
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39