func_jitterbuffer.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011, Digium, Inc.
  5. *
  6. * David Vossel <dvossel@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Put a jitterbuffer on the read side of a channel
  21. *
  22. * \author David Vossel <dvossel@digium.com>
  23. *
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/framehook.h"
  34. #include "asterisk/frame.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/abstract_jb.h"
  37. #include "asterisk/timing.h"
  38. #include "asterisk/app.h"
  39. /*** DOCUMENTATION
  40. <function name="JITTERBUFFER" language="en_US">
  41. <synopsis>
  42. Add a Jitterbuffer to the Read side of the channel. This dejitters the audio stream before it reaches the Asterisk core. This is a write only function.
  43. </synopsis>
  44. <syntax>
  45. <parameter name="jitterbuffer type" required="true">
  46. <optionlist>
  47. <option name="fixed">
  48. <para>Set a fixed jitterbuffer on the channel.</para>
  49. </option>
  50. <option name="adaptive">
  51. <para>Set an adaptive jitterbuffer on the channel.</para>
  52. </option>
  53. <option name="disabled">
  54. <para>Remove a previously set jitterbuffer from the channel.</para>
  55. </option>
  56. </optionlist>
  57. </parameter>
  58. </syntax>
  59. <description>
  60. <para>Jitterbuffers are constructed in two different ways.
  61. The first always take three arguments: <replaceable>max_size</replaceable>,
  62. <replaceable>resync_threshold</replaceable>, and <replaceable>target_extra</replaceable>.
  63. Alternatively, a single argument of <literal>default</literal> can be provided,
  64. which will construct the default jitterbuffer for the given
  65. <replaceable>jitterbuffer type</replaceable>.</para>
  66. <para>The arguments are:</para>
  67. <para><replaceable>max_size</replaceable>: Length in milliseconds of the buffer.
  68. Defaults to 200 ms.</para>
  69. <para><replaceable>resync_threshold</replaceable>: The length in milliseconds over
  70. which a timestamp difference will result in resyncing the jitterbuffer.
  71. Defaults to 1000ms.</para>
  72. <para>target_extra: This option only affects the adaptive jitterbuffer. It represents
  73. the amount time in milliseconds by which the new jitter buffer will pad its size.
  74. Defaults to 40ms.</para>
  75. <example title="Fixed with defaults" language="text">
  76. exten => 1,1,Set(JITTERBUFFER(fixed)=default)
  77. </example>
  78. <example title="Fixed with 200ms max size" language="text">
  79. exten => 1,1,Set(JITTERBUFFER(fixed)=200)
  80. </example>
  81. <example title="Fixed with 200ms max size, resync threshold 1500" language="text">
  82. exten => 1,1,Set(JITTERBUFFER(fixed)=200,1500)
  83. </example>
  84. <example title="Adaptive with defaults" language="text">
  85. exten => 1,1,Set(JITTERBUFFER(adaptive)=default)
  86. </example>
  87. <example title="Adaptive with 200ms max size, 60ms target extra" language="text">
  88. exten => 1,1,Set(JITTERBUFFER(adaptive)=200,,60)
  89. </example>
  90. <example title="Set a fixed jitterbuffer with defaults; then remove it" language="text">
  91. exten => 1,1,Set(JITTERBUFFER(fixed)=default)
  92. exten => 1,n,Set(JITTERBUFFER(disabled)=)
  93. </example>
  94. <note><para>If a channel specifies a jitterbuffer due to channel driver configuration and
  95. the JITTERBUFFER function has set a jitterbuffer for that channel, the jitterbuffer set by
  96. the JITTERBUFFER function will take priority and the jitterbuffer set by the channel
  97. configuration will not be applied.</para></note>
  98. </description>
  99. </function>
  100. ***/
  101. static int jb_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  102. {
  103. struct ast_jb_conf jb_conf;
  104. if (!chan) {
  105. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  106. return -1;
  107. }
  108. /* Initialize and set jb_conf */
  109. ast_jb_conf_default(&jb_conf);
  110. /* Now check user options to see if any of the defaults need to change. */
  111. if (!ast_strlen_zero(data)) {
  112. if (strcasecmp(data, "fixed") &&
  113. strcasecmp(data, "adaptive") &&
  114. strcasecmp(data, "disabled")) {
  115. ast_log(LOG_WARNING, "Unknown Jitterbuffer type %s. Failed to create jitterbuffer.\n", data);
  116. return -1;
  117. }
  118. ast_copy_string(jb_conf.impl, data, sizeof(jb_conf.impl));
  119. }
  120. if (!ast_strlen_zero(value) && strcasecmp(value, "default")) {
  121. char *parse = ast_strdupa(value);
  122. int res = 0;
  123. AST_DECLARE_APP_ARGS(args,
  124. AST_APP_ARG(max_size);
  125. AST_APP_ARG(resync_threshold);
  126. AST_APP_ARG(target_extra);
  127. );
  128. AST_STANDARD_APP_ARGS(args, parse);
  129. if (!ast_strlen_zero(args.max_size)) {
  130. res |= ast_jb_read_conf(&jb_conf,
  131. "jbmaxsize",
  132. args.max_size);
  133. }
  134. if (!ast_strlen_zero(args.resync_threshold)) {
  135. res |= ast_jb_read_conf(&jb_conf,
  136. "jbresyncthreshold",
  137. args.resync_threshold);
  138. }
  139. if (!ast_strlen_zero(args.target_extra)) {
  140. res |= ast_jb_read_conf(&jb_conf,
  141. "jbtargetextra",
  142. args.target_extra);
  143. }
  144. if (res) {
  145. ast_log(LOG_WARNING, "Invalid jitterbuffer parameters %s\n", value);
  146. }
  147. }
  148. ast_jb_create_framehook(chan, &jb_conf, 0);
  149. return 0;
  150. }
  151. static struct ast_custom_function jb_function = {
  152. .name = "JITTERBUFFER",
  153. .write = jb_helper,
  154. };
  155. static int unload_module(void)
  156. {
  157. return ast_custom_function_unregister(&jb_function);
  158. }
  159. static int load_module(void)
  160. {
  161. int res = ast_custom_function_register(&jb_function);
  162. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  163. }
  164. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Jitter buffer for read side of channel.");