CAudioStream.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include <string>
  3. extern "C"
  4. {
  5. #include <libavutil/fifo.h>
  6. #include <libavcodec/avcodec.h>
  7. #include <libavformat/avformat.h>
  8. #include <libswscale/swscale.h>
  9. #include <libswresample/swresample.h>
  10. #include <libavutil/opt.h>
  11. };
  12. #include <SDL.h>
  13. #include <SDL_thread.h>
  14. #define SDL_AUDIO_BUFFER_SIZE 1024
  15. #define MAX_AUDIO_FRAME_SIZE 192000
  16. namespace WallpaperEngine
  17. {
  18. namespace Audio
  19. {
  20. class CAudioStream
  21. {
  22. public:
  23. CAudioStream (const std::string& filename);
  24. CAudioStream (const void* buffer, int length);
  25. CAudioStream (AVCodecContext* context);
  26. void queuePacket (AVPacket* pkt);
  27. /**
  28. * Gets the next packet in the queue
  29. *
  30. * WARNING: BLOCKS UNTIL SOME DATA IS READ FROM IT
  31. *
  32. * @return
  33. */
  34. void dequeuePacket (AVPacket* output);
  35. AVCodecContext* getContext ();
  36. AVFormatContext* getFormatContext ();
  37. int getAudioStream ();
  38. bool isInitialized ();
  39. void setRepeat (bool newRepeat = true);
  40. bool isRepeat ();
  41. void stop ();
  42. const void* getBuffer ();
  43. int getLength ();
  44. int getPosition ();
  45. void setPosition (int current);
  46. int decodeFrame (uint8_t* audioBuffer, int bufferSize);
  47. private:
  48. void loadCustomContent (const char* filename = nullptr);
  49. int resampleAudio (AVFrame * decoded_audio_frame, enum AVSampleFormat out_sample_fmt, int out_channels, int out_sample_rate, uint8_t* out_buf);
  50. bool doQueue (AVPacket* pkt);
  51. void initialize ();
  52. bool m_initialized;
  53. bool m_repeat;
  54. AVCodecContext* m_context = nullptr;
  55. AVFormatContext* m_formatContext = nullptr;
  56. int m_audioStream = -1;
  57. const void* m_buffer;
  58. int m_length;
  59. int m_position = 0;
  60. struct MyAVPacketList
  61. {
  62. AVPacket *packet;
  63. };
  64. struct PacketQueue
  65. {
  66. AVFifoBuffer* packetList;
  67. int nb_packets;
  68. int size;
  69. int64_t duration;
  70. SDL_mutex *mutex;
  71. SDL_cond *cond;
  72. } *m_queue;
  73. };
  74. }
  75. };