-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathplayer.c
279 lines (234 loc) · 6.87 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
/*******************************************************************************
* player.c
*
* history:
* 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();
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;
}
AVPacket flush_pkt;
flush_pkt.data = NULL;
packet_queue_put(&is->video_pkt_queue, &flush_pkt);
packet_queue_put(&is->audio_pkt_queue, &flush_pkt);
if (!(is->continue_read_thread = SDL_CreateCond()))
{
av_log(NULL, AV_LOG_FATAL, "SDL_CreateCond(): %s\n", SDL_GetError());
fail:
player_deinit(is);
goto fail;
}
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;
SDL_WaitThread(is->read_tid, NULL);
/* close each stream */
if (is->audio_idx >= 0)
{
//stream_component_close(is, is->p_audio_stream);
}
if (is->video_idx >= 0)
{
//stream_component_close(is, is->p_video_stream);
}
avformat_close_input(&is->p_fmt_ctx);
packet_queue_abort(&is->video_pkt_queue);
packet_queue_abort(&is->audio_pkt_queue);
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;
}
int player_running(const char *p_input_file)
{
player_stat_t *is = NULL;
is = player_init(p_input_file);
if (is == NULL)
{
printf("player init failed\n");
do_exit(is);
}
open_demux(is);
open_video(is);
open_audio(is);
SDL_Event event;
while (1)
{
SDL_PumpEvents();
// SDL event队列为空,则在while循环中播放视频帧。否则从队列头部取一个event,退出当前函数,在上级函数中处理event
while (!SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT))
{
av_usleep(100000);
SDL_PumpEvents();
}
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
do_exit(is);
break;
}
switch (event.key.keysym.sym) {
case SDLK_SPACE: // 空格键:暂停
toggle_pause(is);
break;
case SDL_WINDOWEVENT:
break;
default:
break;
}
break;
case SDL_QUIT:
case FF_QUIT_EVENT:
do_exit(is);
break;
default:
break;
}
}
return 0;
}