audio_queue.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * This file is part of the FreeStreamer project,
  3. * (C)Copyright 2011-2018 Matias Muhonen <mmu@iki.fi> 穆马帝
  4. * See the file ''LICENSE'' for using the code.
  5. *
  6. * https://github.com/muhku/FreeStreamer
  7. */
  8. #include "audio_queue.h"
  9. #include "stream_configuration.h"
  10. #include <pthread.h>
  11. //#define AQ_DEBUG 1
  12. //#define AQ_DEBUG_LOCKS 1
  13. #if !defined (AQ_DEBUG)
  14. #define AQ_TRACE(...) do {} while (0)
  15. #define AQ_ASSERT(...) do {} while (0)
  16. #else
  17. #include <cassert>
  18. #define AQ_TRACE(...) printf(__VA_ARGS__)
  19. #define AQ_ASSERT(...) assert(__VA_ARGS__)
  20. #endif
  21. #if !defined (AQ_DEBUG_LOCKS)
  22. #define AQ_LOCK_TRACE(...) do {} while (0)
  23. #else
  24. #define AQ_LOCK_TRACE(...) printf(__VA_ARGS__)
  25. #endif
  26. namespace astreamer {
  27. /* public */
  28. Audio_Queue::Audio_Queue()
  29. : m_delegate(0),
  30. m_state(IDLE),
  31. m_outAQ(0),
  32. m_fillBufferIndex(0),
  33. m_bytesFilled(0),
  34. m_packetsFilled(0),
  35. m_buffersUsed(0),
  36. m_audioQueueStarted(false),
  37. m_levelMeteringEnabled(false),
  38. m_lastError(noErr),
  39. m_initialOutputVolume(1.0)
  40. {
  41. Stream_Configuration *config = Stream_Configuration::configuration();
  42. m_audioQueueBuffer = new AudioQueueBufferRef[config->bufferCount];
  43. m_packetDescs = new AudioStreamPacketDescription[config->maxPacketDescs];
  44. m_bufferInUse = new bool[config->bufferCount];
  45. for (size_t i=0; i < config->bufferCount; i++) {
  46. m_bufferInUse[i] = false;
  47. }
  48. if (pthread_mutex_init(&m_mutex, NULL) != 0) {
  49. AQ_TRACE("m_mutex init failed!\n");
  50. }
  51. if (pthread_mutex_init(&m_bufferInUseMutex, NULL) != 0) {
  52. AQ_TRACE("m_bufferInUseMutex init failed!\n");
  53. }
  54. if (pthread_cond_init(&m_bufferFreeCondition, NULL) != 0) {
  55. AQ_TRACE("m_bufferFreeCondition init failed!\n");
  56. }
  57. }
  58. Audio_Queue::~Audio_Queue()
  59. {
  60. stop(true);
  61. cleanup();
  62. delete [] m_audioQueueBuffer;
  63. delete [] m_packetDescs;
  64. delete [] m_bufferInUse;
  65. pthread_mutex_destroy(&m_mutex);
  66. pthread_mutex_destroy(&m_bufferInUseMutex);
  67. pthread_cond_destroy(&m_bufferFreeCondition);
  68. }
  69. bool Audio_Queue::initialized()
  70. {
  71. return (m_outAQ != 0);
  72. }
  73. void Audio_Queue::start()
  74. {
  75. // start the queue if it has not been started already
  76. if (m_audioQueueStarted) {
  77. return;
  78. }
  79. OSStatus err = AudioQueueStart(m_outAQ, NULL);
  80. if (!err) {
  81. m_audioQueueStarted = true;
  82. m_levelMeteringEnabled = false;
  83. m_lastError = noErr;
  84. } else {
  85. AQ_TRACE("%s: AudioQueueStart failed!\n", __PRETTY_FUNCTION__);
  86. m_lastError = err;
  87. }
  88. }
  89. void Audio_Queue::pause()
  90. {
  91. if (m_state == RUNNING) {
  92. if (AudioQueuePause(m_outAQ) != 0) {
  93. AQ_TRACE("%s: AudioQueuePause failed!\n", __PRETTY_FUNCTION__);
  94. }
  95. setState(PAUSED);
  96. } else if (m_state == PAUSED) {
  97. AudioQueueStart(m_outAQ, NULL);
  98. setState(RUNNING);
  99. }
  100. }
  101. void Audio_Queue::stop()
  102. {
  103. stop(true);
  104. }
  105. float Audio_Queue::volume()
  106. {
  107. if (!m_outAQ) {
  108. return 1.0;
  109. }
  110. float vol;
  111. OSStatus err = AudioQueueGetParameter(m_outAQ, kAudioQueueParam_Volume, &vol);
  112. if (!err) {
  113. return vol;
  114. }
  115. return 1.0;
  116. }
  117. void Audio_Queue::setVolume(float volume)
  118. {
  119. if (!m_outAQ) {
  120. return;
  121. }
  122. AudioQueueSetParameter(m_outAQ, kAudioQueueParam_Volume, volume);
  123. }
  124. void Audio_Queue::setPlayRate(float playRate)
  125. {
  126. Stream_Configuration *configuration = Stream_Configuration::configuration();
  127. if (!configuration->enableTimeAndPitchConversion) {
  128. #if defined(DEBUG) || (TARGET_IPHONE_SIMULATOR)
  129. printf("*** FreeStreamer notification: Trying to set play rate for audio queue but enableTimeAndPitchConversion is disabled from configuration. Play rate settign will not work.\n");
  130. #endif
  131. return;
  132. }
  133. if (!m_outAQ) {
  134. return;
  135. }
  136. if (playRate < 0.5) {
  137. playRate = 0.5;
  138. }
  139. if (playRate > 2.0) {
  140. playRate = 2.0;
  141. }
  142. AudioQueueSetParameter(m_outAQ, kAudioQueueParam_PlayRate, playRate);
  143. }
  144. void Audio_Queue::stop(bool stopImmediately)
  145. {
  146. if (!m_audioQueueStarted) {
  147. AQ_TRACE("%s: audio queue already stopped, return!\n", __PRETTY_FUNCTION__);
  148. return;
  149. }
  150. m_audioQueueStarted = false;
  151. m_levelMeteringEnabled = false;
  152. pthread_mutex_lock(&m_bufferInUseMutex);
  153. pthread_cond_signal(&m_bufferFreeCondition);
  154. pthread_mutex_unlock(&m_bufferInUseMutex);
  155. AQ_TRACE("%s: enter\n", __PRETTY_FUNCTION__);
  156. if (AudioQueueFlush(m_outAQ) != 0) {
  157. AQ_TRACE("%s: AudioQueueFlush failed!\n", __PRETTY_FUNCTION__);
  158. }
  159. if (stopImmediately) {
  160. AudioQueueRemovePropertyListener(m_outAQ,
  161. kAudioQueueProperty_IsRunning,
  162. audioQueueIsRunningCallback,
  163. this);
  164. }
  165. if (AudioQueueStop(m_outAQ, stopImmediately) != 0) {
  166. AQ_TRACE("%s: AudioQueueStop failed!\n", __PRETTY_FUNCTION__);
  167. }
  168. if (stopImmediately) {
  169. setState(IDLE);
  170. }
  171. AQ_TRACE("%s: leave\n", __PRETTY_FUNCTION__);
  172. }
  173. AudioTimeStamp Audio_Queue::currentTime()
  174. {
  175. AudioTimeStamp queueTime;
  176. Boolean discontinuity;
  177. memset(&queueTime, 0, sizeof queueTime);
  178. OSStatus err = AudioQueueGetCurrentTime(m_outAQ, NULL, &queueTime, &discontinuity);
  179. if (err) {
  180. AQ_TRACE("AudioQueueGetCurrentTime failed\n");
  181. }
  182. return queueTime;
  183. }
  184. AudioQueueLevelMeterState Audio_Queue::levels()
  185. {
  186. if (!m_levelMeteringEnabled) {
  187. UInt32 enabledLevelMeter = true;
  188. AudioQueueSetProperty(m_outAQ,
  189. kAudioQueueProperty_EnableLevelMetering,
  190. &enabledLevelMeter,
  191. sizeof(UInt32));
  192. m_levelMeteringEnabled = true;
  193. }
  194. AudioQueueLevelMeterState levelMeter;
  195. UInt32 levelMeterSize = sizeof(AudioQueueLevelMeterState);
  196. AudioQueueGetProperty(m_outAQ, kAudioQueueProperty_CurrentLevelMeterDB, &levelMeter, &levelMeterSize);
  197. return levelMeter;
  198. }
  199. void Audio_Queue::init()
  200. {
  201. OSStatus err = noErr;
  202. cleanup();
  203. // create the audio queue
  204. err = AudioQueueNewOutput(&m_streamDesc, audioQueueOutputCallback, this, CFRunLoopGetCurrent(), NULL, 0, &m_outAQ);
  205. if (err) {
  206. AQ_TRACE("%s: error in AudioQueueNewOutput\n", __PRETTY_FUNCTION__);
  207. m_lastError = err;
  208. if (m_delegate) {
  209. m_delegate->audioQueueInitializationFailed();
  210. }
  211. return;
  212. }
  213. Stream_Configuration *configuration = Stream_Configuration::configuration();
  214. // allocate audio queue buffers
  215. for (unsigned int i = 0; i < configuration->bufferCount; ++i) {
  216. err = AudioQueueAllocateBuffer(m_outAQ, configuration->bufferSize, &m_audioQueueBuffer[i]);
  217. if (err) {
  218. /* If allocating the buffers failed, everything else will fail, too.
  219. * Dispose the queue so that we can later on detect that this
  220. * queue in fact has not been initialized.
  221. */
  222. AQ_TRACE("%s: error in AudioQueueAllocateBuffer\n", __PRETTY_FUNCTION__);
  223. (void)AudioQueueDispose(m_outAQ, true);
  224. m_outAQ = 0;
  225. m_lastError = err;
  226. if (m_delegate) {
  227. m_delegate->audioQueueInitializationFailed();
  228. }
  229. return;
  230. }
  231. }
  232. // listen for kAudioQueueProperty_IsRunning
  233. err = AudioQueueAddPropertyListener(m_outAQ, kAudioQueueProperty_IsRunning, audioQueueIsRunningCallback, this);
  234. if (err) {
  235. AQ_TRACE("%s: error in AudioQueueAddPropertyListener\n", __PRETTY_FUNCTION__);
  236. m_lastError = err;
  237. return;
  238. }
  239. if (configuration->enableTimeAndPitchConversion) {
  240. UInt32 enableTimePitchConversion = 1;
  241. err = AudioQueueSetProperty (m_outAQ, kAudioQueueProperty_EnableTimePitch, &enableTimePitchConversion, sizeof(enableTimePitchConversion));
  242. if (err != noErr) {
  243. AQ_TRACE("Failed to enable time and pitch conversion. Play rate setting will fail\n");
  244. }
  245. }
  246. if (m_initialOutputVolume != 1.0) {
  247. setVolume(m_initialOutputVolume);
  248. }
  249. }
  250. void Audio_Queue::handleAudioPackets(UInt32 inNumberBytes, UInt32 inNumberPackets, const void *inInputData, AudioStreamPacketDescription *inPacketDescriptions)
  251. {
  252. if (!initialized()) {
  253. AQ_TRACE("%s: warning: attempt to handle audio packets with uninitialized audio queue. return.\n", __PRETTY_FUNCTION__);
  254. return;
  255. }
  256. // this is called by audio file stream when it finds packets of audio
  257. AQ_TRACE("got data. bytes: %u packets: %u\n", inNumberBytes, (unsigned int)inNumberPackets);
  258. /* Place each packet into a buffer and then send each buffer into the audio
  259. queue */
  260. UInt32 i;
  261. for (i = 0; i < inNumberPackets; i++) {
  262. AudioStreamPacketDescription *desc = &inPacketDescriptions[i];
  263. const void *data = (const char*)inInputData + desc->mStartOffset;
  264. if (!initialized()) {
  265. AQ_TRACE("%s: warning: attempt to handle audio packets with uninitialized audio queue. return.\n", __PRETTY_FUNCTION__);
  266. return;
  267. }
  268. Stream_Configuration *config = Stream_Configuration::configuration();
  269. AQ_TRACE("%s: enter\n", __PRETTY_FUNCTION__);
  270. UInt32 packetSize = desc->mDataByteSize;
  271. /* This shouldn't happen because most of the time we read the packet buffer
  272. size from the file stream, but if we restored to guessing it we could
  273. come up too small here */
  274. if (packetSize > config->bufferSize) {
  275. AQ_TRACE("%s: packetSize %u > AQ_BUFSIZ %li\n", __PRETTY_FUNCTION__, (unsigned int)packetSize, config->bufferSize);
  276. return;
  277. }
  278. // if the space remaining in the buffer is not enough for this packet, then
  279. // enqueue the buffer and wait for another to become available.
  280. if (config->bufferSize - m_bytesFilled < packetSize) {
  281. enqueueBuffer();
  282. if (!m_audioQueueStarted) {
  283. return;
  284. }
  285. } else {
  286. AQ_TRACE("%s: skipped enqueueBuffer AQ_BUFSIZ - m_bytesFilled %lu, packetSize %u\n", __PRETTY_FUNCTION__, (config->bufferSize - m_bytesFilled), (unsigned int)packetSize);
  287. }
  288. // copy data to the audio queue buffer
  289. AudioQueueBufferRef buf = m_audioQueueBuffer[m_fillBufferIndex];
  290. memcpy((char*)buf->mAudioData, data, packetSize);
  291. // fill out packet description to pass to enqueue() later on
  292. m_packetDescs[m_packetsFilled] = *desc;
  293. // Make sure the offset is relative to the start of the audio buffer
  294. m_packetDescs[m_packetsFilled].mStartOffset = m_bytesFilled;
  295. // keep track of bytes filled and packets filled
  296. m_bytesFilled += packetSize;
  297. m_packetsFilled++;
  298. /* If filled our buffer with packets, then commit it to the system */
  299. if (m_packetsFilled >= config->maxPacketDescs) {
  300. enqueueBuffer();
  301. }
  302. }
  303. }
  304. /* private */
  305. void Audio_Queue::cleanup()
  306. {
  307. if (!initialized()) {
  308. AQ_TRACE("%s: warning: attempt to cleanup an uninitialized audio queue. return.\n", __PRETTY_FUNCTION__);
  309. return;
  310. }
  311. Stream_Configuration *config = Stream_Configuration::configuration();
  312. if (m_state != IDLE) {
  313. AQ_TRACE("%s: attemping to cleanup the audio queue when it is still playing, force stopping\n",
  314. __PRETTY_FUNCTION__);
  315. AudioQueueRemovePropertyListener(m_outAQ,
  316. kAudioQueueProperty_IsRunning,
  317. audioQueueIsRunningCallback,
  318. this);
  319. AudioQueueStop(m_outAQ, true);
  320. setState(IDLE);
  321. }
  322. if (AudioQueueDispose(m_outAQ, true) != 0) {
  323. AQ_TRACE("%s: AudioQueueDispose failed!\n", __PRETTY_FUNCTION__);
  324. }
  325. m_outAQ = 0;
  326. m_fillBufferIndex = m_bytesFilled = m_packetsFilled = m_buffersUsed = 0;
  327. for (size_t i=0; i < config->bufferCount; i++) {
  328. m_bufferInUse[i] = false;
  329. }
  330. m_lastError = noErr;
  331. }
  332. void Audio_Queue::setState(State state)
  333. {
  334. if (m_state == state) {
  335. /* We are already in this state! */
  336. return;
  337. }
  338. m_state = state;
  339. if (m_delegate) {
  340. m_delegate->audioQueueStateChanged(state);
  341. }
  342. }
  343. void Audio_Queue::enqueueBuffer()
  344. {
  345. AQ_ASSERT(!m_bufferInUse[m_fillBufferIndex]);
  346. Stream_Configuration *config = Stream_Configuration::configuration();
  347. AQ_TRACE("%s: enter\n", __PRETTY_FUNCTION__);
  348. pthread_mutex_lock(&m_bufferInUseMutex);
  349. m_bufferInUse[m_fillBufferIndex] = true;
  350. m_buffersUsed++;
  351. // enqueue buffer
  352. AudioQueueBufferRef fillBuf = m_audioQueueBuffer[m_fillBufferIndex];
  353. fillBuf->mAudioDataByteSize = m_bytesFilled;
  354. pthread_mutex_unlock(&m_bufferInUseMutex);
  355. AQ_ASSERT(m_packetsFilled > 0);
  356. OSStatus err = AudioQueueEnqueueBuffer(m_outAQ, fillBuf, m_packetsFilled, m_packetDescs);
  357. if (!err) {
  358. m_lastError = noErr;
  359. start();
  360. } else {
  361. /* If we get an error here, it very likely means that the audio queue is no longer
  362. running */
  363. AQ_TRACE("%s: error in AudioQueueEnqueueBuffer\n", __PRETTY_FUNCTION__);
  364. m_lastError = err;
  365. return;
  366. }
  367. pthread_mutex_lock(&m_bufferInUseMutex);
  368. // go to next buffer
  369. if (++m_fillBufferIndex >= config->bufferCount) {
  370. m_fillBufferIndex = 0;
  371. }
  372. // reset bytes filled
  373. m_bytesFilled = 0;
  374. // reset packets filled
  375. m_packetsFilled = 0;
  376. // wait until next buffer is not in use
  377. while (m_bufferInUse[m_fillBufferIndex]) {
  378. AQ_TRACE("waiting for buffer %u\n", (unsigned int)m_fillBufferIndex);
  379. pthread_cond_wait(&m_bufferFreeCondition, &m_bufferInUseMutex);
  380. }
  381. pthread_mutex_unlock(&m_bufferInUseMutex);
  382. }
  383. // this is called by the audio queue when it has finished decoding our data.
  384. // The buffer is now free to be reused.
  385. void Audio_Queue::audioQueueOutputCallback(void *inClientData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer)
  386. {
  387. Audio_Queue *audioQueue = static_cast<Audio_Queue*>(inClientData);
  388. Stream_Configuration *config = Stream_Configuration::configuration();
  389. int bufIndex = -1;
  390. for (unsigned int i = 0; i < config->bufferCount; ++i) {
  391. if (inBuffer == audioQueue->m_audioQueueBuffer[i]) {
  392. AQ_TRACE("findQueueBuffer %i\n", i);
  393. bufIndex = i;
  394. break;
  395. }
  396. }
  397. if (bufIndex == -1) {
  398. return;
  399. }
  400. pthread_mutex_lock(&audioQueue->m_bufferInUseMutex);
  401. AQ_ASSERT(audioQueue->m_bufferInUse[bufIndex]);
  402. audioQueue->m_bufferInUse[bufIndex] = false;
  403. audioQueue->m_buffersUsed--;
  404. AQ_TRACE("signaling buffer free for inuse %i....\n", bufIndex);
  405. pthread_cond_signal(&audioQueue->m_bufferFreeCondition);
  406. AQ_TRACE("signal sent!\n");
  407. if (audioQueue->m_buffersUsed == 0 && audioQueue->m_delegate) {
  408. AQ_LOCK_TRACE("audioQueueOutputCallback: unlock 2\n");
  409. pthread_mutex_unlock(&audioQueue->m_bufferInUseMutex);
  410. if (audioQueue->m_delegate) {
  411. audioQueue->m_delegate->audioQueueBuffersEmpty();
  412. }
  413. } else {
  414. pthread_mutex_unlock(&audioQueue->m_bufferInUseMutex);
  415. if (audioQueue->m_delegate) {
  416. audioQueue->m_delegate->audioQueueFinishedPlayingPacket();
  417. }
  418. }
  419. AQ_LOCK_TRACE("audioQueueOutputCallback: unlock\n");
  420. }
  421. void Audio_Queue::audioQueueIsRunningCallback(void *inClientData, AudioQueueRef inAQ, AudioQueuePropertyID inID)
  422. {
  423. Audio_Queue *audioQueue = static_cast<Audio_Queue*>(inClientData);
  424. AQ_TRACE("%s: enter\n", __PRETTY_FUNCTION__);
  425. UInt32 running;
  426. UInt32 output = sizeof(running);
  427. OSStatus err = AudioQueueGetProperty(inAQ, kAudioQueueProperty_IsRunning, &running, &output);
  428. if (err) {
  429. AQ_TRACE("%s: error in kAudioQueueProperty_IsRunning\n", __PRETTY_FUNCTION__);
  430. return;
  431. }
  432. if (running) {
  433. AQ_TRACE("audio queue running!\n");
  434. audioQueue->setState(RUNNING);
  435. } else {
  436. audioQueue->setState(IDLE);
  437. }
  438. }
  439. } // namespace astreamer