record_audio_wasapi.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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,
  14. const std::string &device_id,
  15. bool is_input);
  16. virtual int start();
  17. virtual int pause();
  18. virtual int resume();
  19. virtual int stop();
  20. virtual const AVRational get_time_base();
  21. virtual int64_t get_start_time();
  22. private:
  23. int64_t convert_layout(DWORD layout, WORD channels);
  24. void init_format(WAVEFORMATEX *wfex);
  25. int init_render();
  26. void render_loop();
  27. void process_data(AVFrame *frame, uint8_t* data, uint32_t sample_count, uint64_t device_ts);
  28. int do_record(AVFrame *frame);
  29. void record_loop();
  30. void clean_wasapi();
  31. private:
  32. WAVEFORMATEX *_wfex;
  33. IMMDeviceEnumerator *_enumerator;
  34. IMMDevice *_device;
  35. IAudioClient *_capture_client;
  36. IAudioCaptureClient *_capture;
  37. IAudioRenderClient *_render;
  38. IAudioClient *_render_client;
  39. std::thread _render_thread;
  40. uint32_t _capture_sample_count;
  41. uint32_t _render_sample_count;
  42. HANDLE _ready_event;
  43. HANDLE _stop_event;
  44. HANDLE _render_event;
  45. bool _co_inited;
  46. bool _is_default;
  47. bool _use_device_ts;
  48. //define time stamps here
  49. int64_t _start_time;
  50. };
  51. }
  52. #endif // !RECORD_AUDIO_WASAPI