AudioPlayer.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**********************************************************************************
  2. AudioPlayer.m
  3. Created by Thong Nguyen on 14/05/2012.
  4. https://github.com/tumtumtum/audjustable
  5. Inspired by Matt Gallagher's AudioStreamer:
  6. https://github.com/mattgallagher/AudioStreamer
  7. Copyright (c) 2012 Thong Nguyen (tumtumtum@gmail.com). All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in the
  14. documentation and/or other materials provided with the distribution.
  15. 3. All advertising materials mentioning features or use of this software
  16. must display the following acknowledgement:
  17. This product includes software developed by Thong Nguyen (tumtumtum@gmail.com)
  18. 4. Neither the name of Thong Nguyen nor the
  19. names of its contributors may be used to endorse or promote products
  20. derived from this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY Thong Nguyen''AS IS'' AND ANY
  22. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  25. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  26. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. **********************************************************************************/
  32. #import <Foundation/Foundation.h>
  33. #import <pthread.h>
  34. #import "DataSource.h"
  35. #include <AudioToolbox/AudioToolbox.h>
  36. #define AudioPlayerDefaultNumberOfAudioQueueBuffers (2 * 1024)
  37. typedef enum
  38. {
  39. AudioPlayerInternalStateInitialised = 0,
  40. AudioPlayerInternalStateRunning = 1,
  41. AudioPlayerInternalStatePlaying = (1 << 1) | AudioPlayerInternalStateRunning,
  42. AudioPlayerInternalStateStartingThread = (1 << 2) | AudioPlayerInternalStateRunning,
  43. AudioPlayerInternalStateWaitingForData = (1 << 3) | AudioPlayerInternalStateRunning,
  44. AudioPlayerInternalStateWaitingForQueueToStart = (1 << 4) | AudioPlayerInternalStateRunning,
  45. AudioPlayerInternalStatePaused = (1 << 5) | AudioPlayerInternalStateRunning,
  46. AudioPlayerInternalStateRebuffering = (1 << 6) | AudioPlayerInternalStateRunning,
  47. AudioPlayerInternalStateStopping = (1 << 7),
  48. AudioPlayerInternalStateStopped = (1 << 8),
  49. AudioPlayerInternalStateDisposed = (1 << 9),
  50. AudioPlayerInternalStateError = (1 << 10)
  51. }
  52. AudioPlayerInternalState;
  53. typedef enum
  54. {
  55. AudioPlayerStateReady,
  56. AudioPlayerStateRunning = 1,
  57. AudioPlayerStatePlaying = (1 << 1) | AudioPlayerStateRunning,
  58. AudioPlayerStatePaused = (1 << 2) | AudioPlayerStateRunning,
  59. AudioPlayerStateStopped = (1 << 3),
  60. AudioPlayerStateError = (1 << 4),
  61. AudioPlayerStateDisposed = (1 << 5)
  62. }
  63. AudioPlayerState;
  64. typedef enum
  65. {
  66. AudioPlayerStopReasonNoStop = 0,
  67. AudioPlayerStopReasonEof,
  68. AudioPlayerStopReasonUserAction,
  69. AudioPlayerStopReasonUserActionFlushStop
  70. }
  71. AudioPlayerStopReason;
  72. typedef enum
  73. {
  74. AudioPlayerErrorNone = 0,
  75. AudioPlayerErrorDataSource,
  76. AudioPlayerErrorStreamParseBytesFailed,
  77. AudioPlayerErrorDataNotFound,
  78. AudioPlayerErrorQueueStartFailed,
  79. AudioPlayerErrorQueuePauseFailed,
  80. AudioPlayerErrorUnknownBuffer,
  81. AudioPlayerErrorQueueStopFailed,
  82. AudioPlayerErrorOther
  83. }
  84. AudioPlayerErrorCode;
  85. @class AudioPlayer;
  86. @protocol AudioPlayerDelegate <NSObject>
  87. -(void) audioPlayer:(AudioPlayer*)audioPlayer stateChanged:(AudioPlayerState)state;
  88. -(void) audioPlayer:(AudioPlayer*)audioPlayer didEncounterError:(AudioPlayerErrorCode)errorCode;
  89. -(void) audioPlayer:(AudioPlayer*)audioPlayer didStartPlayingQueueItemId:(NSObject*)queueItemId;
  90. -(void) audioPlayer:(AudioPlayer*)audioPlayer didFinishBufferingSourceWithQueueItemId:(NSObject*)queueItemId;
  91. -(void) audioPlayer:(AudioPlayer*)audioPlayer didFinishPlayingQueueItemId:(NSObject*)queueItemId withReason:(AudioPlayerStopReason)stopReason andProgress:(double)progress andDuration:(double)duration;
  92. @optional
  93. -(void) audioPlayer:(AudioPlayer*)audioPlayer logInfo:(NSString*)line;
  94. -(void) audioPlayer:(AudioPlayer*)audioPlayer internalStateChanged:(AudioPlayerInternalState)state;
  95. -(void) audioPlayer:(AudioPlayer*)audioPlayer didCancelQueuedItems:(NSArray*)queuedItems;
  96. @end
  97. @class QueueEntry;
  98. typedef struct
  99. {
  100. AudioQueueBufferRef ref;
  101. int bufferIndex;
  102. }
  103. AudioQueueBufferRefLookupEntry;
  104. @interface AudioPlayer : NSObject<DataSourceDelegate>
  105. {
  106. @private
  107. UInt8* readBuffer;
  108. int readBufferSize;
  109. NSOperationQueue* fastApiQueue;
  110. QueueEntry* currentlyPlayingEntry;
  111. QueueEntry* currentlyReadingEntry;
  112. NSMutableArray* upcomingQueue;
  113. NSMutableArray* bufferingQueue;
  114. AudioQueueBufferRef* audioQueueBuffer;
  115. AudioQueueBufferRefLookupEntry* audioQueueBufferLookup;
  116. unsigned int audioQueueBufferRefLookupCount;
  117. unsigned int audioQueueBufferCount;
  118. AudioStreamPacketDescription* packetDescs;
  119. bool* bufferUsed;
  120. int numberOfBuffersUsed;
  121. AudioQueueRef audioQueue;
  122. AudioStreamBasicDescription currentAudioStreamBasicDescription;
  123. NSThread* playbackThread;
  124. NSRunLoop* playbackThreadRunLoop;
  125. NSConditionLock* threadFinishedCondLock;
  126. AudioFileStreamID audioFileStream;
  127. BOOL discontinuous;
  128. int bytesFilled;
  129. int packetsFilled;
  130. int fillBufferIndex;
  131. UIBackgroundTaskIdentifier backgroundTaskId;
  132. AudioPlayerErrorCode errorCode;
  133. AudioPlayerStopReason stopReason;
  134. int currentlyPlayingLock;
  135. pthread_mutex_t playerMutex;
  136. pthread_mutex_t queueBuffersMutex;
  137. pthread_cond_t queueBufferReadyCondition;
  138. volatile BOOL waiting;
  139. volatile BOOL disposeWasRequested;
  140. volatile BOOL seekToTimeWasRequested;
  141. volatile BOOL newFileToPlay;
  142. volatile double requestedSeekTime;
  143. volatile BOOL audioQueueFlushing;
  144. volatile SInt64 audioPacketsReadCount;
  145. volatile SInt64 audioPacketsPlayedCount;
  146. BOOL meteringEnabled;
  147. AudioQueueLevelMeterState* levelMeterState;
  148. NSInteger numberOfChannels;
  149. }
  150. @property (readonly) double duration;
  151. @property (readonly) double progress;
  152. @property (readwrite) AudioPlayerState state;
  153. @property (readonly) AudioPlayerStopReason stopReason;
  154. @property (readwrite, unsafe_unretained) id<AudioPlayerDelegate> delegate;
  155. @property (readwrite) BOOL meteringEnabled;
  156. -(id) init;
  157. -(id) initWithNumberOfAudioQueueBuffers:(int)numberOfAudioQueueBuffers andReadBufferSize:(int)readBufferSizeIn;
  158. -(DataSource*) dataSourceFromURL:(NSURL*)url;
  159. -(void) play:(NSURL*)url;
  160. -(void) queueDataSource:(DataSource*)dataSource withQueueItemId:(NSObject*)queueItemId;
  161. -(void) setDataSource:(DataSource*)dataSourceIn withQueueItemId:(NSObject*)queueItemId;
  162. -(void) seekToTime:(double)value;
  163. -(void) pause;
  164. -(void) resume;
  165. -(void) stop;
  166. -(void) flushStop;
  167. -(void) mute;
  168. -(void) unmute;
  169. -(void) dispose;
  170. -(NSObject*) currentlyPlayingQueueItemId;
  171. -(void) updateMeters;
  172. -(float) peakPowerInDecibelsForChannel:(NSUInteger)channelNumber;
  173. -(float) averagePowerInDecibelsForChannel:(NSUInteger)channelNumber;
  174. @end