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
|
/*
Copyright (c) 2008 Lars-Dominik Braun
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 <unistd.h>
#include <string.h>
#include "player.h"
#include "config.h"
/* LE to BE and reverse
* @param unsigned int
* @return byteswapped unsigned int
*/
unsigned int BarChangeByteorderUI32 (char buf[4]) {
unsigned int ret = 0;
ret = buf[0] << 24 & 0xffffffff;
ret |= buf[1] << 16 & 0xffffff;
ret |= buf[2] << 8 & 0xffff;
ret |= buf[3] << 0 & 0xff;
return ret;
}
/* our heart: plays streamed music
* @param streamed data
* @param block size
* @param received blocks
* @param extra data (player data)
* @return received bytes or less on error
*/
size_t BarPlayerCurlCb (void *ptr, size_t size, size_t nmemb, void *stream) {
char *data = ptr;
struct aacPlayer *player = stream;
if (player->doQuit) {
return 0;
}
/* FIXME: not the best solution to poll every second, but the easiest
* one I know... (pthread's conditions could be another solution) */
if (player->doPause) {
curl_easy_pause (player->audioFd, CURLPAUSE_ALL);
while (player->doPause && !player->doQuit) {
sleep (1);
}
curl_easy_pause (player->audioFd, CURLPAUSE_CONT);
}
/* fill buffer */
if (player->bufferFilled + size*nmemb > sizeof (player->buffer)) {
printf ("Buffer overflow!\n");
return 0;
}
memcpy (player->buffer+player->bufferFilled, data, size*nmemb);
player->bufferFilled += size*nmemb;
player->bufferRead = 0;
if (player->mode == RECV_DATA) {
char *aacDecoded;
NeAACDecFrameInfo frameInfo;
while ((player->bufferFilled - player->bufferRead) >
player->sampleSize[player->sampleSizeCurr]) {
/* decode frame */
aacDecoded = NeAACDecDecode(player->aacHandle, &frameInfo,
(unsigned char *) player->buffer + player->bufferRead,
player->sampleSize[player->sampleSizeCurr]);
if (frameInfo.error != 0) {
printf (PACKAGE ": Decoding error: %s\n\n",
NeAACDecGetErrorMessage (frameInfo.error));
break;
}
/* ao_play needs bytes: 1 sample = 16 bits = 2 bytes */
ao_play (player->audioOutDevice, aacDecoded,
frameInfo.samples * 16 / 8);
player->bufferRead += frameInfo.bytesconsumed;
player->sampleSizeCurr++;
/* going through this loop can take up to a few seconds =>
* allow earlier thread abort */
if (player->doQuit) {
return 0;
}
}
} else {
if (player->mode == FIND_ESDS) {
while (player->bufferRead+4 < player->bufferFilled) {
if (memcmp (player->buffer + player->bufferRead, "esds",
4) == 0) {
player->mode = FOUND_ESDS;
player->bufferRead += 4;
break;
}
player->bufferRead++;
}
}
if (player->mode == FOUND_ESDS) {
/* FIXME: is this the correct way? */
/* we're gonna read 10 bytes */
while (player->bufferRead+1+4+5 < player->bufferFilled) {
if (memcmp (player->buffer + player->bufferRead,
"\x05\x80\x80\x80", 4) == 0) {
ao_sample_format format;
int audioOutDriver;
/* +1+4 needs to be replaced by <something>! */
player->bufferRead += 1+4;
char err = NeAACDecInit2 (player->aacHandle,
(unsigned char *) player->buffer +
player->bufferRead, 5, &player->samplerate,
&player->channels);
player->bufferRead += 5;
if (err != 0) {
printf ("Error while initializing audio decoder"
"(%i)\n", err);
return 1;
}
audioOutDriver = ao_default_driver_id();
format.bits = 16;
format.channels = player->channels;
format.rate = player->samplerate;
format.byte_format = AO_FMT_LITTLE;
player->audioOutDevice = ao_open_live (audioOutDriver,
&format, NULL);
player->mode = AUDIO_INITIALIZED;
break;
}
player->bufferRead++;
}
}
if (player->mode == AUDIO_INITIALIZED) {
while (player->bufferRead+4+8 < player->bufferFilled) {
if (memcmp (player->buffer + player->bufferRead, "stsz",
4) == 0) {
player->mode = FOUND_STSZ;
player->bufferRead += 4;
/* skip version and unknown */
player->bufferRead += 8;
break;
}
player->bufferRead++;
}
}
/* get frame sizes */
if (player->mode == FOUND_STSZ) {
while (player->bufferRead+4 < player->bufferFilled) {
/* how many frames do we have? */
if (player->sampleSizeN == 0) {
/* mp4 uses big endian, convert */
player->sampleSizeN =
BarChangeByteorderUI32 (player->buffer +
player->bufferRead);
player->sampleSize = calloc (player->sampleSizeN,
sizeof (player->sampleSizeN));
player->bufferRead += 4;
player->sampleSizeCurr = 0;
break;
} else {
player->sampleSize[player->sampleSizeCurr] =
BarChangeByteorderUI32 (player->buffer +
player->bufferRead);
player->sampleSizeCurr++;
player->bufferRead += 4;
}
/* all sizes read, nearly ready for data mode */
if (player->sampleSizeCurr >= player->sampleSizeN) {
player->mode = SAMPLESIZE_INITIALIZED;
break;
}
}
}
/* search for data atom and let the show begin... */
if (player->mode == SAMPLESIZE_INITIALIZED) {
while (player->bufferRead+4 < player->bufferFilled) {
if (memcmp (player->buffer + player->bufferRead, "mdat",
4) == 0) {
player->mode = RECV_DATA;
player->sampleSizeCurr = 0;
player->bufferRead += 4;
break;
}
player->bufferRead++;
}
}
}
/* move remaining bytes to buffer beginning */
memmove (player->buffer, player->buffer + player->bufferRead,
(player->bufferFilled - player->bufferRead));
player->bufferFilled = (player->bufferFilled - player->bufferRead);
return size*nmemb;
}
/* player thread; for every song a new thread is started
* @param aacPlayer structure
* @return NULL NULL NULL ...
*/
void *BarPlayerThread (void *data) {
struct aacPlayer *player = data;
NeAACDecConfigurationPtr conf;
player->audioFd = curl_easy_init ();
player->aacHandle = NeAACDecOpen();
conf = NeAACDecGetCurrentConfiguration(player->aacHandle);
conf->outputFormat = FAAD_FMT_16BIT;
conf->downMatrix = 1;
NeAACDecSetConfiguration(player->aacHandle, conf);
curl_easy_setopt (player->audioFd, CURLOPT_URL, player->url);
curl_easy_setopt (player->audioFd, CURLOPT_WRITEFUNCTION, BarPlayerCurlCb);
curl_easy_setopt (player->audioFd, CURLOPT_WRITEDATA, player);
curl_easy_setopt (player->audioFd, CURLOPT_USERAGENT, PACKAGE_STRING);
curl_easy_setopt (player->audioFd, CURLOPT_CONNECTTIMEOUT, 60);
curl_easy_perform (player->audioFd);
NeAACDecClose(player->aacHandle);
ao_close(player->audioOutDevice);
curl_easy_cleanup (player->audioFd);
if (player->sampleSize != NULL) {
free (player->sampleSize);
}
player->finishedPlayback = 1;
return NULL;
}
|