forked from mikebrady/shairport-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_wavstream.c
303 lines (261 loc) · 8.01 KB
/
audio_wavstream.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
/*
* WAVstream output driver. This file is part of Shairport.
* Copyright (c) James Laird 2013, PieVo 2018
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "audio.h"
#include "common.h"
#include <errno.h>
#include <fcntl.h>
#include <memory.h>
#include <stdio.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/socket.h>
#include <netinet/tcp.h>
#define AUDIO_HTTP_PORT 5668
static pthread_t tcp_thread;
static int client_socket = -1;
static int client_connected = 0;
static int server_socket = 0;
static int stop_accept_thread = 0;
int warned = 0;
int wavstream_port = AUDIO_HTTP_PORT;
int tcp_open(int port, int *sock)
{
int sockfd;
struct sockaddr_in serv_addr;
int flag = 1, ret = -1;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
debug(1, "error opening socket");
goto error;
}
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(port);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) {
debug(1, "error binding");
goto error;
}
ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(flag));
if (ret < 0) {
debug(1, "failed to set socket option TCP_NODELAY\n");
goto error;
}
listen(sockfd, 3);
ret = 0;
*sock = sockfd;
error:
return ret;
}
int tcp_write(int socket, const char *buf, unsigned int size)
{
int ret;
int count = size;
char *ptr = (char*)buf;
do {
ret = send(socket, buf, count, MSG_NOSIGNAL);
if (ret == -1)
if (errno != EINTR)
return ret;
ptr += ret;
count -= ret;
} while (count > 0);
return count;
}
int tcp_open_socket(void)
{
int ret, srv_socket;
// Open our server side socket, retry for ever
do {
ret = tcp_open(wavstream_port, &srv_socket);
if (ret < 0) {
debug(1, "failed to open socket, retrying in 5 seconds\n");
sleep(5);
}
} while (ret < 0);
return srv_socket;
}
static void start(__attribute__((unused)) int sample_rate,
__attribute__((unused)) int sample_format) {
debug(1, "wavstream start");
}
static void play(short buf[], int samples) {
// if the file is not open, try to open it.
char errorstring[1024];
if (!client_connected) {
if (!warned) {
debug(1, "no client connected, not playing");
warned = 1;
}
return;
}
// if it's got a reader, write to it.
int rc = non_blocking_write(client_socket, buf, samples * 4);
if ((rc < 0) && (warned == 0)) {
strerror_r(errno, (char *)errorstring, 1024);
warn("Error %d writing to the client socket: %s", errno, errorstring);
warned = 0;
client_connected = 0;
}
}
static void stop(void) {
debug(1, "wavstream stop");
}
int send_wav_header(void)
{
int ret;
// Send WAV header
typedef struct wave_header
{
unsigned char id[4]; // should always contain "RIFF"
unsigned int totallength; // total file length minus 8
unsigned char wavefmt[8]; // should be "WAVEfmt "
unsigned int format; // 16 for PCM format
unsigned short pcm; // 1 for PCM format
unsigned short channels; // channels
unsigned int frequency; // sampling frequency
unsigned int bytes_per_second;
unsigned short bytes_by_capture;
unsigned short bits_per_sample;
unsigned char data[4]; // should always contain "data"
unsigned int bytes_in_data;
}__attribute__((__packed__)) wavfile_t;
wavfile_t wavhdr = {
"RIFF",
~0,
"WAVEfmt ",
16,
1,
2,
44100,
176400,
4,
16,
"data",
~0,
};
ret = tcp_write(client_socket, (char*)&wavhdr, sizeof(wavhdr));
if (ret != 0) {
debug(1, "Failed to write WAVE header to socket");
}
return ret;
}
int send_http_ok(void) {
char http_msg[256];
int offs = 0;
// Send HTTP reply
offs = sprintf(http_msg, "HTTP/1.1 200 OK\r\n");
offs += sprintf(http_msg + offs, "Cache-Control: no-cache\r\n");
offs += sprintf(http_msg + offs, "Content-Type: audio/x-wav\r\n");
offs += sprintf(http_msg + offs, "Accept-Ranges: none\r\n");
offs += sprintf(http_msg + offs, "Content-length: 4294967296\r\n");
offs += sprintf(http_msg + offs, "\r\n");
return tcp_write(client_socket, http_msg, strlen(http_msg));
}
void *accept_thread(void *dummy)
{
struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
server_socket = tcp_open_socket();
debug(1, "http_audio thread started, listening on port %d", wavstream_port);
while(!stop_accept_thread) {
client_socket = accept(server_socket,(struct sockaddr *)&cli_addr, &clilen);
if (client_socket < 0) {
debug(1, "tcp accept returned %d", client_socket);
break;
}
debug(1, "new http audio client connected, client socket %d", client_socket);
// Send OK and WAV header
if (send_http_ok() == 0 && send_wav_header() == 0) {
// Toggle flag so "play function" can directly write to client_socket
client_connected = 1;
}
#if 0
// Test
while(!stop_accept_thread)
{
char buf[1764] = {0};
if (tcp_write(client_socket, (char*)&buf, sizeof(buf)) != 0)
{
debug(1, "write failed, closing socket");
client_connected = 0;
break;
}
usleep(10*1000);
}
#endif
};
debug(1, "tcp accept thread ended");
return NULL;
}
static int init(int argc, char **argv) {
debug(1, "wavstream init");
// set up default values first
config.audio_backend_buffer_desired_length = 1.0;
config.audio_backend_latency_offset = 0;
// do the "general" audio options. Note, these options are in the "general" stanza!
parse_general_audio_options();
if (config.cfg != NULL) {
int port;
if (config_lookup_int(config.cfg, "wavstream.port", &port)) {
wavstream_port = port;
debug(1, "http_audio port changed to %d", wavstream_port);
}
else {
debug(1, "no conf found..");
}
}
// Start accept thread
int ret = pthread_create(&tcp_thread, NULL, &accept_thread, NULL);
return ret;
}
static void deinit(void) {
void* ret = NULL;
debug(1, "wavstream deinit");
// Stop the accept thread
stop_accept_thread = 1;
shutdown(server_socket, SHUT_RDWR);
shutdown(client_socket, SHUT_RDWR);
pthread_join(tcp_thread, &ret);
}
static void help(void) { printf(" wavstream takes 1 argument: the port of the server to listen on.\n"); }
audio_output audio_wavstream = {.name = "wavstream",
.help = &help,
.init = &init,
.deinit = &deinit,
.start = &start,
.stop = &stop,
.flush = NULL,
.delay = NULL,
.play = &play,
.volume = NULL,
.parameters = NULL,
.mute = NULL};