~ubuntu-branches/ubuntu/trusty/mpd/trusty

« back to all changes in this revision

Viewing changes to src/player_thread.c

  • Committer: Bazaar Package Importer
  • Author(s): Angel Abad
  • Date: 2011-02-02 12:26:30 UTC
  • mfrom: (1.5.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110202122630-bdyx8w4k94doz4fs
Tags: 0.16.1-1ubuntu1
* Merge from debian unstable. Remaining changes:
  - debian/control:
    + Don't build-depend on libmikmod2-dev (Debian bug #510675).
    + Move avahi-daemon from Suggests field to Recommends field.
  - debian/mpd.init.d:
    + Read mpd user from mpd.conf.
  - debian/control, debian/rules:
    + Add libmp3lame-dev to the build dependencies and enable lame.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 
2
 * Copyright (C) 2003-2010 The Music Player Daemon Project
3
3
 * http://www.musicpd.org
4
4
 *
5
5
 * This program is free software; you can redistribute it and/or modify
17
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
18
 */
19
19
 
 
20
#include "config.h"
20
21
#include "player_thread.h"
21
22
#include "player_control.h"
22
23
#include "decoder_control.h"
33
34
#include "idle.h"
34
35
#include "main.h"
35
36
#include "buffer.h"
 
37
#include "mpd_error.h"
36
38
 
37
39
#include <glib.h>
38
40
 
46
48
};
47
49
 
48
50
struct player {
 
51
        struct decoder_control *dc;
 
52
 
49
53
        struct music_pipe *pipe;
50
54
 
51
55
        /**
102
106
        struct audio_format play_audio_format;
103
107
 
104
108
        /**
105
 
         * Coefficient for converting a PCM buffer size into a time
106
 
         * span.
 
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
 
113
         * precisly.
107
114
         */
108
 
        double size_to_time;
 
115
        float elapsed_time;
109
116
};
110
117
 
111
118
static struct music_buffer *player_buffer;
112
119
 
113
 
static void player_command_finished(void)
 
120
static void player_command_finished_locked(void)
114
121
{
115
122
        assert(pc.command != PLAYER_COMMAND_NONE);
116
123
 
117
124
        pc.command = PLAYER_COMMAND_NONE;
118
 
        notify_signal(&main_notify);
 
125
        g_cond_signal(main_cond);
 
126
}
 
127
 
 
128
static void player_command_finished(void)
 
129
{
 
130
        player_lock();
 
131
        player_command_finished_locked();
 
132
        player_unlock();
 
133
}
 
134
 
 
135
/**
 
136
 * Start the decoder.
 
137
 *
 
138
 * Player lock is not held.
 
139
 */
 
140
static void
 
141
player_dc_start(struct player *player, struct music_pipe *pipe)
 
142
{
 
143
        struct decoder_control *dc = player->dc;
 
144
 
 
145
        assert(player->queued || pc.command == PLAYER_COMMAND_SEEK);
 
146
        assert(pc.next_song != NULL);
 
147
 
 
148
        dc_start(dc, pc.next_song, player_buffer, pipe);
119
149
}
120
150
 
121
151
/**
130
160
        assert(player != NULL);
131
161
        assert(player->pipe != NULL);
132
162
 
133
 
        return dc.pipe == player->pipe;
 
163
        return player->dc->pipe == player->pipe;
134
164
}
135
165
 
136
166
/**
137
 
 * Has the decoder already begun decoding the next song?
138
 
 *
139
 
 * Note: this function does not check if the decoder is already
140
 
 * finished.
 
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.
141
170
 */
142
171
static bool
143
172
player_dc_at_next_song(const struct player *player)
144
173
{
145
 
        return dc.pipe != NULL && !player_dc_at_current_song(player);
 
174
        return player->dc->pipe != NULL && !player_dc_at_current_song(player);
146
175
}
147
176
 
148
177
/**
149
178
 * Stop the decoder and clears (and frees) its music pipe.
 
179
 *
 
180
 * Player lock is not held.
150
181
 */
