-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsourcethread.cpp
More file actions
422 lines (360 loc) · 10 KB
/
Copy pathsourcethread.cpp
File metadata and controls
422 lines (360 loc) · 10 KB
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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#ifndef WIN32
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
#else
#include <winsock.h>
#include <io.h>
#endif
#ifdef WIN32
#define close _close
#endif
#include "cbuffer.h"
#include "socket_client.h"
#include "liboddcast/liboddcast.h"
int sourceMainLoop = 1;
int sourceInnerLoop = 1;
int sourceThreadRunning = 0;
CBUFFER sourceCBuffer;
CBUFFER metadataCBuffer;
extern void outputStatusCallback(void *gbl, void *pValue);
extern void inputMetadataCallback(void *gbl, void *pValue);
extern oddcastGlobals gMain;
extern oddcastGlobals *g[];
#define MP3_FORMAT 1
#define OGGVORBIS_FORMAT 2
int sourceMedia = MP3_FORMAT;
#define SERVER_FORMAT_ICECAST 1
#define SERVER_FORMAT_SHOUTCAST 2
int decode_pipe[2];
static int oggvHeaderRead = 0;
static int header_read = 0;
static int partial_header = 0;
static int interleavedmetaDataFlag = 0;
static int serverFormatFlag = SERVER_FORMAT_SHOUTCAST;
static int icyMetaInt = 0;
pthread_mutex_t encoders_mutex;
int client_socket = 0;
int processInterleavedMetadata(char *buf, int len, char **outbuf, int *outlen) {
static char metadataBucket[2046];
char title[1024] = "";
static char currentSong[2046] = "";
if (!buf) {
return 0;
}
LogMessage(&gMain, LOG_DEBUG, "Metadata CBuffer: %d", cbuffer_get_size(&metadataCBuffer));
if (cbuffer_insert(&metadataCBuffer, buf, len) == BUFFER_FULL) {
return 0;
}
LogMessage(&gMain, LOG_DEBUG, "Processing Interleaved Metadata, MetaInt = %d", icyMetaInt);
if (cbuffer_get_used(&metadataCBuffer) >= (unsigned long)(icyMetaInt*2)) {
char *pStreamData = (char*)malloc(icyMetaInt);
cbuffer_extract(&metadataCBuffer, pStreamData, icyMetaInt);
char c;
cbuffer_extract(&metadataCBuffer, &c, 1);
if (c > 0) {
// We have metadata, lets read it!
char *pMetaData = (char*)malloc(c*16);
memset(pMetaData, '\000', sizeof(c*16));
cbuffer_extract(&metadataCBuffer, pMetaData, c*16);
LogMessage(&gMain, LOG_DEBUG, "Metadata Triggered (%s)", pMetaData);
char *pStreamTitle = strchr(pMetaData, '\'');
if (pStreamTitle) {
char *pEndStreamTitle = strstr(pStreamTitle+1, "';");
if (pEndStreamTitle) {
memset(title, '\000', sizeof(title));
pStreamTitle++;
strncpy(title, pStreamTitle, pEndStreamTitle-pStreamTitle);
if (!strcmp(currentSong, title)) {
;
}
else {
strcpy(currentSong, title);
inputMetadataCallback(&gMain, title);
pthread_mutex_lock(&encoders_mutex);
for(int i = 0; i < gMain.gNumEncoders; i++) {
setCurrentSongTitle(g[i], title);
}
pthread_mutex_unlock(&encoders_mutex);
}
}
}
if (pMetaData) {
free(pMetaData);
}
}
if (c == 0) {
;
}
*outbuf = pStreamData;
*outlen = icyMetaInt;
return 1;
}
return 0;
}
void parseIcecastHeader(char *http_header) {
char *p2 = 0;
char *p3 = 0;
char tmpBuf[1024] = "";
char icyName[1024] = "";
char icyMetaIntstr[1024] = "";
serverFormatFlag = SERVER_FORMAT_ICECAST;
p2 = strstr(http_header, "icy-name:");
if (p2) {
p2 = p2 + strlen("icy-name:");
p3 = strstr(p2, "\r\n");
if (p3) {
memset(icyName, '\000', sizeof(icyName));
strncpy(icyName, p2, p3-p2);
}
outputStatusCallback(&gMain, (void *)icyName);
}
p2 = strstr(http_header, "icy-metaint:");
if (p2) {
p2 = p2 + strlen("icy-metaint:");
p3 = strstr(p2, "\r\n");
if (p3) {
icyMetaInt = 0;
memset(icyMetaIntstr, '\000', sizeof(icyMetaIntstr));
strncpy(icyMetaIntstr, p2, p3-p2);
icyMetaInt = atoi(icyMetaIntstr);
interleavedmetaDataFlag = 1;
}
}
outputStatusCallback(&gMain, (void *)"Icecast");
p2 = strstr(http_header, "Content-Type: ");
if (p2) {
p2 = p2 + strlen("Content-Type: ");
p3 = strstr(p2, "\r\n");
if (p3) {
memset(tmpBuf, '\000', sizeof(tmpBuf));
strncpy(tmpBuf, p2, p3-p2);
if (!strcmp(tmpBuf, "application/ogg")) {
sourceMedia = OGGVORBIS_FORMAT;
outputStatusCallback(&gMain, (void *)"Vorbis");
}
else {
sourceMedia = MP3_FORMAT;
outputStatusCallback(&gMain, (void *)"MP3");
}
}
}
}
void parseShoutcastHeader(char *http_header) {
char *p2 = 0;
char *p3 = 0;
char icyName[1024] = "";
char icyMetaIntstr[1024] = "";
interleavedmetaDataFlag = 0;
p2 = strstr(http_header, "icy-name:");
if (p2) {
p2 = p2 + strlen("icy-name:");
p3 = strstr(p2, "\r\n");
if (p3) {
memset(icyName, '\000', sizeof(icyName));
strncpy(icyName, p2, p3-p2);
}
}
p2 = strstr(http_header, "icy-metaint:");
if (p2) {
p2 = p2 + strlen("icy-metaint:");
p3 = strstr(p2, "\r\n");
if (p3) {
icyMetaInt = 0;
memset(icyMetaIntstr, '\000', sizeof(icyMetaIntstr));
strncpy(icyMetaIntstr, p2, p3-p2);
icyMetaInt = atoi(icyMetaIntstr);
interleavedmetaDataFlag = 1;
}
}
serverFormatFlag = SERVER_FORMAT_SHOUTCAST;
sourceMedia = MP3_FORMAT;
outputStatusCallback(&gMain, (void *)icyName);
outputStatusCallback(&gMain, (void *)"Shoutcast");
outputStatusCallback(&gMain, (void *)"MP3");
}
void * startSourceThread(void *url) {
char requestURL[1024] = "";
char host[1024] = "";
char port_str[255] = "";
int port = 0;
char mount[1024] = "";
char *p1, *p2, *p3;
char *pSourceURLHeaders = NULL;
cbuffer_init(&sourceCBuffer, 16384*20);
pthread_mutex_init(&encoders_mutex, NULL);
strncpy(requestURL, (char *)url, sizeof(requestURL)-1);
memset(host, '\000', sizeof(host));
memset(port_str, '\000', sizeof(port_str));
memset(mount, '\000', sizeof(mount));
p1 = strstr(requestURL, "http://");
if (!p1) {
fprintf(stderr, "bad URL : %s\n", requestURL);
pthread_exit((void *)1);
return(NULL);
}
p1 = p1 + strlen("http://");
p2 = strchr(p1, ':');
if (!p2) {
fprintf(stderr, "bad URL : %s\n", requestURL);
pthread_exit((void *)1);
return(NULL);
}
p2++;
p3 = strchr(p2, '/');
if (!p3) {
strcpy(mount, "/");
strncpy(port_str, p2, sizeof(port_str)-1);
}
else {
strncpy(mount, p3, sizeof(mount)-1);
strncpy(port_str, p2, p3-p2);
}
strncpy(host, p1, p2-1-p1);
port = atoi(port_str);
char buf[1024] = "";
sprintf(buf, "Connecting to %s:%d%s", host, port, mount);
outputStatusCallback(&gMain, (void *)"Ready to connect");
sourceMainLoop = 1;
sourceThreadRunning = 1;
while (sourceMainLoop) {
LogMessage(&gMain, LOG_DEBUG, "Connecting source thread");
client_socket = connect_client(host, port, mount, &pSourceURLHeaders);
if (client_socket != -1) {
if (!strncmp(pSourceURLHeaders, "ICY 200 OK", strlen("ICY 200 OK"))) {
parseShoutcastHeader(pSourceURLHeaders);
}
else {
parseIcecastHeader(pSourceURLHeaders);
}
outputStatusCallback(&gMain, (void *)"Connected");
if (pSourceURLHeaders) {
free(pSourceURLHeaders);
}
sourceInnerLoop = 1;
#define SOCK_BLOCK 0
#ifdef WIN32
u_long varblock = SOCK_BLOCK;
#else
int varblock = SOCK_BLOCK;
#endif
#ifdef WIN32
ioctlsocket(client_socket, FIONBIO, &varblock);
#else
fcntl(client_socket, F_SETFL, (varblock == SOCK_BLOCK) ? 0 : O_NONBLOCK);
#endif
if (interleavedmetaDataFlag) {
cbuffer_destroy(&metadataCBuffer);
cbuffer_init(&metadataCBuffer, icyMetaInt*4);
}
fd_set readyfds;
fd_set errorfds;
int fd_max = client_socket;
FD_ZERO(&readyfds);
FD_ZERO(&errorfds);
FD_SET(client_socket, &readyfds);
FD_SET(client_socket, &errorfds);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 200000;
//FILE *dump = fopen("dump.mp3", "wb");
while (sourceInnerLoop) {
char buf[4096];
int read = 0;
int selret = 0;
selret = select(fd_max+1, NULL, &readyfds, &errorfds, &tv);
if (selret == 0) {
/* Nothing ready */
#ifdef WIN32
Sleep(100);
#else
usleep(1000);
#endif
}
else {
if (selret < 0) {
/* Problem here */
sourceInnerLoop = 0;
continue;
}
/* Check for any errors */
if (FD_ISSET(client_socket, &errorfds)) {
sourceInnerLoop = 0;
closesocket(client_socket);
continue;
}
read = recv(client_socket, buf, sizeof(buf), 0);
if (sourceInnerLoop == 0) {
break;
}
if (read <= 0) {
outputStatusCallback(&gMain, (void *)"Disconnected from source socket...");
sourceInnerLoop = 0;
}
if (read == 0) {
#ifdef WIN32
Sleep(100);
#else
usleep(1000);
#endif
}
else {
char *pData = buf;
int dataLen = read;
char *pNew = NULL;
int newLen = 0;
bool writeit = true;
if (interleavedmetaDataFlag) {
if (processInterleavedMetadata(buf, read, &pNew, &newLen)) {
pData = pNew;
dataLen = newLen;
}
else {
writeit = false;
}
}
if (writeit) {
LogMessage(&gMain, LOG_DEBUG, "Source CBuffer: %d", cbuffer_get_free(&sourceCBuffer));
if (cbuffer_insert(&sourceCBuffer, pData, dataLen) == BUFFER_FULL) {
outputStatusCallback(&gMain, (void *)"Circular buffer is full...");
sourceInnerLoop = 0;
}
else {
;
}
}
if (pData == pNew) {
free(pNew);
}
}
}
//usleep(500000);
}
LogMessage(&gMain, LOG_DEBUG, "Broke out of source inner loop.");
closesocket(client_socket);
if (sourceMainLoop) {
outputStatusCallback(&gMain, (void *)"Disconnected");
}
}
int countdown = gMain.gReconnectSec;
for (int i=0;i<gMain.gReconnectSec;i++) {
char title[255] = "";
sprintf(title, "Source reconnect in %d seconds", countdown);
countdown--;
outputStatusCallback(&gMain, title);
#ifdef WIN32
Sleep(1000);
#else
sleep(1);
#endif
}
}
sourceThreadRunning = 0;
pthread_exit((void *)1);
return(NULL);
}