forked from DeaDBeeF-Player/deadbeef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer.c
3001 lines (2721 loc) · 93.2 KB
/
streamer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
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
/*
This file is part of Deadbeef Player source code
http://deadbeef.sourceforge.net
streamer implementation
Copyright (C) 2009-2013 Alexey Yakovenko
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Alexey Yakovenko [email protected]
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#ifdef __linux__
#include <sys/prctl.h>
#endif
#include <sys/time.h>
#include <errno.h>
#include "threading.h"
#include "playlist.h"
#include "common.h"
#include "streamer.h"
#include "messagepump.h"
#include "conf.h"
#include "plugins.h"
#include "fastftoi.h"
#include "volume.h"
#include "vfs.h"
#include "premix.h"
#include "ringbuf.h"
#include "replaygain.h"
#include "fft.h"
#include "handler.h"
#include "plugins/libparser/parser.h"
#include "strdupa.h"
//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(fmt,...)
//#define WRITE_DUMP 1
//#define DETECT_PL_LOCK_RC 1
#if WRITE_DUMP
FILE *out;
#endif
#define MAX_PLAYLIST_DOWNLOAD_SIZE 25000
static int
streamer_read_async (char *bytes, int size);
static int
streamer_set_output_format (void);
static intptr_t streamer_tid;
static ddb_dsp_context_t *dsp_chain;
static float dsp_ratio = 1;
static DB_dsp_t *eqplug;
static ddb_dsp_context_t *eq;
static int dsp_on = 0;
static int autoconv_8_to_16 = 1;
static int autoconv_16_to_24 = 0;
static int trace_bufferfill = 0;
static int stop_after_current = 0;
static int stop_after_album = 0;
static int streaming_terminate;
// buffer up to 3 seconds at 44100Hz stereo
#define STREAM_BUFFER_SIZE 0x80000 // slightly more than 3 seconds of 44100 stereo
// how much bigger should read-buffer be to allow upsampling.
// e.g. 8000Hz -> 192000Hz upsampling requires 24x buffer size,
// so if we originally request 4096 bytes blocks -
// that will require 24x buffer size, which is 98304 bytes buffer
#define MAX_DSP_RATIO 24
#define MIN_BLOCK_SIZE 4096
#define MAX_BLOCK_SIZE 16384
#define READBUFFER_SIZE (MAX_BLOCK_SIZE * MAX_DSP_RATIO)
static char readbuffer[READBUFFER_SIZE];
static ringbuf_t streamer_ringbuf;
static char streambuffer[STREAM_BUFFER_SIZE];
static int bytes_until_next_song = 0;
static uintptr_t mutex;
static uintptr_t decodemutex;
static uintptr_t wdl_mutex; // wavedata listener
static int nextsong = -1;
static int nextsong_pstate = -1;
static int badsong = -1;
static float last_seekpos = -1;
static float playpos = 0; // play position of current song
static int avg_bitrate = -1; // avg bitrate of current song
static int last_bitrate = -1; // last bitrate of current song
static playlist_t *streamer_playlist;
static playItem_t *playing_track;
static float playtime; // total playtime of playing track
static time_t started_timestamp; // result of calling time(NULL)
static playItem_t *streaming_track;
static playItem_t *playlist_track;
static ddb_waveformat_t output_format; // format that was requested after DSP
static ddb_waveformat_t orig_output_format; // format that was requested before DSP
static int formatchanged;
static DB_fileinfo_t *fileinfo;
static DB_FILE *fileinfo_file;
static DB_fileinfo_t *new_fileinfo;
static DB_FILE *new_fileinfo_file;
static int streamer_buffering;
// to allow interruption of stall file requests
static DB_FILE *streamer_file;
// for vis plugins
static float freq_data[DDB_FREQ_BANDS * DDB_FREQ_MAX_CHANNELS];
static float audio_data[DDB_FREQ_BANDS * 2 * DDB_FREQ_MAX_CHANNELS];
static int audio_data_fill = 0;
static int audio_data_channels = 0;
// message queue
static struct handler_s *handler;
// visualization stuff
typedef struct wavedata_listener_s {
void *ctx;
void (*callback)(void *ctx, ddb_audio_data_t *data);
struct wavedata_listener_s *next;
} wavedata_listener_t;
static wavedata_listener_t *waveform_listeners;
static wavedata_listener_t *spectrum_listeners;
#if DETECT_PL_LOCK_RC
volatile pthread_t streamer_lock_tid = 0;
#endif
void
streamer_lock (void) {
#if DETECT_PL_LOCK_RC
extern pthread_t pl_lock_tid;
assert (pl_lock_tid != pthread_self()); // not permitted to lock streamer from inside of pl_lock
#endif
mutex_lock (mutex);
#if DETECT_PL_LOCK_RC
streamer_lock_tid = pthread_self();
#endif
}
void
streamer_unlock (void) {
#if DETECT_PL_LOCK_RC
streamer_lock_tid = 0;
#endif
mutex_unlock (mutex);
}
static void
streamer_set_nextsong_real (int song, int pstate);
static int
streamer_move_to_nextsong_real (int r);
static int
streamer_move_to_prevsong_real (int r);
static int
streamer_move_to_randomsong_real (int r);
static void
streamer_play_current_track_real (void);
static void
streamer_set_current_playlist_real (int plt);
static void
streamer_abort_files (void) {
DB_FILE *file = fileinfo_file;
DB_FILE *newfile = new_fileinfo_file;
DB_FILE *strfile = streamer_file;
trace ("\033[0;33mstreamer_abort_files\033[37;0m\n");
trace ("%p %p %p\n", file, newfile, strfile);
if (file) {
deadbeef->fabort (file);
}
if (newfile) {
deadbeef->fabort (newfile);
}
if (strfile) {
deadbeef->fabort (strfile);
}
}
static void
streamer_set_replaygain (playItem_t *it) {
// setup replaygain
pl_lock ();
const char *gain;
gain = pl_find_meta (it, ":REPLAYGAIN_ALBUMGAIN");
float albumgain = gain ? atof (gain) : 1000;
float albumpeak = pl_get_item_replaygain (it, DDB_REPLAYGAIN_ALBUMPEAK);
gain = pl_find_meta (it, ":REPLAYGAIN_TRACKGAIN");
float trackgain = gain ? atof (gain) : 1000;
float trackpeak = pl_get_item_replaygain (it, DDB_REPLAYGAIN_TRACKPEAK);
pl_unlock ();
replaygain_set_values (albumgain, albumpeak, trackgain, trackpeak);
}
static void
send_songstarted (playItem_t *trk) {
ddb_event_track_t *pev = (ddb_event_track_t *)messagepump_event_alloc (DB_EV_SONGSTARTED);
pev->track = DB_PLAYITEM (trk);
pl_item_ref (trk);
pev->playtime = 0;
pev->started_timestamp = time(NULL);
messagepump_push_event ((ddb_event_t*)pev, 0, 0);
}
static void
send_songfinished (playItem_t *trk) {
ddb_event_track_t *pev = (ddb_event_track_t *)messagepump_event_alloc (DB_EV_SONGFINISHED);
pev->track = DB_PLAYITEM (trk);
pl_item_ref (trk);
pev->playtime = playtime;
pev->started_timestamp = started_timestamp;
messagepump_push_event ((ddb_event_t*)pev, 0, 0);
}
static void
send_trackchanged (playItem_t *from, playItem_t *to) {
ddb_event_trackchange_t *event = (ddb_event_trackchange_t *)messagepump_event_alloc (DB_EV_SONGCHANGED);
event->playtime = playtime;
event->started_timestamp = started_timestamp;
if (from) {
pl_item_ref (from);
}
if (to) {
pl_item_ref (to);
}
event->from = (DB_playItem_t *)from;
event->to = (DB_playItem_t *)to;
messagepump_push_event ((ddb_event_t *)event, 0, 0);
}
static void
streamer_start_playback (playItem_t *from, playItem_t *it) {
if (from) {
pl_item_ref (from);
}
if (it) {
pl_item_ref (it);
}
// free old copy of playing
if (playing_track) {
pl_item_unref (playing_track);
playing_track = NULL;
}
pl_lock ();
playlist_track = it;
pl_unlock ();
// assign new
playing_track = it;
if (playing_track) {
pl_item_ref (playing_track);
playing_track->played = 1;
trace ("from=%p (%s), to=%p (%s) [2]\n", from, from ? pl_find_meta (from, ":URI") : "null", it, it ? pl_find_meta (it, ":URI") : "null");
send_trackchanged (from, it);
started_timestamp = time (NULL);
}
if (from) {
pl_item_unref (from);
}
if (it) {
pl_item_unref (it);
}
trace ("streamer_start_playback %s\n", playing_track ? pl_find_meta (playing_track, ":URI") : "null");
}
playItem_t *
streamer_get_streaming_track (void) {
if (streaming_track) {
pl_item_ref (streaming_track);
}
return streaming_track;
}
playItem_t *
streamer_get_playing_track (void) {
playItem_t *it = playing_track;// ? playing_track : playlist_track;
if (it) {
pl_item_ref (it);
}
return it;
}
int
str_get_idx_of (playItem_t *it) {
pl_lock ();
if (!streamer_playlist) {
streamer_playlist = plt_get_curr ();
}
playItem_t *c = streamer_playlist->head[PL_MAIN];
int idx = 0;
while (c && c != it) {
c = c->next[PL_MAIN];
idx++;
}
if (!c) {
pl_unlock ();
return -1;
}
pl_unlock ();
return idx;
}
playItem_t *
str_get_for_idx (int idx) {
pl_lock ();
if (!streamer_playlist) {
streamer_playlist = plt_get_curr ();
}
playItem_t *it = streamer_playlist->head[PL_MAIN];
while (idx--) {
if (!it) {
pl_unlock ();
return NULL;
}
it = it->next[PL_MAIN];
}
if (it) {
pl_item_ref (it);
}
pl_unlock ();
return it;
}
static void
send_trackinfochanged (playItem_t *track) {
ddb_event_track_t *ev = (ddb_event_track_t *)messagepump_event_alloc (DB_EV_TRACKINFOCHANGED);
ev->track = DB_PLAYITEM (track);
if (track) {
pl_item_ref (track);
}
messagepump_push_event ((ddb_event_t*)ev, 0, 0);
}
static int
stop_after_album_check (playItem_t *cur, playItem_t *next) {
if (!stop_after_album) {
return 0;
}
if (!cur) {
return 0;
}
if (!next) {
streamer_buffering = 0;
streamer_set_nextsong_real (-2, -2);
if (conf_get_int ("playlist.stop_after_album_reset", 0)) {
conf_set_int ("playlist.stop_after_album", 0);
stop_after_album = 0;
deadbeef->sendmessage (DB_EV_CONFIGCHANGED, 0, 0, 0);
}
return 1;
}
const char *cur_artist = pl_find_meta_raw (cur, "artist");
const char *next_artist = pl_find_meta_raw (next, "artist");
const char *cur_album = pl_find_meta_raw (cur, "album");
const char *next_album = pl_find_meta_raw (next, "album");
const char *cur_aa = pl_find_meta_raw (cur, "band");
if (!cur_aa) {
cur_aa = pl_find_meta_raw (cur, "album artist");
}
if (!cur_aa) {
cur_aa = pl_find_meta_raw (cur, "albumartist");
}
const char *next_aa = pl_find_meta_raw (next, "band");
if (!next_aa) {
next_aa = pl_find_meta_raw (next, "album artist");
}
if (!next_aa) {
next_aa = pl_find_meta_raw (next, "albumartist");
}
if ((cur_artist == next_artist) && (cur_album == next_album) && (cur_aa == next_aa)) {
return 0;
}
streamer_buffering = 0;
streamer_set_nextsong_real (-2, -2);
if (conf_get_int ("playlist.stop_after_album_reset", 0)) {
conf_set_int ("playlist.stop_after_album", 0);
stop_after_album = 0;
deadbeef->sendmessage (DB_EV_CONFIGCHANGED, 0, 0, 0);
}
return 1;
}
static int
streamer_move_to_nextsong_real (int reason) {
if (reason) {
plug_get_output ()->stop ();
}
trace ("streamer_move_to_nextsong (%d)\n", reason);
pl_lock ();
if (!streamer_playlist) {
streamer_playlist = plt_get_curr ();
}
playItem_t *curr = playlist_track;
while (pl_playqueue_getcount ()) {
trace ("pl_playqueue_getnext\n");
playItem_t *it = pl_playqueue_getnext ();
if (it) {
if (stop_after_album_check(curr, it)) {
pl_unlock ();
return -1;
}
pl_playqueue_pop ();
int r = str_get_idx_of (it);
if (r >= 0) {
pl_item_unref (it);
pl_unlock ();
streamer_set_nextsong_real (r, 1);
return 0;
}
else {
trace ("%s not found in current streaming playlist\n", pl_find_meta (it, ":URI"));
playlist_t *p = pl_get_playlist (it);
if (p) {
if (streamer_playlist) {
plt_unref (streamer_playlist);
}
streamer_playlist = p;
int r = str_get_idx_of (it);
if (r >= 0) {
pl_item_unref (it);
pl_unlock ();
streamer_set_nextsong_real (r, 3);
return 0;
}
}
trace ("%s not found in any playlists\n", pl_find_meta (it, ":URI"));
pl_item_unref (it);
}
}
}
if (reason == 1) {
if (streamer_playlist) {
plt_unref (streamer_playlist);
}
streamer_playlist = plt_get_curr ();
// check if prev song is in this playlist
if (-1 == str_get_idx_of (curr)) {
curr = NULL;
}
}
playlist_t *plt = streamer_playlist;
if (!plt->head[PL_MAIN]) {
pl_unlock ();
streamer_set_nextsong_real (-2, 1);
return 0;
}
int pl_order = pl_get_order ();
int pl_loop_mode = conf_get_int ("playback.loop", 0);
if (reason == 0 && pl_loop_mode == PLAYBACK_MODE_LOOP_SINGLE) { // song finished, loop mode is "loop 1 track"
int r = str_get_idx_of (playing_track);
pl_unlock ();
if (r == -1) {
streamer_set_nextsong_real (-2, 1);
}
else {
streamer_set_nextsong_real (r, 1);
}
return 0;
}
if (pl_order == PLAYBACK_ORDER_SHUFFLE_TRACKS || pl_order == PLAYBACK_ORDER_SHUFFLE_ALBUMS) { // shuffle
if (!curr || pl_order == PLAYBACK_ORDER_SHUFFLE_TRACKS) {
// find minimal notplayed
playItem_t *pmin = NULL; // notplayed minimum
for (playItem_t *i = plt->head[PL_MAIN]; i; i = i->next[PL_MAIN]) {
if (i->played) {
continue;
}
if (!pmin || i->shufflerating < pmin->shufflerating) {
pmin = i;
}
}
playItem_t *it = pmin;
// although it is possible that, although it == NULL, reshuffling the playlist
// will result in the next track belonging to the same album as this one, this
// is most likely not what the user wants.
if (stop_after_album_check(curr, it)) {
pl_unlock ();
return -1;
}
if (!it) {
// all songs played, reshuffle and try again
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) { // loop
plt_reshuffle (streamer_playlist, &it, NULL);
}
}
if (!it) {
streamer_buffering = 0;
send_trackinfochanged (streaming_track);
playItem_t *temp;
plt_reshuffle (streamer_playlist, &temp, NULL);
pl_unlock ();
streamer_set_nextsong_real (-2, -2);
return -1;
}
int r = str_get_idx_of (it);
pl_unlock ();
streamer_set_nextsong_real (r, 1);
return 0;
}
else {
trace ("pl_next_song: reason=%d, loop=%d\n", reason, pl_loop_mode);
// find minimal notplayed above current
int rating = curr->shufflerating;
playItem_t *pmin = NULL; // notplayed minimum
for (playItem_t *i = plt->head[PL_MAIN]; i; i = i->next[PL_MAIN]) {
if (i->played || i->shufflerating < rating) {
continue;
}
if (!pmin || i->shufflerating < pmin->shufflerating) {
pmin = i;
}
}
playItem_t *it = pmin;
if (stop_after_album_check(curr, it)) {
pl_unlock ();
return -1;
}
if (!it) {
// all songs played, reshuffle and try again
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL || reason == 1) { // loop
trace ("all songs played! reshuffle\n");
plt_reshuffle (streamer_playlist, &it, NULL);
}
}
if (!it) {
streamer_buffering = 0;
send_trackinfochanged (streaming_track);
playItem_t *temp;
plt_reshuffle (streamer_playlist, &temp, NULL);
pl_unlock ();
streamer_set_nextsong_real (-2, -2);
return -1;
}
int r = str_get_idx_of (it);
pl_unlock ();
streamer_set_nextsong_real (r, 1);
return 0;
}
}
else if (pl_order == PLAYBACK_ORDER_LINEAR) { // linear
playItem_t *it = NULL;
if (!curr) {
int cur = plt_get_cursor (streamer_playlist, PL_MAIN);
if (cur != -1) {
curr = plt_get_item_for_idx (streamer_playlist, cur, PL_MAIN);
pl_item_unref (curr);
}
}
if (curr) {
it = curr->next[PL_MAIN];
}
else {
it = streamer_playlist->head[PL_MAIN];
}
if (stop_after_album_check(curr, it)) {
pl_unlock ();
return -1;
}
if (!it) {
trace ("streamer_move_nextsong: was last track\n");
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) {
it = plt->head[PL_MAIN];
}
else {
streamer_buffering = 0;
send_trackinfochanged (streaming_track);
badsong = -1;
pl_unlock ();
streamer_set_nextsong_real (-2, -2);
return 0;
}
}
if (!it) {
pl_unlock ();
return -1;
}
int r = str_get_idx_of (it);
pl_unlock ();
streamer_set_nextsong_real (r, 1);
return 0;
}
else if (pl_order == PLAYBACK_ORDER_RANDOM) { // random
pl_unlock ();
int res = streamer_move_to_randomsong_real (0);
if (res == -1) {
trace ("streamer_move_to_randomsong error\n");
streamer_set_nextsong_real (-2, 1);
return -1;
}
return 0;
}
pl_unlock ();
return -1;
}
static int
streamer_move_to_prevsong_real (int r) {
if (r) {
plug_get_output ()->stop ();
}
pl_lock ();
if (streamer_playlist) {
plt_unref (streamer_playlist);
}
streamer_playlist = plt_get_curr ();
// check if prev song is in this playlist
if (-1 == str_get_idx_of (playlist_track)) {
playlist_track = NULL;
}
playlist_t *plt = streamer_playlist;
pl_playqueue_clear ();
if (!plt->head[PL_MAIN]) {
pl_unlock ();
streamer_set_nextsong_real (-2, 1);
return 0;
}
int pl_order = conf_get_int ("playback.order", 0);
int pl_loop_mode = conf_get_int ("playback.loop", 0);
if (pl_order == PLAYBACK_ORDER_SHUFFLE_TRACKS || pl_order == PLAYBACK_ORDER_SHUFFLE_ALBUMS) { // shuffle
if (!playlist_track) {
pl_unlock ();
return streamer_move_to_nextsong_real (0);
}
else {
playlist_track->played = 0;
// find already played song with maximum shuffle rating below prev song
int rating = playlist_track->shufflerating;
playItem_t *pmax = NULL; // played maximum
playItem_t *amax = NULL; // absolute maximum
for (playItem_t *i = plt->head[PL_MAIN]; i; i = i->next[PL_MAIN]) {
if (i != playlist_track && i->played && (!amax || i->shufflerating > amax->shufflerating)) {
amax = i;
}
if (i == playlist_track || i->shufflerating > rating || !i->played) {
continue;
}
if (!pmax || i->shufflerating > pmax->shufflerating) {
pmax = i;
}
}
if (pmax && pl_order == PLAYBACK_ORDER_SHUFFLE_ALBUMS) {
while (pmax && pmax->next[PL_MAIN] && pmax->next[PL_MAIN]->played && pmax->shufflerating == pmax->next[PL_MAIN]->shufflerating) {
pmax = pmax->next[PL_MAIN];
}
}
playItem_t *it = pmax;
if (!it) {
// that means 1st in playlist, take amax
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) {
if (!amax) {
plt_reshuffle (streamer_playlist, NULL, &amax);
}
it = amax;
}
}
if (!it) {
pl_unlock ();
streamer_set_nextsong_real (-2, 1);
return -1;
}
int r = str_get_idx_of (it);
pl_unlock ();
streamer_set_nextsong_real (r, 1);
return 0;
}
}
else if (pl_order == PLAYBACK_ORDER_LINEAR) { // linear
playItem_t *it = NULL;
if (!playlist_track) {
int cur = plt_get_cursor (streamer_playlist, PL_MAIN);
if (cur != -1) {
playlist_track = plt_get_item_for_idx (streamer_playlist, cur, PL_MAIN);
pl_item_unref (playlist_track);
}
}
if (playlist_track) {
it = playlist_track->prev[PL_MAIN];
}
if (!it) {
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) {
it = plt->tail[PL_MAIN];
}
}
if (!it) {
pl_unlock ();
streamer_set_nextsong_real (-2, 1);
return -1;
}
int r = str_get_idx_of (it);
pl_unlock ();
streamer_set_nextsong_real (r, 1);
return 0;
}
else if (pl_order == PLAYBACK_ORDER_RANDOM) { // random
pl_unlock ();
int res = streamer_move_to_randomsong_real (0);
if (res == -1) {
streamer_set_nextsong_real (-2, 1);
trace ("streamer_move_to_randomsong error\n");
return -1;
}
return 0;
}
pl_unlock ();
return -1;
}
static int
streamer_move_to_randomsong_real (int reason) {
if (reason) {
plug_get_output ()->stop ();
}
if (!streamer_playlist) {
streamer_playlist = plt_get_curr ();
}
playlist_t *plt = streamer_playlist;
int cnt = plt->count[PL_MAIN];
if (!cnt) {
trace ("empty playlist\n");
return -1;
}
int curr = str_get_idx_of (playing_track);
int r = rand () / (float)RAND_MAX * cnt;
if (r == curr) {
r++;
if (r >= cnt) {
r = 0;
}
}
streamer_set_nextsong_real (r, 1);
return 0;
}
int
streamer_move_to_nextsong (int r) {
if (r) {
streamer_abort_files ();
}
handler_push (handler, STR_EV_NEXT, 0, r, 0);
}
int
streamer_move_to_prevsong (int r) {
if (r) {
streamer_abort_files ();
}
handler_push (handler, STR_EV_PREV, 0, r, 0);
}
int
streamer_move_to_randomsong (int r) {
if (r) {
streamer_abort_files ();
}
handler_push (handler, STR_EV_RAND, 0, r, 0);
}
// playlist must call that whenever item was removed
void
streamer_song_removed_notify (playItem_t *it) {
if (!mutex) {
return; // streamer is not running
}
if (it == playlist_track) {
playlist_track = playlist_track->prev[PL_MAIN];
}
}
#define CTMAP_MAX_PLUGINS 5
typedef struct ctmap_s {
char *ct;
char *plugins[CTMAP_MAX_PLUGINS];
struct ctmap_s *next;
} ctmap_t;
static ctmap_t *streamer_ctmap;
static char conf_network_ctmapping[2048];
static uintptr_t ctmap_mutex;
static void
ctmap_init_mutex (void) {
ctmap_mutex = mutex_create ();
}
static void
ctmap_free_mutex (void) {
if (ctmap_mutex) {
mutex_free (ctmap_mutex);
ctmap_mutex = 0;
}
}
static void
ctmap_lock (void) {
mutex_lock (ctmap_mutex);
}
static void
ctmap_unlock (void) {
mutex_unlock (ctmap_mutex);
}
static void
ctmap_free (void) {
while (streamer_ctmap) {
ctmap_t *ct = streamer_ctmap;
free (ct->ct);
for (int i = 0; ct->plugins[i]; i++) {
free (ct->plugins[i]);
}
streamer_ctmap = ct->next;
free (ct);
}
}
static void
ctmap_init (void) {
ctmap_free ();
char *mapstr = conf_network_ctmapping;
const char *p = mapstr;
char t[MAX_TOKEN];
char ct[MAX_TOKEN];
char plugins[MAX_TOKEN*5];
ctmap_t *tail = NULL;
for (;;) {
p = gettoken (p, t);
if (!p) {
break;
}
ctmap_t *ctmap = malloc (sizeof (ctmap_t));
memset (ctmap, 0, sizeof (ctmap_t));
ctmap->ct = strdup (t);
int n = 0;
p = gettoken (p, t);
if (!p || strcmp (t, "{")) {
free (ctmap->ct);
free (ctmap);
break;
}
plugins[0] = 0;
for (;;) {
p = gettoken (p, t);
if (!p || !strcmp (t, "}") || n >= CTMAP_MAX_PLUGINS-1) {
break;
}
ctmap->plugins[n++] = strdup (t);
}
ctmap->plugins[n] = NULL;
if (tail) {
tail->next = ctmap;
}
tail = ctmap;
if (!streamer_ctmap) {
streamer_ctmap = ctmap;
}
}
}
static int
is_remote_stream (playItem_t *it) {
int remote = 0;
pl_lock ();
const char *uri = pl_find_meta (it, ":URI");
if (uri && !plug_is_local_file (uri)) {
remote = 1;
}
pl_unlock ();
return remote;
}
static DB_fileinfo_t *dec_open (DB_decoder_t *dec, uint32_t hints, playItem_t *it) {
if (dec->plugin.api_vminor >= 7 && dec->open2) {
DB_fileinfo_t *fi = dec->open2 (hints, DB_PLAYITEM (it));
return fi;
}
return dec->open (hints);
}
// that must be called after last sample from str_playing_song was done reading
static int
streamer_set_current (playItem_t *it) {
trace ("streamer_set_current %s\n", playing_track ? pl_find_meta (playing_track, ":URI") : "null");
DB_output_t *output = plug_get_output ();
int err = 0;
int do_songstarted = 0;
playItem_t *from, *to;
// need to add refs here, because streamer_start_playback can destroy items
from = playing_track;
to = it;
if (from) {
pl_item_ref (from);
}
if (to) {
pl_item_ref (to);
}
trace ("\033[0;35mstreamer_set_current from %p to %p\033[37;0m\n", from, it);
trace ("\033[0;35moutput state: %d\033[37;0m\n", output->state ());
if (!playing_track || output->state () == OUTPUT_STATE_STOPPED) {
streamer_buffering = 1;
trace ("\033[0;35mstreamer_start_playback[1] from %p to %p\033[37;0m\n", from, it);
do_songstarted = 1;
streamer_start_playback (from, it);
bytes_until_next_song = -1;
}
trace ("streamer_set_current %p, buns=%d\n", it, bytes_until_next_song);
mutex_lock (decodemutex);
if (streaming_track) {
pl_item_unref (streaming_track);
streaming_track = NULL;
}
mutex_unlock (decodemutex);
int paused_stream = 0;
if (it && nextsong_pstate == 2) {
paused_stream = is_remote_stream (it);
}
if (!it || paused_stream) {