151
182
static void
152
183
player_dc_stop(struct player *player)
153
184
{
154
 
        dc_stop(&pc.notify);
155
 
 
156
 
        if (dc.pipe != NULL) {
 
185
        struct decoder_control *dc = player->dc;
 
186
 
 
187
        dc_stop(dc);
 
188
 
 
189
        if (dc->pipe != NULL) {
157
190
                /* clear and free the decoder pipe */
158
191
 
159
 
                music_pipe_clear(dc.pipe, player_buffer);
160
 
 
161
 
                if (dc.pipe != player->pipe)
162
 
                        music_pipe_free(dc.pipe);
163
 
 
164
 
                dc.pipe = NULL;
 
192
                music_pipe_clear(dc->pipe, player_buffer);
 
193
 
 
194
                if (dc->pipe != player->pipe)
 
195
                        music_pipe_free(dc->pipe);
 
196
 
 
197
                dc->pipe = NULL;
165
198
        }
166
199
}
167
200
 
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.
 
205
 *
 
206
 * The player lock is not held.
172
207
 */
173
208
static bool
174
209
player_wait_for_decoder(struct player *player)
175
210
{
176
 
        dc_command_wait(&pc.notify);
177
 
 
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;
 
212
 
 
213
        assert(player->queued || pc.command == PLAYER_COMMAND_SEEK);
 
214
        assert(pc.next_song != NULL);
 
215
 
 
216
        player->queued = false;
 
217
 
 
218
        if (decoder_lock_has_failed(dc)) {
 
219
                player_lock();
 
220
                pc.errored_song = dc->song;
181
221
                pc.error = PLAYER_ERROR_FILE;
182
222
                pc.next_song = NULL;
183
 
                player->queued = false;
 
223
                player_unlock();
 
224
 
184
225
                return false;
185
226
        }
186
227
 
187
 
        pc.total_time = pc.next_song->tag != NULL
188
 
                ? pc.next_song->tag->time : 0;
189
 
        pc.bit_rate = 0;
190
 
        audio_format_clear(&pc.audio_format);
191
 
 
192
228
        player->song = pc.next_song;
193
 
        pc.next_song = NULL;
194
 
        pc.elapsed_time = 0;
195
 
        player->queued = false;
 
229
        player->elapsed_time = 0.0;
196
230
 
197
231
        /* set the "starting" flag, which will be cleared by
198
232
           player_check_decoder_startup() */
199
233
        player->decoder_starting = true;
200
234
 
 
235
        player_lock();
 
236
 
 
237
        /* update player_control's song information */
 
238
        pc.total_time = song_get_duration(pc.next_song);
 
239
        pc.bit_rate = 0;
 
240
        audio_format_clear(&pc.audio_format);
 
241
 
 
242
        /* clear the queued song */
 
243
        pc.next_song = NULL;
 
244
 
 
245
        player_unlock();
 
246
 
201
247
        /* call syncPlaylistWithQueue() in the main thread */
202
248
        event_pipe_emit(PIPE_EVENT_PLAYLIST);
203
249
 
205
251
}
206
252
 
207
253
/**
 
254
 * Returns the real duration of the song, comprising the duration
 
255
 * indicated by the decoder plugin.
 
256
 */
 
257
static double
 
258
real_song_duration(const struct song *song, double decoder_duration)
 
259
{
 
260
        assert(song != NULL);
 
261
 
 
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);
 
266
 
 
267
        if (song->end_ms > 0 && song->end_ms / 1000.0 < decoder_duration)
 
268
                return (song->end_ms - song->start_ms) / 1000.0;
 
269
 
 
270
        return decoder_duration - song->start_ms / 1000.0;
 
271
}
 
272
 
 
273
/**
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.
 
277
 *
 
278
 * The player lock is not held.
211
279
 */
