echo.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_ECHO_H__
  20. #define __PJMEDIA_ECHO_H__
  21. /**
  22. * @file echo.h
  23. * @brief Echo Cancellation API.
  24. */
  25. #include <pjmedia/types.h>
  26. /**
  27. * @defgroup PJMEDIA_Echo_Cancel Accoustic Echo Cancellation API
  28. * @ingroup PJMEDIA_PORT
  29. * @brief Echo Cancellation API.
  30. * @{
  31. *
  32. * This section describes API to perform echo cancellation to audio signal.
  33. * There may be multiple echo canceller implementation in PJMEDIA, ranging
  34. * from simple echo suppressor to a full Accoustic Echo Canceller/AEC. By
  35. * using this API, application should be able to use which EC backend to
  36. * use base on the requirement and capability of the platform.
  37. */
  38. PJ_BEGIN_DECL
  39. /**
  40. * Opaque type for PJMEDIA Echo Canceller state.
  41. */
  42. typedef struct pjmedia_echo_state pjmedia_echo_state;
  43. /**
  44. * Echo cancellation options.
  45. */
  46. typedef enum pjmedia_echo_flag
  47. {
  48. /**
  49. * Use any available backend echo canceller algorithm. This is
  50. * the default settings. You can only choose one backend.
  51. */
  52. PJMEDIA_ECHO_DEFAULT= 0,
  53. /**
  54. * Force to use Speex AEC as the backend echo canceller algorithm.
  55. * You can only choose one backend.
  56. */
  57. PJMEDIA_ECHO_SPEEX = 1,
  58. /**
  59. * If PJMEDIA_ECHO_SIMPLE flag is specified during echo canceller
  60. * creation, then a simple echo suppressor will be used instead of
  61. * an accoustic echo cancellation. You can only choose one backend.
  62. */
  63. PJMEDIA_ECHO_SIMPLE = 2,
  64. /**
  65. * Force to use WebRTC AEC as the backend echo canceller algorithm.
  66. * You can only choose one backend.
  67. */
  68. PJMEDIA_ECHO_WEBRTC = 3,
  69. /**
  70. * Force to use WebRTC AEC3 as the backend echo canceller algorithm.
  71. * You can only choose one backend.
  72. */
  73. PJMEDIA_ECHO_WEBRTC_AEC3 = 4,
  74. /**
  75. * For internal use.
  76. */
  77. PJMEDIA_ECHO_ALGO_MASK = 15,
  78. /**
  79. * If PJMEDIA_ECHO_NO_LOCK flag is specified, no mutex will be created
  80. * for the echo canceller, but application will guarantee that echo
  81. * canceller will not be called by different threads at the same time.
  82. */
  83. PJMEDIA_ECHO_NO_LOCK = 16,
  84. /**
  85. * If PJMEDIA_ECHO_USE_SIMPLE_FIFO flag is specified, the delay buffer
  86. * created for the echo canceller will use simple FIFO mechanism, i.e.
  87. * without using WSOLA to expand and shrink audio samples.
  88. */
  89. PJMEDIA_ECHO_USE_SIMPLE_FIFO = 32,
  90. /**
  91. * If PJMEDIA_ECHO_USE_SW_ECHO flag is specified, software echo canceller
  92. * will be used instead of device EC.
  93. */
  94. PJMEDIA_ECHO_USE_SW_ECHO = 64,
  95. /**
  96. * If PJMEDIA_ECHO_USE_NOISE_SUPPRESSOR flag is specified, the echo
  97. * canceller will also apply noise suppressor method to reduce noise.
  98. */
  99. PJMEDIA_ECHO_USE_NOISE_SUPPRESSOR = 128,
  100. /**
  101. * If PJMEDIA_ECHO_USE_GAIN_CONTROLLER flag is specified, the echo
  102. * canceller will also apply automatic gain control.
  103. */
  104. PJMEDIA_ECHO_USE_GAIN_CONTROLLER = 256,
  105. /**
  106. * Use default aggressiveness setting for the echo canceller algorithm.
  107. * This setting is mutually exclusive with the other aggressiveness
  108. * settings.
  109. */
  110. PJMEDIA_ECHO_AGGRESSIVENESS_DEFAULT = 0,
  111. /**
  112. * Use conservative aggressiveness setting for the echo canceller
  113. * algorithm. This setting is mutually exclusive with the other
  114. * aggressiveness settings.
  115. */
  116. PJMEDIA_ECHO_AGGRESSIVENESS_CONSERVATIVE = 0x1000,
  117. /**
  118. * Use moderate aggressiveness setting for the echo canceller algorithm.
  119. * This setting is mutually exclusive with the other aggressiveness
  120. * settings.
  121. */
  122. PJMEDIA_ECHO_AGGRESSIVENESS_MODERATE = 0x2000,
  123. /**
  124. * Use aggressive aggressiveness setting for the echo canceller
  125. * algorithm. This setting is mutually exclusive with the other
  126. * aggressiveness settings.
  127. */
  128. PJMEDIA_ECHO_AGGRESSIVENESS_AGGRESSIVE = 0x3000,
  129. /**
  130. * For internal use.
  131. */
  132. PJMEDIA_ECHO_AGGRESSIVENESS_MASK = 0xF000
  133. } pjmedia_echo_flag;
  134. /** Statistic not specified. */
  135. #define PJMEDIA_ECHO_STAT_NOT_SPECIFIED 999999
  136. /**
  137. * Echo cancellation statistics.
  138. */
  139. typedef struct pjmedia_echo_stat
  140. {
  141. /**
  142. * The name of the EC backend.
  143. * NULL if not specified.
  144. */
  145. const char *name;
  146. /**
  147. * Echo delay value (in ms).
  148. * PJMEDIA_ECHO_STAT_NOT_SPECIFIED if unavailable.
  149. */
  150. int delay;
  151. /**
  152. * Echo return loss.
  153. * PJMEDIA_ECHO_STAT_NOT_SPECIFIED if unavailable.
  154. */
  155. double return_loss;
  156. /**
  157. * Echo return loss enhancement.
  158. * PJMEDIA_ECHO_STAT_NOT_SPECIFIED if unavailable.
  159. */
  160. double return_loss_enh;
  161. /**
  162. * Echo delay standard deviation (in ms).
  163. * PJMEDIA_ECHO_STAT_NOT_SPECIFIED if unavailable.
  164. */
  165. int std;
  166. /**
  167. * Fraction of poor delay. Value between 0 to 1. The closer to 1,
  168. * the poorer the EC quality.
  169. * PJMEDIA_ECHO_STAT_NOT_SPECIFIED if unavailable.
  170. */
  171. float frac_delay;
  172. /**
  173. * Learning still in progress? PJ_TRUE if yes, false if done.
  174. * PJMEDIA_ECHO_STAT_NOT_SPECIFIED if unavailable.
  175. */
  176. unsigned learning;
  177. /**
  178. * Learning duration (in ms).
  179. * PJMEDIA_ECHO_STAT_NOT_SPECIFIED if unavailable.
  180. */
  181. unsigned duration;
  182. /**
  183. * Detected echo tail length (in ms).
  184. * PJMEDIA_ECHO_STAT_NOT_SPECIFIED if unavailable.
  185. */
  186. unsigned tail;
  187. /**
  188. * Minimum scaling factor (in ms).
  189. * PJMEDIA_ECHO_STAT_NOT_SPECIFIED if unavailable.
  190. */
  191. int min_factor;
  192. /**
  193. * Average scaling factor (in ms).
  194. * PJMEDIA_ECHO_STAT_NOT_SPECIFIED if unavailable.
  195. */
  196. int avg_factor;
  197. /**
  198. * Text describing the statistic.
  199. */
  200. pj_str_t stat_info;
  201. /**
  202. * Internal buffer.
  203. */
  204. char buf_[128];
  205. } pjmedia_echo_stat;
  206. /**
  207. * Initialize Echo cancellation stat.
  208. *
  209. * @param stat The statistic to be initialized.
  210. */
  211. PJ_DECL(void) pjmedia_echo_stat_default(pjmedia_echo_stat *stat);
  212. /**
  213. * Create the echo canceller.
  214. *
  215. * @param pool Pool to allocate memory.
  216. * @param clock_rate Media clock rate/sampling rate.
  217. * @param samples_per_frame Number of samples per frame.
  218. * @param tail_ms Tail length, miliseconds.
  219. * @param latency_ms Total lacency introduced by playback and
  220. * recording device. Set to zero if the latency
  221. * is not known.
  222. * @param options Options. If PJMEDIA_ECHO_SIMPLE is specified,
  223. * then a simple echo suppressor implementation
  224. * will be used instead of an accoustic echo
  225. * cancellation.
  226. * See #pjmedia_echo_flag for other options.
  227. * @param p_echo Pointer to receive the Echo Canceller state.
  228. *
  229. * @return PJ_SUCCESS on success, or the appropriate status.
  230. */
  231. PJ_DECL(pj_status_t) pjmedia_echo_create(pj_pool_t *pool,
  232. unsigned clock_rate,
  233. unsigned samples_per_frame,
  234. unsigned tail_ms,
  235. unsigned latency_ms,
  236. unsigned options,
  237. pjmedia_echo_state **p_echo );
  238. /**
  239. * Create multi-channel the echo canceller.
  240. *
  241. * @param pool Pool to allocate memory.
  242. * @param clock_rate Media clock rate/sampling rate.
  243. * @param channel_count Number of channels.
  244. * @param samples_per_frame Number of samples per frame.
  245. * @param tail_ms Tail length, miliseconds.
  246. * @param latency_ms Total lacency introduced by playback and
  247. * recording device. Set to zero if the latency
  248. * is not known.
  249. * @param options Options. If PJMEDIA_ECHO_SIMPLE is specified,
  250. * then a simple echo suppressor implementation
  251. * will be used instead of an accoustic echo
  252. * cancellation.
  253. * See #pjmedia_echo_flag for other options.
  254. * @param p_echo Pointer to receive the Echo Canceller state.
  255. *
  256. * @return PJ_SUCCESS on success, or the appropriate status.
  257. */
  258. PJ_DECL(pj_status_t) pjmedia_echo_create2(pj_pool_t *pool,
  259. unsigned clock_rate,
  260. unsigned channel_count,
  261. unsigned samples_per_frame,
  262. unsigned tail_ms,
  263. unsigned latency_ms,
  264. unsigned options,
  265. pjmedia_echo_state **p_echo );
  266. /**
  267. * Destroy the Echo Canceller.
  268. *
  269. * @param echo The Echo Canceller.
  270. *
  271. * @return PJ_SUCCESS on success.
  272. */
  273. PJ_DECL(pj_status_t) pjmedia_echo_destroy(pjmedia_echo_state *echo );
  274. /**
  275. * Reset the echo canceller.
  276. *
  277. * @param echo The Echo Canceller.
  278. *
  279. * @return PJ_SUCCESS on success.
  280. */
  281. PJ_DECL(pj_status_t) pjmedia_echo_reset(pjmedia_echo_state *echo );
  282. /**
  283. * Get the echo canceller statistics.
  284. *
  285. * @param echo The Echo Canceller.
  286. * @param p_stat Pointer to receive the stat.
  287. *
  288. * @return PJ_SUCCESS on success.
  289. */
  290. PJ_DECL(pj_status_t) pjmedia_echo_get_stat(pjmedia_echo_state *echo,
  291. pjmedia_echo_stat *p_stat);
  292. /**
  293. * Let the Echo Canceller know that a frame has been played to the speaker.
  294. * The Echo Canceller will keep the frame in its internal buffer, to be used
  295. * when cancelling the echo with #pjmedia_echo_capture().
  296. *
  297. * @param echo The Echo Canceller.
  298. * @param play_frm Sample buffer containing frame to be played
  299. * (or has been played) to the playback device.
  300. * The frame must contain exactly samples_per_frame
  301. * number of samples.
  302. *
  303. * @return PJ_SUCCESS on success.
  304. */
  305. PJ_DECL(pj_status_t) pjmedia_echo_playback(pjmedia_echo_state *echo,
  306. pj_int16_t *play_frm );
  307. /**
  308. * Let the Echo Canceller know that a frame has been captured from the
  309. * microphone. The Echo Canceller will cancel the echo from the captured
  310. * signal, using the internal buffer (supplied by #pjmedia_echo_playback())
  311. * as the FES (Far End Speech) reference.
  312. *
  313. * @param echo The Echo Canceller.
  314. * @param rec_frm On input, it contains the input signal (captured
  315. * from microphone) which echo is to be removed.
  316. * Upon returning this function, this buffer contain
  317. * the processed signal with the echo removed.
  318. * The frame must contain exactly samples_per_frame
  319. * number of samples.
  320. * @param options Echo cancellation options, reserved for future use.
  321. * Put zero for now.
  322. *
  323. * @return PJ_SUCCESS on success.
  324. */
  325. PJ_DECL(pj_status_t) pjmedia_echo_capture(pjmedia_echo_state *echo,
  326. pj_int16_t *rec_frm,
  327. unsigned options );
  328. /**
  329. * Perform echo cancellation.
  330. *
  331. * @param echo The Echo Canceller.
  332. * @param rec_frm On input, it contains the input signal (captured
  333. * from microphone) which echo is to be removed.
  334. * Upon returning this function, this buffer contain
  335. * the processed signal with the echo removed.
  336. * @param play_frm Sample buffer containing frame to be played
  337. * (or has been played) to the playback device.
  338. * The frame must contain exactly samples_per_frame
  339. * number of samples.
  340. * @param options Echo cancellation options, reserved for future use.
  341. * Put zero for now.
  342. * @param reserved Reserved for future use, put NULL for now.
  343. *
  344. * @return PJ_SUCCESS on success.
  345. */
  346. PJ_DECL(pj_status_t) pjmedia_echo_cancel( pjmedia_echo_state *echo,
  347. pj_int16_t *rec_frm,
  348. const pj_int16_t *play_frm,
  349. unsigned options,
  350. void *reserved );
  351. PJ_END_DECL
  352. /**
  353. * @}
  354. */
  355. #endif /* __PJMEDIA_ECHO_H__ */