102
106
struct audio_format play_audio_format;
105
* Coefficient for converting a PCM buffer size into a time
109
* The time stamp of the chunk most recently sent to the
110
* output thread. This attribute is only used if
111
* audio_output_all_get_elapsed_time() didn't return a usable
112
* value; the output thread can estimate the elapsed time more
111
118
static struct music_buffer *player_buffer;
113
static void player_command_finished(void)
120
static void player_command_finished_locked(void)
115
122
assert(pc.command != PLAYER_COMMAND_NONE);
117
124
pc.command = PLAYER_COMMAND_NONE;
118
notify_signal(&main_notify);
125
g_cond_signal(main_cond);
128
static void player_command_finished(void)
131
player_command_finished_locked();
138
* Player lock is not held.
141
player_dc_start(struct player *player, struct music_pipe *pipe)
143
struct decoder_control *dc = player->dc;
145
assert(player->queued || pc.command == PLAYER_COMMAND_SEEK);
146
assert(pc.next_song != NULL);
148
dc_start(dc, pc.next_song, player_buffer, pipe);
130
160
assert(player != NULL);
131
161
assert(player->pipe != NULL);
133
return dc.pipe == player->pipe;
163
return player->dc->pipe == player->pipe;
137
* Has the decoder already begun decoding the next song?
139
* Note: this function does not check if the decoder is already
167
* Returns true if the decoder is decoding the next song (or has begun
168
* decoding it, or has finished doing it), and the player hasn't
169
* switched to that song yet.
143
172
player_dc_at_next_song(const struct player *player)
145
return dc.pipe != NULL && !player_dc_at_current_song(player);
174
return player->dc->pipe != NULL && !player_dc_at_current_song(player);
149
178
* Stop the decoder and clears (and frees) its music pipe.
180
* Player lock is not held.
152
183
player_dc_stop(struct player *player)
156
if (dc.pipe != NULL) {
185
struct decoder_control *dc = player->dc;
189
if (dc->pipe != NULL) {
157
190
/* clear and free the decoder pipe */
159
music_pipe_clear(dc.pipe, player_buffer);
161
if (dc.pipe != player->pipe)
162
music_pipe_free(dc.pipe);
192
music_pipe_clear(dc->pipe, player_buffer);
194
if (dc->pipe != player->pipe)
195
music_pipe_free(dc->pipe);
169
202
* After the decoder has been started asynchronously, wait for the
170
203
* "START" command to finish. The decoder may not be initialized yet,
171
204
* i.e. there is no audio_format information yet.
206
* The player lock is not held.
174
209
player_wait_for_decoder(struct player *player)
176
dc_command_wait(&pc.notify);
178
if (decoder_has_failed()) {
179
assert(dc.next_song == NULL || dc.next_song->url != NULL);
180
pc.errored_song = dc.next_song;
211
struct decoder_control *dc = player->dc;
213
assert(player->queued || pc.command == PLAYER_COMMAND_SEEK);
214
assert(pc.next_song != NULL);
216
player->queued = false;
218
if (decoder_lock_has_failed(dc)) {
220
pc.errored_song = dc->song;
181
221
pc.error = PLAYER_ERROR_FILE;
182
222
pc.next_song = NULL;
183
player->queued = false;
187
pc.total_time = pc.next_song->tag != NULL
188
? pc.next_song->tag->time : 0;
190
audio_format_clear(&pc.audio_format);
192
228
player->song = pc.next_song;
195
player->queued = false;
229
player->elapsed_time = 0.0;
197
231
/* set the "starting" flag, which will be cleared by
198
232
player_check_decoder_startup() */
199
233
player->decoder_starting = true;
237
/* update player_control's song information */
238
pc.total_time = song_get_duration(pc.next_song);
240
audio_format_clear(&pc.audio_format);
242
/* clear the queued song */
201
247
/* call syncPlaylistWithQueue() in the main thread */
202
248
event_pipe_emit(PIPE_EVENT_PLAYLIST);
254
* Returns the real duration of the song, comprising the duration
255
* indicated by the decoder plugin.
258
real_song_duration(const struct song *song, double decoder_duration)
260
assert(song != NULL);
262
if (decoder_duration <= 0.0)
263
/* the decoder plugin didn't provide information; fall
264
back to song_get_duration() */
265
return song_get_duration(song);
267
if (song->end_ms > 0 && song->end_ms / 1000.0 < decoder_duration)
268
return (song->end_ms - song->start_ms) / 1000.0;
270
return decoder_duration - song->start_ms / 1000.0;
208
274
* The decoder has acknowledged the "START" command (see
209
275
* player_wait_for_decoder()). This function checks if the decoder
210
276
* initialization has completed yet.
278
* The player lock is not held.
213
281
player_check_decoder_startup(struct player *player)
283
struct decoder_control *dc = player->dc;
215
285
assert(player->decoder_starting);
217
if (decoder_has_failed()) {
289
if (decoder_has_failed(dc)) {
218
290
/* the decoder failed */
219
assert(dc.next_song == NULL || dc.next_song->url != NULL);
221
pc.errored_song = dc.next_song;
294
pc.errored_song = dc->song;
222
295
pc.error = PLAYER_ERROR_FILE;
225
} else if (!decoder_is_starting()) {
299
} else if (!decoder_is_starting(dc)) {
226
300
/* the decoder is ready and ok */
228
304
if (audio_format_defined(&player->play_audio_format) &&
229
305
!audio_output_all_wait(1))
230
306
/* the output devices havn't finished playing
231
307
all chunks yet - wait for that */
234
pc.total_time = dc.total_time;
235
pc.audio_format = dc.in_audio_format;
236
player->play_audio_format = dc.out_audio_format;
237
player->size_to_time =
238
audioFormatSizeToTime(&dc.out_audio_format);
311
pc.total_time = real_song_duration(dc->song, dc->total_time);
312
pc.audio_format = dc->in_audio_format;
315
player->play_audio_format = dc->out_audio_format;
239
316
player->decoder_starting = false;
241
318
if (!player->paused &&
242
!audio_output_all_open(&dc.out_audio_format,
319
!audio_output_all_open(&dc->out_audio_format,
243
320
player_buffer)) {
244
char *uri = song_get_uri(dc.next_song);
321
char *uri = song_get_uri(dc->song);
245
322
g_warning("problems opening audio device "
246
323
"while playing \"%s\"", uri);
249
assert(dc.next_song == NULL || dc.next_song->url != NULL);
250
pc.errored_song = dc.next_song;
251
327
pc.error = PLAYER_ERROR_AUDIO;
253
329
/* pause: the user may resume playback as soon
254
330
as an audio output becomes available */
255
331
pc.state = PLAYER_STATE_PAUSE;
256
334
player->paused = true;
271
350
* Sends a chunk of silence to the audio outputs. This is called when
272
351
* there is not enough decoded data in the pipe yet, to prevent
273
352
* underruns in the hardware buffers.
354
* The player lock is not held.
276
357
player_send_silence(struct player *player)
278
struct music_chunk *chunk;
359
assert(audio_format_defined(&player->play_audio_format));
361
struct music_chunk *chunk = music_buffer_allocate(player_buffer);
363
g_warning("Failed to allocate silence buffer");
368
chunk->audio_format = player->play_audio_format;
279
371
size_t frame_size =
280
372
audio_format_frame_size(&player->play_audio_format);
281
373
/* this formula ensures that we don't send
282
374
partial frames */
283
375
unsigned num_frames = sizeof(chunk->data) / frame_size;
285
assert(audio_format_defined(&player->play_audio_format));
287
chunk = music_buffer_allocate(player_buffer);
289
g_warning("Failed to allocate silence buffer");
294
chunk->audio_format = player->play_audio_format;
377
chunk->times = -1.0; /* undefined time stamp */
297
378
chunk->length = num_frames * frame_size;
298
379
memset(chunk->data, 0, chunk->length);
309
390
* This is the handler for the #PLAYER_COMMAND_SEEK command.
392
* The player lock is not held.
311
394
static bool player_seek_decoder(struct player *player)
396
struct song *song = pc.next_song;
397
struct decoder_control *dc = player->dc;
316
399
assert(pc.next_song != NULL);
318
if (decoder_current_song() != pc.next_song) {
401
if (decoder_current_song(dc) != song) {
319
402
/* the decoder is already decoding the "next" song -
320
403
stop it and start the previous song again */
360
441
/* send the SEEK command */
362
where = pc.seek_where;
443
double where = pc.seek_where;
363
444
if (where > pc.total_time)
364
445
where = pc.total_time - 0.1;
368
ret = dc_seek(&pc.notify, where);
449
if (!dc_seek(dc, where + song->start_ms / 1000.0)) {
370
450
/* decoder failure */
371
451
player_command_finished();
375
pc.elapsed_time = where;
455
player->elapsed_time = where;
376
457
player_command_finished();
378
459
player->xfade = XFADE_UNKNOWN;
470
* Player lock must be held before calling.
388
472
static void player_process_command(struct player *player)
474
G_GNUC_UNUSED struct decoder_control *dc = player->dc;
390
476
switch (pc.command) {
391
477
case PLAYER_COMMAND_NONE:
392
case PLAYER_COMMAND_PLAY:
393
478
case PLAYER_COMMAND_STOP:
394
479
case PLAYER_COMMAND_EXIT:
395
480
case PLAYER_COMMAND_CLOSE_AUDIO:
483
case PLAYER_COMMAND_UPDATE_AUDIO:
485
audio_output_all_enable_disable();
487
player_command_finished_locked();
398
490
case PLAYER_COMMAND_QUEUE:
399
491
assert(pc.next_song != NULL);
400
492
assert(!player->queued);
401
493
assert(!player_dc_at_next_song(player));
403
495
player->queued = true;
404
player_command_finished();
496
player_command_finished_locked();
407
499
case PLAYER_COMMAND_PAUSE:
408
502
player->paused = !player->paused;
409
503
if (player->paused) {
410
504
audio_output_all_pause();
411
507
pc.state = PLAYER_STATE_PAUSE;
412
508
} else if (!audio_format_defined(&player->play_audio_format)) {
413
509
/* the decoder hasn't provided an audio format
414
510
yet - don't open the audio device yet */
416
513
pc.state = PLAYER_STATE_PLAY;
417
514
} else if (audio_output_all_open(&player->play_audio_format, player_buffer)) {
418
515
/* unpaused, continue playing */
419
518
pc.state = PLAYER_STATE_PLAY;
421
520
/* the audio device has failed - rollback to
423
assert(dc.next_song == NULL || dc.next_song->url != NULL);
424
pc.errored_song = dc.next_song;
425
522
pc.error = PLAYER_ERROR_AUDIO;
427
524
player->paused = true;
430
player_command_finished();
529
player_command_finished_locked();
433
532
case PLAYER_COMMAND_SEEK:
434
534
player_seek_decoder(player);
437
538
case PLAYER_COMMAND_CANCEL:
446
if (player_dc_at_next_song(player))
547
if (player_dc_at_next_song(player)) {
447
548
/* the decoder is already decoding the song -
448
549
stop it and reset the position */
449
551
player_dc_stop(player);
451
555
pc.next_song = NULL;
452
556
player->queued = false;
453
player_command_finished();
557
player_command_finished_locked();
560
case PLAYER_COMMAND_REFRESH:
561
if (audio_format_defined(&player->play_audio_format) &&
564
audio_output_all_check();
568
pc.elapsed_time = audio_output_all_get_elapsed_time();
569
if (pc.elapsed_time < 0.0)
570
pc.elapsed_time = player->elapsed_time;
572
player_command_finished_locked();
578
update_song_tag(struct song *song, const struct tag *new_tag)
580
if (song_is_file(song))
581
/* don't update tags of local files, only remote
582
streams may change tags dynamically */
585
struct tag *old_tag = song->tag;
586
song->tag = tag_dup(new_tag);
591
/* the main thread will update the playlist version when he
592
receives this event */
593
event_pipe_emit(PIPE_EVENT_TAG);
595
/* notify all clients that the tag of the current song has
597
idle_add(IDLE_PLAYER);
459
601
* Plays a #music_chunk object (after applying software volume). If
460
602
* it contains a (stream) tag, copy it to the current song, so MPD's
461
603
* playlist reflects the new stream tag.
605
* Player lock is not held.
464
608
play_chunk(struct song *song, struct music_chunk *chunk,
465
const struct audio_format *format, double sizeToTime)
609
const struct audio_format *format)
469
611
assert(music_chunk_check_format(chunk, format));
471
if (chunk->tag != NULL) {
472
if (!song_is_file(song)) {
473
/* always update the tag of remote streams */
474
struct tag *old_tag = song->tag;
476
song->tag = tag_dup(chunk->tag);
481
/* the main thread will update the playlist
482
version when he receives this event */
483
event_pipe_emit(PIPE_EVENT_TAG);
485
/* notify all clients that the tag of the
486
current song has changed */
487
idle_add(IDLE_PLAYER);
613
if (chunk->tag != NULL)
614
update_song_tag(song, chunk->tag);
491
616
if (chunk->length == 0) {
492
617
music_buffer_return(player_buffer, chunk);
496
pc.elapsed_time = chunk->times;
497
621
pc.bit_rate = chunk->bit_rate;
499
/* apply software volume */
501
success = pcm_volume(chunk->data, chunk->length,
502
format, pc.software_volume);
504
g_warning("pcm_volume() failed on %u:%u:%u",
505
format->sample_rate, format->bits, format->channels);
506
pc.errored_song = dc.current_song;
507
pc.error = PLAYER_ERROR_AUDIO;
511
623
/* send the chunk to the audio outputs */
513
if (!audio_output_all_play(chunk)) {
514
pc.errored_song = dc.current_song;
515
pc.error = PLAYER_ERROR_AUDIO;
625
if (!audio_output_all_play(chunk))
519
pc.total_play_time += sizeToTime * chunk->length;
628
pc.total_play_time += (double)chunk->length /
629
audio_format_time_to_size(format);
530
640
play_next_chunk(struct player *player)
532
struct music_chunk *chunk = NULL;
533
unsigned cross_fade_position;
642
struct decoder_control *dc = player->dc;
536
644
if (!audio_output_all_wait(64))
537
645
/* the output pipe is still large enough, don't send
649
unsigned cross_fade_position;
650
struct music_chunk *chunk = NULL;
541
651
if (player->xfade == XFADE_ENABLED &&
542
652
player_dc_at_next_song(player) &&
543
653
(cross_fade_position = music_pipe_size(player->pipe))
544
654
<= player->cross_fade_chunks) {
545
655
/* perform cross fade */
546
656
struct music_chunk *other_chunk =
547
music_pipe_shift(dc.pipe);
657
music_pipe_shift(dc->pipe);
549
659
if (!player->cross_fading) {
550
660
/* beginning of the cross fade - adjust
567
678
other_chunk->tag);
568
679
other_chunk->tag = NULL;
570
cross_fade_apply(chunk, other_chunk,
571
&dc.out_audio_format,
573
player->cross_fade_chunks);
574
music_buffer_return(player_buffer, other_chunk);
681
if (isnan(pc.mixramp_delay_seconds)) {
682
chunk->mix_ratio = ((float)cross_fade_position)
683
/ player->cross_fade_chunks;
685
chunk->mix_ratio = nan("");
688
if (music_chunk_is_empty(other_chunk)) {
689
/* the "other" chunk was a music_chunk
690
which had only a tag, but no music
691
data - we cannot cross-fade that;
692
but since this happens only at the
693
beginning of the new song, we can
694
easily recover by throwing it away
696
music_buffer_return(player_buffer,
701
chunk->other = other_chunk;
576
703
/* there are not enough decoded chunks yet */
577
if (decoder_is_idle()) {
707
if (decoder_is_idle(dc)) {
578
708
/* the decoder isn't running, abort
580
712
player->xfade = XFADE_DISABLED;
582
714
/* wait for the decoder */
583
notify_signal(&dc.notify);
584
notify_wait(&pc.notify);
716
player_wait_decoder(dc);
604
737
/* play the current chunk */
606
success = play_chunk(player->song, chunk, &player->play_audio_format,
607
player->size_to_time);
739
if (!play_chunk(player->song, chunk, &player->play_audio_format)) {
610
740
music_buffer_return(player_buffer, chunk);
744
pc.error = PLAYER_ERROR_AUDIO;
612
746
/* pause: the user may resume playback as soon as an
613
747
audio output becomes available */
614
748
pc.state = PLAYER_STATE_PAUSE;
615
749
player->paused = true;
620
756
/* this formula should prevent that the decoder gets woken up
621
757
with each chunk; it is more efficient to make it decode a
622
758
larger block at a time */
623
if (!decoder_is_idle() &&
624
music_pipe_size(dc.pipe) <= (pc.buffered_before_play +
760
if (!decoder_is_idle(dc) &&
761
music_pipe_size(dc->pipe) <= (pc.buffered_before_play +
625
762
music_buffer_size(player_buffer) * 3) / 4)
626
notify_signal(&dc.notify);
654
800
* basically a state machine, which multiplexes data between the
655
801
* decoder thread and the output threads.
657
static void do_play(void)
803
static void do_play(struct decoder_control *dc)
659
805
struct player player = {
660
807
.buffering = true,
661
808
.decoder_starting = false,
665
812
.xfade = XFADE_UNKNOWN,
666
813
.cross_fading = false,
667
814
.cross_fade_chunks = 0,
668
815
.cross_fade_tag = NULL,
672
821
player.pipe = music_pipe_new();
674
dc.buffer = player_buffer;
675
dc_start(&pc.notify, pc.next_song, player.pipe);
823
player_dc_start(&player, player.pipe);
676
824
if (!player_wait_for_decoder(&player)) {
677
825
player_dc_stop(&player);
678
826
player_command_finished();
679
827
music_pipe_free(player.pipe);
680
828
event_pipe_emit(PIPE_EVENT_PLAYLIST);
685
834
pc.state = PLAYER_STATE_PLAY;
686
player_command_finished();
835
player_command_finished_locked();
689
838
player_process_command(&player);
690
839
if (pc.command == PLAYER_COMMAND_STOP ||
691
840
pc.command == PLAYER_COMMAND_EXIT ||
692
841
pc.command == PLAYER_COMMAND_CLOSE_AUDIO) {
693
843
audio_output_all_cancel();
697
849
if (player.buffering) {
698
850
/* buffering at the start of the song - wait
699
851
until the buffer is large enough, to
700
852
prevent stuttering on slow machines */
702
854
if (music_pipe_size(player.pipe) < pc.buffered_before_play &&
703
!decoder_is_idle()) {
855
!decoder_lock_is_idle(dc)) {
704
856
/* not enough decoded buffer space yet */
706
858
if (!player.paused &&
720
876
if (player.decoder_starting) {
721
877
/* wait until the decoder is initialized completely */
724
success = player_check_decoder_startup(&player);
879
if (!player_check_decoder_startup(&player))
882
/* seek to the beginning of the range */
883
const struct song *song = decoder_current_song(dc);
884
if (song != NULL && song->start_ms > 0 &&
885
/* we must not send a seek command until
886
the decoder is initialized
888
!player.decoder_starting &&
889
!dc_seek(dc, song->start_ms / 1000.0))
890
player_dc_stop(&player);
732
898
music_pipe_check_format(&play_audio_format,
733
899
player.next_song_chunk,
734
&dc.out_audio_format);
900
&dc->out_audio_format);
738
if (decoder_is_idle() && player.queued) {
904
if (decoder_lock_is_idle(dc) && player.queued &&
905
dc->pipe == player.pipe) {
739
906
/* the decoder has finished the current song;
740
907
make it decode the next song */
741
assert(pc.next_song != NULL);
742
assert(!player_dc_at_next_song(&player));
746
player.queued = false;
747
dc_start_async(pc.next_song, music_pipe_new());
909
assert(dc->pipe == NULL || dc->pipe == player.pipe);
911
player_dc_start(&player, music_pipe_new());
750
914
if (player_dc_at_next_song(&player) &&
751
915
player.xfade == XFADE_UNKNOWN &&
752
!decoder_is_starting()) {
916
!decoder_lock_is_starting(dc)) {
753
917
/* enable cross fading in this song? if yes,
754
918
calculate how many chunks will be required
756
920
player.cross_fade_chunks =
757
cross_fade_calc(pc.cross_fade_seconds, dc.total_time,
758
&dc.out_audio_format,
921
cross_fade_calc(pc.cross_fade_seconds, dc->total_time,
923
pc.mixramp_delay_seconds,
925
dc->replay_gain_prev_db,
927
dc->mixramp_prev_end,
928
&dc->out_audio_format,
759
929
&player.play_audio_format,
760
930
music_buffer_size(player_buffer) -
761
931
pc.buffered_before_play);
788
962
if (!player_song_border(&player))
790
} else if (decoder_is_idle()) {
964
} else if (decoder_lock_is_idle(dc)) {
791
965
/* check the size of the pipe again, because
792
966
the decoder thread may have added something
793
967
since we last checked */
794
if (music_pipe_empty(player.pipe))
968
if (music_pipe_empty(player.pipe)) {
969
/* wait for the hardware to finish
971
audio_output_all_drain();
797
975
/* the decoder is too busy and hasn't provided
798
976
new PCM data in time: send silence (if the
815
990
if (player.cross_fade_tag != NULL)
816
991
tag_free(player.cross_fade_tag);
996
assert(pc.next_song != NULL);
818
1000
pc.state = PLAYER_STATE_STOP;
819
1004
event_pipe_emit(PIPE_EVENT_PLAYLIST);
822
1009
static gpointer player_task(G_GNUC_UNUSED gpointer arg)
824
decoder_thread_start();
1011
struct decoder_control dc;
1014
decoder_thread_start(&dc);
826
1016
player_buffer = music_buffer_new(pc.buffer_chunks);
829
1021
switch (pc.command) {
830
case PLAYER_COMMAND_PLAY:
831
1022
case PLAYER_COMMAND_QUEUE:
832
1023
assert(pc.next_song != NULL);
837
1028
case PLAYER_COMMAND_STOP:
838
1030
audio_output_all_cancel();
839
1033
/* fall through */
841
1035
case PLAYER_COMMAND_SEEK:
842
1036
case PLAYER_COMMAND_PAUSE:
843
1037
pc.next_song = NULL;
844
player_command_finished();
1038
player_command_finished_locked();
847
1041
case PLAYER_COMMAND_CLOSE_AUDIO:
848
audio_output_all_close();
849
player_command_finished();
1044
audio_output_all_release();
1047
player_command_finished_locked();
852
1050
/* in the DEBUG build, check for leaked
1059
case PLAYER_COMMAND_UPDATE_AUDIO:
1061
audio_output_all_enable_disable();
1063
player_command_finished_locked();
861
1066
case PLAYER_COMMAND_EXIT:
863
1071
audio_output_all_close();
864
1072
music_buffer_free(player_buffer);
865
1074
player_command_finished();
869
1077
case PLAYER_COMMAND_CANCEL:
870
1078
pc.next_song = NULL;
871
player_command_finished();
1079
player_command_finished_locked();
1082
case PLAYER_COMMAND_REFRESH:
1083
/* no-op when not playing */
1084
player_command_finished_locked();
874
1087
case PLAYER_COMMAND_NONE:
875
notify_wait(&pc.notify);
882
1094
void player_create(void)
1096
assert(pc.thread == NULL);
884
1098
GError *e = NULL;
886
assert(pc.thread == NULL);
888
1099
pc.thread = g_thread_create(player_task, NULL, true, &e);
889
1100
if (pc.thread == NULL)
890
g_error("Failed to spawn player task: %s", e->message);
1101
MPD_ERROR("Failed to spawn player task: %s", e->message);