212
280
static bool
213
281
player_check_decoder_startup(struct player *player)
214
282
{
 
283
        struct decoder_control *dc = player->dc;
 
284
 
215
285
        assert(player->decoder_starting);
216
286
 
217
 
        if (decoder_has_failed()) {
 
287
        decoder_lock(dc);
 
288
 
 
289
        if (decoder_has_failed(dc)) {
218
290
                /* the decoder failed */
219
 
                assert(dc.next_song == NULL || dc.next_song->url != NULL);
 
291
                decoder_unlock(dc);
220
292
 
221
 
                pc.errored_song = dc.next_song;
 
293
                player_lock();
 
294
                pc.errored_song = dc->song;
222
295
                pc.error = PLAYER_ERROR_FILE;
 
296
                player_unlock();
223
297
 
224
298
                return false;
225
 
        } else if (!decoder_is_starting()) {
 
299
        } else if (!decoder_is_starting(dc)) {
226
300
                /* the decoder is ready and ok */
227
301
 
 
302
                decoder_unlock(dc);
 
303
 
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 */
232
308
                        return true;
233
309
 
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);
 
310
                player_lock();
 
311
                pc.total_time = real_song_duration(dc->song, dc->total_time);
 
312
                pc.audio_format = dc->in_audio_format;
 
313
                player_unlock();
 
314
 
 
315
                player->play_audio_format = dc->out_audio_format;
239
316
                player->decoder_starting = false;
240
317
 
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);
247
324
                        g_free(uri);
248
325
 
249
 
                        assert(dc.next_song == NULL || dc.next_song->url != NULL);
250
 
                        pc.errored_song = dc.next_song;
 
326
                        player_lock();
251
327
                        pc.error = PLAYER_ERROR_AUDIO;
252
328
 
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;
 
332
                        player_unlock();
 
333
 
256
334
                        player->paused = true;
257
335
                        return true;
258
336
                }
261
339
        } else {
262
340
                /* the decoder is not yet ready; wait
263
341
                   some more */
264
 
                notify_wait(&pc.notify);
 
342
                player_wait_decoder(dc);
 
343
                decoder_unlock(dc);
265
344
 
266
345
                return true;
267
346
        }
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.
 
353
 *
 
354
 * The player lock is not held.
274
355
 */
