|
| 1 | +// |
| 2 | +// AudioStreamer.h |
| 3 | +// StreamingAudioPlayer |
| 4 | +// |
| 5 | +// Created by Matt Gallagher on 27/09/08. |
| 6 | +// Copyright 2008 Matt Gallagher. All rights reserved. |
| 7 | +// |
| 8 | +// Permission is given to use this source code file, free of charge, in any |
| 9 | +// project, commercial or otherwise, entirely at your risk, with the condition |
| 10 | +// that any redistribution (in part or whole) of source code must retain |
| 11 | +// this copyright and permission notice. Attribution in compiled projects is |
| 12 | +// appreciated but not required. |
| 13 | +// |
| 14 | + |
| 15 | +#ifdef TARGET_OS_IPHONE |
| 16 | +#import <UIKit/UIKit.h> |
| 17 | +#else |
| 18 | +#import <Cocoa/Cocoa.h> |
| 19 | +#endif TARGET_OS_IPHONE |
| 20 | + |
| 21 | +#include <pthread.h> |
| 22 | +#include <AudioToolbox/AudioToolbox.h> |
| 23 | + |
| 24 | +#define kNumAQBufs 32 // number of audio queue buffers we allocate |
| 25 | +#define kAQBufSize 1024 // number of bytes in each audio queue buffer |
| 26 | +#define kAQMaxPacketDescs 512 // number of packet descriptions in our array |
| 27 | + |
| 28 | +typedef enum |
| 29 | +{ |
| 30 | + AS_INITIALIZED = 0, |
| 31 | + AS_STARTING_FILE_THREAD, |
| 32 | + AS_WAITING_FOR_DATA, |
| 33 | + AS_WAITING_FOR_QUEUE_TO_START, |
| 34 | + AS_PLAYING, |
| 35 | + AS_BUFFERING, |
| 36 | + AS_STOPPING, |
| 37 | + AS_STOPPED, |
| 38 | + AS_PAUSED |
| 39 | +} AudioStreamerState; |
| 40 | + |
| 41 | +typedef enum |
| 42 | +{ |
| 43 | + AS_NO_STOP = 0, |
| 44 | + AS_STOPPING_EOF, |
| 45 | + AS_STOPPING_USER_ACTION, |
| 46 | + AS_STOPPING_ERROR, |
| 47 | + AS_STOPPING_END_OF_PRESCAN, |
| 48 | + AS_STOPPING_TEMPORARILY |
| 49 | +} AudioStreamerStopReason; |
| 50 | + |
| 51 | +typedef enum |
| 52 | +{ |
| 53 | + AS_NO_ERROR = 0, |
| 54 | + AS_NETWORK_CONNECTION_FAILED, |
| 55 | + AS_FILE_STREAM_GET_PROPERTY_FAILED, |
| 56 | + AS_FILE_STREAM_SEEK_FAILED, |
| 57 | + AS_FILE_STREAM_PARSE_BYTES_FAILED, |
| 58 | + AS_FILE_STREAM_OPEN_FAILED, |
| 59 | + AS_FILE_STREAM_CLOSE_FAILED, |
| 60 | + AS_AUDIO_DATA_NOT_FOUND, |
| 61 | + AS_AUDIO_QUEUE_CREATION_FAILED, |
| 62 | + AS_AUDIO_QUEUE_BUFFER_ALLOCATION_FAILED, |
| 63 | + AS_AUDIO_QUEUE_ENQUEUE_FAILED, |
| 64 | + AS_AUDIO_QUEUE_ADD_LISTENER_FAILED, |
| 65 | + AS_AUDIO_QUEUE_REMOVE_LISTENER_FAILED, |
| 66 | + AS_AUDIO_QUEUE_START_FAILED, |
| 67 | + AS_AUDIO_QUEUE_PAUSE_FAILED, |
| 68 | + AS_AUDIO_QUEUE_BUFFER_MISMATCH, |
| 69 | + AS_AUDIO_QUEUE_DISPOSE_FAILED, |
| 70 | + AS_AUDIO_QUEUE_STOP_FAILED, |
| 71 | + AS_AUDIO_QUEUE_FLUSH_FAILED, |
| 72 | + AS_AUDIO_STREAMER_FAILED, |
| 73 | + AS_GET_AUDIO_TIME_FAILED |
| 74 | +} AudioStreamerErrorCode; |
| 75 | + |
| 76 | +extern NSString * const ASStatusChangedNotification; |
| 77 | + |
| 78 | +@interface AudioStreamer : NSObject |
| 79 | +{ |
| 80 | + NSURL *url; |
| 81 | + |
| 82 | + // |
| 83 | + // Special threading consideration: |
| 84 | + // The audioQueue property should only ever be accessed inside a |
| 85 | + // synchronized(self) block and only *after* checking that ![self isFinishing] |
| 86 | + // |
| 87 | + AudioQueueRef audioQueue; |
| 88 | + AudioFileStreamID audioFileStream; // the audio file stream parser |
| 89 | + |
| 90 | + AudioQueueBufferRef audioQueueBuffer[kNumAQBufs]; // audio queue buffers |
| 91 | + AudioStreamPacketDescription packetDescs[kAQMaxPacketDescs]; // packet descriptions for enqueuing audio |
| 92 | + unsigned int fillBufferIndex; // the index of the audioQueueBuffer that is being filled |
| 93 | + size_t bytesFilled; // how many bytes have been filled |
| 94 | + size_t packetsFilled; // how many packets have been filled |
| 95 | + bool inuse[kNumAQBufs]; // flags to indicate that a buffer is still in use |
| 96 | + NSInteger buffersUsed; |
| 97 | + |
| 98 | + AudioStreamerState state; |
| 99 | + AudioStreamerStopReason stopReason; |
| 100 | + AudioStreamerErrorCode errorCode; |
| 101 | + OSErr err; |
| 102 | + |
| 103 | + bool discontinuous; // flag to indicate middle of the stream |
| 104 | + |
| 105 | + pthread_mutex_t queueBuffersMutex; // a mutex to protect the inuse flags |
| 106 | + pthread_cond_t queueBufferReadyCondition; // a condition varable for handling the inuse flags |
| 107 | + |
| 108 | + CFReadStreamRef stream; |
| 109 | + NSNotificationCenter *notificationCenter; |
| 110 | + |
| 111 | + NSUInteger dataOffset; |
| 112 | + UInt32 bitRate; |
| 113 | + |
| 114 | + bool seekNeeded; |
| 115 | + double seekTime; |
| 116 | + double sampleRate; |
| 117 | + double lastProgress; |
| 118 | +} |
| 119 | + |
| 120 | +@property AudioStreamerErrorCode errorCode; |
| 121 | +@property (readonly) AudioStreamerState state; |
| 122 | +@property (readonly) double progress; |
| 123 | +@property (readonly) UInt32 bitRate; |
| 124 | + |
| 125 | +- (id)initWithURL:(NSURL *)aURL; |
| 126 | +- (void)start; |
| 127 | +- (void)stop; |
| 128 | +- (void)pause; |
| 129 | +- (BOOL)isPlaying; |
| 130 | +- (BOOL)isPaused; |
| 131 | +- (BOOL)isWaiting; |
| 132 | +- (BOOL)isIdle; |
| 133 | + |
| 134 | +@end |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | + |
0 commit comments