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