-
Notifications
You must be signed in to change notification settings - Fork 2
/
player.c
497 lines (431 loc) · 16.5 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
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "player.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/stat.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#define PLAYER_DURATION_MARGIN 2
static SwrFormat get_dest_sample_fmt_from_sample_fmt(struct SwrContext **swr_ctx, SwrFormat src)
{
int i, ret;
SwrFormat dest = src;
dest.sample_fmt = AV_SAMPLE_FMT_NONE;
struct sample_fmt_entry {
enum AVSampleFormat src_sample_fmt;
enum AVSampleFormat dest_sample_fmt;
int bits;
} sample_fmt_entries[] = {
{ AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_S16, 16 },
};
for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
if (sample_fmt_entries[i].src_sample_fmt == src.sample_fmt) {
dest.sample_fmt = sample_fmt_entries[i].dest_sample_fmt;
dest.bits = sample_fmt_entries[i].bits;
break;
}
}
if (dest.sample_fmt == AV_SAMPLE_FMT_NONE)
return dest;
/* create resampler context */
if (!*swr_ctx) {
*swr_ctx = swr_alloc();
if (!*swr_ctx) {
printf("Could not allocate resampler context\n");
return dest;
}
}
av_opt_set_int(*swr_ctx, "in_channel_layout", src.channel_layout, 0);
av_opt_set_int(*swr_ctx, "in_sample_rate", src.sample_rate, 0);
av_opt_set_sample_fmt(*swr_ctx, "in_sample_fmt", src.sample_fmt, 0);
av_opt_set_int(*swr_ctx, "out_channel_layout", dest.channel_layout, 0);
av_opt_set_int(*swr_ctx, "out_sample_rate", dest.sample_rate, 0);
av_opt_set_sample_fmt(*swr_ctx, "out_sample_fmt", dest.sample_fmt, 0);
/* initialize the resampling context */
if ((ret = swr_init(*swr_ctx)) < 0) {
printf("Failed to initialize the resampling context\n");
return dest;
}
return dest;
}
int fm_player_pos(fm_player_t *pl)
{
return pl->info.duration * pl->info.time_base.num / pl->info.time_base.den;
}
int fm_player_length(fm_player_t *pl)
{
return pl->info.length;
}
static int wait_new_content(fm_player_t *pl)
{
int ret = 0;
pthread_mutex_lock(pl->song->mutex_downloader);
if (pl->song->downloader) {
printf("Waiting for some new content to arrive\n");
pthread_cond_wait(&pl->song->downloader->cond_new_content, pl->song->mutex_downloader);
printf("Wait finished\n");
} else {
// there is no more downloader associated with this song
// check if the current song is already complete
int pos = fm_player_pos(pl);
int len = fm_player_length(pl);
if (len - pos >= PLAYER_DURATION_MARGIN) {
printf("Incomplete song ended with current pos %d / %d\n", pos, len);
// unmark the like field to make sure that this song is removed
pl->song->like = 0;
}
if (pl->tid_ack > 0) {
pthread_kill(pl->tid_ack, pl->sig_ack);
}
ret = -1;
}
pthread_mutex_unlock(pl->song->mutex_downloader);
return ret;
}
static int open_song(fm_player_t *pl)
{
printf("Attempting to open the input\n");
if (avformat_open_input(&pl->format_context, pl->song->filepath, NULL, NULL) < 0) {
printf("Failure on opening the input stream\n");
return -1;
} else
printf("Opened format input\n");
// seek to the beginning of the file to avoid problem
printf("Attempting to find the stream info\n");
if (avformat_find_stream_info(pl->format_context, NULL) < 0) {
printf("Cannot find stream info\n");
return -1;
}
printf("Attempting to find the best stream\n");
printf("Number of streams available: %d\n", pl->format_context->nb_streams);
pl->audio_stream_idx = av_find_best_stream(pl->format_context, AVMEDIA_TYPE_AUDIO, -1, -1, &pl->codec, 0);
if (pl->audio_stream_idx < 0) {
printf("Couldn't find stream information\n");
return -1;
}
// check for bitrate
/*printf("The estimated bitrate of the song is %d\n", pl->format_context->bit_rate);*/
// Get a pointer to the codec context for the audio stream
AVStream *stream = pl->format_context->streams[pl->audio_stream_idx];
// set the timebase
pl->info.time_base = stream->time_base;
pl->context = stream->codec;
printf("Attempting to open the codec\n");
if (avcodec_open2(pl->context, pl->codec, NULL) < 0) {
printf("Could not open codec\n");
return -1;
}
// initialize the ao_sample format
ao_sample_format ao_fmt;
// adjusting for resampling
printf("Attempting to adjusting for resampling\n");
pl->src_swr_format.sample_fmt = pl->context->sample_fmt;
pl->src_swr_format.channel_layout = pl->context->channel_layout;
pl->src_swr_format.sample_rate = pl->context->sample_rate;
pl->src_swr_format.bits = 0;
pl->dest_swr_format = pl->src_swr_format;
switch(pl->context->sample_fmt) {
case AV_SAMPLE_FMT_U8: ao_fmt.bits = 8; pl->resampled = 0; break;
case AV_SAMPLE_FMT_S16: ao_fmt.bits = 16; pl->resampled = 0; break;
case AV_SAMPLE_FMT_S32: ao_fmt.bits = 32; pl->resampled = 0; break;
case AV_SAMPLE_FMT_U8P: ao_fmt.bits = 8; pl->resampled = 0; break;
case AV_SAMPLE_FMT_S16P: ao_fmt.bits = 16; pl->resampled = 0; break;
case AV_SAMPLE_FMT_S32P: ao_fmt.bits = 32; pl->resampled = 0; break;
default:
pl->resampled = 1;
printf("Resampling needs to be done\n");
pl->dest_swr_format = get_dest_sample_fmt_from_sample_fmt(&pl->swr_context, pl->src_swr_format);
if (!pl->swr_context) {
printf("Cannot resample the data in the specified stream. Sample fmt is %d\n", pl->src_swr_format.sample_fmt);
return -1;
}
ao_fmt.bits = pl->dest_swr_format.bits;
break;
}
printf("ao setup: bits is %d\n", ao_fmt.bits);
ao_fmt.channels = pl->context->channels;
printf("ao setup: channels is %d\n", ao_fmt.channels);
ao_fmt.rate = pl->context->sample_rate;
printf("ao setup: sampling rate is %d\n", ao_fmt.rate);
ao_fmt.byte_format = AO_FMT_NATIVE;
ao_fmt.matrix = 0;
pl->dev = ao_open_live(pl->driver, &ao_fmt, pl->ao_options);
if (pl->dev == NULL) {
printf("Failed to open the ao device.\n");
return -1;
}
printf("Song openning process finished.\n");
return 0;
}
static void close_song(fm_player_t *pl)
{
if (pl->context) {
avcodec_close(pl->context);
pl->context = NULL;
}
if (pl->format_context) {
avformat_close_input(&pl->format_context);
pl->format_context = NULL;
}
// close the resampling context, if any
if (pl->swr_context) {
swr_free(&pl->swr_context);
pl->swr_context = NULL;
}
if (pl->swr_buf) {
av_freep(pl->swr_buf);
pl->swr_buf = NULL;
pl->dest_swr_nb_samples = 0;
}
// close the interweave buf, if any
if (pl->interweave_buf) {
av_freep(&pl->interweave_buf);
pl->interweave_buf = NULL;
pl->interweave_buf_size = 0;
}
// close the ao playing device
if (pl->dev) {
ao_close(pl->dev);
pl->dev = NULL;
}
}
static void* play_thread(void *data)
{
printf("Entered play thread\n");
fm_player_t *pl = (fm_player_t*) data;
int ret;
// read the audio frames
int got_frame;
// interweaving
uint8_t *interweave_data;
// for resampling
int dest_nb_samples;
// final read buffer
char *ao_buf;
int ao_size;
// first conditions to satisfy (importance ordered from high to low
// 1. the play state is not STOP
// 2. the filepath is not nil
// 3. there is a minimum part of the file downloaded
while (pl->status != FM_PLAYER_STOP) {
pthread_mutex_lock(&pl->mutex_status);
while (pl->status == FM_PLAYER_PAUSE) {
pthread_cond_wait(&pl->cond_play, &pl->mutex_status);
}
pthread_mutex_unlock(&pl->mutex_status);
if (pl->status == FM_PLAYER_STOP) {
break;
}
if (pl->song->filepath[0] == '\0') {
/*printf("Blocking on waiting for filepath being assigned\n");*/
continue;
}
if (!pl->context) {
// wait for the file to grow at least to a considerable size
struct stat st;
stat(pl->song->filepath, &st);
if (st.st_size < HEADERBUF_SIZE) {
printf("Blocking on waiting for the file to have some initial size\n");
if (wait_new_content(pl) == 0) continue;
else return pl;
} else if (open_song(pl) != 0) {
printf("Opening song failed. Retry again.\n");
close_song(pl);
// we should wait as well, since the natural reason for not able to open the song should be not enough data
if (wait_new_content(pl) == 0) continue;
else return pl;
}
}
// decode the frame
/*printf("Attempting to read the frame\n");*/
if ((ret = av_read_frame(pl->format_context, &pl->avpkt)) < 0) {
// free the packet first
av_free_packet(&pl->avpkt);
// couldn't decode the frame
printf("Could not read the frame\n");
if (wait_new_content(pl) == 0) continue;
else return pl;
}
if (pl->avpkt.stream_index == pl->audio_stream_idx) {
avcodec_get_frame_defaults(pl->frame);
/*printf("Attempting to decode the music\n");*/
ret = avcodec_decode_audio4(pl->context, pl->frame, &got_frame, &pl->avpkt);
if (ret < 0) {
printf("Error decoding audio\n");
} else if (got_frame) {
/*printf("Got frame to play\n");*/
ao_size = av_samples_get_buffer_size(NULL, pl->frame->channels, pl->frame->nb_samples, pl->frame->format, 1);
ao_buf = (char *) pl->frame->extended_data[0];
// first resample the buffer if necessary
if (pl->resampled) {
dest_nb_samples = av_rescale_rnd(swr_get_delay(pl->swr_context, pl->src_swr_format.sample_rate) + pl->frame->nb_samples, pl->src_swr_format.sample_rate, pl->src_swr_format.sample_rate, AV_ROUND_UP);
if (dest_nb_samples > pl->dest_swr_nb_samples) {
printf("dest_nb_samples %d exceeding current nb %d. Reallocating the resampling buffer\n", dest_nb_samples, pl->dest_swr_nb_samples);
if (pl->swr_buf)
av_freep(pl->swr_buf);
if (av_samples_alloc_array_and_samples(&pl->swr_buf, pl->frame->linesize, pl->frame->channels, dest_nb_samples, pl->dest_swr_format.sample_fmt, 0) < 0) {
printf("Could not allocate destination samples\n");
exit(-1);
}
pl->dest_swr_nb_samples = dest_nb_samples;
}
// covert to destination format
ret = swr_convert(pl->swr_context, pl->swr_buf, dest_nb_samples, (const uint8_t **) pl->frame->extended_data, pl->frame->nb_samples);
if (ret < 0) {
printf("Could not resample the audio\n");
exit(-1);
}
// get the resampled buffer size
ao_buf = (char *) *pl->swr_buf;
ao_size = av_samples_get_buffer_size(pl->frame->linesize, pl->frame->channels, ret, pl->dest_swr_format.sample_fmt, 1);
}
// copying the frame
if (av_sample_fmt_is_planar(pl->dest_swr_format.sample_fmt) && pl->frame->channels > 1) {
unsigned sample_size = av_get_bytes_per_sample(pl->dest_swr_format.sample_fmt);
// printf("Copying multiple channels\n");
if(pl->interweave_buf_size < ao_size) {
printf("buf size %d exceeding current interweave buf size %d. Reallocating the interweave buffer\n", ao_size, pl->interweave_buf_size);
if (pl->interweave_buf)
av_freep(&pl->interweave_buf);
pl->interweave_buf = av_malloc(ao_size);
if (!pl->interweave_buf) {
// Not enough memory - shouldn't happen
printf("Unable to allocate the buffer\n");
}
pl->interweave_buf_size = ao_size;
}
interweave_data = pl->interweave_buf;
unsigned f, channel;
for (f = 0; f < pl->frame->nb_samples; ++f) {
for (channel = 0; channel < pl->frame->channels; ++channel) {
memcpy(interweave_data, pl->frame->extended_data[channel] + f * sample_size, sample_size);
interweave_data += sample_size;
}
}
ao_buf = (char *) pl->interweave_buf;
}
ao_play(pl->dev, ao_buf, ao_size);
// add the duration to the info
pl->info.duration += pl->avpkt.duration;
}
}
av_free_packet(&pl->avpkt);
}
return pl;
}
int fm_player_open(fm_player_t *pl, fm_player_config_t *config)
{
pl->config = *config;
pl->driver = ao_driver_id(config->driver);
if (pl->driver == -1) {
return -1;
}
ao_info *driver_info = ao_driver_info(pl->driver);
printf("Player audio driver: %s\n", driver_info->name);
pl->ao_options = NULL;
if (config->dev[0] != '\0') {
ao_append_option(&pl->ao_options, "dev", config->dev);
}
pl->dev = NULL;
pl->tid_ack = 0;
pthread_mutex_init(&pl->mutex_status, NULL);
pthread_cond_init(&pl->cond_play, NULL);
pl->status = FM_PLAYER_STOP;
pl->song = NULL;
pl->context = NULL;
pl->format_context = NULL;
pl->codec = NULL;
pl->audio_stream_idx = 0;
// intialize the av frame
pl->frame = av_frame_alloc();
// interweave buffer setting
pl->interweave_buf = NULL;
pl->interweave_buf_size = 0;
// swr settings
pl->resampled = 0;
pl->swr_context = NULL;
pl->swr_buf = NULL;
pl->dest_swr_nb_samples = 0;
return 0;
}
void fm_player_close(fm_player_t *pl)
{
fm_player_stop(pl);
if (pl->ao_options)
ao_free_options(pl->ao_options);
pthread_mutex_destroy(&pl->mutex_status);
pthread_cond_destroy(&pl->cond_play);
// free the ffmpeg stuff
av_frame_free(&pl->frame);
}
int fm_player_set_song(fm_player_t *pl, fm_song_t *song)
{
fm_player_stop(pl);
if (!song) {
printf("No song to play\n");
return -1;
}
// set the song
pl->song = song;
// set the relevant properties
pl->info.duration = 0;
pl->info.time_base.num = pl->info.time_base.den = 1;
pl->info.length = song->length;
return 0;
}
void fm_player_set_ack(fm_player_t *pl, pthread_t tid, int sig)
{
pl->tid_ack = tid;
pl->sig_ack = sig;
}
void fm_player_play(fm_player_t *pl)
{
printf("Player play\n");
if (pl->status == FM_PLAYER_STOP) {
pl->status = FM_PLAYER_PLAY;
printf("Creating play thread\n");
pthread_create(&pl->tid_play, NULL, play_thread, pl);
printf("Finished creating play thread\n");
}
else if (pl->status == FM_PLAYER_PAUSE) {
pthread_mutex_lock(&pl->mutex_status);
pl->status = FM_PLAYER_PLAY;
pthread_mutex_unlock(&pl->mutex_status);
pthread_cond_signal(&pl->cond_play);
}
}
void fm_player_pause(fm_player_t *pl)
{
printf("Player pause\n");
pthread_mutex_lock(&pl->mutex_status);
pl->status = FM_PLAYER_PAUSE;
pthread_mutex_unlock(&pl->mutex_status);
}
void fm_player_stop(fm_player_t *pl)
{
if (pl->status != FM_PLAYER_STOP) {
printf("Player stop\n");
pthread_mutex_lock(&pl->mutex_status);
pl->status = FM_PLAYER_STOP;
pthread_mutex_unlock(&pl->mutex_status);
pthread_cond_signal(&pl->cond_play);
printf("Trying to signal cond new content\n");
if (pl->song && pl->song->downloader)
pthread_cond_signal(&pl->song->downloader->cond_new_content);
pthread_join(pl->tid_play, NULL);
close_song(pl);
pl->song = NULL;
}
}
void fm_player_init()
{
ao_initialize();
avcodec_register_all();
av_register_all();
}
void fm_player_exit()
{
ao_shutdown();
}