audio_queue.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef ASTREAMER_AUDIO_QUEUE_H
  11. #define ASTREAMER_AUDIO_QUEUE_H
  12. #include <AudioToolbox/AudioToolbox.h> /* AudioFileStreamID */
  13. #include <vector>
  14. namespace astreamer {
  15. class Audio_Queue_Delegate;
  16. struct queued_packet;
  17. class Audio_Queue {
  18. public:
  19. Audio_Queue_Delegate *m_delegate;
  20. static const size_t AQ_BUFFERS = 16; // number of audio queue buffers we allocate
  21. static const size_t AQ_BUFSIZ = 2048; // number of bytes in each audio queue buffer
  22. static const size_t AQ_MAX_PACKET_DESCS = 512; // number of packet descriptions in our array
  23. enum State {
  24. IDLE,
  25. RUNNING,
  26. PAUSED
  27. };
  28. Audio_Queue();
  29. virtual ~Audio_Queue();
  30. bool initialized();
  31. void handlePropertyChange(AudioFileStreamID inAudioFileStream, AudioFileStreamPropertyID inPropertyID, UInt32 *ioFlags);
  32. void handleAudioPackets(UInt32 inNumberBytes, UInt32 inNumberPackets, const void *inInputData, AudioStreamPacketDescription *inPacketDescriptions);
  33. int handlePacket(const void *data, AudioStreamPacketDescription *desc);
  34. void start();
  35. void pause();
  36. void stop(bool stopImmediately);
  37. void stop();
  38. double packetDuration();
  39. unsigned timePlayedInSeconds();
  40. unsigned bitrate();
  41. private:
  42. Audio_Queue(const Audio_Queue&);
  43. Audio_Queue& operator=(const Audio_Queue&);
  44. State m_state;
  45. AudioStreamBasicDescription m_streamDesc;
  46. AudioQueueRef m_outAQ; // the audio queue
  47. AudioQueueBufferRef m_audioQueueBuffer[AQ_BUFFERS]; // audio queue buffers
  48. AudioStreamPacketDescription m_packetDescs[AQ_MAX_PACKET_DESCS]; // packet descriptions for enqueuing audio
  49. UInt32 m_fillBufferIndex; // the index of the audioQueueBuffer that is being filled
  50. UInt32 m_bytesFilled; // how many bytes have been filled
  51. UInt32 m_packetsFilled; // how many packets have been filled
  52. UInt32 m_buffersUsed; // how many buffers are used
  53. UInt32 m_processedPacketsSizeTotal; // global packet statistics: total size
  54. UInt32 m_processedPacketsCount; // global packet statistics: count
  55. bool m_audioQueueStarted; // flag to indicate that the queue has been started
  56. bool m_bufferInUse[AQ_BUFFERS]; // flags to indicate that a buffer is still in use
  57. bool m_waitingOnBuffer;
  58. struct queued_packet *m_queuedHead;
  59. struct queued_packet *m_queuedTail;
  60. std::vector<AudioStreamPacketDescription*> m_cbrPacketDescriptions;
  61. OSStatus m_lastError;
  62. void cleanup();
  63. void setCookiesForStream(AudioFileStreamID inAudioFileStream);
  64. void setState(State state);
  65. int enqueueBuffer();
  66. int findQueueBuffer(AudioQueueBufferRef inBuffer);
  67. void enqueueCachedData();
  68. static void audioQueueOutputCallback(void *inClientData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer);
  69. static void audioQueueIsRunningCallback(void *inClientData, AudioQueueRef inAQ, AudioQueuePropertyID inID);
  70. };
  71. class Audio_Queue_Delegate {
  72. public:
  73. virtual void audioQueueStateChanged(Audio_Queue::State state) = 0;
  74. virtual void audioQueueBuffersEmpty() = 0;
  75. virtual void audioQueueOverflow() = 0;
  76. virtual void audioQueueUnderflow() = 0;
  77. virtual void audioQueueInitializationFailed() = 0;
  78. };
  79. } // namespace astreamer
  80. #endif // ASTREAMER_AUDIO_QUEUE_H