275
356
static bool
276
357
player_send_silence(struct player *player)
277
358
{
278
 
        struct music_chunk *chunk;
 
359
        assert(audio_format_defined(&player->play_audio_format));
 
360
 
 
361
        struct music_chunk *chunk = music_buffer_allocate(player_buffer);
 
362
        if (chunk == NULL) {
 
363
                g_warning("Failed to allocate silence buffer");
 
364
                return false;
 
365
        }
 
366
 
 
367
#ifndef NDEBUG
 
368
        chunk->audio_format = player->play_audio_format;
 
369
#endif
 
370
 
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;
284
376
 
285
 
        assert(audio_format_defined(&player->play_audio_format));
286
 
 
287
 
        chunk = music_buffer_allocate(player_buffer);
288
 
        if (chunk == NULL) {
289
 
                g_warning("Failed to allocate silence buffer");
290
 
                return false;
291
 
        }
292
 
 
293
 
#ifndef NDEBUG
294
 
        chunk->audio_format = player->play_audio_format;
295
 
#endif
296
 
 
 
377
        chunk->times = -1.0; /* undefined time stamp */
297
378
        chunk->length = num_frames * frame_size;
298
379
        memset(chunk->data, 0, chunk->length);
299
380
 
307
388
 
308
389
/**
309
390
 * This is the handler for the #PLAYER_COMMAND_SEEK command.
 
391
 *
 
392
 * The player lock is not held.
310
393
 */
311
394
static bool player_seek_decoder(struct player *player)
312
395
{
313
 
        double where;
314
 
        bool ret;
 
396
        struct song *song = pc.next_song;
 
397
        struct decoder_control *dc = player->dc;
315
398
 
316
399
        assert(pc.next_song != NULL);
317
400
 
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 */
321
404
 
326
409
                music_pipe_clear(player->pipe, player_buffer);
327
410
 
328
411
                /* re-start the decoder */
329
 
                dc_start_async(pc.next_song, player->pipe);
330
 
                ret = player_wait_for_decoder(player);
331
 
                if (!ret) {
 
412
                player_dc_start(player, player->pipe);
 
413
                if (!player_wait_for_decoder(player)) {
332
414
                        /* decoder failure */
333
415
                        player_command_finished();
334
416
                        return false;
339
421
                           but it is the same song file; exchange the pipe */
340
422
                        music_pipe_clear(player->pipe, player_buffer);
341
423
                        music_pipe_free(player->pipe);
342
 
                        player->pipe = dc.pipe;
 
424
                        player->pipe = dc->pipe;
343
425
                }
344
426
 
345
427
                pc.next_song = NULL;
349
431
        /* wait for the decoder to complete initialization */
350
432
 
351
433
        while (player->decoder_starting) {
352
 
                ret = player_check_decoder_startup(player);
353
 
                if (!ret) {
 
434
                if (!player_check_decoder_startup(player)) {
354
435
                        /* decoder failure */
355
436
                        player_command_finished();
356
437
                        return false;
359
440
 
360
441
        /* send the SEEK command */
361
442
 
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;
365
446
        if (where < 0.0)
366
447
                where = 0.0;
367
448
 
368
 
        ret = dc_seek(&pc.notify, where);
369
 
        if (!ret) {
 
449
        if (!dc_seek(dc, where + song->start_ms / 1000.0)) {
370
450
                /* decoder failure */
371
451
                player_command_finished();
372
452
                return false;
373
453
        }
374
454
 
375
 
        pc.elapsed_time = where;
 
455
        player->elapsed_time = where;
 
456
 
376
457
        player_command_finished();
377
458
 
378
459
        player->xfade = XFADE_UNKNOWN;
385
466
        return true;
386
467
}
387
468
 
 
469
/**
 
470
 * Player lock must be held before calling.
 
471
 */
388
472
static void player_process_command(struct player *player)
389
473
{
 
474
        G_GNUC_UNUSED struct decoder_control *dc = player->dc;
 
475
 
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:
396
481
                break;
397
482
 
 
483
        case PLAYER_COMMAND_UPDATE_AUDIO:
 
484
                player_unlock();
 
485
                audio_output_all_enable_disable();
 
486
                player_lock();
 
487
                player_command_finished_locked();
 
488
                break;
 
489
 
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));
402
494
 
403
495
                player->queued = true;
404
 
                player_command_finished();
 
496
                player_command_finished_locked();
405
497
                break;
406
498
 
407
499
        case PLAYER_COMMAND_PAUSE:
 
500
                player_unlock();
 
501
 
408
502
                player->paused = !player->paused;
409
503
                if (player->paused) {
410
504
                        audio_output_all_pause();
 
505
                        player_lock();
 
506
 
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 */
 
511
                        player_lock();
415
512
 
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 */
 
516
                        player_lock();
 
517
 
419
518
                        pc.state = PLAYER_STATE_PLAY;
420
519
                } else {
421
520
                        /* the audio device has failed - rollback to
422
521
                           pause mode */
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;
426
523
 
427
524
                        player->paused = true;
 
525
 
 
526
                        player_lock();
428
527
                }
429
528
 
430
 
                player_command_finished();
 
529
                player_command_finished_locked();
431
530
                break;
432
531
 
433
532
        case PLAYER_COMMAND_SEEK:
 
533
                player_unlock();
434
534
                player_seek_decoder(player);
 
535
                player_lock();
435
536
                break;
436
537
 
437
538
        case PLAYER_COMMAND_CANCEL:
443
544
                        return;
444
545
                }
445
546
 
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 */
 
550
                        player_unlock();
449
551
                        player_dc_stop(player);
 
552
                        player_lock();
 
553
                }
450
554
 
451
555
                pc.next_song = NULL;
452
556
                player->queued = false;
453
 
                player_command_finished();
 
557
                player_command_finished_locked();
 
558
                break;
 
559
 
 
560
        case PLAYER_COMMAND_REFRESH:
 
561
                if (audio_format_defined(&player->play_audio_format) &&
 
562
                    !player->paused) {
 
563
                        player_unlock();
 
564
                        audio_output_all_check();
 
565
                        player_lock();
 
566
                }
 
567
 
 
568
                pc.elapsed_time = audio_output_all_get_elapsed_time();
 
569
                if (pc.elapsed_time < 0.0)
 
570
                        pc.elapsed_time = player->elapsed_time;
 
571
 
 
572
                player_command_finished_locked();
454
573
                break;
455
574
        }
456
575
}
457
576
 
 
577
static void
 
578
update_song_tag(struct song *song, const struct tag *new_tag)
 
579
{
 
580
        if (song_is_file(song))
 
581
                /* don't update tags of local files, only remote
 
582
                   streams may change tags dynamically */
 
583
                return;
 
584
 
 
585
        struct tag *old_tag = song->tag;
 
586
        song->tag = tag_dup(new_tag);
 
587
 
 
588
        if (old_tag != NULL)
 
589
                tag_free(old_tag);
 
590
 
 
591
        /* the main thread will update the playlist version when he
 
592
           receives this event */
 
593
        event_pipe_emit(PIPE_EVENT_TAG);
 
594
 
 
595
        /* notify all clients that the tag of the current song has
 
596
           changed */
 
597
        idle_add(IDLE_PLAYER);
 
598
}
 
599
 
458
600
/**
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.
 
604
 *
 
605
 * Player lock is not held.
462
606
 */
463
607
static bool
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)
466
610
{
467
 
        bool success;
468
 
 
469
611
        assert(music_chunk_check_format(chunk, format));
470
612
 
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;
475
 
 
476
 
                        song->tag = tag_dup(chunk->tag);
477
 
 
478
 
                        if (old_tag != NULL)
479
 
                                tag_free(old_tag);
480
 
 
481
 
                        /* the main thread will update the playlist
482
 
                           version when he receives this event */
483
 
                        event_pipe_emit(PIPE_EVENT_TAG);
484
 
 
485
 
                        /* notify all clients that the tag of the
486
 
                           current song has changed */
487
 
                        idle_add(IDLE_PLAYER);
488
 
                }
