AudioStream.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #pragma once
  2. #include <atomic>
  3. #include <string>
  4. #include <vector>
  5. extern "C" {
  6. #include <libavcodec/avcodec.h>
  7. #include <libavformat/avformat.h>
  8. #include <libavutil/fifo.h>
  9. #include <libavutil/opt.h>
  10. #include <libswresample/swresample.h>
  11. #include <libswscale/swscale.h>
  12. }
  13. #include <SDL.h>
  14. #include <SDL_thread.h>
  15. #include "WallpaperEngine/Audio/AudioContext.h"
  16. #ifndef FF_API_FIFO_OLD_API
  17. #define FF_API_FIFO_OLD_API (LIBAVUTIL_VERSION_MAJOR < 59)
  18. #endif
  19. #ifndef FF_API_OLD_CHANNEL_LAYOUT
  20. #define FF_API_OLD_CHANNEL_LAYOUT (LIBAVUTIL_VERSION_MAJOR < 59)
  21. #endif
  22. #define MAX_QUEUE_SIZE (5 * 1024 * 1024)
  23. #define MIN_FRAMES (25)
  24. #define NO_AUDIO_STREAM (-1)
  25. namespace WallpaperEngine::Audio {
  26. class AudioContext;
  27. using namespace WallpaperEngine::FileSystem;
  28. class AudioStream {
  29. public:
  30. AudioStream (AudioContext& context, const std::string& filename);
  31. // repeat is set before the reader starts, a short file could be read to its end before a setRepeat () call
  32. AudioStream (AudioContext& context, const ReadStreamSharedPtr& buffer, bool repeat = false);
  33. AudioStream (AudioContext& audioContext, AVCodecContext* context);
  34. ~AudioStream ();
  35. void queuePacket (AVPacket* pkt);
  36. enum class Dequeued { Packet, Empty, Finished };
  37. // next packet into m_decodePacket without waiting; Empty if nothing is queued yet, Finished once nothing will be
  38. Dequeued dequeuePacket ();
  39. // from the reader thread when a repeating stream starts over
  40. void queueLoopMarker ();
  41. // from the reader thread once it won't queue more packets
  42. void markReaderFinished ();
  43. [[nodiscard]] AudioContext& getAudioContext () const;
  44. [[nodiscard]] AVCodecContext* getContext () const;
  45. [[nodiscard]] AVFormatContext* getFormatContext () const;
  46. [[nodiscard]] int getAudioStream () const;
  47. [[nodiscard]] bool isInitialized () const;
  48. // seconds, 0 if the container doesn't say
  49. [[nodiscard]] double getDuration () const;
  50. // before resampling to the driver's layout
  51. [[nodiscard]] int getSourceChannels () const;
  52. void setRepeat (bool newRepeat = true);
  53. [[nodiscard]] bool isRepeat () const;
  54. void stop ();
  55. [[nodiscard]] ReadStreamSharedPtr& getBuffer ();
  56. [[nodiscard]] SDL_cond* getWaitCondition () const;
  57. [[nodiscard]] size_t getQueueSize () const;
  58. [[nodiscard]] int getQueuePacketCount () const;
  59. // seconds
  60. [[nodiscard]] int64_t getQueueDuration () const;
  61. [[nodiscard]] AVRational getTimeBase () const;
  62. // plays at the driver's rate like an OpenAL Soft 1.21.1 source (WE hands files to OpenAL as is, sub_18000D530):
  63. // linear interpolation, 12 bit fixed point step, last frame held past the end returns frames written, 0 if nothing
  64. // is decoded yet, < 0 once the voice is over
  65. int read (float* out, int frames);
  66. // how OpenAL splits the next update into mixing passes (Voice::mix), at most BufferLineSize source frames each
  67. [[nodiscard]] std::vector<int> passes (int frames) const;
  68. /** Whether the voice played past the file's end, OpenAL then stops it after the current update */
  69. [[nodiscard]] bool ended () const;
  70. // a stopping voice's last update without buffers: up to 24 upcoming frames, cut per channel before the one closest
  71. // to zero and held (voice.cpp)
  72. void stopTail (float* out, int frames);
  73. private:
  74. void loadCustomContent (const char* filename = nullptr);
  75. /** Converts the decoded frame to float in the driver's channel layout at the file's rate, onto m_source */
  76. bool convertFrame ();
  77. /** Converts what the converter still holds once the decoder is drained */
  78. void flushConverter ();
  79. // decodes the next frame onto m_source without waiting: 1 added, 0 nothing queued, -1 end or error
  80. int decodeSource ();
  81. /** Makes sure m_source holds this many frames past the read position, unless the file ends first */
  82. void fillSource (size_t frames);
  83. [[nodiscard]] const float* sourceFrame (size_t frame) const;
  84. bool doQueue (AVPacket* pkt);
  85. void initialize ();
  86. SwrContext* m_swrctx = nullptr;
  87. AudioContext& m_audioContext;
  88. bool m_initialized = false;
  89. bool m_repeat = false;
  90. /** Set once the reader thread has exited, so nothing waits for packets that will never come */
  91. std::atomic<bool> m_readerFinished = false;
  92. AVCodecContext* m_context = nullptr;
  93. AVFormatContext* m_formatContext = nullptr;
  94. int m_audioStream = NO_AUDIO_STREAM;
  95. ReadStreamSharedPtr m_buffer = nullptr;
  96. struct MyAVPacketList {
  97. AVPacket* packet;
  98. };
  99. /** decoded frames at the file's rate, interleaved; m_sourcePos and m_sourceFrac are the voice position in it */
  100. std::vector<float> m_source;
  101. int m_channels = 0;
  102. size_t m_sourcePos = 0;
  103. uint32_t m_sourceFrac = 0;
  104. uint32_t m_step = 0;
  105. bool m_sourceEnded = false;
  106. bool m_ended = false;
  107. AVPacket* m_decodePacket = nullptr;
  108. AVFrame* m_decodeFrame = nullptr;
  109. bool m_loopDrain = false;
  110. struct PacketQueue {
  111. #if FF_API_FIFO_OLD_API
  112. AVFifoBuffer* packetList = nullptr;
  113. #else
  114. AVFifo* packetList = nullptr;
  115. #endif
  116. int nb_packets = 0;
  117. size_t size = 0;
  118. int64_t duration = 0;
  119. SDL_mutex* mutex = nullptr;
  120. SDL_cond* wait = nullptr;
  121. }* m_queue {};
  122. SDL_Thread* m_audioThread = nullptr;
  123. };
  124. } // namespace WallpaperEngine::Audio