-
Notifications
You must be signed in to change notification settings - Fork 7
/
AudioStreamer.h
executable file
·283 lines (210 loc) · 8.57 KB
/
AudioStreamer.h
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
//
// AudioStreamer.h
// StreamingAudioPlayer
//
// Created by Matt Gallagher on 27/09/08.
// Copyright 2008 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file, free of charge, in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
#import <UIKit/UIKit.h>
#include <pthread.h>
#include <AudioToolbox/AudioToolbox.h>
#include "AudioPool.h"
#include "Packet.h"
#include "ConcurrentOp.h"
#include "Game.h"
#define LOG_QUEUED_BUFFERS 1
#define kPlaybackFileLocation CFSTR("ipod-library://item/item.m4a?id=1053020204400037178")
#define kNumAQBufs 24 // Number of audio queue buffers we allocate. (Formerly 16)
// Needs to be big enough to keep audio pipeline
// busy (non-zero number of queued buffers) but
// not so big that audio takes too long to begin
// (kNumAQBufs * kAQBufSize of data must be
// loaded before playback will start).
// Set LOG_QUEUED_BUFFERS to 1 to log how many
// buffers are queued at any time -- if it drops
// to zero too often, this value may need to
// increase. Min 3, typical 8-24.
// we chose 32784 b/c that's the size of packets we're getting
#define kAQDefaultBufSize 2048 // Number of bytes in each audio queue buffer
// Needs to be big enough to hold a packet of
// audio from the audio file. If number is too
// large, queuing of audio before playback starts
// will take too long.
// Highly compressed files can use smaller
// numbers (512 or less). 2048 should hold all
// but the largest packets. A buffer size error
// will occur if this number is too small.
#define kAQMaxPacketDescs 6 // Number of packet descriptions in our array (formerly 512)
#define networkTimeOutTime 5
bool interruptedOnPlayback;
typedef enum
{
AS_INITIALIZED = 0,
AS_STARTING_FILE_THREAD,
AS_WAITING_FOR_DATA,
AS_WAITING_FOR_QUEUE_TO_START,
AS_READY_TO_PLAY,
AS_PLAYING,
AS_BUFFERING,
AS_STOPPING,
AS_STOPPED,
AS_PAUSED
} AudioStreamerState;
typedef enum
{
AS_NO_STOP = 0,
AS_STOPPING_EOF,
AS_STOPPING_USER_ACTION,
AS_STOPPING_ERROR,
AS_STOPPING_TEMPORARILY
} AudioStreamerStopReason;
typedef enum
{
AS_NO_ERROR = 0,
AS_NETWORK_CONNECTION_FAILED,
AS_FILE_STREAM_GET_PROPERTY_FAILED,
AS_FILE_STREAM_SEEK_FAILED,
AS_FILE_STREAM_PARSE_BYTES_FAILED,
AS_FILE_STREAM_OPEN_FAILED,
AS_FILE_STREAM_CLOSE_FAILED,
AS_AUDIO_DATA_NOT_FOUND,
AS_AUDIO_QUEUE_CREATION_FAILED,
AS_AUDIO_QUEUE_BUFFER_ALLOCATION_FAILED,
AS_AUDIO_QUEUE_ENQUEUE_FAILED,
AS_AUDIO_QUEUE_ADD_LISTENER_FAILED,
AS_AUDIO_QUEUE_REMOVE_LISTENER_FAILED,
AS_AUDIO_QUEUE_START_FAILED,
AS_AUDIO_QUEUE_PAUSE_FAILED,
AS_AUDIO_QUEUE_BUFFER_MISMATCH,
AS_AUDIO_QUEUE_DISPOSE_FAILED,
AS_AUDIO_QUEUE_STOP_FAILED,
AS_AUDIO_QUEUE_FLUSH_FAILED,
AS_AUDIO_STREAMER_FAILED,
AS_GET_AUDIO_TIME_FAILED,
AS_AUDIO_BUFFER_TOO_SMALL,
ABSD_SETUP_FAILED
} AudioStreamerErrorCode;
extern NSString * const ASStatusChangedNotification;
@class Game;
@interface AudioStreamer : NSObject
{
@public
NSURL *url;
CFURLRef cfURL;
//
// Special threading consideration:
// The audioQueue property should only ever be accessed inside a
// synchronized(self) block and only *after* checking that ![self isFinishing]
//
AudioQueueRef audioQueue;
AudioFileStreamID audioFileStream; // the audio file stream parser
AudioStreamBasicDescription asbd; // description of the audio
AudioQueueBufferRef audioQueueBuffer[kNumAQBufs]; // audio queue buffers
AudioStreamPacketDescription packetDescs[kAQMaxPacketDescs]; // packet descriptions for enqueuing audio
UInt32 fillBufferIndex; // the index of the audioQueueBuffer that is being filled
size_t bytesFilled; // how many bytes have been filled
size_t byteOffset;
size_t packetsFilled; // how many packets have been filled
bool inuse[kNumAQBufs]; // flags to indicate that a buffer is still in use
NSInteger buffersUsed;
UInt64 audioDataByteCount; // Used when the actual number of audio bytes in
// the file is known (more accurate than assuming
// the whole file is audio)
NSInteger fileLength; // Length of the file in bytes
AudioStreamerState state;
double delayTime;
AudioStreamerStopReason stopReason;
AudioStreamerErrorCode errorCode;
OSStatus err;
bool discontinuous; // flag to indicate middle of the stream
pthread_mutex_t queueBuffersMutex; // a mutex to protect the inuse flags
pthread_cond_t queueBufferReadyCondition; // a condition varable for handling the inuse flags
CFReadStreamRef stream;
NSNotificationCenter *notificationCenter;
NSUInteger dataOffset;
UInt32 bitRate;
bool seekNeeded;
double seekTime;
double sampleRate;
double lastProgress;
int numBuffersToEnqueueLater;
//from book
Boolean isDone;
SInt64 packetPosition;
UInt32 numPacketsToRead;
double packetDuration; // sample rate times frames per packet
UInt32 packetBufferSize;
Boolean isAudioItemSet;
Boolean isStreamerSet;
NSString * currentSong; // used to indicate which song we're currently sending data about/reading from
bool isSongChanged;
UInt32 totalBytesReceived;
UInt32 totalBytesRead;
UInt32 totalBytesHandeled;
VirtualRingBuffer *ringBuffer; //reference to a ring buffer
UInt32 allBytesAvailable;
Game * _game;
Player *_localPlayerObj;
NSTimer * ringBufferReaderTimer;
}
@property (nonatomic) AudioStreamerErrorCode errorCode;
@property (nonatomic, readwrite) OSStatus err;
@property (nonatomic, readwrite) AudioStreamerState state;
@property (nonatomic, readwrite) size_t bytesFilled;
@property (nonatomic, readwrite) size_t byteOffset;
@property (nonatomic, readonly) double progress;
@property (nonatomic, readwrite) UInt32 bitRate;
@property (nonatomic, readwrite) AudioFileStreamID audioFileStream;
@property (nonatomic, readwrite) pthread_mutex_t queueBuffersMutex;
@property (nonatomic, readwrite) pthread_cond_t queueBufferReadyCondition;
@property (nonatomic, readwrite) Boolean isDone;
@property (nonatomic, readwrite) SInt64 packetPosition;
@property (nonatomic, readwrite) UInt32 numPacketsToRead;
// we copy this to avoid an overrite by the ring buffer read thread
//@property (nonatomic, copy) NSData *ringBufferReadData;
- (id)initWithURL:(NSURL *)aURL;
- (id)initWithCFURL:(CFURLRef)aCFURL;
- (id)initWithRingBuffer:(VirtualRingBuffer *)ringBuff
bufferByteSize:(UInt32)bufferByteSize
numPacketsToRead:(UInt32)numPacketsToRead
gameObj:(Game *)game;
- (id)initStreamer;
- (void)start;
- (void)startInternal;
- (void)stop;
- (void)pause;
- (BOOL)isPlaying;
- (BOOL)isPaused;
- (BOOL)isWaiting;
- (BOOL)isIdle;
- (void)handleReadGKSessionData:(NSData *)data;
- (void)failWithErrorCode:(AudioStreamerErrorCode)anErrorCode;
- (void)handleAudioPackets:(const void *)inInputData
numberBytes:(UInt32)inNumberBytes
numberPackets:(UInt32)inNumberPackets
packetDescriptions:(AudioStreamPacketDescription *)inPacketDescriptions;
-(void)handleContainerPackets:(const void *)inInputData
numberBytes:(UInt32)inNumberBytes;
void ASPropertyListenerProc( void * inClientData,
AudioFileStreamID inAudioFileStream,
AudioFileStreamPropertyID inPropertyID,
UInt32 * ioFlags);
void ASPacketsProc( void * inClientData,
UInt32 inNumberBytes,
UInt32 inNumberPackets,
const void * inInputData,
AudioStreamPacketDescription *inPacketDescriptions);
void MyAudioQueueOutputCallback( void* inClientData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer);
void MyAudioQueueOutputCallback2( void* inClientData,
AudioQueueRef inAQ,
AudioQueueBufferRef inBuffer);
void MyAudioQueueIsRunningCallback(void *inUserData, AudioQueueRef inAQ, AudioQueuePropertyID inID);
@end