forked from DeaDBeeF-Player/deadbeef
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer.c
2650 lines (2326 loc) · 72.5 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-2017 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 "fft.h"
#include "handler.h"
#include "plugins/libparser/parser.h"
#include "strdupa.h"
#include "playqueue.h"
#include "streamreader.h"
#include "dsp.h"
#ifdef trace
#undef trace
#define trace(...)
#endif
//#define WRITE_DUMP 1
//#define DETECT_PL_LOCK_RC 1
#if WRITE_DUMP
FILE *out;
#endif
#define MAX_PLAYLIST_DOWNLOAD_SIZE 25000
#define STREAMER_HINTS (DDB_DECODER_HINT_NEED_BITRATE|DDB_DECODER_HINT_CAN_LOOP)
static intptr_t streamer_tid;
static int autoconv_8_to_16 = 1;
static int autoconv_16_to_24 = 0;
static int conf_streamer_override_samplerate = 0;
static int conf_streamer_use_dependent_samplerate = 0;
static int conf_streamer_samplerate = 44100;
static int conf_streamer_samplerate_mult_48 = 48000;
static int conf_streamer_samplerate_mult_44 = 44100;
static int trace_bufferfill = 0;
static int stop_after_current = 0;
static int stop_after_album = 0;
static int streaming_terminate;
static uintptr_t mutex;
static uintptr_t wdl_mutex; // wavedata listener
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 streamer_is_buffering;
static playlist_t *streamer_playlist;
static playItem_t *playing_track;
static playItem_t *buffering_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 *last_played; // this is the last track that was played, should avoid setting this to NULL
static int _format_change_wait;
static ddb_waveformat_t prev_output_format; // last format that was sent to output via streamer_set_output_format
static ddb_waveformat_t last_block_fmt; // input file format corresponding to the current output
static DB_fileinfo_t *fileinfo;
static DB_FILE *fileinfo_file;
static DB_fileinfo_t *new_fileinfo;
static DB_FILE *new_fileinfo_file;
// This counter is incremented by one for each streamer_read call, which returns -1,
// which means audio should stop, but we need to wait a bit until buffered data has finished playing,
// so we wait AUDIO_STALL_WAIT periods
#define AUDIO_STALL_WAIT 20
static int _audio_stall_count;
// to allow interruption of stall file requests
static DB_FILE *streamer_file;
#if defined(HAVE_XGUI) || defined(ANDROID)
#include "equalizer.h"
#endif
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
play_index (int idx, int startpaused);
static void
play_current (void);
static void
play_next (int dir);
static void
streamer_set_current_playlist_real (int plt);
static int
stream_track (playItem_t *track, int startpaused);
static void
_handle_playback_stopped (void);
static void
_streamer_mark_album_played_up_to (playItem_t *item);
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
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) {
if (from == to) {
return;
}
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
set_last_played (playItem_t *track) {
if (last_played) {
pl_item_unref (last_played);
}
last_played = track;
if (last_played) {
pl_item_ref (last_played);
}
}
static void
streamer_start_playback (playItem_t *from, playItem_t *it) {
if (from) {
pl_item_ref (from);
}
if (it) {
pl_item_ref (it);
}
streamer_set_playing_track (it);
if (playing_track) {
playing_track->played = 1;
set_last_played (playing_track);
playItem_t *qnext = playqueue_getnext();
if (qnext == playing_track) {
playqueue_pop ();
}
if (qnext) {
pl_item_unref (qnext);
}
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 = (buffering_track && !playing_track) ? buffering_track : playing_track;
if (it) {
pl_item_ref (it);
}
return it;
}
playItem_t *
streamer_get_buffering_track (void) {
playItem_t *it = buffering_track;
if (it) {
pl_item_ref (it);
}
return it;
}
int
str_get_idx_of (playItem_t *it) {
pl_lock ();
if (!streamer_playlist) {
playlist_t *plt = plt_get_curr ();
streamer_set_streamer_playlist (plt);
plt_unref (plt);
}
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) {
playlist_t *plt = plt_get_curr ();
streamer_set_streamer_playlist (plt);
plt_unref (plt);
}
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 int
stop_after_album_check (playItem_t *cur, playItem_t *next) {
if (!stop_after_album) {
return 0;
}
if (!cur) {
return 0;
}
if (!next) {
stream_track (NULL, 0);
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_album = pl_find_meta_raw (cur, "album");
const char *next_album = pl_find_meta_raw (next, "album");
const char *keys[] = {
"band",
"album artist",
"albumartist",
"artist",
NULL
};
const char *cur_artist = NULL;
const char *next_artist = NULL;
for (int i = 0; keys[i]; i++) {
if (!cur_artist) {
cur_artist = pl_find_meta_raw (cur, keys[i]);
}
if (!next_artist) {
next_artist = pl_find_meta_raw (next, keys[i]);
}
if (cur_artist && next_artist) {
break;
}
}
if (cur_artist == next_artist && cur_album == next_album) {
return 0;
}
stream_track (NULL, 0);
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 playItem_t *
get_random_track (void) {
if (!streamer_playlist) {
playlist_t *plt = plt_get_curr ();
streamer_set_streamer_playlist (plt);
plt_unref (plt);
}
playlist_t *plt = streamer_playlist;
int cnt = plt->count[PL_MAIN];
if (!cnt) {
trace ("empty playlist\n");
return NULL;
}
int curr = str_get_idx_of (streaming_track);
int r = rand () / (float)RAND_MAX * cnt;
if (r == curr) {
r++;
if (r >= cnt) {
r = 0;
}
}
return plt_get_item_for_idx (plt, r, PL_MAIN);
}
static playItem_t *
get_next_track (playItem_t *curr) {
pl_lock ();
if (!streamer_playlist) {
playlist_t *plt = plt_get_curr ();
streamer_set_streamer_playlist (plt);
plt_unref (plt);
}
while (playqueue_getcount ()) {
trace ("playqueue_getnext\n");
playItem_t *it = playqueue_getnext ();
if (it) {
pl_unlock ();
return it; // from playqueue
}
}
playlist_t *plt = streamer_playlist;
if (!plt->head[PL_MAIN]) {
pl_unlock ();
return NULL; // empty playlist
}
int pl_order = pl_get_order ();
int pl_loop_mode = conf_get_int ("playback.loop", 0);
if (pl_loop_mode == PLAYBACK_MODE_LOOP_SINGLE) { // song finished, loop mode is "loop 1 track"
int r = str_get_idx_of (curr);
pl_unlock ();
if (r == -1) {
return NULL; // track is not in current playlist
}
else {
pl_item_ref (curr);
return curr;
}
return 0;
}
if (pl_order == PLAYBACK_ORDER_SHUFFLE_TRACKS || pl_order == PLAYBACK_ORDER_SHUFFLE_ALBUMS) { // shuffle
playItem_t *it = NULL;
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;
}
}
it = pmin;
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) { // nothing found after reshuffle
pl_unlock ();
return NULL;
}
}
else {
// 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;
}
}
it = pmin;
if (!it) {
// all songs played, reshuffle and try again
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) { // loop
trace ("all songs played! reshuffle\n");
plt_reshuffle (streamer_playlist, &it, NULL);
}
}
if (!it) {
playItem_t *temp;
plt_reshuffle (streamer_playlist, &temp, NULL);
pl_unlock ();
return NULL;
}
}
// prevent repeating the same track after reshuffle
if (it == curr) {
if (it->next[PL_MAIN]) {
it = it->next[PL_MAIN];
}
else if (plt->head[PL_MAIN] && plt->head[PL_MAIN] != it) {
it = plt->head[PL_MAIN];
}
}
// plt_reshuffle doesn't add ref
pl_item_ref (it);
pl_unlock ();
return it;
}
else if (pl_order == PLAYBACK_ORDER_LINEAR) { // linear
playItem_t *it = NULL;
if (curr) {
it = curr->next[PL_MAIN];
}
if (!it) {
trace ("streamer_move_nextsong: was last track\n");
if (pl_loop_mode == PLAYBACK_MODE_LOOP_ALL) {
it = plt->head[PL_MAIN];
}
else {
pl_unlock ();
return NULL;
}
}
if (!it) {
pl_unlock ();
return NULL;
}
pl_item_ref (it);
pl_unlock ();
return it;
}
else if (pl_order == PLAYBACK_ORDER_RANDOM) { // random
pl_unlock ();
return get_random_track ();
}
pl_unlock ();
return NULL;
}
static playItem_t *
get_prev_track (playItem_t *curr) {
pl_lock ();
// check if prev song is in this playlist
if (-1 == str_get_idx_of (curr)) {
curr = NULL;
}
if (!streamer_playlist) {
playlist_t *plt = plt_get_curr ();
streamer_set_streamer_playlist (plt);
plt_unref (plt);
}
playlist_t *plt = streamer_playlist;
if (!plt->head[PL_MAIN]) {
pl_unlock ();
return NULL;
}
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 (!curr) {
playItem_t *it = plt->head[PL_MAIN];
pl_item_ref(it);
pl_unlock ();
return it;
}
else {
curr->played = 0;
// find already played song with maximum shuffle rating below prev song
int rating = curr->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 != curr && i->played && (!amax || i->shufflerating > amax->shufflerating)) {
amax = i;
}
if (i == curr || 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 ();
return NULL;
}
pl_item_ref (it);
pl_unlock ();
return it;
}
}
else if (pl_order == PLAYBACK_ORDER_LINEAR) { // linear
playItem_t *it = NULL;
if (curr) {
it = curr->prev[PL_MAIN];
}
if (!it) {
pl_unlock ();
return NULL;
}
pl_item_ref(it);
pl_unlock ();
return it;
}
else if (pl_order == PLAYBACK_ORDER_RANDOM) { // random
pl_unlock();
return get_random_track();
}
pl_unlock ();
return NULL;
}
int
streamer_move_to_nextsong (int r) {
if (r) {
streamer_abort_files ();
}
handler_push (handler, STR_EV_NEXT, 0, r, 0);
return 0;
}
int
streamer_move_to_prevsong (int r) {
if (r) {
streamer_abort_files ();
}
handler_push (handler, STR_EV_PREV, 0, r, 0);
return 0;
}
int
streamer_move_to_randomsong (int r) {
if (r) {
streamer_abort_files ();
}
handler_push (handler, STR_EV_RAND, 0, r, 0);
return 0;
}
// playlist must call that whenever item was removed
void
streamer_song_removed_notify (playItem_t *it) {
if (!mutex) {
return; // streamer is not running
}
}
#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 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);
}
static playItem_t *first_failed_track;
// handle "first failed track" logic,
// and initialize dummy fileinfo for handling track switching for the tracks which can't be played
static void
streamer_play_failed (playItem_t *failed_track) {
streamer_lock();
if (!first_failed_track) {
first_failed_track = failed_track;
if (first_failed_track) {
pl_item_ref (first_failed_track);
}
}
else if (!failed_track) { // reset fail check
if (first_failed_track) {
pl_item_unref (first_failed_track);
first_failed_track = NULL;
}
}
if (failed_track) {
set_last_played (failed_track);
// the track has failed to be played,
// but we want to send it down to streamreader for proper track switching etc.
fileinfo = calloc(sizeof (DB_fileinfo_t), 1);
fileinfo_file = NULL;
new_fileinfo = NULL;
new_fileinfo_file = NULL;
if (streaming_track) {
pl_item_unref (streaming_track);
}
streaming_track = failed_track;
if (streaming_track) {
pl_item_ref (streaming_track);
}
}
streamer_unlock();
}
static void
fileinfo_free (DB_fileinfo_t *fileinfo) {
if (fileinfo->plugin) {
fileinfo->plugin->free (fileinfo);
}
else {
free (fileinfo);
}
}
static int
stream_track (playItem_t *it, int startpaused) {
if (fileinfo) {
fileinfo_free (fileinfo);
fileinfo = NULL;
fileinfo_file = NULL;
}
trace ("stream_track %s\n", playing_track ? pl_find_meta (playing_track, ":URI") : "null");
int err = 0;
playItem_t *from = NULL;
playItem_t *to = NULL;
if (first_failed_track && first_failed_track == it) {
streamer_play_failed (NULL); // looped to the first failed track
goto error;
}
// 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);
}
streamer_lock ();
if (streaming_track) {
pl_item_unref (streaming_track);
streaming_track = NULL;
}
streamer_unlock ();
int paused_stream = 0;
if (it && startpaused) {
paused_stream = is_remote_stream (it);
}
if (!it || paused_stream) {
goto success;
}
char decoder_id[100] = "";
char filetype[100] = "";
pl_lock ();
const char *dec = pl_find_meta (it, ":DECODER");
if (dec) {
strncpy (decoder_id, dec, sizeof (decoder_id));
}
if (!decoder_id[0]) {
// some decoders set filetype override,
// but the override is invalid when decoder is not set.
// reset to default here, so that tracks become playable after failures
pl_delete_meta(it, "!FILETYPE");
}
const char *ft = pl_find_meta (it, ":FILETYPE");
if (ft) {
strncpy (filetype, ft, sizeof (filetype));
}
pl_unlock ();
char *cct = "undefined";
char *plugs[CTMAP_MAX_PLUGINS] = {NULL};
if (!decoder_id[0] && (!strcmp (filetype, "content") || !filetype[0])) {
// try to get content-type
trace ("\033[0;34mopening file %s\033[37;0m\n", pl_find_meta (it, ":URI"));
pl_lock ();
char *uri = strdupa (pl_find_meta (it, ":URI"));
pl_unlock ();
DB_FILE *fp = streamer_file = vfs_fopen (uri);
trace ("\033[0;34mgetting content-type\033[37;0m\n");
if (!fp) {
err = -1;
goto error;
}
const char *ct = vfs_get_content_type (fp);
if (!ct) {
vfs_fclose (fp);
fp = NULL;
streamer_file = NULL;
if (!startpaused) {
streamer_play_failed (it);
}
else {
err = -1;
}
pl_lock ();
trace_err ("Failed to play track: %s\n", pl_find_meta(it, ":URI"));
pl_unlock ();
goto error;
}
trace ("got content-type: %s\n", ct);
cct = strdupa (ct);
char *sc = strchr (cct, ';');
if (sc) {
*sc = 0;
}
ctmap_lock ();
ctmap_t *ctmap = streamer_ctmap;
while (ctmap) {
if (!strcmp (cct, ctmap->ct)) {
break;
}
ctmap = ctmap->next;
}