-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplayer.c
391 lines (336 loc) · 10.8 KB
/
player.c
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
/*******************************************************************************
* player.c
*
* history:
* 2023-05-18 - [piaodazhu] Improve: progress bar
* 2023-05-18 - [piaodazhu] Fix: return value uncheck
* 2023-05-18 - [piaodazhu] Improve: better print message
* 2023-05-17 - [piaodazhu] Improve: support seek
* 2023-05-17 - [piaodazhu] Improve: support window resize
* 2023-05-17 - [piaodazhu] Fix: audio cannot be paused
* 2023-05-16 - [piaodazhu] Fix: cannot normally quit
* 2023-05-16 - [piaodazhu] Fix: AVPacketList is deprecated
*
* 2018-11-27 - [lei] Create file: a simplest ffmpeg player
* 2018-12-01 - [lei] Playing audio
* 2018-12-06 - [lei] Playing audio&vidio
* 2019-01-06 - [lei] Add audio resampling, fix bug of unsupported audio
* format(such as planar)
* 2019-01-16 - [lei] Sync video to audio.
*
* details:
* A simple ffmpeg player.
*
* refrence:
* ffplay.c in FFmpeg 4.1 project.
*******************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include "player.h"
#include "frame.h"
#include "packet.h"
#include "demux.h"
#include "video.h"
#include "audio.h"
static player_stat_t *player_init(const char *p_input_file);
static int player_deinit(player_stat_t *is);
// 返回值:返回上一帧的pts更新值(上一帧pts+流逝的时间)
double get_clock(play_clock_t *c)
{
if (*c->queue_serial != c->serial)
{
return NAN;
}
if (c->paused)
{
return c->pts;
}
else
{
double time = av_gettime_relative() / 1000000.0;
double ret = c->pts_drift + time; // 展开得: c->pts + (time - c->last_updated)
return ret;
}
}
void set_clock_at(play_clock_t *c, double pts, int serial, double time)
{
c->pts = pts;
c->last_updated = time;
c->pts_drift = c->pts - time;
c->serial = serial;
}
void set_clock(play_clock_t *c, double pts, int serial)
{
double time = av_gettime_relative() / 1000000.0;
set_clock_at(c, pts, serial, time);
}
static void set_clock_speed(play_clock_t *c, double speed)
{
set_clock(c, get_clock(c), c->serial);
c->speed = speed;
}
void init_clock(play_clock_t *c, int *queue_serial)
{
c->speed = 1.0;
c->paused = 0;
c->queue_serial = queue_serial;
set_clock(c, NAN, -1);
}
static void sync_play_clock_to_slave(play_clock_t *c, play_clock_t *slave)
{
double clock = get_clock(c);
double slave_clock = get_clock(slave);
if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD))
set_clock(c, slave_clock, slave->serial);
}
static void do_exit(player_stat_t *is)
{
if (is)
{
player_deinit(is);
}
if (is->sdl_video.renderer)
SDL_DestroyRenderer(is->sdl_video.renderer);
if (is->sdl_video.window)
SDL_DestroyWindow(is->sdl_video.window);
avformat_network_deinit();
SDL_Quit();
av_log(NULL, AV_LOG_INFO, "\nQUIT\n");
exit(0);
}
static player_stat_t *player_init(const char *p_input_file)
{
player_stat_t *is;
is = av_mallocz(sizeof(player_stat_t));
if (!is)
{
return NULL;
}
is->filename = av_strdup(p_input_file);
if (is->filename == NULL)
{
goto fail;
}
/* start video display */
if (frame_queue_init(&is->video_frm_queue, &is->video_pkt_queue, VIDEO_PICTURE_QUEUE_SIZE, 1) < 0 ||
frame_queue_init(&is->audio_frm_queue, &is->audio_pkt_queue, SAMPLE_QUEUE_SIZE, 1) < 0)
{
goto fail;
}
if (packet_queue_init(&is->video_pkt_queue) < 0 ||
packet_queue_init(&is->audio_pkt_queue) < 0)
{
goto fail;
}
packet_queue_put_nullpacket(&is->video_pkt_queue, is->video_idx);
packet_queue_put_nullpacket(&is->audio_pkt_queue, is->audio_idx);
if (!(is->continue_read_thread = SDL_CreateCond()))
{
av_log(NULL, AV_LOG_FATAL, "SDL_CreateCond(): %s\n", SDL_GetError());
fail:
player_deinit(is);
exit(1);
}
init_clock(&is->video_clk, &is->video_pkt_queue.serial);
init_clock(&is->audio_clk, &is->audio_pkt_queue.serial);
is->abort_request = 0;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
{
av_log(NULL, AV_LOG_FATAL, "Could not initialize SDL - %s\n", SDL_GetError());
av_log(NULL, AV_LOG_FATAL, "(Did you set the DISPLAY variable?)\n");
exit(1);
}
return is;
}
static int player_deinit(player_stat_t *is)
{
/* XXX: use a special url_shutdown call to abort parse cleanly */
is->abort_request = 1;
packet_queue_abort(&is->video_pkt_queue);
packet_queue_abort(&is->audio_pkt_queue);
SDL_WaitThread(is->read_tid, NULL);
avformat_close_input(&is->p_fmt_ctx);
SDL_WaitThread(is->audio_dec_tid, NULL);
SDL_WaitThread(is->video_dec_tid, NULL);
SDL_WaitThread(is->video_ply_tid, NULL);
packet_queue_destroy(&is->video_pkt_queue);
packet_queue_destroy(&is->audio_pkt_queue);
/* free all pictures */
frame_queue_destory(&is->video_frm_queue);
frame_queue_destory(&is->audio_frm_queue);
SDL_DestroyCond(is->continue_read_thread);
sws_freeContext(is->img_convert_ctx);
av_free(is->filename);
if (is->sdl_video.texture)
{
SDL_DestroyTexture(is->sdl_video.texture);
}
av_free(is);
return 0;
}
/* pause or resume the video */
static void stream_toggle_pause(player_stat_t *is)
{
if (is->paused)
{
// 这里表示当前是暂停状态,将切换到继续播放状态。在继续播放之前,先将暂停期间流逝的时间加到frame_timer中
is->frame_timer += av_gettime_relative() / 1000000.0 - is->video_clk.last_updated;
set_clock(&is->video_clk, get_clock(&is->video_clk), is->video_clk.serial);
}
is->paused = is->audio_clk.paused = is->video_clk.paused = !is->paused;
}
static void toggle_pause(player_stat_t *is)
{
stream_toggle_pause(is);
is->step = 0;
}
/* seek in the stream */
static void stream_seek(player_stat_t *is, int64_t pos, int64_t rel)
{
if (!is->seek_req) {
is->seek_pos = pos;
is->seek_rel = rel;
is->seek_req = 1;
SDL_CondSignal(is->continue_read_thread);
}
}
int time_str(double time, char *buf, int len) {
if (len <= strlen("hh:mm:ss.ff")) {
*buf = 0;
return -1;
}
double integer = floor(time);
double fractional = time - integer;
int i = (int)integer;
int f = (int)(100 * fractional);
int hh = i / 3600;
i %= 3600;
int mm = i / 60;
i %= 60;
int ss = i;
return snprintf(buf, len, "%02d:%02d:%02d.%02d", hh, mm, ss, f);
}
int progress_bar(double time, double total, char *buf, int len) {
if (len <= 60) {
*buf = 0;
return -1;
}
int n = (int)ceil((time * 60) / total);
int idx = 0;
while (idx < n - 1) {
buf[idx++] = '=';
}
buf[idx++] = '>';
while (idx < 60) {
buf[idx++] = '.';
}
buf[idx++] = 0;
return 60;
}
int player_running(const char *p_input_file)
{
player_stat_t *is = NULL;
int ret;
// 初始化队列,初始化SDL系统,分配player_stat_t结构体
is = player_init(p_input_file);
if (is == NULL)
{
do_exit(is);
}
// 文件解封装
ret = open_demux(is);
if (ret < 0) {
do_exit(is);
}
// 视频解码与播放
ret = open_video(is);
if (ret < 0) {
do_exit(is);
}
// 音频解码与播放
ret = open_audio(is);
if (ret < 0) {
do_exit(is);
}
SDL_Event event;
double incr, pos;
double duration, now;
duration = (double)is->p_fmt_ctx->duration / AV_TIME_BASE;
char totaltime[20], playtime[20], bar[80];
time_str(duration, totaltime, 20);
av_log(NULL, AV_LOG_INFO, "Control: \n\tquit: <ESC>\n\tpause/unpause: <SPACE>\n\tforward/backward: <R/L/U/D>\n\n");
while (1)
{
SDL_PumpEvents();
// SDL event队列为空,则在while循环中播放视频帧。否则从队列头部取一个event,退出当前函数,在上级函数中处理event
while (!SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT))
{
now = is->audio_clk.pts;
if (isnan(now)) {
now = 0.0;
}
time_str(now, playtime, 20);
progress_bar(now, duration, bar, 80);
av_log(NULL, AV_LOG_INFO, "[%s/%s] (%s) \r", playtime, totaltime, bar);
av_usleep(100000);
SDL_PumpEvents();
}
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE) // ESC: 退出
{
do_exit(is);
break;
}
switch (event.key.keysym.sym) {
case SDLK_SPACE: // 空格键: 暂停
toggle_pause(is);
break;
case SDLK_LEFT: // 方向键: 快进快退
incr = -10.0;
goto do_seek;
case SDLK_RIGHT:
incr = 10.0;
goto do_seek;
case SDLK_UP:
incr = 60.0;
goto do_seek;
case SDLK_DOWN:
incr = -60.0;
do_seek:
pos = is->audio_clk.pts;
pos += incr;
if (is->start_time != AV_NOPTS_VALUE && pos < is->start_time / (double)AV_TIME_BASE)
pos = is->start_time / (double)AV_TIME_BASE;
stream_seek(is, (int64_t)(pos * AV_TIME_BASE), (int64_t)(incr * AV_TIME_BASE));
break;
default:
break;
}
break;
case SDL_WINDOWEVENT:
// 窗口大小伸缩 -> 画面适应
switch (event.window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
is->sdl_video.window_width = event.window.data1;
is->sdl_video.window_height = event.window.data2;
if (is->sdl_video.window_width * is->sdl_video.height_width_ratio < (double)is->sdl_video.window_height) {
is->sdl_video.width = is->sdl_video.window_width;
is->sdl_video.height = (int)(is->sdl_video.window_width * is->sdl_video.height_width_ratio);
} else {
is->sdl_video.height = is->sdl_video.window_height;
is->sdl_video.width = (int)(is->sdl_video.window_height / is->sdl_video.height_width_ratio);
}
}
break;
case SDL_QUIT:
case FF_QUIT_EVENT:
do_exit(is);
break;
default:
break;
}
}
return 0;
}