bridge_simple.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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 Simple two channel bridging module
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. *
  24. * \ingroup bridges
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include "asterisk/module.h"
  37. #include "asterisk/channel.h"
  38. #include "asterisk/bridging.h"
  39. #include "asterisk/bridging_technology.h"
  40. #include "asterisk/frame.h"
  41. static int simple_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  42. {
  43. struct ast_channel *c0 = AST_LIST_FIRST(&bridge->channels)->chan, *c1 = AST_LIST_LAST(&bridge->channels)->chan;
  44. /* If this is the first channel we can't make it compatible... unless we make it compatible with itself O.o */
  45. if (AST_LIST_FIRST(&bridge->channels) == AST_LIST_LAST(&bridge->channels)) {
  46. return 0;
  47. }
  48. /* See if we need to make these compatible */
  49. if ((ast_format_cmp(&c0->writeformat, &c1->readformat) == AST_FORMAT_CMP_EQUAL) &&
  50. (ast_format_cmp(&c0->readformat, &c1->writeformat) == AST_FORMAT_CMP_EQUAL) &&
  51. (ast_format_cap_identical(c0->nativeformats, c1->nativeformats))) {
  52. return 0;
  53. }
  54. /* BOOM! We do. */
  55. return ast_channel_make_compatible(c0, c1);
  56. }
  57. static enum ast_bridge_write_result simple_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  58. {
  59. struct ast_bridge_channel *other = NULL;
  60. /* If this is the only channel in this bridge then immediately exit */
  61. if (AST_LIST_FIRST(&bridge->channels) == AST_LIST_LAST(&bridge->channels)) {
  62. return AST_BRIDGE_WRITE_FAILED;
  63. }
  64. /* Find the channel we actually want to write to */
  65. if (!(other = (AST_LIST_FIRST(&bridge->channels) == bridge_channel ? AST_LIST_LAST(&bridge->channels) : AST_LIST_FIRST(&bridge->channels)))) {
  66. return AST_BRIDGE_WRITE_FAILED;
  67. }
  68. /* Write the frame out if they are in the waiting state... don't worry about freeing it, the bridging core will take care of it */
  69. if (other->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
  70. ast_write(other->chan, frame);
  71. }
  72. return AST_BRIDGE_WRITE_SUCCESS;
  73. }
  74. static struct ast_bridge_technology simple_bridge = {
  75. .name = "simple_bridge",
  76. .capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_THREAD,
  77. .preference = AST_BRIDGE_PREFERENCE_MEDIUM,
  78. .join = simple_bridge_join,
  79. .write = simple_bridge_write,
  80. };
  81. static int unload_module(void)
  82. {
  83. ast_format_cap_destroy(simple_bridge.format_capabilities);
  84. return ast_bridge_technology_unregister(&simple_bridge);
  85. }
  86. static int load_module(void)
  87. {
  88. if (!(simple_bridge.format_capabilities = ast_format_cap_alloc())) {
  89. return AST_MODULE_LOAD_DECLINE;
  90. }
  91. ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_AUDIO);
  92. ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_VIDEO);
  93. ast_format_cap_add_all_by_type(simple_bridge.format_capabilities, AST_FORMAT_TYPE_TEXT);
  94. return ast_bridge_technology_register(&simple_bridge);
  95. }
  96. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Simple two channel bridging module");