-
Notifications
You must be signed in to change notification settings - Fork 4
/
streamer.c
2269 lines (2090 loc) · 71.9 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
/*
DeaDBeeF - ultimate music player for GNU/Linux systems with X11
Copyright (C) 2009-2011 Alexey Yakovenko <waker@users.sourceforge.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#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 "optmath.h"
#include "volume.h"
#include "vfs.h"
#include "premix.h"
#include "ringbuf.h"
#include "replaygain.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 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 int nextsong = -1;
static int nextsong_pstate = -1;
static int badsong = -1;
static float 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_fileinfo_t *new_fileinfo;
static int streamer_buffering;
// to allow interruption of stall file requests
static DB_FILE *streamer_file;
#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_abort_files (void) {
trace ("\033[0;33mstreamer_abort_files\033[37;0m\n");
if (fileinfo && fileinfo->file) {
deadbeef->fabort (fileinfo->file);
}
if (new_fileinfo && new_fileinfo->file) {
deadbeef->fabort (new_fileinfo->file);
}
if (streamer_file) {
deadbeef->fabort (streamer_file);
}
}
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);
}
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);
}
int
streamer_move_to_nextsong (int reason) {
trace ("streamer_move_to_nextsong (%d)\n", reason);
pl_lock ();
if (!streamer_playlist) {
streamer_playlist = plt_get_curr ();
}
while (pl_playqueue_getcount ()) {
trace ("pl_playqueue_getnext\n");
playItem_t *it = pl_playqueue_getnext ();
if (it) {
pl_playqueue_pop ();
int r = str_get_idx_of (it);
if (r >= 0) {
pl_item_unref (it);
pl_unlock ();
streamer_set_nextsong (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 (r, 3);
return 0;
}
}
trace ("%s not found in any playlists\n", pl_find_meta (it, ":URI"));
pl_item_unref (it);
}
}
}
playItem_t *curr = playlist_track;
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 (-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 (-2, 1);
}
else {
streamer_set_nextsong (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;
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 (-2, -2);
return -1;
}
int r = str_get_idx_of (it);
pl_unlock ();
streamer_set_nextsong (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 (!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 (-2, -2);
return -1;
}
int r = str_get_idx_of (it);
pl_unlock ();
streamer_set_nextsong (r, 1);
return 0;
}
}
else if (pl_order == PLAYBACK_ORDER_LINEAR) { // linear
playItem_t *it = NULL;
if (curr) {
it = curr->next[PL_MAIN];
}
else {
it = streamer_playlist->head[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 {
streamer_buffering = 0;
send_trackinfochanged (streaming_track);
badsong = -1;
pl_unlock ();
streamer_set_nextsong (-2, -2);
return 0;
}
}
if (!it) {
pl_unlock ();
return -1;
}
int r = str_get_idx_of (it);
pl_unlock ();
streamer_set_nextsong (r, 1);
return 0;
}
else if (pl_order == PLAYBACK_ORDER_RANDOM) { // random
pl_unlock ();
int res = streamer_move_to_randomsong ();
if (res == -1) {
trace ("streamer_move_to_randomsong error\n");
streamer_set_nextsong (-2, 1);
return -1;
}
return 0;
}
pl_unlock ();
return -1;
}
int
streamer_move_to_prevsong (void) {
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 (-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 (1);
}
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 (-2, 1);
return -1;
}
int r = str_get_idx_of (it);
pl_unlock ();
streamer_set_nextsong (r, 1);
return 0;
}
}
else if (pl_order == PLAYBACK_ORDER_LINEAR) { // linear
playItem_t *it = NULL;
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 (-2, 1);
return -1;
}
int r = str_get_idx_of (it);
pl_unlock ();
streamer_set_nextsong (r, 1);
return 0;
}
else if (pl_order == PLAYBACK_ORDER_RANDOM) { // random
pl_unlock ();
int res = streamer_move_to_randomsong ();
if (res == -1) {
streamer_set_nextsong (-2, 1);
trace ("streamer_move_to_randomsong error\n");
return -1;
}
return 0;
}
pl_unlock ();
return -1;
}
int
streamer_move_to_randomsong (void) {
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;
}
}
if (pl_get_order () == PLAYBACK_ORDER_SHUFFLE_ALBUMS) {
plt_init_shuffle_albums (plt, r);
}
streamer_set_nextsong (r, 1);
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
}
if (it == playlist_track) {
playlist_track = playlist_track->prev[PL_MAIN];
}
}
// 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);
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);
if (!it) {
goto success;
}
if (to) {
trace ("draw before init: %p->%p, playing_track=%p, playlist_track=%p\n", from, to, playing_track, playlist_track);
send_trackinfochanged (to);
}
if (from) {
send_trackinfochanged (from);
}
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));
}
const char *ft = pl_find_meta (it, ":FILETYPE");
if (ft) {
strncpy (filetype, ft, sizeof (filetype));
}
pl_unlock ();
if (!decoder_id[0] && (!strcmp (filetype, "content") || !filetype[0])) {
// try to get content-type
mutex_lock (decodemutex);
trace ("\033[0;34mopening file %s\033[37;0m\n", pl_find_meta (it, ":URI"));
DB_FILE *fp = streamer_file = vfs_fopen (pl_find_meta (it, ":URI"));
mutex_unlock (decodemutex);
const char *plug = NULL;
trace ("\033[0;34mgetting content-type\033[37;0m\n");
if (!fp) {
goto error;
}
const char *ct = vfs_get_content_type (fp);
if (!ct) {
vfs_fclose (fp);
goto error;
}
trace ("got content-type: %s\n", ct);
if (!strcmp (ct, "audio/mpeg")) {
plug = "stdmpg";
}
else if (!strcmp (ct, "application/ogg")) {
plug = "stdogg";
}
else if (!strcmp (ct, "audio/aacp")) {
plug = "aac";
}
else if (!strcmp (ct, "audio/aac")) {
plug = "aac";
}
else if (!strcmp (ct, "audio/wma")) {
plug = "ffmpeg";
}
else if (!strcmp (ct, "audio/x-mpegurl") || !strncmp (ct, "text/html", 9)) {
// download playlist into temp file
char *buf = NULL;
int fd = -1;
FILE *out = NULL;
int size = vfs_fgetlength (fp);
if (size <= 0) {
size = MAX_PLAYLIST_DOWNLOAD_SIZE;
}
buf = malloc (size);
if (!buf) {
trace ("failed to alloc %d bytes for playlist buffer\n");
goto m3u_error;
}
int rd = vfs_fread (buf, 1, size, fp);
if (rd != size) {
trace ("failed to download %d bytes (got %d bytes)\n", size, rd);
goto m3u_error;
}
char tempfile[1000];
const char *tmpdir = getenv ("TMPDIR");
if (!tmpdir) {
tmpdir = "/tmp";
}
snprintf (tempfile, sizeof (tempfile), "%s/ddbm3uXXXXXX", tmpdir);
fd = mkstemp (tempfile);
if (fd == -1) {
trace ("failed to open temp file %s\n", tempfile);
goto m3u_error;
}
out = fdopen (fd, "w+b");
if (!out) {
trace ("fdopen failed for %s\n", tempfile);
goto m3u_error;
}
int rw = fwrite (buf, 1, size, out);
if (rw != size) {
trace ("failed to write %d bytes into file %s\n", size, tempfile);
goto m3u_error;
}
fclose (out);
fd = -1;
out = NULL;
// load playlist
playlist_t *plt = plt_alloc ("temp");
DB_playlist_t **plug = plug_get_playlist_list ();
int p, e;
DB_playItem_t *m3u = NULL;
for (p = 0; plug[p]; p++) {
if (plug[p]->load) {
m3u = plug[p]->load ((ddb_playlist_t *)plt, NULL, tempfile, NULL, NULL, NULL);
if (m3u) {
break;
}
}
}
if (!m3u) {
trace ("failed to load playlist from %s using any of the installed playlist plugins\n", tempfile);
plt_free (plt);
goto m3u_error;
}
// for every playlist uri: override stream uri with the one from playlist, and try to play it
playItem_t *i = (playItem_t *)m3u;
pl_item_ref (i);
int res = -1;
while (i) {
pl_replace_meta (it, "!URI", pl_find_meta_raw (i, ":URI"));
res = streamer_set_current (it);
if (!res) {
pl_item_unref (i);
break;
}
playItem_t *next = pl_get_next (i, PL_MAIN);
pl_item_unref (i);
i = next;
}
pl_item_unref ((playItem_t*)m3u);
plt_free (plt);
if (res == 0) {
// succeeded -- playing now
if (from) {
pl_item_unref (from);
}
if (to) {
pl_item_unref (to);
}
return res;
}
unlink (tempfile);
m3u_error:
if (buf) {
free (buf);
}
if (out) {
fclose (out);
}
else if (fd != -1) {
close (fd);
}
goto error;
}
mutex_lock (decodemutex);
streamer_file = NULL;
vfs_fclose (fp);
mutex_unlock (decodemutex);
if (plug) {
DB_decoder_t **decoders = plug_get_decoder_list ();
// match by decoder
for (int i = 0; decoders[i]; i++) {
if (!strcmp (decoders[i]->plugin.id, plug)) {
pl_replace_meta (it, "!DECODER", decoders[i]->plugin.id);
strncpy (decoder_id, decoders[i]->plugin.id, sizeof (decoder_id));
trace ("\033[0;34mfound plugin %s\033[37;0m\n", plug);
break;
}
}
}
else {
trace ("\033[0;34mclosed file %s (bad or interrupted)\033[37;0m\n", pl_find_meta (it, ":URI"));
}
}
playlist_track = it;
if (decoder_id[0]) {
DB_decoder_t *dec = NULL;
dec = plug_get_decoder_for_id (decoder_id);
if (!dec) {
// find new decoder by file extension
const char *fname = pl_find_meta (it, ":URI");
const char *ext = strrchr (fname, '.');
if (ext) {
ext++;
DB_decoder_t **decs = plug_get_decoder_list ();
for (int i = 0; decs[i]; i++) {
const char **exts = decs[i]->exts;
if (exts) {
for (int j = 0; exts[j]; j++) {
if (!strcasecmp (exts[j], ext)) {
fprintf (stderr, "streamer: %s : changed decoder plugin to %s\n", fname, decs[i]->plugin.id);
pl_replace_meta (it, "!DECODER", decs[i]->plugin.id);
pl_replace_meta (it, "!FILETYPE", ext);
dec = decs[i];
break;
}
}
}
}
}
}
if (dec) {
trace ("\033[0;33minit decoder for %s (%s)\033[37;0m\n", pl_find_meta (it, ":URI"), decoder_id);
new_fileinfo = dec->open (0);
if (new_fileinfo && dec->init (new_fileinfo, DB_PLAYITEM (it)) != 0) {
trace ("\033[0;31mfailed to init decoder\033[37;0m\n")
dec->free (new_fileinfo);
new_fileinfo = NULL;
goto error;
}
}
if (!dec || !new_fileinfo) {
it->played = 1;
trace ("decoder->init returned %p\n", new_fileinfo);
streamer_buffering = 0;
if (playlist_track == it) {
trace ("redraw track %d; playing_track=%p; playlist_track=%p\n", to, playing_track, playlist_track);
send_trackinfochanged (to);
}
err = -1;
goto error;
}
else {
mutex_lock (decodemutex);
if (streaming_track) {
pl_item_unref (streaming_track);
}
streaming_track = it;
if (streaming_track) {
pl_item_ref (streaming_track);
streamer_set_replaygain (streaming_track);
}
mutex_unlock (decodemutex);
trace ("bps=%d, channels=%d, samplerate=%d\n", new_fileinfo->fmt.bps, new_fileinfo->fmt.channels, new_fileinfo->fmt.samplerate);
}
}
else {
trace ("no decoder in playitem!\n");
it->played = 1;
streamer_buffering = 0;
if (playlist_track == it) {
send_trackinfochanged (to);
}
if (from) {
pl_item_unref (from);
}
if (to) {
pl_item_unref (to);
}
return -1;
}
success:
mutex_lock (decodemutex);
if (new_fileinfo) {
if (fileinfo) {
fileinfo->plugin->free (fileinfo);
fileinfo = NULL;
}
fileinfo = new_fileinfo;
new_fileinfo = NULL;
}
mutex_unlock (decodemutex);
if (do_songstarted && playing_track) {
trace ("songstarted %s\n", playing_track ? pl_find_meta (playing_track, ":URI") : "null");
playtime = 0;
send_songstarted (playing_track);
}
send_trackinfochanged (to);
trace ("\033[0;32mstr: %p (%s), ply: %p (%s)\033[37;0m\n", streaming_track, streaming_track ? pl_find_meta (streaming_track, ":URI") : "null", playing_track, playing_track ? pl_find_meta (playing_track, ":URI") : "null");
error:
if (from) {
pl_item_unref (from);
}
if (to) {
pl_item_unref (to);
}
return err;
}
float
streamer_get_playpos (void) {
float seek = seekpos;
if (seek >= 0) {
return seek;
}
return playpos;
}
void
streamer_set_bitrate (int bitrate) {
if (bytes_until_next_song <= 0) { // prevent next track from resetting current playback bitrate
last_bitrate = bitrate;
}
}
int
streamer_get_apx_bitrate (void) {
return avg_bitrate;
}
void
streamer_set_nextsong (int song, int pstate) {
DB_output_t *output = plug_get_output ();
trace ("streamer_set_nextsong %d %d\n", song, pstate);
streamer_abort_files ();
streamer_lock ();
nextsong = song;