489
 
        }
 
613
        if (chunk->tag != NULL)
 
614
                update_song_tag(song, chunk->tag);
490
615
 
491
616
        if (chunk->length == 0) {
492
617
                music_buffer_return(player_buffer, chunk);
493
618
                return true;
494
619
        }
495
620
 
496
 
        pc.elapsed_time = chunk->times;
497
621
        pc.bit_rate = chunk->bit_rate;
498
622
 
499
 
        /* apply software volume */
500
 
 
501
 
        success = pcm_volume(chunk->data, chunk->length,
502
 
                             format, pc.software_volume);
503
 
        if (!success) {
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;
508
 
                return false;
509
 
        }
510
 
 
511
623
        /* send the chunk to the audio outputs */
512
624
 
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))
516
626
                return false;
517
 
        }
518
627
 
519
 
        pc.total_play_time += sizeToTime * chunk->length;
 
628
        pc.total_play_time += (double)chunk->length /
 
629
                audio_format_time_to_size(format);
520
630
        return true;
521
631
}
522
632
 
529
639
static bool
530
640
play_next_chunk(struct player *player)
531
641
{
532
 
        struct music_chunk *chunk = NULL;
533
 
        unsigned cross_fade_position;
534
 
        bool success;
 
642
        struct decoder_control *dc = player->dc;
535
643
 
536
644
        if (!audio_output_all_wait(64))
537
645
                /* the output pipe is still large enough, don't send
538
646
                   another chunk */
539
647
                return true;
540
648
 
 
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);
548
658
 
