MythTV  master
decoderhandler.cpp
Go to the documentation of this file.
1 // c/c++
2 #include <cassert>
3 #include <cstdio>
4 #include <unistd.h>
5 
6 // qt
7 #include <QApplication>
8 #include <QUrl>
9 #include <QFileInfo>
10 
11 // mythtv
12 #include <mythdownloadmanager.h>
13 #include <mythdirs.h>
14 #include <mythlogging.h>
15 #include <compat.h> // For random() on MINGW32
16 #include <remotefile.h>
17 #include <mythcorecontext.h>
18 #include <musicmetadata.h>
19 
20 
21 // mythmusic
22 #include "decoderhandler.h"
23 #include "decoder.h"
24 
25 /**********************************************************************/
26 
27 QEvent::Type DecoderHandlerEvent::Ready = (QEvent::Type) QEvent::registerEventType();
28 QEvent::Type DecoderHandlerEvent::Meta = (QEvent::Type) QEvent::registerEventType();
29 QEvent::Type DecoderHandlerEvent::BufferStatus = (QEvent::Type) QEvent::registerEventType();
30 QEvent::Type DecoderHandlerEvent::OperationStart = (QEvent::Type) QEvent::registerEventType();
31 QEvent::Type DecoderHandlerEvent::OperationStop = (QEvent::Type) QEvent::registerEventType();
32 QEvent::Type DecoderHandlerEvent::Error = (QEvent::Type) QEvent::registerEventType();
33 
35  : MythEvent(type)
36 {
37  m_meta = new MusicMetadata(meta);
38 }
39 
41 {
42  delete m_msg;
43  delete m_meta;
44 }
45 
47 {
48  DecoderHandlerEvent *result = new DecoderHandlerEvent(*this);
49 
50  if (m_msg)
51  result->m_msg = new QString(*m_msg);
52 
53  if (m_meta)
54  result->m_meta = new MusicMetadata(*m_meta);
55 
56  result->m_available = m_available;
57  result->m_maxSize = m_maxSize;
58 
59  return result;
60 }
61 
62 void DecoderHandlerEvent::getBufferStatus(int *available, int *maxSize) const
63 {
64  *available = m_available;
65  *maxSize = m_maxSize;
66 }
67 
68 /**********************************************************************/
69 
71 {
72  stop();
73 }
74 
76 {
77  m_state = LOADING;
78 
79  m_playlist.clear();
80  m_meta = *mdata;
81  m_playlist_pos = -1;
82  m_redirects = 0;
83 
84  if (QFileInfo(mdata->Filename()).isAbsolute())
85  m_url = QUrl::fromLocalFile(mdata->Filename());
86  else
87  m_url.setUrl(mdata->Filename());
88 
90 }
91 
92 void DecoderHandler::doStart(bool result)
93 {
95 
96  if (QFileInfo(m_meta.Filename()).isAbsolute())
97  m_url = QUrl::fromLocalFile(m_meta.Filename());
98  else
99  m_url.setUrl(m_meta.Filename());
100 
101  if (m_state == LOADING && result)
102  {
103  for (int ii = 0; ii < m_playlist.size(); ii++)
104  LOG(VB_PLAYBACK, LOG_INFO, QString("Track %1 = %2")
105  .arg(ii) .arg(m_playlist.get(ii)->File()));
106  next();
107  }
108  else
109  {
110  if (m_state == STOPPED)
111  {
112  doFailed(m_url, "Could not get playlist");
113  }
114  }
115 }
116 
117 void DecoderHandler::error(const QString &e)
118 {
119  QString *str = new QString(e);
121  dispatch(ev);
122 }
123 
125 {
126  if (m_state == STOPPED)
127  return true;
128 
129  if (m_playlist_pos + 1 >= m_playlist.size())
130  {
131  m_state = STOPPED;
132  return true;
133  }
134 
135  return false;
136 }
137 
139 {
140  if (done())
141  return false;
142 
143  if (m_meta.Format() == "cast")
144  {
146  }
147  else
148  {
149  m_playlist_pos++;
150  }
151 
153 
154  if (QFileInfo(entry->File()).isAbsolute())
155  m_url = QUrl::fromLocalFile(entry->File());
156  else
157  m_url.setUrl(entry->File());
158 
159  LOG(VB_PLAYBACK, LOG_INFO, QString("Now playing '%1'").arg(m_url.toString()));
160 
161  // we use the avfdecoder for everything except CD tracks
162  if (m_url.toString().endsWith(".cda"))
163  doConnectDecoder(m_url, ".cda");
164  else
165  {
166  // we don't know what format radio stations are so fake a format
167  // and hope avfdecoder can decode it
168  doConnectDecoder(m_url, ".mp3");
169  }
170 
171  m_state = ACTIVE;
172 
173  return true;
174 }
175 
177 {
178  LOG(VB_PLAYBACK, LOG_INFO, QString("DecoderHandler: Stopping decoder"));
179 
180  if (m_decoder && m_decoder->isRunning())
181  {
182  m_decoder->lock();
183  m_decoder->stop();
184  m_decoder->unlock();
185  }
186 
187  if (m_decoder)
188  {
189  m_decoder->lock();
190  m_decoder->cond()->wakeAll();
191  m_decoder->unlock();
192  }
193 
194  if (m_decoder)
195  {
196  m_decoder->wait();
197  delete m_decoder;
198  m_decoder = nullptr;
199  }
200 
201  doOperationStop();
202 
203  m_state = STOPPED;
204 }
205 
206 void DecoderHandler::customEvent(QEvent *event)
207 {
208  if (DecoderHandlerEvent *dhe = dynamic_cast<DecoderHandlerEvent*>(event))
209  {
210  // Proxy all DecoderHandlerEvents
211  return dispatch(*dhe);
212  }
213  if (event->type() == MythEvent::MythEventMessage)
214  {
215  MythEvent *me = static_cast<MythEvent *>(event);
216  QStringList tokens = me->Message().split(" ", QString::SkipEmptyParts);
217 
218  if (tokens.isEmpty())
219  return;
220 
221  if (tokens[0] == "DOWNLOAD_FILE")
222  {
223  QStringList args = me->ExtraDataList();
224 
225  if (tokens[1] == "UPDATE")
226  {
227  }
228  else if (tokens[1] == "FINISHED")
229  {
230  QString downloadUrl = args[0];
231  int fileSize = args[2].toInt();
232  int errorCode = args[4].toInt();
233  QString filename = args[1];
234 
235  if ((errorCode != 0) || (fileSize == 0))
236  {
237  LOG(VB_GENERAL, LOG_ERR, QString("DecoderHandler: failed to download playlist from '%1'")
238  .arg(downloadUrl));
239  QUrl url(downloadUrl);
240  m_state = STOPPED;
241  doOperationStop();
242  doFailed(url, "Could not get playlist");
243  }
244  else
245  {
246  QUrl fileUrl = QUrl::fromLocalFile(filename);
247  createPlaylistFromFile(fileUrl);
248  }
249  }
250  }
251  }
252 }
253 
254 void DecoderHandler::createPlaylist(const QUrl &url)
255 {
256  QString extension = QFileInfo(url.path()).suffix();
257  LOG(VB_NETWORK, LOG_INFO,
258  QString("File %1 has extension %2")
259  .arg(QFileInfo(url.path()).fileName()).arg(extension));
260 
261  if (extension == "pls" || extension == "m3u" || extension == "asx")
262  {
263  if (url.scheme() == "file" || QFileInfo(url.toString()).isAbsolute())
265  else
267 
268  return;
269  }
270 
272 }
273 
275 {
277 
278  if (url.scheme() == "file" || QFileInfo(url.toString()).isAbsolute())
279  entry->setFile(url.toLocalFile());
280  else
281  entry->setFile(url.toString());
282 
283  m_playlist.add(entry);
284 
285  doStart((m_playlist.size() > 0));
286 }
287 
289 {
290  QString file = url.toLocalFile();
291 
293 
294  doStart((m_playlist.size() > 0));
295 }
296 
298 {
299  LOG(VB_NETWORK, LOG_INFO,
300  QString("Retrieving playlist from '%1'").arg(url.toString()));
301 
302  doOperationStart(tr("Retrieving playlist"));
303 
304  QString extension = QFileInfo(url.path()).suffix().toLower();
305  QString saveFilename = GetConfDir() + "/MythMusic/playlist." + extension;
306  GetMythDownloadManager()->queueDownload(url.toString(), saveFilename, this);
307 
308  //TODO should find a better way to do this
309  QTime time;
310  time.start();
311  while (m_state == LOADING)
312  {
313  if (time.elapsed() > 30000)
314  {
315  doOperationStop();
316  GetMythDownloadManager()->cancelDownload(url.toString());
317  LOG(VB_GENERAL, LOG_ERR, QString("DecoderHandler:: Timed out trying to download playlist from: %1")
318  .arg(url.toString()));
319  m_state = STOPPED;
320  }
321 
322  qApp->processEvents();
323  usleep(500);
324  }
325 }
326 
327 void DecoderHandler::doConnectDecoder(const QUrl &url, const QString &format)
328 {
329  if (m_decoder && !m_decoder->factory()->supports(format))
330  {
331  delete m_decoder;
332  m_decoder = nullptr;
333  }
334 
335  if (!m_decoder)
336  {
337  if ((m_decoder = Decoder::create(format, nullptr, true)) == nullptr)
338  {
339  doFailed(url, QString("No decoder for this format '%1'").arg(format));
340  return;
341  }
342  }
343 
344  m_decoder->setURL(url.toString());
345 
347  dispatch(ev);
348 }
349 
350 void DecoderHandler::doFailed(const QUrl &url, const QString &message)
351 {
352  LOG(VB_NETWORK, LOG_ERR,
353  QString("DecoderHandler error: '%1' - %2").arg(message).arg(url.toString()));
354  DecoderHandlerEvent ev(DecoderHandlerEvent::Error, new QString(message));
355  dispatch(ev);
356 }
357 
359 {
360  m_op = true;
362  dispatch(ev);
363 }
364 
366 {
367  if (!m_op)
368  return;
369 
370  m_op = false;
372  dispatch(ev);
373 }
void createPlaylistForSingleFile(const QUrl &url)
void add(PlayListFileEntry *e)
Add a entry to the playlist.
Definition: pls.h:80
void queueDownload(const QString &url, const QString &dest, QObject *caller, const bool reload=false)
Adds a url to the download queue.
DecoderFactory * factory() const
Definition: decoder.h:75
static Type MythEventMessage
Definition: mythevent.h:66
virtual void unlock(void)
Definition: decoder.h:82
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:311
QString File(void)
Definition: pls.h:25
void clear(void)
Clear out all the entries.
Definition: pls.h:83
MusicMetadata m_meta
Events sent by the DecoderHandler and it's helper classes.
void doOperationStop(void)
virtual ~DecoderHandler(void)
MusicMetadata * m_meta
QString GetConfDir(void)
Definition: mythdirs.cpp:224
virtual void lock(void)
Definition: decoder.h:81
This class is used as a container for messages.
Definition: mythevent.h:16
void dispatch(const MythEvent &event)
Dispatch an event to all listeners.
static Decoder * create(const QString &, AudioOutput *, bool=false)
Definition: decoder.cpp:108
virtual bool supports(const QString &source) const =0
int size(void) const
Get the number of entries in the pls file.
Definition: pls.h:54
bool isRunning(void) const
Definition: mthread.cpp:274
MythDownloadManager * GetMythDownloadManager(void)
Gets the pointer to the MythDownloadManager singleton.
void doOperationStart(const QString &name)
static Type OperationStart
void setFile(const QString &f)
Definition: pls.h:29
void createPlaylistFromFile(const QUrl &url)
const char * name
Definition: ParseText.cpp:328
void createPlaylistFromRemoteUrl(const QUrl &url)
QWaitCondition * cond()
Definition: decoder.h:85
void error(const QString &e)
PlayListFileEntry * get(int i)
Get a file entry.
Definition: pls.h:60
QString Format() const
void doFailed(const QUrl &url, const QString &message)
virtual void stop()=0
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
void doStart(bool result)
Class for representing entries in a pls file.
Definition: pls.h:19
MythEvent * clone(void) const override
void doConnectDecoder(const QUrl &url, const QString &format)
void customEvent(QEvent *e) override
QString Filename(bool find=true)
static Type BufferStatus
void getBufferStatus(int *available, int *maxSize) const
static Type OperationStop
void cancelDownload(const QString &url, bool block=true)
Cancel a queued or current download.
PlayListFile m_playlist
static long int random(void)
Definition: compat.h:149
static int parse(PlayListFile *pls, const QString &filename)
Parse a pls, m3u or asx playlist file.
Definition: pls.cpp:37
Decoder * m_decoder
DecoderHandlerEvent(Type type)
const QString & Message() const
Definition: mythevent.h:58
const QStringList & ExtraDataList() const
Definition: mythevent.h:60
void createPlaylist(const QUrl &url)
void start(MusicMetadata *mdata)
void setURL(const QString &url)
Definition: decoder.h:79