audio_queue.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifndef ASTREAMER_AUDIO_QUEUE_H
  9. #define ASTREAMER_AUDIO_QUEUE_H
  10. #include <AudioToolbox/AudioToolbox.h> /* AudioFileStreamID */
  11. namespace astreamer {
  12. class Audio_Queue_Delegate;
  13. struct queued_packet;
  14. class Audio_Queue {
  15. public:
  16. Audio_Queue_Delegate *m_delegate;
  17. enum State {
  18. IDLE,
  19. RUNNING,
  20. PAUSED
  21. };
  22. Audio_Queue();
  23. virtual ~Audio_Queue();
  24. bool initialized();
  25. void init();
  26. // Notice: the queue blocks if it has no free buffers
  27. void handleAudioPackets(UInt32 inNumberBytes, UInt32 inNumberPackets, const void *inInputData, AudioStreamPacketDescription *inPacketDescriptions);
  28. void start();
  29. void pause();
  30. void stop(bool stopImmediately);
  31. void stop();
  32. float volume();
  33. void setVolume(float volume);
  34. void setPlayRate(float playRate);
  35. AudioTimeStamp currentTime();
  36. AudioQueueLevelMeterState levels();
  37. private:
  38. Audio_Queue(const Audio_Queue&);
  39. Audio_Queue& operator=(const Audio_Queue&);
  40. State m_state;
  41. AudioQueueRef m_outAQ; // the audio queue
  42. AudioQueueBufferRef *m_audioQueueBuffer; // audio queue buffers
  43. AudioStreamPacketDescription *m_packetDescs; // packet descriptions for enqueuing audio
  44. UInt32 m_fillBufferIndex; // the index of the audioQueueBuffer that is being filled
  45. UInt32 m_bytesFilled; // how many bytes have been filled
  46. UInt32 m_packetsFilled; // how many packets have been filled
  47. UInt32 m_buffersUsed; // how many buffers are used
  48. bool m_audioQueueStarted; // flag to indicate that the queue has been started
  49. bool *m_bufferInUse; // flags to indicate that a buffer is still in use
  50. bool m_levelMeteringEnabled;
  51. pthread_mutex_t m_mutex;
  52. pthread_mutex_t m_bufferInUseMutex;
  53. pthread_cond_t m_bufferFreeCondition;
  54. public:
  55. OSStatus m_lastError;
  56. AudioStreamBasicDescription m_streamDesc;
  57. float m_initialOutputVolume;
  58. private:
  59. void cleanup();
  60. void setCookiesForStream(AudioFileStreamID inAudioFileStream);
  61. void setState(State state);
  62. void enqueueBuffer();
  63. static void audioQueueOutputCallback(void *inClientData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer);
  64. static void audioQueueIsRunningCallback(void *inClientData, AudioQueueRef inAQ, AudioQueuePropertyID inID);
  65. };
  66. class Audio_Queue_Delegate {
  67. public:
  68. virtual void audioQueueStateChanged(Audio_Queue::State state) = 0;
  69. virtual void audioQueueBuffersEmpty() = 0;
  70. virtual void audioQueueInitializationFailed() = 0;
  71. virtual void audioQueueFinishedPlayingPacket() = 0;
  72. };
  73. } // namespace astreamer
  74. #endif // ASTREAMER_AUDIO_QUEUE_H