audio_queue.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * This file is part of the FreeStreamer project,
  3. * (C)Copyright 2011-2013 Matias Muhonen.
  4. * See the file ''LICENSE'' for using the code.
  5. *
  6. * Part of the code in this file has been rewritten from
  7. * the AudioFileStreamExample / afsclient.cpp
  8. * example, Copyright © 2007 Apple Inc.
  9. *
  10. * The threadless playback has been adapted from
  11. * Alex Crichton's AudioStreamer.
  12. */
  13. #include "audio_queue.h"
  14. #include <cassert>
  15. //#define AQ_DEBUG 1
  16. #if !defined (AQ_DEBUG)
  17. #define AQ_TRACE(...) do {} while (0)
  18. #else
  19. #define AQ_TRACE(...) printf(__VA_ARGS__)
  20. #endif
  21. namespace astreamer {
  22. typedef struct queued_packet {
  23. AudioStreamPacketDescription desc;
  24. struct queued_packet *next;
  25. char data[];
  26. } queued_packet_t;
  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_processedPacketsSizeTotal(0),
  37. m_processedPacketsCount(0),
  38. m_audioQueueStarted(false),
  39. m_waitingOnBuffer(false),
  40. m_queuedHead(0),
  41. m_queuedTail(0),
  42. m_lastError(noErr)
  43. {
  44. for (size_t i=0; i < AQ_BUFFERS; i++) {
  45. m_bufferInUse[i] = false;
  46. }
  47. }
  48. Audio_Queue::~Audio_Queue()
  49. {
  50. stop(true);
  51. cleanup();
  52. }
  53. bool Audio_Queue::initialized()
  54. {
  55. return (m_outAQ != 0);
  56. }
  57. void Audio_Queue::start()
  58. {
  59. // start the queue if it has not been started already
  60. if (m_audioQueueStarted) {
  61. return;
  62. }
  63. OSStatus err = AudioQueueStart(m_outAQ, NULL);
  64. if (!err) {
  65. m_audioQueueStarted = true;
  66. m_lastError = noErr;
  67. } else {
  68. AQ_TRACE("%s: AudioQueueStart failed!\n", __PRETTY_FUNCTION__);
  69. m_lastError = err;
  70. }
  71. }
  72. void Audio_Queue::pause()
  73. {
  74. if (m_state == RUNNING) {
  75. if (AudioQueuePause(m_outAQ) != 0) {
  76. AQ_TRACE("%s: AudioQueuePause failed!\n", __PRETTY_FUNCTION__);
  77. }
  78. setState(PAUSED);
  79. } else if (m_state == PAUSED) {
  80. AudioQueueStart(m_outAQ, NULL);
  81. setState(RUNNING);
  82. }
  83. }
  84. void Audio_Queue::stop()
  85. {
  86. stop(true);
  87. }
  88. void Audio_Queue::stop(bool stopImmediately)
  89. {
  90. if (!m_audioQueueStarted) {
  91. AQ_TRACE("%s: audio queue already stopped, return!\n", __PRETTY_FUNCTION__);
  92. return;
  93. }
  94. m_audioQueueStarted = false;
  95. AQ_TRACE("%s: enter\n", __PRETTY_FUNCTION__);
  96. if (AudioQueueFlush(m_outAQ) != 0) {
  97. AQ_TRACE("%s: AudioQueueFlush failed!\n", __PRETTY_FUNCTION__);
  98. }
  99. if (stopImmediately) {
  100. AudioQueueRemovePropertyListener(m_outAQ,
  101. kAudioQueueProperty_IsRunning,
  102. audioQueueIsRunningCallback,
  103. this);
  104. }
  105. if (AudioQueueStop(m_outAQ, stopImmediately) != 0) {
  106. AQ_TRACE("%s: AudioQueueStop failed!\n", __PRETTY_FUNCTION__);
  107. }
  108. if (stopImmediately) {
  109. setState(IDLE);
  110. }
  111. AQ_TRACE("%s: leave\n", __PRETTY_FUNCTION__);
  112. }
  113. double Audio_Queue::packetDuration()
  114. {
  115. return m_streamDesc.mFramesPerPacket / m_streamDesc.mSampleRate;
  116. }
  117. unsigned Audio_Queue::timePlayedInSeconds()
  118. {
  119. unsigned timePlayed = 0;
  120. AudioTimeStamp queueTime;
  121. Boolean discontinuity;
  122. OSStatus err = AudioQueueGetCurrentTime(m_outAQ, NULL, &queueTime, &discontinuity);
  123. if (err) {
  124. goto out;
  125. }
  126. timePlayed = queueTime.mSampleTime / m_streamDesc.mSampleRate;
  127. out:
  128. return timePlayed;
  129. }
  130. unsigned Audio_Queue::bitrate()
  131. {
  132. unsigned bitrate = 0;
  133. double packetDuration = this->packetDuration();
  134. if (packetDuration > 0 && m_processedPacketsCount > 50) {
  135. double averagePacketByteSize = m_processedPacketsSizeTotal / m_processedPacketsCount;
  136. bitrate = 8 * averagePacketByteSize / packetDuration;
  137. }
  138. return bitrate;
  139. }
  140. void Audio_Queue::handlePropertyChange(AudioFileStreamID inAudioFileStream, AudioFileStreamPropertyID inPropertyID, UInt32 *ioFlags)
  141. {
  142. OSStatus err = noErr;
  143. AQ_TRACE("found property '%lu%lu%lu%lu'\n", (inPropertyID>>24)&255, (inPropertyID>>16)&255, (inPropertyID>>8)&255, inPropertyID&255);
  144. switch (inPropertyID) {
  145. case kAudioFileStreamProperty_ReadyToProducePackets:
  146. {
  147. cleanup();
  148. // the file stream parser is now ready to produce audio packets.
  149. // get the stream format.
  150. memset(&m_streamDesc, 0, sizeof(m_streamDesc));
  151. UInt32 asbdSize = sizeof(m_streamDesc);
  152. err = AudioFileStreamGetProperty(inAudioFileStream, kAudioFileStreamProperty_DataFormat, &asbdSize, &m_streamDesc);
  153. if (err) {
  154. AQ_TRACE("%s: error in kAudioFileStreamProperty_DataFormat\n", __PRETTY_FUNCTION__);
  155. m_lastError = err;
  156. break;
  157. }
  158. // create the audio queue
  159. err = AudioQueueNewOutput(&m_streamDesc, audioQueueOutputCallback, this, CFRunLoopGetCurrent(), NULL, 0, &m_outAQ);
  160. if (err) {
  161. AQ_TRACE("%s: error in AudioQueueNewOutput\n", __PRETTY_FUNCTION__);
  162. if (m_delegate) {
  163. m_delegate->audioQueueInitializationFailed();
  164. }
  165. m_lastError = err;
  166. break;
  167. }
  168. // allocate audio queue buffers
  169. for (unsigned int i = 0; i < AQ_BUFFERS; ++i) {
  170. err = AudioQueueAllocateBuffer(m_outAQ, AQ_BUFSIZ, &m_audioQueueBuffer[i]);
  171. if (err) {
  172. /* If allocating the buffers failed, everything else will fail, too.
  173. * Dispose the queue so that we can later on detect that this
  174. * queue in fact has not been initialized.
  175. */
  176. AQ_TRACE("%s: error in AudioQueueAllocateBuffer\n", __PRETTY_FUNCTION__);
  177. (void)AudioQueueDispose(m_outAQ, true);
  178. m_outAQ = 0;
  179. if (m_delegate) {
  180. m_delegate->audioQueueInitializationFailed();
  181. }
  182. m_lastError = err;
  183. break;
  184. }
  185. }
  186. setCookiesForStream(inAudioFileStream);
  187. // listen for kAudioQueueProperty_IsRunning
  188. err = AudioQueueAddPropertyListener(m_outAQ, kAudioQueueProperty_IsRunning, audioQueueIsRunningCallback, this);
  189. if (err) {
  190. AQ_TRACE("%s: error in AudioQueueAddPropertyListener\n", __PRETTY_FUNCTION__);
  191. m_lastError = err;
  192. break;
  193. }
  194. break;
  195. }
  196. }
  197. }
  198. void Audio_Queue::handleAudioPackets(UInt32 inNumberBytes, UInt32 inNumberPackets, const void *inInputData, AudioStreamPacketDescription *inPacketDescriptions)
  199. {
  200. if (!initialized()) {
  201. AQ_TRACE("%s: warning: attempt to handle audio packets with uninitialized audio queue. return.\n", __PRETTY_FUNCTION__);
  202. return;
  203. }
  204. // this is called by audio file stream when it finds packets of audio
  205. AQ_TRACE("got data. bytes: %lu packets: %lu\n", inNumberBytes, inNumberPackets);
  206. /* Place each packet into a buffer and then send each buffer into the audio
  207. queue */
  208. UInt32 i;
  209. if (!inPacketDescriptions) {
  210. AQ_TRACE("%s: notice: supplying the packet descriptions for a supposed CBR data.\n", __PRETTY_FUNCTION__);
  211. // If no packet descriptions are supplied, assume we are dealing with CBR data
  212. UInt32 base = inNumberBytes / inNumberPackets;
  213. AudioStreamPacketDescription *descriptions = new AudioStreamPacketDescription[inNumberPackets];
  214. for (i = 0; i < inNumberPackets; i++) {
  215. descriptions[i].mStartOffset = (base * i);
  216. descriptions[i].mDataByteSize = base;
  217. descriptions[i].mVariableFramesInPacket = 0;
  218. }
  219. inPacketDescriptions = descriptions;
  220. m_cbrPacketDescriptions.push_back(descriptions);
  221. }
  222. for (i = 0; i < inNumberPackets && !m_waitingOnBuffer && m_queuedHead == NULL; i++) {
  223. AudioStreamPacketDescription *desc = &inPacketDescriptions[i];
  224. int ret = handlePacket((const char*)inInputData + desc->mStartOffset, desc);
  225. if (!ret) break;
  226. }
  227. if (i == inNumberPackets) {
  228. return;
  229. }
  230. for (; i < inNumberPackets; i++) {
  231. /* Allocate the packet */
  232. UInt32 size = inPacketDescriptions[i].mDataByteSize;
  233. queued_packet_t *packet = (queued_packet_t *)malloc(sizeof(queued_packet_t) + size);
  234. /* Prepare the packet */
  235. packet->next = NULL;
  236. packet->desc = inPacketDescriptions[i];
  237. packet->desc.mStartOffset = 0;
  238. memcpy(packet->data, (const char *)inInputData + inPacketDescriptions[i].mStartOffset,
  239. size);
  240. if (m_queuedHead == NULL) {
  241. m_queuedHead = m_queuedTail = packet;
  242. } else {
  243. m_queuedTail->next = packet;
  244. m_queuedTail = packet;
  245. }
  246. }
  247. }
  248. int Audio_Queue::handlePacket(const void *data, AudioStreamPacketDescription *desc)
  249. {
  250. if (!initialized()) {
  251. AQ_TRACE("%s: warning: attempt to handle audio packets with uninitialized audio queue. return.\n", __PRETTY_FUNCTION__);
  252. return -1;
  253. }
  254. AQ_TRACE("%s: enter\n", __PRETTY_FUNCTION__);
  255. UInt32 packetSize = desc->mDataByteSize;
  256. /* This shouldn't happen because most of the time we read the packet buffer
  257. size from the file stream, but if we restored to guessing it we could
  258. come up too small here */
  259. if (packetSize > AQ_BUFSIZ) {
  260. AQ_TRACE("%s: packetSize %lli > AQ_BUFSIZ %li\n", __PRETTY_FUNCTION__, packetSize, AQ_BUFSIZ);
  261. return -1;
  262. }
  263. // if the space remaining in the buffer is not enough for this packet, then
  264. // enqueue the buffer and wait for another to become available.
  265. if (AQ_BUFSIZ - m_bytesFilled < packetSize) {
  266. int hasFreeBuffer = enqueueBuffer();
  267. if (hasFreeBuffer <= 0) {
  268. return hasFreeBuffer;
  269. }
  270. } else {
  271. AQ_TRACE("%s: skipped enqueueBuffer AQ_BUFSIZ - m_bytesFilled %lu, packetSize %lli\n", __PRETTY_FUNCTION__, (AQ_BUFSIZ - m_bytesFilled), packetSize);
  272. }
  273. m_processedPacketsSizeTotal += packetSize;
  274. m_processedPacketsCount++;
  275. // copy data to the audio queue buffer
  276. AudioQueueBufferRef buf = m_audioQueueBuffer[m_fillBufferIndex];
  277. memcpy((char*)buf->mAudioData + m_bytesFilled, data, packetSize);
  278. // fill out packet description to pass to enqueue() later on
  279. m_packetDescs[m_packetsFilled] = *desc;
  280. // Make sure the offset is relative to the start of the audio buffer
  281. m_packetDescs[m_packetsFilled].mStartOffset = m_bytesFilled;
  282. // keep track of bytes filled and packets filled
  283. m_bytesFilled += packetSize;
  284. m_packetsFilled++;
  285. /* Maximum number of packets which can be contained in one buffer */
  286. #define kAQMaxPacketDescs 512
  287. /* If filled our buffer with packets, then commit it to the system */
  288. if (m_packetsFilled >= kAQMaxPacketDescs) {
  289. return enqueueBuffer();
  290. }
  291. return 1;
  292. }
  293. /* private */
  294. void Audio_Queue::cleanup()
  295. {
  296. if (!initialized()) {
  297. AQ_TRACE("%s: warning: attempt to cleanup an uninitialized audio queue. return.\n", __PRETTY_FUNCTION__);
  298. return;
  299. }
  300. if (AudioQueueDispose(m_outAQ, true) != 0) {
  301. AQ_TRACE("%s: AudioQueueDispose failed!\n", __PRETTY_FUNCTION__);
  302. }
  303. m_outAQ = 0;
  304. m_fillBufferIndex = m_bytesFilled = m_packetsFilled = m_buffersUsed = m_processedPacketsSizeTotal = m_processedPacketsCount = 0;
  305. for (size_t i=0; i < AQ_BUFFERS; i++) {
  306. m_bufferInUse[i] = false;
  307. }
  308. queued_packet_t *cur = m_queuedHead;
  309. while (cur) {
  310. queued_packet_t *tmp = cur->next;
  311. free(cur);
  312. cur = tmp;
  313. }
  314. m_queuedHead = m_queuedHead = 0;
  315. for (size_t i=0; i < m_cbrPacketDescriptions.size(); i++) {
  316. delete[] m_cbrPacketDescriptions[i];
  317. }
  318. m_cbrPacketDescriptions.clear();
  319. m_waitingOnBuffer = false;
  320. m_lastError = noErr;
  321. }
  322. void Audio_Queue::setCookiesForStream(AudioFileStreamID inAudioFileStream)
  323. {
  324. OSStatus err;
  325. // get the cookie size
  326. UInt32 cookieSize;
  327. Boolean writable;
  328. err = AudioFileStreamGetPropertyInfo(inAudioFileStream, kAudioFileStreamProperty_MagicCookieData, &cookieSize, &writable);
  329. if (err) {
  330. AQ_TRACE("error in info kAudioFileStreamProperty_MagicCookieData\n");
  331. return;
  332. }
  333. AQ_TRACE("cookieSize %lu\n", cookieSize);
  334. // get the cookie data
  335. void* cookieData = calloc(1, cookieSize);
  336. err = AudioFileStreamGetProperty(inAudioFileStream, kAudioFileStreamProperty_MagicCookieData, &cookieSize, cookieData);
  337. if (err) {
  338. AQ_TRACE("error in get kAudioFileStreamProperty_MagicCookieData");
  339. free(cookieData);
  340. return;
  341. }
  342. // set the cookie on the queue.
  343. err = AudioQueueSetProperty(m_outAQ, kAudioQueueProperty_MagicCookie, cookieData, cookieSize);
  344. free(cookieData);
  345. if (err) {
  346. AQ_TRACE("error in set kAudioQueueProperty_MagicCookie");
  347. }
  348. }
  349. void Audio_Queue::setState(State state)
  350. {
  351. if (m_state == state) {
  352. /* We are already in this state! */
  353. return;
  354. }
  355. m_state = state;
  356. if (m_delegate) {
  357. m_delegate->audioQueueStateChanged(m_state);
  358. }
  359. }
  360. int Audio_Queue::enqueueBuffer()
  361. {
  362. assert(!m_bufferInUse[m_fillBufferIndex]);
  363. AQ_TRACE("%s: enter\n", __PRETTY_FUNCTION__);
  364. m_bufferInUse[m_fillBufferIndex] = true;
  365. m_buffersUsed++;
  366. // enqueue buffer
  367. AudioQueueBufferRef fillBuf = m_audioQueueBuffer[m_fillBufferIndex];
  368. fillBuf->mAudioDataByteSize = m_bytesFilled;
  369. assert(m_packetsFilled > 0);
  370. OSStatus err = AudioQueueEnqueueBuffer(m_outAQ, fillBuf, m_packetsFilled, m_packetDescs);
  371. if (!err) {
  372. m_lastError = noErr;
  373. start();
  374. } else {
  375. /* If we get an error here, it very likely means that the audio queue is no longer
  376. running */
  377. AQ_TRACE("%s: error in AudioQueueEnqueueBuffer\n", __PRETTY_FUNCTION__);
  378. m_lastError = err;
  379. return 1;
  380. }
  381. // go to next buffer
  382. if (++m_fillBufferIndex >= AQ_BUFFERS) {
  383. m_fillBufferIndex = 0;
  384. }
  385. // reset bytes filled
  386. m_bytesFilled = 0;
  387. // reset packets filled
  388. m_packetsFilled = 0;
  389. // wait until next buffer is not in use
  390. if (m_bufferInUse[m_fillBufferIndex]) {
  391. AQ_TRACE("waiting for buffer %lu\n", m_fillBufferIndex);
  392. if (m_delegate) {
  393. m_delegate->audioQueueOverflow();
  394. }
  395. m_waitingOnBuffer = true;
  396. return 0;
  397. }
  398. return 1;
  399. }
  400. int Audio_Queue::findQueueBuffer(AudioQueueBufferRef inBuffer)
  401. {
  402. for (unsigned int i = 0; i < AQ_BUFFERS; ++i) {
  403. if (inBuffer == m_audioQueueBuffer[i]) {
  404. AQ_TRACE("findQueueBuffer %i\n", i);
  405. return i;
  406. }
  407. }
  408. return -1;
  409. }
  410. void Audio_Queue::enqueueCachedData()
  411. {
  412. assert(!m_waitingOnBuffer);
  413. assert(!m_bufferInUse[m_fillBufferIndex]);
  414. /* Queue up as many packets as possible into the buffers */
  415. queued_packet_t *cur = m_queuedHead;
  416. while (cur) {
  417. int ret = handlePacket(cur->data, &cur->desc);
  418. if (ret == 0) {
  419. break;
  420. }
  421. queued_packet_t *next = cur->next;
  422. free(cur);
  423. cur = next;
  424. }
  425. m_queuedHead = cur;
  426. /* If we finished queueing all our saved packets, we can re-schedule the
  427. * stream to run */
  428. if (cur == NULL) {
  429. m_queuedTail = NULL;
  430. if (m_delegate) {
  431. m_delegate->audioQueueUnderflow();
  432. }
  433. }
  434. }
  435. // this is called by the audio queue when it has finished decoding our data.
  436. // The buffer is now free to be reused.
  437. void Audio_Queue::audioQueueOutputCallback(void *inClientData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer)
  438. {
  439. Audio_Queue *audioQueue = static_cast<Audio_Queue*>(inClientData);
  440. unsigned int bufIndex = audioQueue->findQueueBuffer(inBuffer);
  441. assert(audioQueue->m_bufferInUse[bufIndex]);
  442. audioQueue->m_bufferInUse[bufIndex] = false;
  443. audioQueue->m_buffersUsed--;
  444. if (audioQueue->m_buffersUsed == 0 && !audioQueue->m_queuedHead && audioQueue->m_delegate) {
  445. audioQueue->m_delegate->audioQueueBuffersEmpty();
  446. } else if (audioQueue->m_waitingOnBuffer) {
  447. audioQueue->m_waitingOnBuffer = false;
  448. audioQueue->enqueueCachedData();
  449. }
  450. }
  451. void Audio_Queue::audioQueueIsRunningCallback(void *inClientData, AudioQueueRef inAQ, AudioQueuePropertyID inID)
  452. {
  453. Audio_Queue *audioQueue = static_cast<Audio_Queue*>(inClientData);
  454. AQ_TRACE("%s: enter\n", __PRETTY_FUNCTION__);
  455. UInt32 running;
  456. UInt32 output = sizeof(running);
  457. OSStatus err = AudioQueueGetProperty(inAQ, kAudioQueueProperty_IsRunning, &running, &output);
  458. if (err) {
  459. AQ_TRACE("%s: error in kAudioQueueProperty_IsRunning\n", __PRETTY_FUNCTION__);
  460. return;
  461. }
  462. if (running) {
  463. AQ_TRACE("audio queue running!\n");
  464. audioQueue->setState(RUNNING);
  465. } else {
  466. audioQueue->setState(IDLE);
  467. }
  468. }
  469. } // namespace astreamer