func_jitterbuffer.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include "asterisk/module.h"
  31. #include "asterisk/channel.h"
  32. #include "asterisk/framehook.h"
  33. #include "asterisk/frame.h"
  34. #include "asterisk/pbx.h"
  35. #include "asterisk/abstract_jb.h"
  36. #include "asterisk/timing.h"
  37. #include "asterisk/app.h"
  38. /*** DOCUMENTATION
  39. <function name="JITTERBUFFER" language="en_US">
  40. <synopsis>
  41. 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.
  42. </synopsis>
  43. <syntax>
  44. <parameter name="jitterbuffer type" required="true">
  45. <optionlist>
  46. <option name="fixed">
  47. <para>Set a fixed jitterbuffer on the channel.</para>
  48. </option>
  49. <option name="adaptive">
  50. <para>Set an adaptive jitterbuffer on the channel.</para>
  51. </option>
  52. <option name="disabled">
  53. <para>Remove a previously set jitterbuffer from the channel.</para>
  54. </option>
  55. </optionlist>
  56. </parameter>
  57. </syntax>
  58. <description>
  59. <para>Jitterbuffers are constructed in two different ways.
  60. The first always take four arguments: <replaceable>max_size</replaceable>,
  61. <replaceable>resync_threshold</replaceable>, <replaceable>target_extra</replaceable>,
  62. and <replaceable>sync_video</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. <para>sync_video: This option enables video synchronization with the audio stream. It can be
  76. turned on and off. Defaults to off.</para>
  77. <example title="Fixed with defaults" language="text">
  78. exten => 1,1,Set(JITTERBUFFER(fixed)=default)
  79. </example>
  80. <example title="Fixed with 200ms max size" language="text">
  81. exten => 1,1,Set(JITTERBUFFER(fixed)=200)
  82. </example>
  83. <example title="Fixed with 200ms max size and video sync support" language="text">
  84. exten => 1,1,Set(JITTERBUFFER(fixed)=200,,,yes)
  85. </example>
  86. <example title="Fixed with 200ms max size, resync threshold 1500" language="text">
  87. exten => 1,1,Set(JITTERBUFFER(fixed)=200,1500)
  88. </example>
  89. <example title="Adaptive with defaults" language="text">
  90. exten => 1,1,Set(JITTERBUFFER(adaptive)=default)
  91. </example>
  92. <example title="Adaptive with 200ms max size, 60ms target extra" language="text">
  93. exten => 1,1,Set(JITTERBUFFER(adaptive)=200,,60)
  94. </example>
  95. <example title="Adaptive with 200ms max size and video sync support" language="text">
  96. exten => 1,1,Set(JITTERBUFFER(adaptive)=200,,,yes)
  97. </example>
  98. <example title="Set a fixed jitterbuffer with defaults; then remove it" language="text">
  99. exten => 1,1,Set(JITTERBUFFER(fixed)=default)
  100. exten => 1,n,Set(JITTERBUFFER(disabled)=)
  101. </example>
  102. <note><para>If a channel specifies a jitterbuffer due to channel driver configuration and
  103. the JITTERBUFFER function has set a jitterbuffer for that channel, the jitterbuffer set by
  104. the JITTERBUFFER function will take priority and the jitterbuffer set by the channel
  105. configuration will not be applied.</para></note>
  106. </description>
  107. </function>
  108. ***/
  109. static int jb_helper(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  110. {
  111. struct ast_jb_conf jb_conf;
  112. if (!chan) {
  113. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  114. return -1;
  115. }
  116. /* Initialize and set jb_conf */
  117. ast_jb_conf_default(&jb_conf);
  118. /* Now check user options to see if any of the defaults need to change. */
  119. if (!ast_strlen_zero(data)) {
  120. if (strcasecmp(data, "fixed") &&
  121. strcasecmp(data, "adaptive") &&
  122. strcasecmp(data, "disabled")) {
  123. ast_log(LOG_WARNING, "Unknown Jitterbuffer type %s. Failed to create jitterbuffer.\n", data);
  124. return -1;
  125. }
  126. ast_copy_string(jb_conf.impl, data, sizeof(jb_conf.impl));
  127. }
  128. if (!ast_strlen_zero(value) && strcasecmp(value, "default")) {
  129. char *parse = ast_strdupa(value);
  130. int res = 0;
  131. AST_DECLARE_APP_ARGS(args,
  132. AST_APP_ARG(max_size);
  133. AST_APP_ARG(resync_threshold);
  134. AST_APP_ARG(target_extra);
  135. AST_APP_ARG(sync_video);
  136. );
  137. AST_STANDARD_APP_ARGS(args, parse);
  138. if (!ast_strlen_zero(args.max_size)) {
  139. res |= ast_jb_read_conf(&jb_conf,
  140. "jbmaxsize",
  141. args.max_size);
  142. }
  143. if (!ast_strlen_zero(args.resync_threshold)) {
  144. res |= ast_jb_read_conf(&jb_conf,
  145. "jbresyncthreshold",
  146. args.resync_threshold);
  147. }
  148. if (!ast_strlen_zero(args.target_extra)) {
  149. res |= ast_jb_read_conf(&jb_conf,
  150. "jbtargetextra",
  151. args.target_extra);
  152. }
  153. if (!ast_strlen_zero(args.sync_video)) {
  154. res |= ast_jb_read_conf(&jb_conf,
  155. "jbsyncvideo",
  156. args.sync_video);
  157. }
  158. if (res) {
  159. ast_log(LOG_WARNING, "Invalid jitterbuffer parameters %s\n", value);
  160. }
  161. }
  162. ast_jb_create_framehook(chan, &jb_conf, 0);
  163. return 0;
  164. }
  165. static struct ast_custom_function jb_function = {
  166. .name = "JITTERBUFFER",
  167. .write = jb_helper,
  168. };
  169. static int unload_module(void)
  170. {
  171. return ast_custom_function_unregister(&jb_function);
  172. }
  173. static int load_module(void)
  174. {
  175. int res = ast_custom_function_register(&jb_function);
  176. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  177. }
  178. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Jitter buffer for read side of channel.");