-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathThread.cxx
1344 lines (1084 loc) · 30.4 KB
/
Thread.cxx
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
/* \file
*
* The player thread controls the playback. It acts as a bridge
* between the decoder thread and the output thread(s): it receives
* #MusicChunk objects from the decoder, optionally mixes them
* (cross-fading), applies software volume, and sends them to the
* audio outputs via PlayerOutputs::Play()
* (i.e. MultipleOutputs::Play()).
*
* It is controlled by the main thread (the playlist code), see
* Control.hxx. The playlist enqueues new songs into the player
* thread and sends it commands.
*
* The player thread itself does not do any I/O. It synchronizes with
* other threads via #GMutex and #GCond objects, and passes
* #MusicChunk instances around in #MusicPipe objects.
*/
#include "Control.hxx"
#include "Outputs.hxx"
#include "Listener.hxx"
#include "decoder/Control.hxx"
#include "MusicPipe.hxx"
#include "MusicBuffer.hxx"
#include "MusicChunk.hxx"
#include "song/DetachedSong.hxx"
#include "CrossFade.hxx"
#include "pcm/MixRampGlue.hxx"
#include "tag/Tag.hxx"
#include "util/Domain.hxx"
#include "thread/Name.hxx"
#include "Log.hxx"
#include <exception>
#include <memory>
#include <string.h>
static constexpr Domain player_domain("player");
/**
* Start playback as soon as enough data for this duration has been
* pushed to the decoder pipe.
*/
static constexpr auto buffer_before_play_duration = std::chrono::seconds(1);
class Player {
PlayerControl &pc;
DecoderControl &dc;
MusicBuffer &buffer;
std::shared_ptr<MusicPipe> pipe;
/**
* the song currently being played
*/
std::unique_ptr<DetachedSong> song;
/**
* The tag of the "next" song during cross-fade. It is
* postponed, and sent to the output thread when the new song
* really begins.
*/
std::unique_ptr<Tag> cross_fade_tag;
/**
* Start playback as soon as this number of chunks has been
* pushed to the decoder pipe. This is calculated based on
* #buffer_before_play_duration.
*/
unsigned buffer_before_play;
/**
* If the decoder pipe gets consumed below this threshold,
* it's time to wake up the decoder.
*
* It is calculated in a way which should prevent a wakeup
* after each single consumed chunk; it is more efficient to
* make the decoder decode a larger block at a time.
*/
const unsigned decoder_wakeup_threshold;
/**
* Are we waiting for #buffer_before_play?
*/
bool buffering = true;
/**
* true if the decoder is starting and did not provide data
* yet
*/
bool decoder_starting = false;
/**
* Did we wake up the DecoderThread recently? This avoids
* duplicate wakeup calls.
*/
bool decoder_woken = false;
/**
* is the player paused?
*/
bool paused = false;
/**
* is there a new song in pc.next_song?
*/
bool queued = true;
/**
* Was any audio output opened successfully? It might have
* failed meanwhile, but was not explicitly closed by the
* player thread. When this flag is unset, some output
* methods must not be called.
*/
bool output_open = false;
/**
* Is cross-fading to the next song enabled?
*/
enum class CrossFadeState : uint8_t {
/**
* The initial state: we don't know yet if we will
* cross-fade; it will be determined soon.
*/
UNKNOWN,
/**
* Cross-fading is disabled for the transition to the
* next song.
*/
DISABLED,
/**
* Cross-fading is enabled (but may not yet be in
* progress), will start near the end of the current
* song.
*/
ENABLED,
/**
* Currently cross-fading to the next song.
*/
ACTIVE,
} xfade_state = CrossFadeState::UNKNOWN;
/**
* The number of chunks used for crossfading.
*/
unsigned cross_fade_chunks = 0;
/**
* The current audio format for the audio outputs.
*/
AudioFormat play_audio_format = AudioFormat::Undefined();
/**
* The time stamp of the chunk most recently sent to the
* output thread. This attribute is only used if
* MultipleOutputs::GetElapsedTime() didn't return a usable
* value; the output thread can estimate the elapsed time more
* precisely.
*/
SongTime elapsed_time = SongTime::zero();
/**
* If this is positive, then we need to ask the decoder to
* seek after it has completed startup. This is needed if the
* decoder is in the middle of startup while the player
* receives another seek command.
*
* This is only valid while #decoder_starting is true.
*/
SongTime pending_seek;
public:
Player(PlayerControl &_pc, DecoderControl &_dc,
MusicBuffer &_buffer) noexcept
:pc(_pc), dc(_dc), buffer(_buffer),
decoder_wakeup_threshold(buffer.GetSize() * 3 / 4)
{
}
private:
/**
* Reset cross-fading to the initial state. A check to
* re-enable it at an appropriate time will be scheduled.
*/
void ResetCrossFade() noexcept {
xfade_state = CrossFadeState::UNKNOWN;
}
template<typename P>
void ReplacePipe(P &&_pipe) noexcept {
ResetCrossFade();
pipe = std::forward<P>(_pipe);
}
/**
* Start the decoder.
*
* Caller must lock the mutex.
*/
void StartDecoder(std::unique_lock<Mutex> &lock,
std::shared_ptr<MusicPipe> pipe,
bool initial_seek_essential) noexcept;
/**
* The decoder has acknowledged the "START" command (see
* ActivateDecoder()). This function checks if the decoder
* initialization has completed yet. If not, it will wait
* some more.
*
* Caller must lock the mutex.
*
* @return false if the decoder has failed, true on success
* (though the decoder startup may or may not yet be finished)
*/
bool CheckDecoderStartup(std::unique_lock<Mutex> &lock) noexcept;
/**
* Stop the decoder and clears (and frees) its music pipe.
*
* Caller must lock the mutex.
*/
void StopDecoder(std::unique_lock<Mutex> &lock) noexcept;
/**
* Is the decoder still busy on the same song as the player?
*
* Note: this function does not check if the decoder is already
* finished.
*/
[[nodiscard]] [[gnu::pure]]
bool IsDecoderAtCurrentSong() const noexcept {
assert(pipe != nullptr);
return dc.pipe == pipe;
}
/**
* Returns true if the decoder is decoding the next song (or has begun
* decoding it, or has finished doing it), and the player hasn't
* switched to that song yet.
*/
[[nodiscard]] [[gnu::pure]]
bool IsDecoderAtNextSong() const noexcept {
return dc.pipe != nullptr && !IsDecoderAtCurrentSong();
}
/**
* Invoke DecoderControl::Seek() and update our state or
* handle errors.
*
* Caller must lock the mutex.
*
* @return false if the decoder has failed
*/
bool SeekDecoder(std::unique_lock<Mutex> &lock,
SongTime seek_time) noexcept;
/**
* This is the handler for the #PlayerCommand::SEEK command.
*
* Caller must lock the mutex.
*
* @return false if the decoder has failed
*/
bool SeekDecoder(std::unique_lock<Mutex> &lock) noexcept;
void CancelPendingSeek() noexcept {
pending_seek = SongTime::zero();
pc.CancelPendingSeek();
}
/**
* Check if the decoder has reported an error, and forward it
* to PlayerControl::SetError().
*
* @return false if an error has occurred
*/
bool ForwardDecoderError() noexcept;
/**
* After the decoder has been started asynchronously, activate
* it for playback. That is, make the currently decoded song
* active (assign it to #song), clear PlayerControl::next_song
* and #queued, initialize #elapsed_time, and set
* #decoder_starting.
*
* When returning, the decoder may not have completed startup
* yet, therefore we don't know the audio format yet. To
* finish decoder startup, call CheckDecoderStartup().
*
* Caller must lock the mutex.
*/
void ActivateDecoder() noexcept;
/**
* Wrapper for MultipleOutputs::Open(). Upon failure, it
* pauses the player.
*
* Caller must lock the mutex.
*
* @return true on success
*/
bool OpenOutput() noexcept;
std::string UnlockAnalyzeMixRamp(const MusicPipe &pipe,
const AudioFormat &audio_format,
MixRampDirection direction) noexcept;
/**
* @return false if more chunks of the next song are needed to
* scan for MixRamp data
*/
[[nodiscard]]
bool MixRampScannerReady() noexcept;
void CheckCrossFade() noexcept;
/**
* Obtains the next chunk from the music pipe, optionally applies
* cross-fading, and sends it to all audio outputs.
*
* @return true on success, false on error (playback will be stopped)
*/
bool PlayNextChunk() noexcept;
unsigned UnlockCheckOutputs() noexcept {
const ScopeUnlock unlock(pc.mutex);
return pc.outputs.CheckPipe();
}
/**
* Player lock must be held before calling.
*
* @return false to stop playback
*/
bool ProcessCommand(std::unique_lock<Mutex> &lock) noexcept;
/**
* This is called at the border between two songs: the audio output
* has consumed all chunks of the current song, and we should start
* sending chunks from the next one.
*
* Caller must lock the mutex.
*/
void SongBorder() noexcept;
public:
/*
* The main loop of the player thread, during playback. This
* is basically a state machine, which multiplexes data
* between the decoder thread and the output threads.
*/
void Run() noexcept;
};
void
Player::StartDecoder(std::unique_lock<Mutex> &lock,
std::shared_ptr<MusicPipe> _pipe,
bool initial_seek_essential) noexcept
{
assert(queued || pc.command == PlayerCommand::SEEK);
assert(pc.next_song != nullptr);
/* copy ReplayGain parameters to the decoder */
dc.replay_gain_mode = pc.replay_gain_mode;
SongTime start_time = pc.next_song->GetStartTime() + pc.seek_time;
dc.Start(lock, std::make_unique<DetachedSong>(*pc.next_song),
start_time, pc.next_song->GetEndTime(),
initial_seek_essential,
buffer, std::move(_pipe));
}
void
Player::StopDecoder(std::unique_lock<Mutex> &lock) noexcept
{
const PlayerControl::ScopeOccupied occupied(pc);
dc.Stop(lock);
if (dc.pipe != nullptr) {
/* clear and free the decoder pipe */
dc.pipe->Clear();
dc.pipe.reset();
/* just in case we've been cross-fading: cancel it
now, because we just deleted the new song's decoder
pipe */
ResetCrossFade();
}
}
bool
Player::ForwardDecoderError() noexcept
{
try {
dc.CheckRethrowError();
} catch (...) {
pc.SetError(PlayerError::DECODER, std::current_exception());
return false;
}
return true;
}
void
Player::ActivateDecoder() noexcept
{
assert(queued || pc.command == PlayerCommand::SEEK);
assert(pc.next_song != nullptr);
queued = false;
pc.ClearTaggedSong();
song = std::exchange(pc.next_song, nullptr);
elapsed_time = pc.seek_time;
/* set the "starting" flag, which will be cleared by
CheckDecoderStartup() */
decoder_starting = true;
pending_seek = SongTime::zero();
/* update PlayerControl's song information */
pc.total_time = song->GetDuration();
pc.bit_rate = 0;
pc.audio_format.Clear();
{
/* call playlist::SyncWithPlayer() in the main thread */
const ScopeUnlock unlock(pc.mutex);
pc.listener.OnPlayerSync();
}
}
/**
* Returns the real duration of the song, comprising the duration
* indicated by the decoder plugin.
*/
static SignedSongTime
real_song_duration(const DetachedSong &song,
SignedSongTime decoder_duration) noexcept
{
if (decoder_duration.IsNegative())
/* the decoder plugin didn't provide information; fall
back to Song::GetDuration() */
return song.GetDuration();
const SongTime start_time = song.GetStartTime();
const SongTime end_time = song.GetEndTime();
if (end_time.IsPositive() && end_time < SongTime(decoder_duration))
return {end_time - start_time};
return {SongTime(decoder_duration) - start_time};
}
std::string
Player::UnlockAnalyzeMixRamp(const MusicPipe &_pipe,
const AudioFormat &audio_format,
MixRampDirection direction) noexcept
{
const ScopeUnlock unlock(pc.mutex);
return AnalyzeMixRamp(_pipe, audio_format, direction);
}
inline bool
Player::MixRampScannerReady() noexcept
{
assert(pipe);
assert(dc.pipe);
if (!pc.cross_fade.IsMixRampEnabled())
return true;
if (!pc.config.mixramp_analyzer)
/* always ready if the scanner is disabled */
return true;
if (dc.GetMixRampPreviousEnd() == nullptr) {
// TODO: scan incrementally backwards until mixrampdb is reached
auto s = UnlockAnalyzeMixRamp(*pipe, play_audio_format,
MixRampDirection::END);
if (!s.empty()) {
FmtDebug(player_domain, "Analyzed MixRamp end: {}", s);
dc.SetMixRampPreviousEnd(std::move(s));
}
if (dc.GetMixRampStart() == nullptr)
/* scan the next song in the next call; first,
let the main loop submit a few more chunks
to the outputs for playback to avoid
xrun */
return false;
}
if (dc.GetMixRampStart() == nullptr) {
const std::size_t want_pipe_bytes =
dc.out_audio_format.TimeToSize(std::chrono::seconds{20});
const std::size_t want_pipe_chunks =
std::min((want_pipe_bytes + sizeof(MusicChunk::data) - 1)
/ sizeof(MusicChunk::data),
buffer.GetSize() / std::size_t{3});
if (dc.pipe->GetSize() < want_pipe_chunks) {
/* need more data */
if (!buffer.IsFull()) {
decoder_woken = true;
dc.Signal();
}
return false;
}
// TODO: scan incrementally until mixrampdb is reached
auto s = UnlockAnalyzeMixRamp(*dc.pipe, dc.out_audio_format,
MixRampDirection::START);
if (!s.empty()) {
FmtDebug(player_domain, "Analyzed MixRamp start: {}", s);
dc.SetMixRampStart(std::move(s));
}
}
return true;
}
bool
Player::OpenOutput() noexcept
{
assert(play_audio_format.IsDefined());
assert(pc.state == PlayerState::PLAY ||
pc.state == PlayerState::PAUSE);
try {
const ScopeUnlock unlock(pc.mutex);
pc.outputs.Open(play_audio_format);
} catch (...) {
LogError(std::current_exception());
output_open = false;
/* pause: the user may resume playback as soon as an
audio output becomes available */
paused = true;
pc.SetOutputError(std::current_exception());
return false;
}
output_open = true;
paused = false;
pc.state = PlayerState::PLAY;
pc.listener.OnPlayerStateChanged();
return true;
}
inline bool
Player::CheckDecoderStartup(std::unique_lock<Mutex> &lock) noexcept
{
assert(decoder_starting);
if (!ForwardDecoderError()) {
/* the decoder failed */
return false;
} else if (!dc.IsStarting()) {
/* the decoder is ready and ok */
if (output_open &&
!pc.WaitOutputConsumed(lock, 1))
/* the output devices havn't finished playing
all chunks yet - wait for that */
return true;
pc.total_time = real_song_duration(*dc.song,
dc.total_time);
pc.audio_format = dc.in_audio_format;
play_audio_format = dc.out_audio_format;
decoder_starting = false;
const size_t buffer_before_play_size =
play_audio_format.TimeToSize(buffer_before_play_duration);
buffer_before_play =
(buffer_before_play_size + sizeof(MusicChunk::data) - 1)
/ sizeof(MusicChunk::data);
pc.listener.OnPlayerStateChanged();
if (pending_seek > SongTime::zero()) {
assert(pc.seeking);
bool success = SeekDecoder(lock, pending_seek);
pc.seeking = false;
pc.ClientSignal();
if (!success)
return false;
/* re-fill the buffer after seeking */
buffering = true;
} else if (pc.seeking) {
pc.seeking = false;
pc.ClientSignal();
/* re-fill the buffer after seeking */
buffering = true;
}
if (!paused && !OpenOutput()) {
FmtError(player_domain,
"problems opening audio device "
"while playing \"{}\"",
dc.song->GetURI());
return true;
}
return true;
} else {
/* the decoder is not yet ready; wait
some more */
dc.WaitForDecoder(lock);
return true;
}
}
bool
Player::SeekDecoder(std::unique_lock<Mutex> &lock, SongTime seek_time) noexcept
{
assert(song);
assert(!decoder_starting);
if (!pc.total_time.IsNegative()) {
const SongTime total_time(pc.total_time);
if (seek_time > total_time)
seek_time = total_time;
}
try {
const PlayerControl::ScopeOccupied occupied(pc);
dc.Seek(lock, song->GetStartTime() + seek_time);
} catch (...) {
/* decoder failure */
pc.SetError(PlayerError::DECODER, std::current_exception());
return false;
}
elapsed_time = seek_time;
return true;
}
inline bool
Player::SeekDecoder(std::unique_lock<Mutex> &lock) noexcept
{
assert(pc.next_song != nullptr);
if (pc.seek_time > SongTime::zero() && // TODO: allow this only if the song duration is known
dc.IsUnseekableCurrentSong(*pc.next_song)) {
/* seeking into the current song; but we already know
it's not seekable, so let's fail early */
/* note the seek_time>0 check: if seeking to the
beginning, we can simply restart the decoder */
pc.next_song.reset();
pc.SetError(PlayerError::DECODER,
std::make_exception_ptr(std::runtime_error("Not seekable")));
pc.CommandFinished();
return true;
}
CancelPendingSeek();
{
const ScopeUnlock unlock(pc.mutex);
pc.outputs.Cancel();
}
pc.listener.OnPlayerStateChanged();
if (!dc.IsSeekableCurrentSong(*pc.next_song)) {
/* the decoder is already decoding the "next" song -
stop it and start the previous song again */
StopDecoder(lock);
/* clear music chunks which might still reside in the
pipe */
pipe->Clear();
/* re-start the decoder */
StartDecoder(lock, pipe, true);
ActivateDecoder();
pc.seeking = true;
pc.CommandFinished();
assert(xfade_state == CrossFadeState::UNKNOWN);
return true;
} else {
if (!IsDecoderAtCurrentSong()) {
/* the decoder is already decoding the "next" song,
but it is the same song file; exchange the pipe */
ReplacePipe(dc.pipe);
}
pc.next_song.reset();
queued = false;
if (decoder_starting) {
/* wait for the decoder to complete
initialization; postpone the SEEK
command */
pending_seek = pc.seek_time;
pc.seeking = true;
pc.CommandFinished();
return true;
} else {
/* send the SEEK command */
if (!SeekDecoder(lock, pc.seek_time)) {
pc.CommandFinished();
return false;
}
}
}
pc.CommandFinished();
assert(xfade_state == CrossFadeState::UNKNOWN);
/* re-fill the buffer after seeking */
buffering = true;
{
/* call syncPlaylistWithQueue() in the main thread */
const ScopeUnlock unlock(pc.mutex);
pc.listener.OnPlayerSync();
}
return true;
}
inline bool
Player::ProcessCommand(std::unique_lock<Mutex> &lock) noexcept
{
switch (pc.command) {
case PlayerCommand::NONE:
break;
case PlayerCommand::STOP:
case PlayerCommand::EXIT:
case PlayerCommand::CLOSE_AUDIO:
return false;
case PlayerCommand::UPDATE_AUDIO:
{
const ScopeUnlock unlock(pc.mutex);
pc.outputs.EnableDisable();
}
pc.CommandFinished();
break;
case PlayerCommand::QUEUE:
assert(pc.next_song != nullptr);
assert(!queued);
assert(!IsDecoderAtNextSong());
queued = true;
pc.CommandFinished();
if (dc.IsIdle())
StartDecoder(lock, std::make_shared<MusicPipe>(),
false);
break;
case PlayerCommand::PAUSE:
paused = !paused;
if (paused) {
pc.state = PlayerState::PAUSE;
const ScopeUnlock unlock(pc.mutex);
pc.outputs.Pause();
} else if (!play_audio_format.IsDefined()) {
/* the decoder hasn't provided an audio format
yet - don't open the audio device yet */
pc.state = PlayerState::PLAY;
} else {
OpenOutput();
}
pc.CommandFinished();
break;
case PlayerCommand::SEEK:
return SeekDecoder(lock);
case PlayerCommand::CANCEL:
if (pc.next_song == nullptr)
/* the cancel request arrived too late, we're
already playing the queued song... stop
everything now */
return false;
if (IsDecoderAtNextSong())
/* the decoder is already decoding the song -
stop it and reset the position */
StopDecoder(lock);
pc.next_song.reset();
queued = false;
pc.CommandFinished();
break;
case PlayerCommand::REFRESH:
if (output_open && !paused) {
const ScopeUnlock unlock(pc.mutex);
pc.outputs.CheckPipe();
}
pc.elapsed_time = !pc.outputs.GetElapsedTime().IsNegative()
? SongTime(pc.outputs.GetElapsedTime())
: elapsed_time;
pc.CommandFinished();
break;
}
return true;
}
inline void
Player::CheckCrossFade() noexcept
{
if (xfade_state != CrossFadeState::UNKNOWN)
/* already decided */
return;
if (pc.border_pause) {
/* no cross-fading if MPD is going to pause at the end
of the current song */
xfade_state = CrossFadeState::UNKNOWN;
return;
}
if (!IsDecoderAtNextSong() || dc.IsStarting())
/* we need information about the next song before we
can decide */
return;
if (!pc.cross_fade.CanCrossFade(pc.total_time, dc.total_time,
dc.out_audio_format,
play_audio_format)) {
/* cross fading is disabled or the next song is too
short */
xfade_state = CrossFadeState::DISABLED;
return;
}
if (!MixRampScannerReady())
/* need more chunks for the MixRamp scanner */
return;
/* enable cross fading in this song? if yes, calculate how
many chunks will be required for it */
cross_fade_chunks =
pc.cross_fade.Calculate(dc.replay_gain_db,
dc.replay_gain_prev_db,
dc.GetMixRampStart(),
dc.GetMixRampPreviousEnd(),
play_audio_format,
buffer.GetSize() -
buffer_before_play);
if (cross_fade_chunks > 0)
xfade_state = CrossFadeState::ENABLED;
else
// TODO: eliminate this "else" branch
xfade_state = CrossFadeState::DISABLED;
}
inline void
PlayerControl::LockUpdateSongTag(DetachedSong &song,
const Tag &new_tag) noexcept
{
if (song.IsFile())
/* don't update tags of local files, only remote
streams may change tags dynamically */
return;
song.SetTag(new_tag);
LockSetTaggedSong(song);
/* the main thread will update the playlist version when he
receives this event */
listener.OnPlayerTagModified();
}
inline void
PlayerControl::PlayChunk(DetachedSong &song, MusicChunkPtr chunk,
const AudioFormat &format)
{
assert(chunk->CheckFormat(format));
if (chunk->tag != nullptr)
LockUpdateSongTag(song, *chunk->tag);
if (chunk->IsEmpty())
return;
{
const std::scoped_lock<Mutex> lock(mutex);
bit_rate = chunk->bit_rate;
}
/* send the chunk to the audio outputs */
const double chunk_length(chunk->length);
outputs.Play(std::move(chunk));
total_play_time += format.SizeToTime<decltype(total_play_time)>(chunk_length);
}
inline bool
Player::PlayNextChunk() noexcept
{
if (!pc.LockWaitOutputConsumed(64))
/* the output pipe is still large enough, don't send
another chunk */
return true;
/* activate cross-fading? */
if (xfade_state == CrossFadeState::ENABLED &&
IsDecoderAtNextSong() &&
pipe->GetSize() <= cross_fade_chunks) {
/* beginning of the cross fade - adjust
cross_fade_chunks which might be bigger than the
remaining number of chunks in the old song */
cross_fade_chunks = pipe->GetSize();
xfade_state = CrossFadeState::ACTIVE;
}
MusicChunkPtr chunk;
if (xfade_state == CrossFadeState::ACTIVE) {
/* perform cross fade */
assert(IsDecoderAtNextSong());
unsigned cross_fade_position = pipe->GetSize();
assert(cross_fade_position <= cross_fade_chunks);
auto other_chunk = dc.pipe->Shift();
if (other_chunk != nullptr) {
chunk = pipe->Shift();
assert(chunk != nullptr);
assert(chunk->other == nullptr);
/* don't send the tags of the new song (which
is being faded in) yet; postpone it until
the current song is faded out */
cross_fade_tag = Tag::Merge(std::move(cross_fade_tag),
std::move(other_chunk->tag));
if (pc.cross_fade.mixramp_delay <= FloatDuration::zero()) {
chunk->mix_ratio = ((float)cross_fade_position)
/ cross_fade_chunks;
} else {
chunk->mix_ratio = -1;
}
if (other_chunk->IsEmpty()) {
/* the "other" chunk was a MusicChunk
which had only a tag, but no music
data - we cannot cross-fade that;
but since this happens only at the
beginning of the new song, we can
easily recover by throwing it away
now */
other_chunk.reset();
}
chunk->other = std::move(other_chunk);
} else {
/* there are not enough decoded chunks yet */