549
659
                if (!player->cross_fading) {
550
660
                        /* beginning of the cross fade - adjust
558
668
                if (other_chunk != NULL) {
559
669
                        chunk = music_pipe_shift(player->pipe);
560
670
                        assert(chunk != NULL);
 
671
                        assert(chunk->other == NULL);
561
672
 
562
673
                        /* don't send the tags of the new song (which
563
674
                           is being faded in) yet; postpone it until
567
678
                                                  other_chunk->tag);
568
679
                        other_chunk->tag = NULL;
569
680
 
570
 
                        cross_fade_apply(chunk, other_chunk,
571
 
                                         &dc.out_audio_format,
572
 
                                         cross_fade_position,
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;
 
684
                        } else {
 
685
                                chunk->mix_ratio = nan("");
 
686
                        }
 
687
 
 
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
 
695
                                   now */
 
696
                                music_buffer_return(player_buffer,
 
697
                                                    other_chunk);
 
698
                                other_chunk = NULL;
 
699
                        }
 
700
 
 
701
                        chunk->other = other_chunk;
575
702
                } else {
576
703
                        /* there are not enough decoded chunks yet */
577
 
                        if (decoder_is_idle()) {
 
704
 
 
705
                        decoder_lock(dc);
 
706
 
 
707
                        if (decoder_is_idle(dc)) {
578
708
                                /* the decoder isn't running, abort
579
709
                                   cross fading */
 
710
                                decoder_unlock(dc);
 
711
 
580
712
                                player->xfade = XFADE_DISABLED;
581
713
                        } else {
582
714
                                /* wait for the decoder */
583
 
                                notify_signal(&dc.notify);
584
 
                                notify_wait(&pc.notify);
 
715
                                decoder_signal(dc);
 
716
                                player_wait_decoder(dc);
 
717
                                decoder_unlock(dc);
585
718
 
586
719
                                return true;
587
720
                        }
603
736
 
604
737
        /* play the current chunk */
605
738
 
606
 
        success = play_chunk(player->song, chunk, &player->play_audio_format,
607
 
                             player->size_to_time);
608
 
 
609
 
        if (!success) {
 
739
        if (!play_chunk(player->song, chunk, &player->play_audio_format)) {
610
740
                music_buffer_return(player_buffer, chunk);
611
741
 
 
742
                player_lock();
 
743
 
 
744
                pc.error = PLAYER_ERROR_AUDIO;
 
745
 
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;
616
750
 
 
751
                player_unlock();
 
752
 
617
753
                return false;
618
754
        }
619
755
 
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 +
 
759
        decoder_lock(dc);
 
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);
 
763
                decoder_signal(dc);
 
764
        decoder_unlock(dc);
627
765
 
628
766
        return true;
629
767
}
633
771
 * has consumed all chunks of the current song, and we should start
634
772
 * sending chunks from the next one.
635
773
 *
 
774
 * The player lock is not held.
 
775
 *
636
776
 * @return true on success, false on error (playback will be stopped)
637
777
 */
638
778
static bool
640
780
{
641
781
        player->xfade = XFADE_UNKNOWN;
642
782
 
 
783
        char *uri = song_get_uri(player->song);
 
784
        g_message("played \"%s\"", uri);
 
785
        g_free(uri);
 
786
 
643
787
        music_pipe_free(player->pipe);
644
 
        player->pipe = dc.pipe;
 
788
        player->pipe = player->dc->pipe;
 
789
 
 
790
        audio_output_all_song_border();
645
791
 
646
792
        if (!player_wait_for_decoder(player))
647
793
                return false;
654
800
 * basically a state machine, which multiplexes data between the
655
801
 * decoder thread and the output threads.
656
802
 */
657
 
static void do_play(void)
 
803
static void do_play(struct decoder_control *dc)
658
804
{
659
805
        struct player player = {
 
806
                .dc = dc,
660
807
                .buffering = true,
661
808
                .decoder_starting = false,
662
809
                .paused = false,
663
 
                .queued = false,
 
810
                .queued = true,
664
811
                .song = NULL,
665
812
                .xfade = XFADE_UNKNOWN,
666
813
                .cross_fading = false,
667
814
                .cross_fade_chunks = 0,
668
815
                .cross_fade_tag = NULL,
669
 
                .size_to_time = 0.0,
 
816
                .elapsed_time = 0.0,
670
817
        };
671
818
 
 
819
        player_unlock();
 
820
 
672
821
        player.pipe = music_pipe_new();
673
822
 
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);
 
829
                player_lock();
681
830
                return;
682
831
        }
683
832
 
684
 
        pc.elapsed_time = 0;
 
833
        player_lock();
685
834
        pc.state = PLAYER_STATE_PLAY;
686
 
        player_command_finished();
 
835
        player_command_finished_locked();
687
836
 
688
837
        while (true) {
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) {
 
842
                        player_unlock();
693
843
                        audio_output_all_cancel();
694
844
                        break;
695
845
                }
696
846
 
 
847
                player_unlock();
 
848
 
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 */
701
853
 
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 */
705
857
 
706
858
                                if (!player.paused &&
709
861
                                    !player_send_silence(&player))
710
862
                                        break;
711
863
 
712
 
                                notify_wait(&pc.notify);
 
864
                                decoder_lock(dc);
 
865
                                /* XXX race condition: check decoder again */
 
866
                                player_wait_decoder(dc);
 
867
                                decoder_unlock(dc);
 
868
                                player_lock();
713
869
                                continue;
714
870
                        } else {
715
871
                                /* buffering is complete */
719
875
 
720
876
                if (player.decoder_starting) {
721
877
                        /* wait until the decoder is initialized completely */
722
 
                        bool success;
723
878
 
724
 
                        success = player_check_decoder_startup(&player);
725
 
                        if (!success)
 
879
                        if (!player_check_decoder_startup(&player))
726
880
                                break;
 
881
 
 
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
 
887
                               completely */
 
888
                            !player.decoder_starting &&
 
889
                            !dc_seek(dc, song->start_ms / 1000.0))
 
890
                                player_dc_stop(&player);
 
891
 
 
892
                        player_lock();
727
893
                        continue;
728
894
                }
