autochan.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2009, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@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 "smart" channels
  21. *
  22. * \author Mark Michelson <mmichelson@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/autochan.h"
  30. #include "asterisk/utils.h"
  31. #include "asterisk/linkedlists.h"
  32. #include "asterisk/options.h"
  33. #include "asterisk/channel.h"
  34. struct ast_autochan *ast_autochan_setup(struct ast_channel *chan)
  35. {
  36. struct ast_autochan *autochan;
  37. if (!chan) {
  38. return NULL;
  39. }
  40. if (!(autochan = ast_calloc(1, sizeof(*autochan)))) {
  41. return NULL;
  42. }
  43. autochan->chan = ast_channel_ref(chan);
  44. ast_channel_lock(autochan->chan);
  45. AST_LIST_INSERT_TAIL(ast_channel_autochans(autochan->chan), autochan, list);
  46. ast_channel_unlock(autochan->chan);
  47. ast_debug(1, "Created autochan %p to hold channel %s (%p)\n", autochan, ast_channel_name(chan), chan);
  48. return autochan;
  49. }
  50. void ast_autochan_destroy(struct ast_autochan *autochan)
  51. {
  52. struct ast_autochan *autochan_iter;
  53. ast_channel_lock(autochan->chan);
  54. AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_autochans(autochan->chan), autochan_iter, list) {
  55. if (autochan_iter == autochan) {
  56. AST_LIST_REMOVE_CURRENT(list);
  57. ast_debug(1, "Removed autochan %p from the list, about to free it\n", autochan);
  58. break;
  59. }
  60. }
  61. AST_LIST_TRAVERSE_SAFE_END;
  62. ast_channel_unlock(autochan->chan);
  63. autochan->chan = ast_channel_unref(autochan->chan);
  64. ast_free(autochan);
  65. }
  66. void ast_autochan_new_channel(struct ast_channel *old_chan, struct ast_channel *new_chan)
  67. {
  68. struct ast_autochan *autochan;
  69. AST_LIST_APPEND_LIST(ast_channel_autochans(new_chan), ast_channel_autochans(old_chan), list);
  70. AST_LIST_TRAVERSE(ast_channel_autochans(new_chan), autochan, list) {
  71. if (autochan->chan == old_chan) {
  72. autochan->chan = ast_channel_unref(old_chan);
  73. autochan->chan = ast_channel_ref(new_chan);
  74. ast_debug(1, "Autochan %p used to hold channel %s (%p) but now holds channel %s (%p)\n",
  75. autochan, ast_channel_name(old_chan), old_chan, ast_channel_name(new_chan), new_chan);
  76. }
  77. }
  78. }