MythTV  master
videoplayercommand.cpp
Go to the documentation of this file.
1 #include <QDir>
2 
3 #include "mythcontext.h"
4 
5 #include "mythmainwindow.h"
6 #include "mythsystemlegacy.h"
7 #include "remoteutil.h"
8 #include "lcddevice.h"
9 #include "mythmiscutil.h"
10 #include "dbaccess.h"
11 #include "videometadata.h"
12 #include "videoutils.h"
13 
14 #include "videoplayercommand.h"
15 
16 namespace
17 {
18  QString ShellEscape(const QString &src)
19  {
20  return QString(src)
21  .replace(QRegExp("\""), "\\\"")
22  .replace(QRegExp("`"), "\\`")
23  .replace(QRegExp("\\$"), "\\$");
24  }
25 
26  QString ExpandPlayCommand(const QString &command, const QString &filename)
27  {
28  // If handler contains %d, substitute default player command
29  // This would be used to add additional switches to the default without
30  // needing to retype the whole default command. But, if the
31  // command and the default command both contain %s, drop the %s from
32  // the default since the new command already has it
33  //
34  // example: default: mplayer -fs %s
35  // custom : %d -ao alsa9:spdif %s
36  // result : mplayer -fs -ao alsa9:spdif %s
37  QString tmp = command;
38  if (tmp.contains("%d"))
39  {
40  QString default_handler =
41  gCoreContext->GetSetting("VideoDefaultPlayer");
42  if (tmp.contains("%s") && default_handler.contains("%s"))
43  default_handler = default_handler.replace(QRegExp("%s"), "");
44  tmp.replace(QRegExp("%d"), default_handler);
45  }
46 
47  QString arg = QString("\"%1\"").arg(ShellEscape(filename));
48 
49  if (tmp.contains("%s"))
50  return tmp.replace(QRegExp("%s"), arg);
51 
52  return QString("%1 %2").arg(tmp).arg(arg);
53  }
54 }
55 
57 
59 {
60  protected:
61  VideoPlayProc() = default;
63 
64  public:
65  virtual ~VideoPlayProc() = default;
66  VideoPlayProc(const VideoPlayProc&) = default;
67 
68  // returns true if item played
69  virtual bool Play() const = 0;
70 
71  virtual QString GetCommandDisplayName() const = 0;
72 
73  virtual VideoPlayProc *Clone() const = 0;
74 };
75 
77 
79 {
80  private:
81  VideoPlayHandleMedia(QString handler, QString mrl,
82  QString plot, QString title, QString subtitle,
83  QString director, int season, int episode, QString inetref,
84  int length, QString year, QString id) :
85  m_handler(std::move(handler)), m_mrl(std::move(mrl)),
86  m_plot(std::move(plot)), m_title(std::move(title)),
87  m_subtitle(std::move(subtitle)),
88  m_director(std::move(director)), m_season(season),
89  m_episode(episode), m_inetref(std::move(inetref)),
90  m_length(length), m_year(std::move(year)),
91  m_id(std::move(id))
92  {
93  }
94 
95  public:
96  static VideoPlayHandleMedia *Create(const QString &handler,
97  const QString &mrl, const QString &plot, const QString &title,
98  const QString &subtitle, const QString &director,
99  int season, int episode, const QString &inetref,
100  int length, const QString &year, const QString &id)
101  {
102  return new VideoPlayHandleMedia(handler, mrl, plot, title, subtitle,
103  director, season, episode, inetref, length, year, id);
104  }
105 
106  bool Play() const override // VideoPlayProc
107  {
111  }
112 
113  QString GetCommandDisplayName() const override // VideoPlayProc
114  {
115  return m_handler;
116  }
117 
118  VideoPlayHandleMedia *Clone() const override // VideoPlayProc
119  {
120  return new VideoPlayHandleMedia(*this);
121  }
122 
123  private:
124  QString m_handler;
125  QString m_mrl;
126  QString m_plot;
127  QString m_title;
128  QString m_subtitle;
129  QString m_director;
130  int m_season;
132  QString m_inetref;
133  int m_length;
134  QString m_year;
135  QString m_id;
136 };
137 
139 
141 {
142  private:
143  VideoPlayMythSystem(QString disp_command,
144  QString play_command) :
145  m_displayCommand(std::move(disp_command)), m_playCommand(std::move(play_command))
146  {
147  }
148 
149  public:
150  static VideoPlayMythSystem *Create(const QString &command,
151  const QString &filename)
152  {
153  return new VideoPlayMythSystem(command,
154  ExpandPlayCommand(command, filename));
155  }
156 
157  bool Play() const override // VideoPlayProc
158  {
160 
161  return true;
162  }
163 
164  QString GetCommandDisplayName() const override // VideoPlayProc
165  {
166  return m_displayCommand;
167  }
168 
169  VideoPlayMythSystem *Clone() const override // VideoPlayProc
170  {
171  return new VideoPlayMythSystem(*this);
172  }
173 
174  private:
176  QString m_playCommand;
177 };
178 
180 
182 {
183  public:
184  VideoPlayerCommandPrivate() = default;
185 
187  {
188  for (auto *player : other.m_playerProcs)
189  {
190  m_playerProcs.push_back(player->Clone());
191  }
192  }
193 
195 
197  {
198  ClearPlayerList();
199  }
200 
201  void AltPlayerFor(const VideoMetadata *item)
202  {
203  if (item)
204  {
205  QString play_command =
206  gCoreContext->GetSetting("mythvideo.VideoAlternatePlayer");
207  QString filename;
208 
209  if (item->IsHostSet())
210  {
211  filename = generate_file_url("Videos", item->GetHost(),
212  item->GetFilename());
213  }
214  else
215  {
216  filename = item->GetFilename();
217  }
218 
219  if (play_command.length())
220  {
221  AddPlayer(play_command, filename, item->GetPlot(),
222  item->GetTitle(), item->GetSubtitle(),
223  item->GetDirector(), item->GetSeason(),
224  item->GetEpisode(), item->GetInetRef(),
225  item->GetLength(), QString::number(item->GetYear()),
226  QString::number(item->GetID()));
227  }
228  else
229  {
230  PlayerFor(filename, item);
231  }
232  }
233  }
234 
235  void PlayerFor(const VideoMetadata *item)
236  {
237  if (item)
238  {
239  QString play_command = item->GetPlayCommand();
240  QString filename;
241 
242  if (item->IsHostSet())
243  {
244  filename = generate_file_url("Videos", item->GetHost(),
245  item->GetFilename());
246  }
247  else
248  {
249  filename = item->GetFilename();
250  }
251 
252  if (play_command.length())
253  {
254  AddPlayer(play_command, filename, item->GetPlot(),
255  item->GetTitle(), item->GetSubtitle(),
256  item->GetDirector(), item->GetSeason(),
257  item->GetEpisode(), item->GetInetRef(),
258  item->GetLength(), QString::number(item->GetYear()),
259  QString::number(item->GetID()));
260  }
261  else
262  {
263  PlayerFor(filename, item);
264  }
265  }
266  }
267 
268  void PlayerFor(const QString &filename, const VideoMetadata *extraData = nullptr)
269  {
270  QString extension = filename.section(".", -1, -1);
271  QDir dir_test(QString("%1/VIDEO_TS").arg(filename));
272  if (dir_test.exists())
273  extension = "VIDEO_TS";
274  QDir bd_dir_test(QString("%1/BDMV").arg(filename));
275  if (bd_dir_test.exists())
276  extension = "BDMV";
277 
278  QString play_command = gCoreContext->GetSetting("VideoDefaultPlayer");
279 
280  const FileAssociations::association_list fa_list =
282  for (const auto & fa : fa_list)
283  {
284  if (fa.extension.toLower() == extension.toLower() &&
285  !fa.use_default)
286  {
287  play_command = fa.playcommand;
288  break;
289  }
290  }
291 
292  if (play_command.trimmed().isEmpty())
293  play_command = "Internal";
294 
295  QString plot;
296  QString title = VideoMetadata::FilenameToMeta(filename, 1);
297  QString subtitle = VideoMetadata::FilenameToMeta(filename, 4);
298  QString director;
299  int season = 0;
300  int episode = 0;
301  QString inetref;
302  int length = 0;
303  QString year = QString::number(VIDEO_YEAR_DEFAULT);
304  QString id;
305 
306  if (extraData)
307  {
308  plot = extraData->GetPlot();
309  title = extraData->GetTitle();
310  subtitle = extraData->GetSubtitle();
311  director = extraData->GetDirector();
312  season = extraData->GetSeason();
313  episode = extraData->GetEpisode();
314  inetref = extraData->GetInetRef();
315  length = extraData->GetLength();
316  year = QString::number(extraData->GetYear());
317  id = QString::number(extraData->GetID());
318  }
319 
320  AddPlayer(play_command, filename, plot, title, subtitle, director,
321  season, episode, inetref, length, year, id);
322  }
323 
325  {
326  for (auto & player : m_playerProcs)
327  delete player;
328  m_playerProcs.clear();
329  }
330 
331  void Play() const
332  {
333  for (auto *player : m_playerProcs)
334  if (player->Play())
335  break;
336  }
337 
338  QString GetCommandDisplayName() const
339  {
340  if (!m_playerProcs.empty())
341  return m_playerProcs.front()->GetCommandDisplayName();
342  return QString();
343  }
344 
345  private:
346  void AddPlayer(const QString &player, const QString &filename,
347  const QString &plot, const QString &title, const QString &subtitle,
348  const QString &director, int season, int episode, const QString &inetref,
349  int length, const QString &year, const QString &id)
350  {
352  plot, title, subtitle, director, season, episode, inetref,
353  length, year, id));
355  }
356 
357  private:
358  using player_list = std::vector<VideoPlayProc *>;
360 };
361 
363 
365 {
366  VideoPlayerCommand ret;
367  ret.m_d->AltPlayerFor(item);
368  return ret;
369 }
370 
372 {
373  VideoPlayerCommand ret;
374  ret.m_d->PlayerFor(item);
375  return ret;
376 }
377 
379 {
380  VideoPlayerCommand ret;
381  ret.m_d->PlayerFor(filename);
382  return ret;
383 }
384 
386 {
388 }
389 
391 {
392  delete m_d;
393  m_d = nullptr;
394 }
395 
397 {
398  m_d = new VideoPlayerCommandPrivate(*other.m_d);
399 }
400 
402 {
403  if (this != &rhs)
404  {
405  delete m_d;
406  m_d = new VideoPlayerCommandPrivate(*rhs.m_d);
407  }
408  return *this;
409 }
410 
412 {
413  LCD *lcd = LCD::Get();
414 
415  if (lcd) {
416  lcd->setFunctionLEDs(FUNC_TV, false);
417  lcd->setFunctionLEDs(FUNC_MOVIE, true);
418  }
419  gCoreContext->WantingPlayback(nullptr);
421  m_d->Play();
424  GetMythMainWindow()->raise();
425  GetMythMainWindow()->activateWindow();
426  if (lcd)
427  lcd->setFunctionLEDs(FUNC_MOVIE, false);
428 }
429 
431 {
432  return m_d->GetCommandDisplayName();
433 }
void PlayerFor(const QString &filename, const VideoMetadata *extraData=nullptr)
virtual VideoPlayProc * Clone() const =0
int GetLength() const
QString GetCommandDisplayName() const
Returns the player command suitable for display to the user.
const QString & GetPlayCommand() const
VideoPlayHandleMedia(QString handler, QString mrl, QString plot, QString title, QString subtitle, QString director, int season, int episode, QString inetref, int length, QString year, QString id)
int GetSeason() const
VideoPlayMythSystem * Clone() const override
void AltPlayerFor(const VideoMetadata *item)
VideoPlayerCommandPrivate(const VideoPlayerCommandPrivate &other)
const association_list & getList() const
Definition: dbaccess.cpp:806
static VideoPlayHandleMedia * Create(const QString &handler, const QString &mrl, const QString &plot, const QString &title, const QString &subtitle, const QString &director, int season, int episode, const QString &inetref, int length, const QString &year, const QString &id)
static VideoPlayerCommand AltPlayerFor(const VideoMetadata *item)
virtual ~VideoPlayProc()=default
static VideoPlayMythSystem * Create(const QString &command, const QString &filename)
bool IsHostSet() const
VideoPlayProc & operator=(const VideoPlayProc &)
const QString & GetDirector() const
std::vector< file_association > association_list
Definition: dbaccess.h:154
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
void PlayerFor(const VideoMetadata *item)
bool Play() const override
static guint32 * tmp
Definition: goom_core.c:35
unsigned int GetID() const
class VideoPlayerCommandPrivate * m_d
virtual QString GetCommandDisplayName() const =0
static LCD * Get(void)
Definition: lcddevice.cpp:65
int GetYear() const
VideoPlayerCommand & operator=(const VideoPlayerCommand &rhs)
const QString & GetPlot() const
void emitTVPlaybackStopped(void)
int GetEpisode() const
QString GetSetting(const QString &key, const QString &defaultval="")
virtual bool Play() const =0
QString GetCommandDisplayName() const override
QString GetCommandDisplayName() const override
void AddPlayer(const QString &player, const QString &filename, const QString &plot, const QString &title, const QString &subtitle, const QString &director, int season, int episode, const QString &inetref, int length, const QString &year, const QString &id)
static QString FilenameToMeta(const QString &file_name, int position)
static VideoPlayerCommand PlayerFor(const VideoMetadata *item)
uint myth_system(const QString &command, uint flags, uint timeout)
const QString & GetHost() const
static FileAssociations & getFileAssociation()
Definition: dbaccess.cpp:831
MythMainWindow * GetMythMainWindow(void)
VideoPlayerCommandPrivate & operator=(const VideoPlayerCommandPrivate &rhs)=delete
void PauseIdleTimer(bool pause)
const QString & GetFilename() const
VideoPlayHandleMedia * Clone() const override
VideoPlayerCommandPrivate()=default
Definition: lcddevice.h:169
QString generate_file_url(const QString &storage_group, const QString &host, const QString &path)
Definition: videoutils.h:65
const QString & GetSubtitle() const
VideoPlayMythSystem(QString disp_command, QString play_command)
bool HandleMedia(const QString &handler, const QString &mrl, const QString &plot="", const QString &title="", const QString &subtitle="", const QString &director="", int season=0, int episode=0, const QString &inetref="", int lenMins=120, const QString &year="1895", const QString &id="", bool useBookmarks=false)
const QString & GetInetRef() const
const QString & GetTitle() const
void WantingPlayback(QObject *sender)
All the objects that have registered using MythCoreContext::RegisterForPlayback but sender will be ca...
void setFunctionLEDs(enum LCDFunctionSet func, bool on)
Definition: lcddevice.cpp:426
VideoPlayProc()=default
bool Play() const override
std::vector< VideoPlayProc * > player_list