-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutput_sonos.c
201 lines (155 loc) · 5.2 KB
/
output_sonos.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
// SONOS::Squeezebox -- deploy Sonos in a Logitech Media Server (LMS) streaming environment
//
// Copyright (c) 2023 Martin van der Werff <github (at) newinnovations.nl>
//
// This file is part of SONOS::Squeezebox.
//
// SONOS::Squeezebox 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 3
// of the License, or (at your option) any later version.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "squeezelite.h"
#include "output_sonos.h"
#if BYTES_PER_FRAME != 8
#error BYTES_PER_FRAME not 8 bytes
#endif
#define FRAME_BLOCK MAX_SILENCE_FRAMES
static log_level loglevel;
static bool running = true;
extern struct outputstate output;
extern struct buffer* outputbuf;
#define LOCK mutex_lock(outputbuf->mutex)
#define UNLOCK mutex_unlock(outputbuf->mutex)
void encode_squeezebox_audio(const char* data, int len);
void close_squeezebox_audio();
static u8_t* buf;
static unsigned buffill;
static int bytes_per_frame;
static unsigned squeezebox_stream_id = 0;
static bool silent = true;
void new_squeezebox_stream_id(void)
{
++squeezebox_stream_id;
printf("Creating new stream (%u) for Sonos\n", squeezebox_stream_id);
}
unsigned get_squeezebox_stream_id(void)
{
return squeezebox_stream_id;
}
static int _sonos_write_frames(frames_t out_frames, bool silence, s32_t gainL, s32_t gainR, u8_t flags,
s32_t cross_gain_in, s32_t cross_gain_out, s32_t** cross_ptr)
{
u8_t* obuf;
if (!silence) {
if (silent) {
printf("From silent to non-silent\n");
new_squeezebox_stream_id();
silent = false;
}
if (output.fade == FADE_ACTIVE && output.fade_dir == FADE_CROSS && *cross_ptr) {
_apply_cross(outputbuf, out_frames, cross_gain_in, cross_gain_out, cross_ptr);
}
obuf = outputbuf->readp;
} else {
if (!silent) {
printf("From non-silent to silent\n");
close_squeezebox_audio();
silent = true;
}
return 0; // no silence output
}
// _scale_and_pack_frames(buf + buffill * bytes_per_frame, (s32_t *)(void *)obuf, out_frames, gainL, gainR, flags, output.format);
_scale_and_pack_frames(buf + buffill * bytes_per_frame, (s32_t*)(void*)obuf, out_frames, FIXED_ONE, FIXED_ONE, 0, output.format);
buffill += out_frames;
return (int)out_frames;
}
uint32_t get_sb_time_ms(void)
{
uint32_t res = gettime_ms();
if (res) {
return res;
}
return 0;
}
static void* output_thread()
{
while (running) {
LOCK;
output.device_frames = 0;
output.updated = gettime_ms();
output.frames_played_dmp = output.frames_played;
_output_frames(FRAME_BLOCK);
UNLOCK;
if (buffill) {
encode_squeezebox_audio((const char*)buf, buffill * bytes_per_frame);
buffill = 0;
}
usleep(10);
}
return 0;
}
static thread_type thread;
void output_init_sonos(log_level level, unsigned output_buf_size, char* params, unsigned rates[], unsigned rate_delay)
{
loglevel = level;
LOG_INFO("init output sonos");
buf = malloc(FRAME_BLOCK * BYTES_PER_FRAME);
if (!buf) {
LOG_ERROR("unable to malloc buf");
return;
}
buffill = 0;
memset(&output, 0, sizeof(output));
output.format = S16_LE;
output.start_frames = FRAME_BLOCK * 2;
output.write_cb = &_sonos_write_frames;
output.rate_delay = rate_delay;
switch (output.format) {
case S24_3LE:
bytes_per_frame = 3 * 2;
break;
case S16_LE:
bytes_per_frame = 2 * 2;
break;
case S32_LE:
default:
bytes_per_frame = 4 * 2;
break;
}
// ensure output rate is specified to avoid test open
if (!rates[0]) {
rates[0] = 44100;
}
output_init_common(level, "-", output_buf_size, rates, 0);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + OUTPUT_THREAD_STACK_SIZE);
pthread_create(&thread, &attr, output_thread, NULL);
pthread_attr_destroy(&attr);
}
void output_close_sonos(void)
{
LOG_INFO("close output");
LOCK;
running = false;
UNLOCK;
free(buf);
output_close_common();
}
bool test_open(const char* device, unsigned rates[], bool userdef_rates)
{
rates[0] = 44100;
return true;
}
void set_volume(unsigned left, unsigned right)
{
// printf("SONOS: set_volume(%d, %d)\n", left, right);
}