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 {
71  if (m_streamHandler)
73  else
74  m_loc = GetDevice();
75 
76  return m_loc;
77 }
78 
80 {
81  if (m_streamHandler)
83  else
84  m_loc = GetDevice();
85 
86  return m_loc;
87 }
88 
89 bool 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 
99  if (!m_streamHandler->HasTuner())
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 
205 bool ExternalChannel::Tune(const QString &freqid, int /*finetune*/)
206 {
207  return ExternalChannel::Tune(freqid);
208 }
209 
211 {
212  Close();
213  return true;
214 }
215 
217 {
218  if (!m_backgroundTuning)
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 }
LOC
#define LOC
-*- Mode: c++ -*- Class ExternalChannel
Definition: ExternalChannel.cpp:12
ExternalChannel::GetDescription
QString GetDescription(void)
Definition: ExternalChannel.cpp:79
StreamHandler::HasError
bool HasError(void) const
Definition: streamhandler.h:65
ExternalChannel::m_loc
QString m_loc
Definition: ExternalChannel.h:64
ExternalStreamHandler::HasTuner
bool HasTuner(void) const
Definition: ExternalStreamHandler.h:95
ChannelBase::m_inputId
uint m_inputId
Definition: channelbase.h:137
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
ExternalChannel::m_streamHandler
ExternalStreamHandler * m_streamHandler
Definition: ExternalChannel.h:63
ExternalChannel::Close
void Close(void) override
Closes the channel changing hardware to use.
Definition: ExternalChannel.cpp:58
ExternalChannel::GetDevice
QString GetDevice(void) const override
Returns String representing device, useful for debugging.
Definition: ExternalChannel.h:44
mythlogging.h
ExternalStreamHandler::APIVersion
int APIVersion(void) const
Definition: ExternalStreamHandler.h:123
ExternalStreamHandler::Get
static ExternalStreamHandler * Get(const QString &devname, int inputid, int majorid)
Definition: ExternalStreamHandler.cpp:464
mpegtables.h
ExternalChannel::Open
bool Open(void) override
Opens the channel changing hardware for use.
Definition: ExternalChannel.cpp:20
uint
unsigned int uint
Definition: compat.h:81
ExternalStreamHandler::ProcessJson
bool ProcessJson(const QVariantMap &vmsg, QVariantMap &elements, QByteArray &response, std::chrono::milliseconds timeout=4s, uint retry_cnt=3)
Definition: ExternalStreamHandler.cpp:1525
ExternalStreamHandler::UpdateDescription
QString UpdateDescription(void)
Definition: ExternalStreamHandler.cpp:899
TVRec::GetRecording
ProgramInfo * GetRecording(void)
Allocates and returns a ProgramInfo for the current recording.
Definition: tv_rec.cpp:278
ExternalStreamHandler::IsAppOpen
bool IsAppOpen(void)
Definition: ExternalStreamHandler.cpp:1007
ExternalChannel::Tune
bool Tune(const DTVMultiplex &) override
This performs the actual frequency tuning and in some cases input switching.
Definition: ExternalChannel.h:34
ChannelUtil::GetChannelData
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)
Definition: channelutil.cpp:1904
ProgramInfo
Holds information on recordings and videos.
Definition: programinfo.h:67
ChannelBase::m_systemStatus
uint m_systemStatus
These get mapped from the GENERIC_EXIT_* to these values for use with the signalmonitor code.
Definition: channelbase.h:150
ExternalChannel::GetTuneStatus
uint GetTuneStatus(void)
Definition: ExternalChannel.cpp:216
ChannelBase::m_sourceId
uint m_sourceId
Definition: channelbase.h:138
ExternalChannel::EnterPowerSavingMode
bool EnterPowerSavingMode(void) override
Enters power saving mode if the card supports it.
Definition: ExternalChannel.cpp:210
ExternalChannel.h
ExternalChannel::~ExternalChannel
~ExternalChannel(void) override
Definition: ExternalChannel.cpp:14
ExternalChannel::m_tuneTimeout
std::chrono::milliseconds m_tuneTimeout
Definition: ExternalChannel.h:59
ExternalChannel::m_backgroundTuning
bool m_backgroundTuning
Definition: ExternalChannel.h:60
ExternalStreamHandler::ProcessCommand
bool ProcessCommand(const QString &cmd, QString &result, std::chrono::milliseconds timeout=4s, uint retry_cnt=3)
Definition: ExternalStreamHandler.cpp:1242
tv_rec.h
ChannelBase::GetInputID
virtual int GetInputID(void) const
Definition: channelbase.h:67
ExternalChannel::UpdateDescription
QString UpdateDescription(void)
Definition: ExternalChannel.cpp:69
ExternalChannel::IsOpen
bool IsOpen(void) const override
Reports whether channel is already open.
Definition: ExternalChannel.h:42
ProgramInfo::GetRecordingRuleID
uint GetRecordingRuleID(void) const
Definition: programinfo.h:453
ExternalChannel::m_device
QString m_device
Definition: ExternalChannel.h:61
ChannelBase::GetMajorID
int GetMajorID(void)
Definition: channelbase.cpp:860
ChannelBase::InitializeInput
virtual bool InitializeInput(void)
Fills in input map from DB.
Definition: channelbase.cpp:554
ExternalStreamHandler::Return
static void Return(ExternalStreamHandler *&ref, int inputid)
Definition: ExternalStreamHandler.cpp:495
ChannelBase::m_pParent
TVRec * m_pParent
Definition: channelbase.h:134
ExternalStreamHandler::GetDescription
QString GetDescription(void)
Definition: ExternalStreamHandler.h:89