forked from owntone/owntone-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
outputs.c
1300 lines (1016 loc) · 32.3 KB
/
outputs.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
/*
* Copyright (C) 2016 Espen Jürgensen <espenjurgensen@gmail.com>
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <inttypes.h>
#include <event2/event.h>
#include "logger.h"
#include "misc.h"
#include "transcode.h"
#include "db.h"
#include "player.h" //TODO remove me when player_pmap is removed again
#include "worker.h"
#include "outputs.h"
extern struct output_definition output_raop;
extern struct output_definition output_airplay;
extern struct output_definition output_streaming;
extern struct output_definition output_dummy;
extern struct output_definition output_fifo;
#ifdef HAVE_ALSA
extern struct output_definition output_alsa;
#endif
#ifdef HAVE_LIBPULSE
extern struct output_definition output_pulse;
#endif
#ifdef CHROMECAST
extern struct output_definition output_cast;
#endif
/* From player.c */
extern struct event_base *evbase_player;
// Must be in sync with enum output_types
static struct output_definition *outputs[] = {
&output_raop,
&output_airplay,
&output_streaming,
&output_dummy,
&output_fifo,
#ifdef HAVE_ALSA
&output_alsa,
#endif
#ifdef HAVE_LIBPULSE
&output_pulse,
#endif
#ifdef CHROMECAST
&output_cast,
#endif
NULL
};
// Default volume (must be from 0 - 100)
#define OUTPUTS_DEFAULT_VOLUME 50
// When we stop, we keep the outputs open for a while, just in case we are
// actually just restarting. This timeout determines how long we wait before
// full stop.
// (value is in seconds)
#define OUTPUTS_STOP_TIMEOUT 10
#define OUTPUTS_MAX_CALLBACKS 64
struct outputs_callback_register
{
output_status_cb cb;
struct output_device *device;
// We have received the callback with the result from the backend
bool ready;
// We store a device_id to avoid the risk of dangling device pointer
uint64_t device_id;
enum output_device_state state;
};
struct output_quality_subscription
{
int count;
struct media_quality quality;
struct encode_ctx *encode_ctx;
};
// Buffer used to pass data to the backends
static struct output_buffer output_buffer;
static struct output_device *outputs_device_list;
static int outputs_master_volume;
static struct outputs_callback_register outputs_cb_register[OUTPUTS_MAX_CALLBACKS];
static struct event *outputs_deferredev;
static struct timeval outputs_stop_timeout = { OUTPUTS_STOP_TIMEOUT, 0 };
// Last element is a zero terminator
static struct output_quality_subscription output_quality_subscriptions[OUTPUTS_MAX_QUALITY_SUBSCRIPTIONS + 1];
static bool outputs_got_new_subscription;
/* ------------------------------- MISC HELPERS ----------------------------- */
static output_status_cb
callback_get(struct output_device *device)
{
int callback_id;
for (callback_id = 0; callback_id < ARRAY_SIZE(outputs_cb_register); callback_id++)
{
if (outputs_cb_register[callback_id].device == device)
return outputs_cb_register[callback_id].cb;
}
return NULL;
}
static void
callback_remove(struct output_device *device)
{
int callback_id;
if (!device)
return;
for (callback_id = 0; callback_id < ARRAY_SIZE(outputs_cb_register); callback_id++)
{
if (outputs_cb_register[callback_id].device == device)
{
DPRINTF(E_DBG, L_PLAYER, "Removing callback to %s, id %d\n", player_pmap(outputs_cb_register[callback_id].cb), callback_id);
memset(&outputs_cb_register[callback_id], 0, sizeof(struct outputs_callback_register));
}
}
}
static int
callback_add(struct output_device *device, output_status_cb cb)
{
int callback_id;
if (!cb)
return -1;
// We will replace any previously registered callbacks, since that's what the
// player expects
callback_remove(device);
// Find a free slot in the queue
for (callback_id = 0; callback_id < ARRAY_SIZE(outputs_cb_register); callback_id++)
{
if (outputs_cb_register[callback_id].cb == NULL)
break;
}
if (callback_id == ARRAY_SIZE(outputs_cb_register))
{
DPRINTF(E_LOG, L_PLAYER, "Output callback queue is full! (size is %d)\n", OUTPUTS_MAX_CALLBACKS);
return -1;
}
outputs_cb_register[callback_id].cb = cb;
outputs_cb_register[callback_id].device = device; // Don't dereference this later, it might become invalid!
DPRINTF(E_DBG, L_PLAYER, "Registered callback to %s with id %d (device %p, %s)\n", player_pmap(cb), callback_id, device, device->name);
int active = 0;
for (int i = 0; i < ARRAY_SIZE(outputs_cb_register); i++)
if (outputs_cb_register[i].cb)
active++;
DPRINTF(E_DBG, L_PLAYER, "Number of active callbacks: %d\n", active);
return callback_id;
};
static void
deferred_cb(int fd, short what, void *arg)
{
struct output_device *device;
output_status_cb cb;
enum output_device_state state;
int callback_id;
for (callback_id = 0; callback_id < ARRAY_SIZE(outputs_cb_register); callback_id++)
{
if (outputs_cb_register[callback_id].ready)
{
// Must copy before making callback, since you never know what the
// callback might result in (could call back in)
cb = outputs_cb_register[callback_id].cb;
state = outputs_cb_register[callback_id].state;
// Will be NULL if the device has disappeared
device = outputs_device_get(outputs_cb_register[callback_id].device_id);
memset(&outputs_cb_register[callback_id], 0, sizeof(struct outputs_callback_register));
// The device has left the building (stopped/failed), and the backend
// is not using it any more
if (device && !device->advertised && !device->session)
{
outputs_device_remove(device);
device = NULL;
}
else if (device)
device->state = state;
DPRINTF(E_DBG, L_PLAYER, "Making deferred callback to %s, id was %d\n", player_pmap(cb), callback_id);
cb(device, state);
}
}
for (int i = 0; i < ARRAY_SIZE(outputs_cb_register); i++)
{
if (outputs_cb_register[i].cb)
DPRINTF(E_DBG, L_PLAYER, "%d. Active callback: %s\n", i, player_pmap(outputs_cb_register[i].cb));
}
}
static void
stop_timer_cb(int fd, short what, void *arg)
{
struct output_device *device = arg;
output_status_cb cb = callback_get(device);
outputs_device_stop(device, cb);
}
static void
device_stop_cb(struct output_device *device, enum output_device_state state)
{
if (device)
device->state = state;
if (state == OUTPUT_STATE_FAILED)
DPRINTF(E_WARN, L_PLAYER, "Failed to stop device\n");
else
DPRINTF(E_INFO, L_PLAYER, "Device stopped properly\n");
}
static enum transcode_profile
quality_to_xcode(struct media_quality *quality)
{
if (quality->bits_per_sample == 16)
return XCODE_PCM16;
if (quality->bits_per_sample == 24)
return XCODE_PCM24;
if (quality->bits_per_sample == 32)
return XCODE_PCM32;
return XCODE_UNKNOWN;
}
static int
encoding_reset(struct media_quality *quality)
{
struct output_quality_subscription *subscription;
struct decode_ctx *decode_ctx;
enum transcode_profile profile;
int i;
profile = quality_to_xcode(quality);
if (profile == XCODE_UNKNOWN)
{
DPRINTF(E_LOG, L_PLAYER, "Could not create subscription decoding context, invalid quality (%d/%d/%d)\n",
quality->sample_rate, quality->bits_per_sample, quality->channels);
return -1;
}
decode_ctx = transcode_decode_setup_raw(profile, quality);
if (!decode_ctx)
{
DPRINTF(E_LOG, L_PLAYER, "Could not create subscription decoding context (profile %d)\n", profile);
return -1;
}
for (i = 0; output_quality_subscriptions[i].count > 0; i++)
{
subscription = &output_quality_subscriptions[i]; // Just for short-hand
transcode_encode_cleanup(&subscription->encode_ctx); // Will also point the ctx to NULL
if (quality_is_equal(quality, &subscription->quality))
continue; // No resampling required
profile = quality_to_xcode(&subscription->quality);
if (profile != XCODE_UNKNOWN)
subscription->encode_ctx = transcode_encode_setup(profile, &subscription->quality, decode_ctx, NULL, 0, 0);
else
DPRINTF(E_LOG, L_PLAYER, "Could not setup resampling to %d/%d/%d for output\n",
subscription->quality.sample_rate, subscription->quality.bits_per_sample, subscription->quality.channels);
}
transcode_decode_cleanup(&decode_ctx);
return 0;
}
static void
buffer_fill(struct output_buffer *obuf, void *buf, size_t bufsize, struct media_quality *quality, int nsamples, struct timespec *pts)
{
transcode_frame *frame;
int ret;
int i;
int n;
obuf->pts = *pts;
// The resampling/encoding (transcode) contexts work for a given input quality,
// so if the quality changes we need to reset the contexts. We also do that if
// we have received a subscription for a new quality.
if (!quality_is_equal(quality, &obuf->data[0].quality) || outputs_got_new_subscription)
{
encoding_reset(quality);
outputs_got_new_subscription = false;
}
// The first element of the output_buffer is always just the raw input data
// TODO can we avoid the copy below? we can't use evbuffer_add_buffer_reference,
// because then the outputs can't use it and we would need to copy there instead
evbuffer_add(obuf->data[0].evbuf, buf, bufsize);
obuf->data[0].buffer = buf;
obuf->data[0].bufsize = bufsize;
obuf->data[0].quality = *quality;
obuf->data[0].samples = nsamples;
for (i = 0, n = 1; output_quality_subscriptions[i].count > 0; i++)
{
if (quality_is_equal(&output_quality_subscriptions[i].quality, quality))
continue; // Skip, no resampling required and we have the data in element 0
if (!output_quality_subscriptions[i].encode_ctx)
continue;
frame = transcode_frame_new(buf, bufsize, nsamples, quality);
if (!frame)
continue;
ret = transcode_encode(obuf->data[n].evbuf, output_quality_subscriptions[i].encode_ctx, frame, 0);
transcode_frame_free(frame);
if (ret < 0)
continue;
obuf->data[n].buffer = evbuffer_pullup(obuf->data[n].evbuf, -1);
obuf->data[n].bufsize = evbuffer_get_length(obuf->data[n].evbuf);
obuf->data[n].quality = output_quality_subscriptions[i].quality;
obuf->data[n].samples = BTOS(obuf->data[n].bufsize, obuf->data[n].quality.bits_per_sample, obuf->data[n].quality.channels);
n++;
}
}
static void
buffer_drain(struct output_buffer *obuf)
{
int i;
for (i = 0; obuf->data[i].buffer; i++)
{
evbuffer_drain(obuf->data[i].evbuf, obuf->data[i].bufsize);
obuf->data[i].buffer = NULL;
obuf->data[i].bufsize = 0;
// We don't reset quality and samples, would be a waste of time
}
}
static void
device_list_sort(void)
{
struct output_device *device;
struct output_device *next;
struct output_device *prev;
int swaps;
// Swap sorting since even the most inefficient sorting should do fine here
do
{
swaps = 0;
prev = NULL;
for (device = outputs_device_list; device && device->next; device = device->next)
{
next = device->next;
if ( (outputs_priority(device) > outputs_priority(next)) ||
(outputs_priority(device) == outputs_priority(next) && strcasecmp(device->name, next->name) > 0) )
{
if (device == outputs_device_list)
outputs_device_list = next;
if (prev)
prev->next = next;
device->next = next->next;
next->next = device;
swaps++;
}
prev = device;
}
}
while (swaps > 0);
}
// Convenience function
static inline int
device_state_update(struct output_device *device, int ret)
{
if (ret < 0)
device->state = OUTPUT_STATE_FAILED;
return ret;
}
static void
metadata_cb_send(int fd, short what, void *arg)
{
struct output_metadata *metadata = arg;
int ret;
event_free(metadata->ev);
metadata->ev = NULL;
ret = metadata->finalize_cb(metadata);
if (ret < 0)
return;
outputs[metadata->type]->metadata_send(metadata);
}
// *** Worker thread ***
static void
metadata_cb_prepare(void *arg)
{
struct output_metadata *metadata = *((struct output_metadata **)arg);
metadata->priv = outputs[metadata->type]->metadata_prepare(metadata);
if (!metadata->priv)
{
event_free(metadata->ev);
free(metadata);
return;
}
// Metadata is prepared, let the player thread do the actual sending
event_active(metadata->ev, 0, 0);
}
static void
metadata_send(enum output_types type, uint32_t item_id, bool startup, output_metadata_finalize_cb cb)
{
struct output_metadata *metadata;
CHECK_NULL(L_PLAYER, metadata = calloc(1, sizeof(struct output_metadata)));
metadata->type = type;
metadata->item_id = item_id;
metadata->startup = startup;
metadata->finalize_cb = cb;
metadata->ev = event_new(evbase_player, -1, 0, metadata_cb_send, metadata);
if (outputs[type]->metadata_prepare)
worker_execute(metadata_cb_prepare, &metadata, sizeof(struct output_metadata *), 0);
else
outputs[type]->metadata_send(metadata);
}
/* ----------------------------- Volume helpers ----------------------------- */
static int
rel_to_vol(int relvol, int master_volume)
{
float vol;
if (relvol == 100)
return master_volume;
vol = ((float)relvol * (float)master_volume) / 100.0;
return (int)vol;
}
static int
vol_to_rel(int volume, int master_volume)
{
float rel;
if (volume == master_volume)
return 100;
rel = ((float)volume / (float)master_volume) * 100.0;
return (int)rel;
}
static void
vol_adjust(void)
{
struct output_device *device;
int selected_highest = -1;
int all_highest = -1;
for (device = outputs_device_list; device; device = device->next)
{
if (OUTPUTS_DEVICE_DISPLAY_SELECTED(device) && (device->volume > selected_highest))
selected_highest = device->volume;
if (device->volume > all_highest)
all_highest = device->volume;
}
outputs_master_volume = (selected_highest >= 0) ? selected_highest : all_highest;
for (device = outputs_device_list; device; device = device->next)
{
device->relvol = vol_to_rel(device->volume, outputs_master_volume);
}
#ifdef DEBUG_VOLUME
DPRINTF(E_DBG, L_PLAYER, "*** Master: %d\n", outputs_master_volume);
for (device = outputs_device_list; device; device = device->next)
{
DPRINTF(E_DBG, L_PLAYER, "*** %s: abs %d rel %d selected %d\n", device->name, device->volume, device->relvol, OUTPUTS_DEVICE_DISPLAY_SELECTED(device));
}
#endif
}
/* ----------------------------------- API ---------------------------------- */
struct output_device *
outputs_device_get(uint64_t device_id)
{
struct output_device *device;
for (device = outputs_device_list; device; device = device->next)
{
if (device_id == device->id)
return device;
}
DPRINTF(E_WARN, L_PLAYER, "Output device with id %" PRIu64 " has disappeared from our list\n", device_id);
return NULL;
}
/* ----------------------- Called by backend modules ------------------------ */
// Sessions free their sessions themselves, but should not touch the device,
// since they can't know for sure that it is still valid in memory
int
outputs_device_session_add(uint64_t device_id, void *session)
{
struct output_device *device;
device = outputs_device_get(device_id);
if (!device)
return -1;
device->session = session;
return 0;
}
void
outputs_device_session_remove(uint64_t device_id)
{
struct output_device *device;
device = outputs_device_get(device_id);
if (device)
device->session = NULL;
return;
}
int
outputs_quality_subscribe(struct media_quality *quality)
{
int i;
// If someone else is already subscribing to this quality we just increase the
// reference count.
for (i = 0; output_quality_subscriptions[i].count > 0; i++)
{
if (!quality_is_equal(quality, &output_quality_subscriptions[i].quality))
continue;
output_quality_subscriptions[i].count++;
DPRINTF(E_DBG, L_PLAYER, "Subscription request for quality %d/%d/%d (now %d subscribers)\n",
quality->sample_rate, quality->bits_per_sample, quality->channels, output_quality_subscriptions[i].count);
return 0;
}
if (i >= (ARRAY_SIZE(output_quality_subscriptions) - 1))
{
DPRINTF(E_LOG, L_PLAYER, "Bug! The number of different quality levels requested by outputs is too high\n");
return -1;
}
output_quality_subscriptions[i].quality = *quality;
output_quality_subscriptions[i].count++;
DPRINTF(E_DBG, L_PLAYER, "Subscription request for quality %d/%d/%d (now %d subscribers)\n",
quality->sample_rate, quality->bits_per_sample, quality->channels, output_quality_subscriptions[i].count);
// Better way of signaling this?
outputs_got_new_subscription = true;
return 0;
}
void
outputs_quality_unsubscribe(struct media_quality *quality)
{
int i;
// Find subscription
for (i = 0; output_quality_subscriptions[i].count > 0; i++)
{
if (quality_is_equal(quality, &output_quality_subscriptions[i].quality))
break;
}
if (output_quality_subscriptions[i].count == 0)
{
DPRINTF(E_LOG, L_PLAYER, "Bug! Unsubscription request for a quality level that there is no subscription for\n");
return;
}
output_quality_subscriptions[i].count--;
DPRINTF(E_DBG, L_PLAYER, "Unsubscription request for quality %d/%d/%d (now %d subscribers)\n",
quality->sample_rate, quality->bits_per_sample, quality->channels, output_quality_subscriptions[i].count);
if (output_quality_subscriptions[i].count > 0)
return;
transcode_encode_cleanup(&output_quality_subscriptions[i].encode_ctx);
// Shift elements
for (; i < ARRAY_SIZE(output_quality_subscriptions) - 1; i++)
output_quality_subscriptions[i] = output_quality_subscriptions[i + 1];
}
// Output backends call back through the below wrapper to make sure that:
// 1. Callbacks are always deferred
// 2. The callback never has a dangling pointer to a device (a device that has been removed from our list)
void
outputs_cb(int callback_id, uint64_t device_id, enum output_device_state state)
{
if (callback_id < 0)
return;
if (!(callback_id < ARRAY_SIZE(outputs_cb_register)) || !outputs_cb_register[callback_id].cb)
{
DPRINTF(E_LOG, L_PLAYER, "Bug! Output backend called us with an illegal callback id (%d)\n", callback_id);
return;
}
DPRINTF(E_DBG, L_PLAYER, "Callback request received, id is %i\n", callback_id);
outputs_cb_register[callback_id].ready = true;
outputs_cb_register[callback_id].device_id = device_id;
outputs_cb_register[callback_id].state = state;
event_active(outputs_deferredev, 0, 0);
}
/* ---------------------------- Called by player ---------------------------- */
struct output_device *
outputs_device_add(struct output_device *add, bool new_deselect)
{
struct output_device *device;
char *keep_name;
int ret;
for (device = outputs_device_list; device; device = device->next)
{
if (device->id == add->id)
break;
}
// This is relevant for Airplay 1 and 2 where the same device can support both
if (device && device->type != add->type)
{
if (outputs_priority(device) < outputs_priority(add))
{
DPRINTF(E_DBG, L_PLAYER, "Ignoring type %s for device '%s', will use type %s\n", add->type_name, add->name, device->type_name);
outputs_device_free(add);
return NULL;
}
// Remove existing device, higher priority device will be added below
outputs_device_remove(device);
device = NULL;
}
// New device
if (!device)
{
device = add;
device->stop_timer = evtimer_new(evbase_player, stop_timer_cb, device);
keep_name = strdup(device->name);
ret = db_speaker_get(device, device->id);
if (ret < 0)
{
device->selected = 0;
device->volume = (outputs_master_volume >= 0) ? outputs_master_volume : OUTPUTS_DEFAULT_VOLUME;;
}
free(device->name);
device->name = keep_name;
if (new_deselect)
device->selected = 0;
device->next = outputs_device_list;
outputs_device_list = device;
}
// Update to a device already in the list
else
{
if (add->v4_address)
{
free(device->v4_address);
device->v4_address = add->v4_address;
device->v4_port = add->v4_port;
// Address is ours now
add->v4_address = NULL;
}
if (add->v6_address)
{
free(device->v6_address);
device->v6_address = add->v6_address;
device->v6_port = add->v6_port;
// Address is ours now
add->v6_address = NULL;
}
free(device->name);
device->name = add->name;
add->name = NULL;
device->has_password = add->has_password;
device->password = add->password;
outputs_device_free(add);
}
device_list_sort();
vol_adjust();
device->advertised = 1;
return device;
}
void
outputs_device_remove(struct output_device *remove)
{
struct output_device *device;
struct output_device *prev;
int ret;
// Device stop should be able to handle that we invalidate the device, even
// if it is an async stop. It might call outputs_device_session_remove(), but
// that just won't do anything since the id will be unknown.
if (remove->session)
outputs_device_stop(remove, device_stop_cb);
prev = NULL;
for (device = outputs_device_list; device; device = device->next)
{
if (device == remove)
break;
prev = device;
}
if (!device)
return;
// Save device volume
ret = db_speaker_save(remove);
if (ret < 0)
DPRINTF(E_LOG, L_PLAYER, "Could not save state for %s device '%s'\n", remove->type_name, remove->name);
DPRINTF(E_INFO, L_PLAYER, "Removing %s device '%s'\n", remove->type_name, remove->name);
if (!prev)
outputs_device_list = remove->next;
else
prev->next = remove->next;
outputs_device_free(remove);
vol_adjust();
}
void
outputs_device_select(struct output_device *device, int max_volume)
{
device->selected = 1;
device->prevent_playback = 0;
device->busy = 0;
// The purpose of this is to cap the volume for a newly selected device. It is
// used by the player to avoid this scenario:
// 1 Play on two speakers, say Kitchen (100) and Office (75), master is 100
// 2 Disable Office, reduce master to 25, Kitchen is now 25, Office is still 75
// 3 Turn on Office, it now blasts at 75
// We could avoid this by reducing the unselected Office in step 2, but that
// leads to issue #1077, where volumes of unselected devices go to 0 (e.g. by
// reducing master to 0 and then increasing again -> unselected stays at 0).
if (max_volume >= 0 && device->volume > max_volume)
device->volume = max_volume;
vol_adjust();
}
void
outputs_device_deselect(struct output_device *device)
{
device->selected = 0;
vol_adjust();
}
int
outputs_device_start(struct output_device *device, output_status_cb cb, bool only_probe)
{
int ret;
if (outputs[device->type]->disabled || !outputs[device->type]->device_start || !outputs[device->type]->device_probe)
return -1;
if (device->session)
return 0; // Device is already running, nothing to do
if (only_probe)
ret = outputs[device->type]->device_probe(device, callback_add(device, cb));
else
ret = outputs[device->type]->device_start(device, callback_add(device, cb));
return device_state_update(device, ret);;
}
int
outputs_device_stop(struct output_device *device, output_status_cb cb)
{
int ret;
if (outputs[device->type]->disabled || !outputs[device->type]->device_stop)
return -1;
if (!device->session)
return 0; // Device is already stopped, nothing to do
ret = outputs[device->type]->device_stop(device, callback_add(device, cb));
return device_state_update(device, ret);
}
int
outputs_device_stop_delayed(struct output_device *device, output_status_cb cb)
{
if (outputs[device->type]->disabled || !outputs[device->type]->device_stop)
return -1;
if (!device->session)
return 0; // Device is already stopped, nothing to do
outputs[device->type]->device_cb_set(device, callback_add(device, cb));
event_add(device->stop_timer, &outputs_stop_timeout);
return 1;
}
int
outputs_device_flush(struct output_device *device, output_status_cb cb)
{
int ret;
if (outputs[device->type]->disabled || !outputs[device->type]->device_flush)
return -1;
if (!device->session)
return 0; // Nothing to flush
ret = outputs[device->type]->device_flush(device, callback_add(device, cb));
return ret; // We don't change device state just because of a failed flush
}
void
outputs_device_volume_register(struct output_device *device, int absvol, int relvol)
{
if (absvol > -1)
device->volume = absvol;
else if (relvol > -1)
device->volume = rel_to_vol(relvol, outputs_master_volume);
vol_adjust();
}
int
outputs_device_volume_set(struct output_device *device, output_status_cb cb)
{
int ret;
if (outputs[device->type]->disabled || !outputs[device->type]->device_volume_set)
return -1;
if (!device->session)
return 0; // Device isn't active
ret = outputs[device->type]->device_volume_set(device, callback_add(device, cb));
return ret; // We don't change device state just because of a failed volume change
}
int
outputs_device_volume_to_pct(struct output_device *device, const char *volume)
{
if (outputs[device->type]->disabled || !outputs[device->type]->device_volume_to_pct)
return -1;
return outputs[device->type]->device_volume_to_pct(device, volume);
}
int
outputs_device_quality_set(struct output_device *device, struct media_quality *quality, output_status_cb cb)
{
int ret;
if (outputs[device->type]->disabled || !outputs[device->type]->device_quality_set)
return -1;
ret = outputs[device->type]->device_quality_set(device, quality, callback_add(device, cb));
return device_state_update(device, ret);
}
int
outputs_device_authorize(struct output_device *device, const char *pin, output_status_cb cb)
{
int ret;
if (outputs[device->type]->disabled || !outputs[device->type]->device_authorize)
return -1;
if (device->session)
return 0; // We are already connected to the device - no auth required
ret = outputs[device->type]->device_authorize(device, pin, callback_add(device, cb));
return device_state_update(device, ret); // If ret < 0 then we couldn't reach the speaker
}
void
outputs_device_cb_set(struct output_device *device, output_status_cb cb)
{
if (outputs[device->type]->disabled || !outputs[device->type]->device_cb_set)
return;
if (!device->session)
return;