729
895
 
731
897
                /*
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);
735
901
                */
736
902
#endif
737
903
 
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));
743
 
 
744
 
                        dc.pipe = NULL;
745
 
 
746
 
                        player.queued = false;
747
 
                        dc_start_async(pc.next_song, music_pipe_new());
 
908
 
 
909
                        assert(dc->pipe == NULL || dc->pipe == player.pipe);
 
910
 
 
911
                        player_dc_start(&player, music_pipe_new());
748
912
                }
749
913
 
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
755
919
                           for it */
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,
 
922
                                                pc.mixramp_db,
 
923
                                                pc.mixramp_delay_seconds,
 
924
                                                dc->replay_gain_db,
 
925
                                                dc->replay_gain_prev_db,
 
926
                                                dc->mixramp_start,
 
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);
768
938
                                player.xfade = XFADE_DISABLED;
769
939
                }
770
940
 
771
 
                if (player.paused)
772
 
                        notify_wait(&pc.notify);
773
 
                else if (!music_pipe_empty(player.pipe)) {
 
941
                if (player.paused) {
 
942
                        player_lock();
 
943
 
 
944
                        if (pc.command == PLAYER_COMMAND_NONE)
 
945
                                player_wait();
 
946
                        continue;
 
947
                } else if (!music_pipe_empty(player.pipe)) {
774
948
                        /* at least one music chunk is ready - send it
775
949
                           to the audio output */
776
950
 
787
961
 
788
962
                        if (!player_song_border(&player))
789
963
                                break;
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
 
970
                                   playback */
 
971
                                audio_output_all_drain();
795
972
                                break;
 
973
                        }
796
974
                } else {
797
975
                        /* the decoder is too busy and hasn't provided
798
976
                           new PCM data in time: send silence (if the
800
978
                        if (!player_send_silence(&player))
801
979
                                break;
802
980
                }
803
 
        }
804
981
 
805
 
        if (player.queued) {
806
 
                assert(pc.next_song != NULL);
807
 
                pc.next_song = NULL;
 
982
                player_lock();
808
983
        }
809
984
 
810
985
        player_dc_stop(&player);
815
990
        if (player.cross_fade_tag != NULL)
816
991
                tag_free(player.cross_fade_tag);
817
992
 
 
993
        player_lock();
 
994
 
 
995
        if (player.queued) {
 
996
                assert(pc.next_song != NULL);
 
997
                pc.next_song = NULL;
 
998
        }
 
999
 
818
1000
        pc.state = PLAYER_STATE_STOP;
 
1001
 
 
1002
        player_unlock();
 
1003
 
819
1004
        event_pipe_emit(PIPE_EVENT_PLAYLIST);
 
1005
 
 
1006
        player_lock();
820
1007
}
821
1008
 
822
1009
static gpointer player_task(G_GNUC_UNUSED gpointer arg)
823
1010
{
824
 
        decoder_thread_start();
 
1011
        struct decoder_control dc;
 
1012
 
 
1013
        dc_init(&dc);
 
1014
        decoder_thread_start(&dc);
825
1015
 
826
1016
        player_buffer = music_buffer_new(pc.buffer_chunks);
827
1017
 
 
1018
        player_lock();
 
1019
 
828
1020
        while (1) {
829
1021
                switch (pc.command) {
830
 
                case PLAYER_COMMAND_PLAY:
831
1022
                case PLAYER_COMMAND_QUEUE:
832
1023
                        assert(pc.next_song != NULL);
833
1024
 
834
 
                        do_play();
 
1025
                        do_play(&dc);
835
1026
                        break;
836
1027
 
837
1028
                case PLAYER_COMMAND_STOP:
 
1029
                        player_unlock();
838
1030
                        audio_output_all_cancel();
 
1031
                        player_lock();
 
1032
 
839
1033
                        /* fall through */
840
1034
 
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();
845
1039
                        break;
846
1040
 
847
1041
                case PLAYER_COMMAND_CLOSE_AUDIO:
848
 
                        audio_output_all_close();
849
 
                        player_command_finished();
 
1042
                        player_unlock();
 
1043
 
 
1044
                        audio_output_all_release();
 
1045
 
 
1046
                        player_lock();
 
1047
                        player_command_finished_locked();
850
1048
 
851
1049
#ifndef NDEBUG
852
1050
                        /* in the DEBUG build, check for leaked
858
1056
 
859
1057
                        break;
860
1058
 
 
1059
                case PLAYER_COMMAND_UPDATE_AUDIO:
 
1060
                        player_unlock();
 
1061
                        audio_output_all_enable_disable();
 
1062
                        player_lock();
 
1063
                        player_command_finished_locked();
 
1064
                        break;
 
1065
 
861
1066
                case PLAYER_COMMAND_EXIT:
862
 
                        dc_quit();
 
1067
                        player_unlock();
 
1068
 
 
1069
                        dc_quit(&dc);
 
1070
                        dc_deinit(&dc);
863
1071
                        audio_output_all_close();
864
1072
                        music_buffer_free(player_buffer);
 
1073
 
865
1074
                        player_command_finished();
866
 
                        g_thread_exit(NULL);
867
 
                        break;
 
1075
                        return NULL;
868
1076
 
869
1077
                case PLAYER_COMMAND_CANCEL:
870
1078
                        pc.next_song = NULL;
871
 
                        player_command_finished();
 
1079
                        player_command_finished_locked();
 
1080
                        break;
 
1081
 
 
1082
                case PLAYER_COMMAND_REFRESH:
 
1083
                        /* no-op when not playing */
 
1084
                        player_command_finished_locked();
872
1085
                        break;
873
1086
 
874
1087
                case PLAYER_COMMAND_NONE:
875
 
                        notify_wait(&pc.notify);
 
1088
                        player_wait();
876
1089
                        break;
877
1090
                }
878
1091
        }
879
 
        return NULL;
880
1092
}
881
1093
 
882
1094
void player_create(void)
883
1095
{
 
1096
        assert(pc.thread == NULL);
 
1097
 
884
1098
        GError *e = NULL;
885
 
 
886
 
        assert(pc.thread == NULL);
887
 
 
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);
891
1102
}