-
Notifications
You must be signed in to change notification settings - Fork 0
/
afsclient.cpp
372 lines (303 loc) · 14.1 KB
/
afsclient.cpp
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
/* Copyright © 2007 Apple Inc. All Rights Reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by
Apple Inc. ("Apple") in consideration of your agreement to the
following terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of these
terms. If you do not agree with these terms, please do not use,
install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc.
may be used to endorse or promote products derived from the Apple
Software without specific prior written permission from Apple. Except
as expressly stated in this notice, no other rights or licenses, express
or implied, are granted by Apple herein, including but not limited to
any patent rights that may be infringed by your derivative works or by
other works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
#include <AudioToolbox/AudioToolbox.h>
#define PRINTERROR(LABEL) printf("%s err %4.4s %d\n", LABEL, &err, err)
const int port = 51515; // the port we will use
const unsigned int kNumAQBufs = 3; // number of audio queue buffers we allocate
const size_t kAQBufSize = 128 * 1024; // number of bytes in each audio queue buffer
const size_t kAQMaxPacketDescs = 512; // number of packet descriptions in our array
struct MyData
{
AudioFileStreamID audioFileStream; // the audio file stream parser
AudioQueueRef audioQueue; // the audio queue
AudioQueueBufferRef audioQueueBuffer[kNumAQBufs]; // audio queue buffers
AudioStreamPacketDescription packetDescs[kAQMaxPacketDescs]; // packet descriptions for enqueuing audio
unsigned int fillBufferIndex; // the index of the audioQueueBuffer that is being filled
size_t bytesFilled; // how many bytes have been filled
size_t packetsFilled; // how many packets have been filled
bool inuse[kNumAQBufs]; // flags to indicate that a buffer is still in use
bool started; // flag to indicate that the queue has been started
bool failed; // flag to indicate an error occurred
pthread_mutex_t mutex; // a mutex to protect the inuse flags
pthread_cond_t cond; // a condition varable for handling the inuse flags
pthread_cond_t done; // a condition varable for handling the inuse flags
};
typedef struct MyData MyData;
int MyConnectSocket();
void MyAudioQueueOutputCallback(void* inClientData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer);
void MyAudioQueueIsRunningCallback(void *inUserData, AudioQueueRef inAQ, AudioQueuePropertyID inID);
void MyPropertyListenerProc( void * inClientData,
AudioFileStreamID inAudioFileStream,
AudioFileStreamPropertyID inPropertyID,
UInt32 * ioFlags);
void MyPacketsProc( void * inClientData,
UInt32 inNumberBytes,
UInt32 inNumberPackets,
const void * inInputData,
AudioStreamPacketDescription *inPacketDescriptions);
OSStatus MyEnqueueBuffer(MyData* myData);
void WaitForFreeBuffer(MyData* myData);
int main (int argc, char * const argv[])
{
// allocate a struct for storing our state
MyData* myData = (MyData*)calloc(1, sizeof(MyData));
// initialize a mutex and condition so that we can block on buffers in use.
pthread_mutex_init(&myData->mutex, NULL);
pthread_cond_init(&myData->cond, NULL);
pthread_cond_init(&myData->done, NULL);
// get connected
int connection_socket = MyConnectSocket();
if (connection_socket < 0) return 1;
printf("connected\n");
// allocate a buffer for reading data from a socket
const size_t kRecvBufSize = 40000;
char* buf = (char*)malloc(kRecvBufSize * sizeof(char));
// create an audio file stream parser
OSStatus err = AudioFileStreamOpen(myData, MyPropertyListenerProc, MyPacketsProc,
kAudioFileAAC_ADTSType, &myData->audioFileStream);
if (err) { PRINTERROR("AudioFileStreamOpen"); return 1; }
while (!myData->failed) {
// read data from the socket
printf("->recv\n");
ssize_t bytesRecvd = recv(connection_socket, buf, kRecvBufSize, 0);
printf("bytesRecvd %d\n", bytesRecvd);
if (bytesRecvd <= 0) break; // eof or failure
// parse the data. this will call MyPropertyListenerProc and MyPacketsProc
err = AudioFileStreamParseBytes(myData->audioFileStream, bytesRecvd, buf, 0);
if (err) { PRINTERROR("AudioFileStreamParseBytes"); break; }
}
// enqueue last buffer
MyEnqueueBuffer(myData);
printf("flushing\n");
err = AudioQueueFlush(myData->audioQueue);
if (err) { PRINTERROR("AudioQueueFlush"); return 1; }
printf("stopping\n");
err = AudioQueueStop(myData->audioQueue, false);
if (err) { PRINTERROR("AudioQueueStop"); return 1; }
printf("waiting until finished playing..\n");
pthread_mutex_lock(&myData->mutex);
pthread_cond_wait(&myData->done, &myData->mutex);
pthread_mutex_unlock(&myData->mutex);
printf("done\n");
// cleanup
free(buf);
err = AudioFileStreamClose(myData->audioFileStream);
err = AudioQueueDispose(myData->audioQueue, false);
close(connection_socket);
free(myData);
return 0;
}
void MyPropertyListenerProc( void * inClientData,
AudioFileStreamID inAudioFileStream,
AudioFileStreamPropertyID inPropertyID,
UInt32 * ioFlags)
{
// this is called by audio file stream when it finds property values
MyData* myData = (MyData*)inClientData;
OSStatus err = noErr;
printf("found property '%c%c%c%c'\n", (inPropertyID>>24)&255, (inPropertyID>>16)&255, (inPropertyID>>8)&255, inPropertyID&255);
switch (inPropertyID) {
case kAudioFileStreamProperty_ReadyToProducePackets :
{
// the file stream parser is now ready to produce audio packets.
// get the stream format.
AudioStreamBasicDescription asbd;
UInt32 asbdSize = sizeof(asbd);
err = AudioFileStreamGetProperty(inAudioFileStream, kAudioFileStreamProperty_DataFormat, &asbdSize, &asbd);
if (err) { PRINTERROR("get kAudioFileStreamProperty_DataFormat"); myData->failed = true; break; }
// create the audio queue
err = AudioQueueNewOutput(&asbd, MyAudioQueueOutputCallback, myData, NULL, NULL, 0, &myData->audioQueue);
if (err) { PRINTERROR("AudioQueueNewOutput"); myData->failed = true; break; }
// allocate audio queue buffers
for (unsigned int i = 0; i < kNumAQBufs; ++i) {
err = AudioQueueAllocateBuffer(myData->audioQueue, kAQBufSize, &myData->audioQueueBuffer[i]);
if (err) { PRINTERROR("AudioQueueAllocateBuffer"); myData->failed = true; break; }
}
// get the cookie size
UInt32 cookieSize;
Boolean writable;
err = AudioFileStreamGetPropertyInfo(inAudioFileStream, kAudioFileStreamProperty_MagicCookieData, &cookieSize, &writable);
if (err) { PRINTERROR("info kAudioFileStreamProperty_MagicCookieData"); break; }
printf("cookieSize %d\n", cookieSize);
// get the cookie data
void* cookieData = calloc(1, cookieSize);
err = AudioFileStreamGetProperty(inAudioFileStream, kAudioFileStreamProperty_MagicCookieData, &cookieSize, cookieData);
if (err) { PRINTERROR("get kAudioFileStreamProperty_MagicCookieData"); free(cookieData); break; }
// set the cookie on the queue.
err = AudioQueueSetProperty(myData->audioQueue, kAudioQueueProperty_MagicCookie, cookieData, cookieSize);
free(cookieData);
if (err) { PRINTERROR("set kAudioQueueProperty_MagicCookie"); break; }
// listen for kAudioQueueProperty_IsRunning
err = AudioQueueAddPropertyListener(myData->audioQueue, kAudioQueueProperty_IsRunning, MyAudioQueueIsRunningCallback, myData);
if (err) { PRINTERROR("AudioQueueAddPropertyListener"); myData->failed = true; break; }
break;
}
}
}
void MyPacketsProc( void * inClientData,
UInt32 inNumberBytes,
UInt32 inNumberPackets,
const void * inInputData,
AudioStreamPacketDescription *inPacketDescriptions)
{
// this is called by audio file stream when it finds packets of audio
MyData* myData = (MyData*)inClientData;
printf("got data. bytes: %d packets: %d\n", inNumberBytes, inNumberPackets);
// the following code assumes we're streaming VBR data. for CBR data, you'd need another code branch here.
for (int i = 0; i < inNumberPackets; ++i) {
SInt64 packetOffset = inPacketDescriptions[i].mStartOffset;
SInt64 packetSize = inPacketDescriptions[i].mDataByteSize;
// if the space remaining in the buffer is not enough for this packet, then enqueue the buffer.
size_t bufSpaceRemaining = kAQBufSize - myData->bytesFilled;
if (bufSpaceRemaining < packetSize) {
MyEnqueueBuffer(myData);
WaitForFreeBuffer(myData);
}
// copy data to the audio queue buffer
AudioQueueBufferRef fillBuf = myData->audioQueueBuffer[myData->fillBufferIndex];
memcpy((char*)fillBuf->mAudioData + myData->bytesFilled, (const char*)inInputData + packetOffset, packetSize);
// fill out packet description
myData->packetDescs[myData->packetsFilled] = inPacketDescriptions[i];
myData->packetDescs[myData->packetsFilled].mStartOffset = myData->bytesFilled;
// keep track of bytes filled and packets filled
myData->bytesFilled += packetSize;
myData->packetsFilled += 1;
// if that was the last free packet description, then enqueue the buffer.
size_t packetsDescsRemaining = kAQMaxPacketDescs - myData->packetsFilled;
if (packetsDescsRemaining == 0) {
MyEnqueueBuffer(myData);
WaitForFreeBuffer(myData);
}
}
}
OSStatus StartQueueIfNeeded(MyData* myData)
{
OSStatus err = noErr;
if (!myData->started) { // start the queue if it has not been started already
err = AudioQueueStart(myData->audioQueue, NULL);
if (err) { PRINTERROR("AudioQueueStart"); myData->failed = true; return err; }
myData->started = true;
printf("started\n");
}
return err;
}
OSStatus MyEnqueueBuffer(MyData* myData)
{
OSStatus err = noErr;
myData->inuse[myData->fillBufferIndex] = true; // set in use flag
// enqueue buffer
AudioQueueBufferRef fillBuf = myData->audioQueueBuffer[myData->fillBufferIndex];
fillBuf->mAudioDataByteSize = myData->bytesFilled;
err = AudioQueueEnqueueBuffer(myData->audioQueue, fillBuf, myData->packetsFilled, myData->packetDescs);
if (err) { PRINTERROR("AudioQueueEnqueueBuffer"); myData->failed = true; return err; }
StartQueueIfNeeded(myData);
return err;
}
void WaitForFreeBuffer(MyData* myData)
{
// go to next buffer
if (++myData->fillBufferIndex >= kNumAQBufs) myData->fillBufferIndex = 0;
myData->bytesFilled = 0; // reset bytes filled
myData->packetsFilled = 0; // reset packets filled
// wait until next buffer is not in use
printf("->lock\n");
pthread_mutex_lock(&myData->mutex);
while (myData->inuse[myData->fillBufferIndex]) {
printf("... WAITING ...\n");
pthread_cond_wait(&myData->cond, &myData->mutex);
}
pthread_mutex_unlock(&myData->mutex);
printf("<-unlock\n");
}
int MyFindQueueBuffer(MyData* myData, AudioQueueBufferRef inBuffer)
{
for (unsigned int i = 0; i < kNumAQBufs; ++i) {
if (inBuffer == myData->audioQueueBuffer[i])
return i;
}
return -1;
}
void MyAudioQueueOutputCallback( void* inClientData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer)
{
// this is called by the audio queue when it has finished decoding our data.
// The buffer is now free to be reused.
MyData* myData = (MyData*)inClientData;
unsigned int bufIndex = MyFindQueueBuffer(myData, inBuffer);
// signal waiting thread that the buffer is free.
pthread_mutex_lock(&myData->mutex);
myData->inuse[bufIndex] = false;
pthread_cond_signal(&myData->cond);
pthread_mutex_unlock(&myData->mutex);
}
void MyAudioQueueIsRunningCallback( void* inClientData,
AudioQueueRef inAQ,
AudioQueuePropertyID inID)
{
MyData* myData = (MyData*)inClientData;
UInt32 running;
UInt32 size;
OSStatus err = AudioQueueGetProperty(inAQ, kAudioQueueProperty_IsRunning, &running, &size);
if (err) { PRINTERROR("get kAudioQueueProperty_IsRunning"); return; }
if (!running) {
pthread_mutex_lock(&myData->mutex);
pthread_cond_signal(&myData->done);
pthread_mutex_unlock(&myData->mutex);
}
}
int MyConnectSocket()
{
int connection_socket;
struct hostent *host = gethostbyname("127.0.0.1");
if (!host) { printf("can't get host\n"); return -1; }
connection_socket = socket(AF_INET, SOCK_STREAM, 0);
if (connection_socket < 0) { printf("can't create socket\n"); return -1; }
struct sockaddr_in server_sockaddr;
server_sockaddr.sin_family = host->h_addrtype;
memcpy(&server_sockaddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
server_sockaddr.sin_port = htons(port);
int err = connect(connection_socket, (struct sockaddr*)&server_sockaddr, sizeof(server_sockaddr));
if (err < 0) { printf("can't connect\n"); return -1; }
return connection_socket;
}