record_audio_wasapi.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef RECORD_AUDIO_WASAPI
  2. #define RECORD_AUDIO_WASAPI
  3. #include "record_audio.h"
  4. #ifdef _WIN32
  5. #include "headers_mmdevice.h"
  6. #endif // _WIN32
  7. namespace am {
  8. class record_audio_wasapi : public record_audio
  9. {
  10. public:
  11. record_audio_wasapi();
  12. ~record_audio_wasapi();
  13. virtual int init(const std::string &device_name, const std::string &device_id, bool is_input);
  14. virtual int start();
  15. virtual int pause();
  16. virtual int resume();
  17. virtual int stop();
  18. virtual const AVRational get_time_base();
  19. virtual int64_t get_start_time();
  20. private:
  21. int64_t convert_layout(DWORD layout, WORD channels);
  22. void init_format(WAVEFORMATEX *wfex);
  23. int init_render();
  24. void render_loop();
  25. void process_data(AVFrame *frame, uint8_t *data, uint32_t sample_count, uint64_t device_ts);
  26. int do_record(AVFrame *frame);
  27. void record_loop();
  28. void clean_wasapi();
  29. private:
  30. WAVEFORMATEX *_wfex;
  31. IMMDeviceEnumerator *_enumerator;
  32. IMMDevice *_device;
  33. IAudioClient *_capture_client;
  34. IAudioCaptureClient *_capture;
  35. IAudioRenderClient *_render;
  36. IAudioClient *_render_client;
  37. std::thread _render_thread;
  38. uint32_t _capture_sample_count;
  39. uint32_t _render_sample_count;
  40. HANDLE _ready_event;
  41. HANDLE _stop_event;
  42. HANDLE _render_event;
  43. bool _co_inited;
  44. bool _is_default;
  45. bool _use_device_ts;
  46. //define time stamps here
  47. int64_t _start_time;
  48. };
  49. } // namespace am
  50. #endif // !RECORD_AUDIO_WASAPI