chan_bridge_media.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013 Digium, Inc.
  5. *
  6. * Jonathan Rose <jrose@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. /*!
  19. * \file
  20. * \brief Bridge Media Channels driver
  21. *
  22. * \author Jonathan Rose <jrose@digium.com>
  23. * \author Richard Mudgett <rmudgett@digium.com>
  24. *
  25. * \brief Bridge Media Channels
  26. *
  27. * \ingroup channel_drivers
  28. */
  29. /*** MODULEINFO
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include "asterisk/channel.h"
  35. #include "asterisk/bridge.h"
  36. #include "asterisk/core_unreal.h"
  37. #include "asterisk/module.h"
  38. static int media_call(struct ast_channel *chan, const char *addr, int timeout)
  39. {
  40. /* ast_call() will fail unconditionally against channels provided by this driver */
  41. return -1;
  42. }
  43. static int media_hangup(struct ast_channel *ast)
  44. {
  45. struct ast_unreal_pvt *p = ast_channel_tech_pvt(ast);
  46. int res;
  47. if (!p) {
  48. return -1;
  49. }
  50. /* Give the pvt a ref to fulfill calling requirements. */
  51. ao2_ref(p, +1);
  52. res = ast_unreal_hangup(p, ast);
  53. ao2_ref(p, -1);
  54. return res;
  55. }
  56. static struct ast_channel *announce_request(const char *type, struct ast_format_cap *cap,
  57. const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
  58. static struct ast_channel *record_request(const char *type, struct ast_format_cap *cap,
  59. const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
  60. static struct ast_channel_tech announce_tech = {
  61. .type = "Announcer",
  62. .description = "Bridge Media Announcing Channel Driver",
  63. .requester = announce_request,
  64. .call = media_call,
  65. .hangup = media_hangup,
  66. .send_digit_begin = ast_unreal_digit_begin,
  67. .send_digit_end = ast_unreal_digit_end,
  68. .read = ast_unreal_read,
  69. .write = ast_unreal_write,
  70. .write_video = ast_unreal_write,
  71. .exception = ast_unreal_read,
  72. .indicate = ast_unreal_indicate,
  73. .fixup = ast_unreal_fixup,
  74. .send_html = ast_unreal_sendhtml,
  75. .send_text = ast_unreal_sendtext,
  76. .queryoption = ast_unreal_queryoption,
  77. .setoption = ast_unreal_setoption,
  78. .properties = AST_CHAN_TP_INTERNAL,
  79. };
  80. static struct ast_channel_tech record_tech = {
  81. .type = "Recorder",
  82. .description = "Bridge Media Recording Channel Driver",
  83. .requester = record_request,
  84. .call = media_call,
  85. .hangup = media_hangup,
  86. .send_digit_begin = ast_unreal_digit_begin,
  87. .send_digit_end = ast_unreal_digit_end,
  88. .read = ast_unreal_read,
  89. .write = ast_unreal_write,
  90. .write_video = ast_unreal_write,
  91. .exception = ast_unreal_read,
  92. .indicate = ast_unreal_indicate,
  93. .fixup = ast_unreal_fixup,
  94. .send_html = ast_unreal_sendhtml,
  95. .send_text = ast_unreal_sendtext,
  96. .queryoption = ast_unreal_queryoption,
  97. .setoption = ast_unreal_setoption,
  98. .properties = AST_CHAN_TP_INTERNAL,
  99. };
  100. static struct ast_channel *media_request_helper(struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids,
  101. const struct ast_channel *requestor, const char *data, struct ast_channel_tech *tech, const char *role)
  102. {
  103. struct ast_channel *chan;
  104. RAII_VAR(struct ast_callid *, callid, NULL, ast_callid_cleanup);
  105. RAII_VAR(struct ast_unreal_pvt *, pvt, NULL, ao2_cleanup);
  106. if (!(pvt = ast_unreal_alloc(sizeof(*pvt), ast_unreal_destructor, cap))) {
  107. return NULL;
  108. }
  109. ast_copy_string(pvt->name, data, sizeof(pvt->name));
  110. ast_set_flag(pvt, AST_UNREAL_NO_OPTIMIZATION);
  111. callid = ast_read_threadstorage_callid();
  112. chan = ast_unreal_new_channels(pvt, tech,
  113. AST_STATE_UP, AST_STATE_UP, NULL, NULL, assignedids, requestor, callid);
  114. if (!chan) {
  115. return NULL;
  116. }
  117. ast_answer(pvt->owner);
  118. ast_answer(pvt->chan);
  119. if (ast_channel_add_bridge_role(pvt->chan, role)) {
  120. ast_hangup(chan);
  121. return NULL;
  122. }
  123. return chan;
  124. }
  125. static struct ast_channel *announce_request(const char *type, struct ast_format_cap *cap,
  126. const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
  127. {
  128. return media_request_helper(cap, assignedids, requestor, data, &announce_tech, "announcer");
  129. }
  130. static struct ast_channel *record_request(const char *type, struct ast_format_cap *cap,
  131. const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
  132. {
  133. return media_request_helper(cap, assignedids, requestor, data, &record_tech, "recorder");
  134. }
  135. static void cleanup_capabilities(void)
  136. {
  137. if (announce_tech.capabilities) {
  138. ao2_ref(announce_tech.capabilities, -1);
  139. announce_tech.capabilities = NULL;
  140. }
  141. if (record_tech.capabilities) {
  142. ao2_ref(record_tech.capabilities, -1);
  143. record_tech.capabilities = NULL;
  144. }
  145. }
  146. static int unload_module(void)
  147. {
  148. ast_channel_unregister(&announce_tech);
  149. ast_channel_unregister(&record_tech);
  150. cleanup_capabilities();
  151. return 0;
  152. }
  153. static int load_module(void)
  154. {
  155. announce_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  156. if (!announce_tech.capabilities) {
  157. return AST_MODULE_LOAD_DECLINE;
  158. }
  159. record_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  160. if (!record_tech.capabilities) {
  161. return AST_MODULE_LOAD_DECLINE;
  162. }
  163. ast_format_cap_append_by_type(announce_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
  164. ast_format_cap_append_by_type(record_tech.capabilities, AST_MEDIA_TYPE_UNKNOWN);
  165. if (ast_channel_register(&announce_tech)) {
  166. ast_log(LOG_ERROR, "Unable to register channel technology %s(%s).\n",
  167. announce_tech.type, announce_tech.description);
  168. cleanup_capabilities();
  169. return AST_MODULE_LOAD_DECLINE;
  170. }
  171. if (ast_channel_register(&record_tech)) {
  172. ast_log(LOG_ERROR, "Unable to register channel technology %s(%s).\n",
  173. record_tech.type, record_tech.description);
  174. cleanup_capabilities();
  175. return AST_MODULE_LOAD_DECLINE;
  176. }
  177. return AST_MODULE_LOAD_SUCCESS;
  178. }
  179. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Bridge Media Channel Driver",
  180. .support_level = AST_MODULE_SUPPORT_CORE,
  181. .load = load_module,
  182. .unload = unload_module,
  183. );