audiomicspkpulse.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #pragma once
  2. #include <pulse/sample.h>
  3. #include <pulse/pulseaudio.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #define AMS_OPT_RECORD 0x01
  8. #define AMS_OPT_PLAY 0x02
  9. #define AMS_OPT_RECPLAY 0x03
  10. #define AMS_OPT_AS_STREAM 0x00
  11. #define AMS_OPT_AS_ENGINE 0x04
  12. #include "precompile.h"
  13. #include "audiostream.h"
  14. #include <stdbool.h>
  15. #ifndef MAX_PATH_EX
  16. #define MAX_PATH_EX 512
  17. #endif
  18. #ifndef MAX_PATH
  19. #define MAX_PATH 260
  20. #endif
  21. #ifndef RVC_DELAY_AUDIO_LEN
  22. #define RVC_DELAY_AUDIO_LEN 160
  23. #endif
  24. #ifndef NSEC_PER_SEC
  25. #define NSEC_PER_SEC 1000000000LL
  26. #endif
  27. #define pa_memzero(x,l) (memset((x), 0, (l)))
  28. #define pa_zero(x) (pa_memzero(&(x), sizeof(x)))
  29. /*internally is always float*/
  30. typedef int16_t sample_t;
  31. typedef struct audio_data_callback_s
  32. {
  33. void* input;
  34. size_t inputlen;
  35. unsigned long frameCount;
  36. size_t usamprate;
  37. }audio_data_callback_t;
  38. typedef struct audio_device_s
  39. {
  40. int id; /*audo device id*/
  41. int channels; /*max channels*/
  42. int samprate; /*default samplerate*/
  43. double low_latency; /*default low latency*/
  44. double high_latency; /*default high latency*/
  45. char name[MAX_PATH_EX]; /*device name*/
  46. char description[MAX_PATH]; /*device description*/
  47. } audio_device_t;
  48. typedef struct audio_context_s
  49. {
  50. void* micspkpulse_parent;
  51. bool bstart_put_flag;
  52. bool bstart_get_flag;
  53. int num_input_dev; /*number of audio input devices in list*/
  54. audio_device_t* list_input_devices; /*audio input devices list*/
  55. int num_output_dev;
  56. audio_device_t* list_output_devices; /*audio output devices list*/
  57. int device; /*current device list index*/
  58. int channels; /*channels*/
  59. int samprate; /*sample rate*/
  60. double latency; /*current sugested latency*/
  61. pa_sample_format_t eformat;
  62. int play_device; /*current device list index*/
  63. int play_channels; /*channels*/
  64. int play_samprate; /*sample rate*/
  65. double play_latency; /*current sugested latency*/
  66. pa_sample_format_t play_eformat;
  67. /*all ts are monotonic based: both real and generated*/
  68. int64_t current_ts; /*current buffer generated timestamp*/
  69. int64_t last_ts; /*last real timestamp (in nanosec)*/
  70. int64_t snd_begintime; /*sound capture start ref time*/
  71. int64_t ts_drift; /*drift between real and generated ts*/
  72. volatile int stream_flag; /*stream flag*/
  73. volatile int play_stream_flag; /*stream flag*/
  74. pthread_t readthreadid;
  75. pthread_t writethreadid;
  76. char* paudio_buffer;
  77. size_t uaudio_len;
  78. char* paudio_in;
  79. size_t uaudio_inlen;
  80. }audio_context_t;
  81. typedef struct audiomicspkpulse_s
  82. {
  83. audiostream_t base;
  84. int opt;
  85. int rec_dev_id;
  86. int ply_dev_id;
  87. int play_frame_samples;
  88. int capture_frame_samples;
  89. void* ply_dbuf;
  90. short* ply_buf;
  91. unsigned ply_buf_cnt;
  92. void* rec_buf;
  93. void* rec_dbuf;
  94. unsigned long rec_buf_cnt;
  95. apr_thread_t* audio_work_thread;
  96. sem_t* audio_device_started_sem;
  97. bool baudio_device_started_flag;
  98. int dev_type;
  99. int (*on_rx_audio)(char* frame, void* user_data);
  100. int (*on_tx_audio)(void* audiodata, void* user_data);
  101. int (*on_audio_ns)(void* pdst, size_t udstlen, void* psrc, size_t usrclen, void* user_data);
  102. int (*on_audio_play_ns)(void* pdst, size_t udstlen, void* psrc, size_t usrclen, void* user_data);
  103. int (*on_audio_playing)(void* pdata, size_t ulen, void* user_data);
  104. void (*on_audio_device_event)(bool bopen, int iret, bool bmicro, int idev, const char* strmessage, void* user_data);
  105. void* user_data;
  106. audio_context_t* audio_ctx;
  107. }audiomicspkpulse_t;
  108. typedef void (*lpfn_audio_device_event)(bool bopen, int iret, bool bmicro, int idev, const char* strmessage, void* user_data);
  109. apr_status_t audiomicspkpulse_create(apr_pool_t* pool,
  110. audioengine_t* engine,
  111. int opt,
  112. int clock,
  113. const char* rec_dev_key,
  114. const char* ply_dev_key,
  115. int idev_type,
  116. lpfn_audio_device_event lpevent,
  117. audiomicspkpulse_t** p_micspk);
  118. void audiomicspkpulse_destroy(audiomicspkpulse_t* micspk);
  119. #ifdef __cplusplus
  120. } // extern "C" {
  121. #endif