-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
307 lines (263 loc) · 7.67 KB
/
player.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
/*
* Mediaplayer plugin for VDR
*
* (C) 2018 by zille. All Rights Reserved.
*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Or, point your browser to http://www.gnu.org/copyleft/gpl.html
*/
#include <fstream>
#include <string>
#include <cstdlib>
using std::ifstream;
using std::string;
#include <vdr/player.h>
#include <vdr/plugin.h>
#include "player.h"
#include "control.h"
#include "menu.h"
#include "setupmenu.h"
#include "mediaplayer.h"
extern "C"
{
#include <libavformat/avformat.h>
}
volatile int cMediaPlayer::Running = 0;
cMediaPlayer *cMediaPlayer::pPlayer = NULL;
cMediaPlayer::cMediaPlayer(const char *PL)
:cPlayer (pmAudioOnly)
{
pPlayer= this;
Playlist = (char *) malloc(1 + strlen(PL));
strcpy(Playlist, PL);
DeviceClear();
ReadPL();
StopPlay = 0;
PausePlay = 0;
ThisFile = 0;
RandomPlay = 0;
}
cMediaPlayer::~cMediaPlayer(void)
{
Running = 0;
StopFile = 1;
StopPlay = 1;
free(Playlist);
}
void cMediaPlayer::Activate(bool On)
{
if (On)
Start();
}
void cMediaPlayer::ReadPL(void)
{
ifstream f;
int i = 0;
f.open(Playlist);
if (!f.good())
esyslog("Mediaplayer: open PL %s failed\n", Playlist);
while (!f.eof() && i < 5000) {
string s;
getline(f, s);
if (s.size() && s.compare(1, 1, "/")) {
MediaFiles[i].Path = s;
MediaFiles[i].File = MediaFiles[i].Path.substr(MediaFiles[i].Path.find_last_of("/")+1,
string::npos);
string SubString = MediaFiles[i].Path.substr(0, MediaFiles[i].Path.find_last_of("/"));
MediaFiles[i].SubFolder = SubString.substr(SubString.find_last_of("/")+1, string::npos);
string FolderString = MediaFiles[i].Path.substr(0, SubString.find_last_of("/"));
MediaFiles[i].Folder = FolderString.substr(FolderString.find_last_of("/")+1, string::npos);
i++;
}
}
LastFile = i - 1;
CurrentFile = 0;
f.close();
}
void cMediaPlayer::DelFromPL(int title)
{
LastFile--;
while (LastFile >= title) {
MediaFiles[title] = MediaFiles[title + 1];
title++;
}
if (cMediaPlayerControl::Control()->MenuOpen)
cMediaPlayerMenu::Menu()->PlayMenu();
// Write new PL
FILE *playlist = fopen(Playlist, "w");
for (int i = 0; i <= LastFile; i++) {
fprintf(playlist, "%s\n", MediaFiles[i].Path.c_str());
}
fclose (playlist);
}
void cMediaPlayer::PlayFile(const char *path)
{
struct buf {
unsigned char *buff;
size_t size;
};
int max_buffers = 150;
int next_buffer = 0;
struct buf bufs[max_buffers];
int err = 0;
cPoller oPoller;
static const unsigned char header[] = {
0x00, // byte 0: PES header
0x00, // byte 1
0x01, // byte 2
0xC0, // byte 3: Stream ID: one MPEG1 or MPEG2 audio stream
0x00, // byte 4: PES packet length
0x00, // byte 5
0x87, // byte 6: mpeg2, aligned, copyright, original
0x00, // byte 7: no pts/dts
0x00, // byte 8: PES header length after this field, 0 can be only if video stream
0x00, // byte 9
0xFF, // byte 10
0x00, // byte 11
0x00, // byte 12
0x00, // byte 13
0x01, // byte 14
0x80 // byte 15
};
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,18,100)
av_register_all();
#endif
// get format from audio file
AVFormatContext* format = avformat_alloc_context();
if (avformat_open_input(&format, path, NULL, NULL) != 0) {
esyslog("Mediaplayer: Could not open file '%s'\n", path);
return;
}
if (avformat_find_stream_info(format, NULL) < 0) {
esyslog("Mediaplayer: Could not retrieve stream info from file '%s'\n", path);
return;
}
AVPacket packet;
av_init_packet(&packet);
int secs = format->duration / AV_TIME_BASE;
int us = format->duration % AV_TIME_BASE;
int mins = secs / 60;
secs %= 60;
int hours = mins / 60;
mins %= 60;
#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(57,89,100)
dsyslog("Mediaplayer: Play %s codec: %s duration %02d:%02d:%02d.%02d\n",
format->url, format->iformat->name, hours, mins, secs,
(100 * us) / AV_TIME_BASE);
#else
dsyslog("Mediaplayer: Play %s codec: %s duration %02d:%02d:%02d.%02d\n",
format->filename, format->iformat->name, hours, mins, secs,
(100 * us) / AV_TIME_BASE);
#endif
while (next_buffer != max_buffers) {
bufs[next_buffer].buff = NULL;
bufs[next_buffer].size = 0;
next_buffer++;
}
next_buffer = 0;
// iterate through frames
while (err == 0 && !StopFile) {
err = av_read_frame(format, &packet);
if (err == 0) {
// make header + data
if (bufs[next_buffer].buff && (bufs[next_buffer].size != (size_t)packet.size + sizeof(header))) {
free(bufs[next_buffer].buff);
bufs[next_buffer].buff = NULL;
bufs[next_buffer].size = 0;
}
if (!bufs[next_buffer].buff) {
bufs[next_buffer].buff = (unsigned char *) malloc(packet.size + sizeof(header));
bufs[next_buffer].size = packet.size + sizeof(header);
}
memcpy(bufs[next_buffer].buff, header, sizeof(header));
memcpy(bufs[next_buffer].buff + sizeof(header), packet.data, packet.size);
bufs[next_buffer].buff[4] = ((bufs[next_buffer].size - 6) >> 8) & 0xFF;
bufs[next_buffer].buff[5] = (bufs[next_buffer].size - 6) & 0xFF;
bufs[next_buffer].buff[8] = 7; // pes_header_data_length
repeat:
DevicePoll(oPoller, 100);
if (PlayPes(bufs[next_buffer].buff, bufs[next_buffer].size, false) <= 0) {
usleep(packet.duration * AV_TIME_BASE * format->streams[0]->time_base.num
/ format->streams[0]->time_base.den);
goto repeat;
}
if (next_buffer == max_buffers - 1)
next_buffer = 0;
else next_buffer++;
if (PausePlay) {
DeviceClear();
Jump = cPluginMediaplayer::BufferOffset;
while (PausePlay) {
sleep(1);
}
}
if (Jump) {
av_seek_frame(format, format->streams[0]->index,
packet.pts + (int64_t)((Jump - cPluginMediaplayer::BufferOffset) *
format->streams[0]->time_base.den / format->streams[0]->time_base.num),
AVSEEK_FLAG_ANY);
DeviceClear();
Jump = 0;
}
}
}
if (StopFile)
DeviceClear();
next_buffer = max_buffers - 1;
while (next_buffer > -1) {
free(bufs[next_buffer].buff);
next_buffer--;
}
next_buffer = 0;
av_packet_unref(&packet);
avformat_close_input(&format);
avformat_free_context(format);
}
void cMediaPlayer::Play(int index)
{
CurrentFile = index;
ThisFile = 1;
StopFile = 1;
}
void cMediaPlayer::Next(void)
{
StopFile = 1;
}
void cMediaPlayer::Previous(void)
{
CurrentFile--;
StopFile = 1;
}
void cMediaPlayer::Action(void)
{
Running = 1;
DevicePlay();
while (CurrentFile <= LastFile && !StopPlay) {
StopFile = 0;
if (cMediaPlayerControl::Control()->MenuOpen)
cMediaPlayerMenu::Menu()->PlayMenu();
PlayFile(MediaFiles[CurrentFile].Path.c_str());
if (ThisFile) {
ThisFile = 0;
continue;
}
if (RandomPlay) {
srand (time (NULL));
CurrentFile = std::rand() % (LastFile + 1);
} else CurrentFile++;
}
sleep (10);
cMediaPlayerControl::Control()->Close = 1;
}