summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/app_voicemail.c39
-rw-r--r--bridges/bridge_native_rtp.c32
-rw-r--r--bridges/bridge_simple.c32
-rw-r--r--include/asterisk/bridge_channel.h2
-rw-r--r--include/asterisk/bridge_channel_internal.h11
-rw-r--r--include/asterisk/bridge_technology.h3
-rw-r--r--include/asterisk/res_pjsip_session.h11
-rw-r--r--main/bridge.c1
-rw-r--r--main/bridge_channel.c36
-rw-r--r--res/res_pjsip_pubsub.c8
-rw-r--r--res/res_pjsip_refer.c8
-rw-r--r--res/res_pjsip_session.c26
-rw-r--r--res/res_pjsip_session.exports.in1
-rw-r--r--tests/test_bridging.c292
14 files changed, 488 insertions, 14 deletions
diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index f62c7d8c9..0c0e2ba6a 100644
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -2732,7 +2732,7 @@ static int imap_store_file(const char *dir, const char *mailboxuser, const char
}
if (fread(buf, 1, len, p) != len) {
if (ferror(p)) {
- ast_log(LOG_ERROR, "Error while reading mail file: %s\n");
+ ast_log(LOG_ERROR, "Error while reading mail file: %s\n", tmp);
return -1;
}
}
@@ -13029,6 +13029,40 @@ static void mwi_sub_destroy(struct mwi_sub *mwi_sub)
ast_free(mwi_sub);
}
+#ifdef IMAP_STORAGE
+static void imap_logout(const char *mailbox_id)
+{
+ char *context;
+ char *mailbox;
+ struct ast_vm_user vmus;
+ RAII_VAR(struct ast_vm_user *, vmu, NULL, free_user);
+ struct vm_state *vms = NULL;
+
+ if (ast_strlen_zero(mailbox_id)
+ || separate_mailbox(ast_strdupa(mailbox_id), &mailbox, &context)) {
+ return;
+ }
+
+ memset(&vmus, 0, sizeof(vmus));
+
+ if (!(vmu = find_user(&vmus, context, mailbox)) || vmu->imapuser[0] == '\0') {
+ return;
+ }
+
+ vms = get_vm_state_by_imapuser(vmu->imapuser, 0);
+ if (!vms) {
+ vms = get_vm_state_by_mailbox(mailbox, context, 0);
+ }
+ if (!vms) {
+ return;
+ }
+
+ vms->mailstream = mail_close(vms->mailstream);
+ vmstate_delete(vms);
+}
+
+#endif
+
static int handle_unsubscribe(void *datap)
{
struct mwi_sub *mwi_sub;
@@ -13040,6 +13074,9 @@ static int handle_unsubscribe(void *datap)
AST_LIST_REMOVE_CURRENT(entry);
/* Don't break here since a duplicate uniqueid
* may have been added as a result of a cache dump. */
+#ifdef IMAP_STORAGE
+ imap_logout(mwi_sub->mailbox);
+#endif
mwi_sub_destroy(mwi_sub);
}
}
diff --git a/bridges/bridge_native_rtp.c b/bridges/bridge_native_rtp.c
index a80ef4c5a..78e35a16b 100644
--- a/bridges/bridge_native_rtp.c
+++ b/bridges/bridge_native_rtp.c
@@ -510,7 +510,37 @@ static void native_rtp_bridge_leave(struct ast_bridge *bridge, struct ast_bridge
static int native_rtp_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
{
- return ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
+ const struct ast_control_t38_parameters *t38_parameters;
+ int defer = 0;
+
+ if (!ast_bridge_queue_everyone_else(bridge, bridge_channel, frame)) {
+ /* This frame was successfully queued so no need to defer */
+ return 0;
+ }
+
+ /* Depending on the frame defer it so when the next channel joins it receives it */
+ switch (frame->frametype) {
+ case AST_FRAME_CONTROL:
+ switch (frame->subclass.integer) {
+ case AST_CONTROL_T38_PARAMETERS:
+ t38_parameters = frame->data.ptr;
+ switch (t38_parameters->request_response) {
+ case AST_T38_REQUEST_NEGOTIATE:
+ defer = -1;
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+
+ return defer;
}
static struct ast_bridge_technology native_rtp_bridge = {
diff --git a/bridges/bridge_simple.c b/bridges/bridge_simple.c
index 570453500..3e2a73e46 100644
--- a/bridges/bridge_simple.c
+++ b/bridges/bridge_simple.c
@@ -63,7 +63,37 @@ static int simple_bridge_join(struct ast_bridge *bridge, struct ast_bridge_chann
static int simple_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
{
- return ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
+ const struct ast_control_t38_parameters *t38_parameters;
+ int defer = 0;
+
+ if (!ast_bridge_queue_everyone_else(bridge, bridge_channel, frame)) {
+ /* This frame was successfully queued so no need to defer */
+ return 0;
+ }
+
+ /* Depending on the frame defer it so when the next channel joins it receives it */
+ switch (frame->frametype) {
+ case AST_FRAME_CONTROL:
+ switch (frame->subclass.integer) {
+ case AST_CONTROL_T38_PARAMETERS:
+ t38_parameters = frame->data.ptr;
+ switch (t38_parameters->request_response) {
+ case AST_T38_REQUEST_NEGOTIATE:
+ defer = -1;
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
+
+ return defer;
}
static struct ast_bridge_technology simple_bridge = {
diff --git a/include/asterisk/bridge_channel.h b/include/asterisk/bridge_channel.h
index 55c2b3a76..a16695e07 100644
--- a/include/asterisk/bridge_channel.h
+++ b/include/asterisk/bridge_channel.h
@@ -145,6 +145,8 @@ struct ast_bridge_channel {
AST_LIST_ENTRY(ast_bridge_channel) entry;
/*! Queue of outgoing frames to the channel. */
AST_LIST_HEAD_NOLOCK(, ast_frame) wr_queue;
+ /*! Queue of deferred frames, queued onto channel when other party joins. */
+ AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_queue;
/*! Pipe to alert thread when frames are put into the wr_queue. */
int alert_pipe[2];
/*!
diff --git a/include/asterisk/bridge_channel_internal.h b/include/asterisk/bridge_channel_internal.h
index fb8e781e8..ba71e9fc4 100644
--- a/include/asterisk/bridge_channel_internal.h
+++ b/include/asterisk/bridge_channel_internal.h
@@ -98,6 +98,17 @@ void bridge_channel_settle_owed_events(struct ast_bridge *orig_bridge, struct as
/*!
* \internal
+ * \brief Queue any deferred frames on the channel.
+ * \since 13.17.0
+ *
+ * \param bridge_channel Channel that the deferred frames should be pulled from and queued to.
+ *
+ * \return Nothing
+ */
+void bridge_channel_queue_deferred_frames(struct ast_bridge_channel *bridge_channel);
+
+/*!
+ * \internal
* \brief Push the bridge channel into its specified bridge.
* \since 12.0.0
*
diff --git a/include/asterisk/bridge_technology.h b/include/asterisk/bridge_technology.h
index 402b54e98..5add4551f 100644
--- a/include/asterisk/bridge_technology.h
+++ b/include/asterisk/bridge_technology.h
@@ -156,6 +156,9 @@ struct ast_bridge_technology {
* \retval -1 Frame needs to be deferred.
*
* \note On entry, bridge is already locked.
+ *
+ * \note Deferred frames will be automatically queued onto the channel when another
+ * channel joins the bridge.
*/
int (*write)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame);
/*!
diff --git a/include/asterisk/res_pjsip_session.h b/include/asterisk/res_pjsip_session.h
index 5e8eb3acc..457edd8d4 100644
--- a/include/asterisk/res_pjsip_session.h
+++ b/include/asterisk/res_pjsip_session.h
@@ -153,6 +153,10 @@ struct ast_sip_session {
struct ast_sip_aor *aor;
/*! From header saved at invite creation */
pjsip_fromto_hdr *saved_from_hdr;
+ /*! Whether the end of the session should be deferred */
+ unsigned int defer_end:1;
+ /*! Session end (remote hangup) requested while termination deferred */
+ unsigned int ended_while_deferred:1;
};
typedef int (*ast_sip_session_request_creation_cb)(struct ast_sip_session *session, pjsip_tx_data *tdata);
@@ -482,6 +486,13 @@ int ast_sip_session_defer_termination(struct ast_sip_session *session);
void ast_sip_session_defer_termination_cancel(struct ast_sip_session *session);
/*!
+ * \brief End the session if it had been previously deferred
+ *
+ * \param session The session to end if it had been deferred
+ */
+void ast_sip_session_end_if_deferred(struct ast_sip_session *session);
+
+/*!
* \brief Register an SDP handler
*
* An SDP handler is responsible for parsing incoming SDP streams and ensuring that
diff --git a/main/bridge.c b/main/bridge.c
index 7f6fbbef9..b2beb8637 100644
--- a/main/bridge.c
+++ b/main/bridge.c
@@ -471,6 +471,7 @@ static void bridge_complete_join(struct ast_bridge *bridge)
}
AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
+ bridge_channel_queue_deferred_frames(bridge_channel);
if (!bridge_channel->just_joined) {
continue;
}
diff --git a/main/bridge_channel.c b/main/bridge_channel.c
index eba5ae40a..0af688ad4 100644
--- a/main/bridge_channel.c
+++ b/main/bridge_channel.c
@@ -639,18 +639,21 @@ void ast_bridge_channel_kick(struct ast_bridge_channel *bridge_channel, int caus
static int bridge_channel_write_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
{
const struct ast_control_t38_parameters *t38_parameters;
+ int deferred;
ast_assert(frame->frametype != AST_FRAME_BRIDGE_ACTION_SYNC);
ast_bridge_channel_lock_bridge(bridge_channel);
-/*
- * XXX need to implement a deferred write queue for when there
- * is no peer channel in the bridge (yet or it was kicked).
- *
- * The tech decides if a frame needs to be pushed back for deferral.
- * simple_bridge/native_bridge are likely the only techs that will do this.
- */
- bridge_channel->bridge->technology->write(bridge_channel->bridge, bridge_channel, frame);
+
+ deferred = bridge_channel->bridge->technology->write(bridge_channel->bridge, bridge_channel, frame);
+ if (deferred) {
+ struct ast_frame *dup;
+
+ dup = ast_frdup(frame);
+ if (dup) {
+ AST_LIST_INSERT_HEAD(&bridge_channel->deferred_queue, dup, frame_list);
+ }
+ }
/* Remember any owed events to the bridge. */
switch (frame->frametype) {
@@ -754,6 +757,18 @@ void bridge_channel_settle_owed_events(struct ast_bridge *orig_bridge, struct as
}
}
+void bridge_channel_queue_deferred_frames(struct ast_bridge_channel *bridge_channel)
+{
+ struct ast_frame *frame;
+
+ ast_channel_lock(bridge_channel->chan);
+ while ((frame = AST_LIST_REMOVE_HEAD(&bridge_channel->deferred_queue, frame_list))) {
+ ast_queue_frame_head(bridge_channel->chan, frame);
+ ast_frfree(frame);
+ }
+ ast_channel_unlock(bridge_channel->chan);
+}
+
/*!
* \internal
* \brief Suspend a channel from a bridge.
@@ -2854,6 +2869,11 @@ static void bridge_channel_destroy(void *obj)
}
ast_alertpipe_close(bridge_channel->alert_pipe);
+ /* Flush any unhandled deferred_queue frames. */
+ while ((fr = AST_LIST_REMOVE_HEAD(&bridge_channel->deferred_queue, frame_list))) {
+ ast_frfree(fr);
+ }
+
ast_cond_destroy(&bridge_channel->cond);
ao2_cleanup(bridge_channel->write_format);
diff --git a/res/res_pjsip_pubsub.c b/res/res_pjsip_pubsub.c
index fbb1ad4e8..c62bddd56 100644
--- a/res/res_pjsip_pubsub.c
+++ b/res/res_pjsip_pubsub.c
@@ -1108,7 +1108,9 @@ static void remove_subscription(struct sip_subscription_tree *obj)
static void destroy_subscription(struct ast_sip_subscription *sub)
{
ast_debug(3, "Destroying SIP subscription from '%s->%s'\n",
- ast_sorcery_object_get_id(sub->tree->endpoint), sub->resource);
+ sub->tree->endpoint ? ast_sorcery_object_get_id(sub->tree->endpoint) : "Unknown",
+ sub->resource);
+
ast_free(sub->body_text);
AST_VECTOR_FREE(&sub->children);
@@ -1265,14 +1267,14 @@ static void subscription_tree_destructor(void *obj)
sub_tree->endpoint ? ast_sorcery_object_get_id(sub_tree->endpoint) : "Unknown",
sub_tree->root ? sub_tree->root->resource : "Unknown");
- ao2_cleanup(sub_tree->endpoint);
-
destroy_subscriptions(sub_tree->root);
if (sub_tree->dlg) {
ast_sip_push_task_synchronous(sub_tree->serializer, subscription_unreference_dialog, sub_tree);
}
+ ao2_cleanup(sub_tree->endpoint);
+
ast_taskprocessor_unreference(sub_tree->serializer);
ast_module_unref(ast_module_info->self);
}
diff --git a/res/res_pjsip_refer.c b/res/res_pjsip_refer.c
index 111c4b2a1..ff8bc6a66 100644
--- a/res/res_pjsip_refer.c
+++ b/res/res_pjsip_refer.c
@@ -543,6 +543,7 @@ static int refer_attended_task(void *data)
}
}
+ ast_sip_session_end_if_deferred(attended->transferer);
if (response != 200) {
if (!ast_sip_push_task(attended->transferer->serializer,
defer_termination_cancel, attended->transferer)) {
@@ -772,6 +773,7 @@ static int refer_incoming_attended_request(struct ast_sip_session *session, pjsi
/* Push it to the other session, which will have both channels with minimal locking */
if (ast_sip_push_task(other_session->serializer, refer_attended_task, attended)) {
+ ast_sip_session_end_if_deferred(session);
ast_sip_session_defer_termination_cancel(session);
ao2_cleanup(attended);
return 500;
@@ -810,9 +812,12 @@ static int refer_incoming_attended_request(struct ast_sip_session *session, pjsi
response = xfer_response_code2sip(ast_bridge_transfer_blind(1, session->channel,
"external_replaces", context, refer_blind_callback, &refer));
+
+ ast_sip_session_end_if_deferred(session);
if (response != 200) {
ast_sip_session_defer_termination_cancel(session);
}
+
return response;
}
}
@@ -865,9 +870,12 @@ static int refer_incoming_blind_request(struct ast_sip_session *session, pjsip_r
response = xfer_response_code2sip(ast_bridge_transfer_blind(1, session->channel,
exten, context, refer_blind_callback, &refer));
+
+ ast_sip_session_end_if_deferred(session);
if (response != 200) {
ast_sip_session_defer_termination_cancel(session);
}
+
return response;
}
diff --git a/res/res_pjsip_session.c b/res/res_pjsip_session.c
index 59a8915e1..d6c5fbcc1 100644
--- a/res/res_pjsip_session.c
+++ b/res/res_pjsip_session.c
@@ -1903,6 +1903,9 @@ int ast_sip_session_defer_termination(struct ast_sip_session *session)
session->defer_terminate = 1;
+ session->defer_end = 1;
+ session->ended_while_deferred = 0;
+
session->scheduled_termination.id = 0;
ao2_ref(session, +1);
session->scheduled_termination.user_data = session;
@@ -1940,6 +1943,7 @@ void ast_sip_session_defer_termination_cancel(struct ast_sip_session *session)
/* Already canceled or timer fired. */
return;
}
+
session->defer_terminate = 0;
if (session->terminate_while_deferred) {
@@ -1951,6 +1955,22 @@ void ast_sip_session_defer_termination_cancel(struct ast_sip_session *session)
sip_session_defer_termination_stop_timer(session);
}
+void ast_sip_session_end_if_deferred(struct ast_sip_session *session)
+{
+ if (!session->defer_end) {
+ return;
+ }
+
+ session->defer_end = 0;
+
+ if (session->ended_while_deferred) {
+ /* Complete the session end started by the remote hangup. */
+ ast_debug(3, "Ending session (%p) after being deferred\n", session);
+ session->ended_while_deferred = 0;
+ session_end(session);
+ }
+}
+
struct ast_sip_session *ast_sip_dialog_get_session(pjsip_dialog *dlg)
{
pjsip_inv_session *inv_session = pjsip_dlg_get_inv_session(dlg);
@@ -2636,6 +2656,12 @@ static void session_inv_on_state_changed(pjsip_inv_session *inv, pjsip_event *e)
}
if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
+ if (session->defer_end) {
+ ast_debug(3, "Deferring session (%p) end\n", session);
+ session->ended_while_deferred = 1;
+ return;
+ }
+
if (ast_sip_push_task(session->serializer, session_end, session)) {
/* Do it anyway even though this is not the right thread. */
session_end(session);
diff --git a/res/res_pjsip_session.exports.in b/res/res_pjsip_session.exports.in
index a39485e66..fdfc5fb47 100644
--- a/res/res_pjsip_session.exports.in
+++ b/res/res_pjsip_session.exports.in
@@ -3,6 +3,7 @@
LINKER_SYMBOL_PREFIXast_sip_session_terminate;
LINKER_SYMBOL_PREFIXast_sip_session_defer_termination;
LINKER_SYMBOL_PREFIXast_sip_session_defer_termination_cancel;
+ LINKER_SYMBOL_PREFIXast_sip_session_end_if_deferred;
LINKER_SYMBOL_PREFIXast_sip_session_register_sdp_handler;
LINKER_SYMBOL_PREFIXast_sip_session_unregister_sdp_handler;
LINKER_SYMBOL_PREFIXast_sip_session_register_supplement;
diff --git a/tests/test_bridging.c b/tests/test_bridging.c
new file mode 100644
index 000000000..08a2fcc1f
--- /dev/null
+++ b/tests/test_bridging.c
@@ -0,0 +1,292 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2017, Digium, Inc.
+ *
+ * Joshua Colp <jcolp@digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Bridging unit tests
+ *
+ * \author Joshua Colp <jcolp@digium.com>
+ *
+ */
+
+/*** MODULEINFO
+ <depend>TEST_FRAMEWORK</depend>
+ <support_level>core</support_level>
+ ***/
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/module.h"
+#include "asterisk/test.h"
+#include "asterisk/channel.h"
+#include "asterisk/time.h"
+#include "asterisk/bridge.h"
+#include "asterisk/bridge_basic.h"
+#include "asterisk/features.h"
+#include "asterisk/format_cache.h"
+
+#define TEST_CATEGORY "/main/bridging/"
+
+#define CHANNEL_TECH_NAME "BridgingTestChannel"
+
+#define TEST_CHANNEL_FORMAT ast_format_slin
+
+/*! \brief A private structure for the test channel */
+struct test_bridging_chan_pvt {
+ /* \brief The expected indication */
+ int condition;
+ /*! \brief The number of indicated things */
+ unsigned int indicated;
+};
+
+/*! \brief Callback function for when a frame is written to a channel */
+static int test_bridging_chan_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
+{
+ struct test_bridging_chan_pvt *test_pvt = ast_channel_tech_pvt(chan);
+
+ if (condition == test_pvt->condition) {
+ test_pvt->indicated++;
+ }
+
+ return 0;
+}
+
+/*! \brief Callback function for when a channel is hung up */
+static int test_bridging_chan_hangup(struct ast_channel *chan)
+{
+ struct test_bridging_chan_pvt *test_pvt = ast_channel_tech_pvt(chan);
+
+ ast_free(test_pvt);
+ ast_channel_tech_pvt_set(chan, NULL);
+
+ return 0;
+}
+
+/*! \brief A channel technology used for the unit tests */
+static struct ast_channel_tech test_bridging_chan_tech = {
+ .type = CHANNEL_TECH_NAME,
+ .description = "Mock channel technology for bridge tests",
+ .indicate = test_bridging_chan_indicate,
+ .hangup = test_bridging_chan_hangup,
+ .properties = AST_CHAN_TP_INTERNAL,
+};
+
+static void test_nanosleep(int secs, long nanosecs)
+{
+ struct timespec sleep_time = {secs, nanosecs};
+
+ while ((nanosleep(&sleep_time, &sleep_time) == -1) && (errno == EINTR)) {
+ }
+}
+
+/*! \brief Wait until a channel is bridged */
+static void wait_for_bridged(struct ast_channel *channel)
+{
+ ast_channel_lock(channel);
+ while (!ast_channel_is_bridged(channel)) {
+ ast_channel_unlock(channel);
+ test_nanosleep(0, 1000000);
+ ast_channel_lock(channel);
+ }
+ ast_channel_unlock(channel);
+}
+
+/*! \brief Wait until a channel is not bridged */
+static void wait_for_unbridged(struct ast_channel *channel)
+{
+ ast_channel_lock(channel);
+ while (ast_channel_is_bridged(channel)) {
+ ast_channel_unlock(channel);
+ test_nanosleep(0, 1000000);
+ ast_channel_lock(channel);
+ }
+ ast_channel_unlock(channel);
+}
+
+/*! \brief Wait until a channel has no frames on its read queue */
+static void wait_for_empty_queue(struct ast_channel *channel)
+{
+ ast_channel_lock(channel);
+ while (!AST_LIST_EMPTY(ast_channel_readq(channel))) {
+ ast_channel_unlock(channel);
+ test_nanosleep(0, 1000000);
+ ast_channel_lock(channel);
+ }
+ ast_channel_unlock(channel);
+}
+
+/*! \brief Create a \ref test_bridging_chan_tech for Alice. */
+#define START_ALICE(channel, pvt) START_CHANNEL(channel, pvt, "Alice", "100")
+
+/*! \brief Create a \ref test_bridging_chan_tech for Bob. */
+#define START_BOB(channel, pvt) START_CHANNEL(channel, pvt, "Bob", "200")
+
+#define START_CHANNEL(channel, pvt, name, number) do { \
+ channel = ast_channel_alloc(0, AST_STATE_UP, number, name, number, number, \
+ "default", NULL, NULL, 0, CHANNEL_TECH_NAME "/" name); \
+ pvt = ast_calloc(1, sizeof(*pvt)); \
+ ast_channel_tech_pvt_set(channel, pvt); \
+ ast_channel_nativeformats_set(channel, test_bridging_chan_tech.capabilities); \
+ ast_channel_set_rawwriteformat(channel, TEST_CHANNEL_FORMAT); \
+ ast_channel_set_rawreadformat(channel, TEST_CHANNEL_FORMAT); \
+ ast_channel_set_writeformat(channel, TEST_CHANNEL_FORMAT); \
+ ast_channel_set_readformat(channel, TEST_CHANNEL_FORMAT); \
+ ast_channel_unlock(channel); \
+ } while (0)
+
+/*! \brief Hang up a test channel safely */
+#define HANGUP_CHANNEL(channel) do { \
+ ao2_ref(channel, +1); \
+ ast_hangup((channel)); \
+ ao2_cleanup(channel); \
+ channel = NULL; \
+ } while (0)
+
+static void safe_channel_release(struct ast_channel *chan)
+{
+ if (!chan) {
+ return;
+ }
+ ast_channel_release(chan);
+}
+
+static void safe_bridge_destroy(struct ast_bridge *bridge)
+{
+ if (!bridge) {
+ return;
+ }
+ ast_bridge_destroy(bridge, 0);
+}
+
+static void stream_periodic_frames(struct ast_channel *chan, int ms, int interval_ms)
+{
+ long nanosecs;
+
+ ast_assert(chan != NULL);
+ ast_assert(0 < ms);
+ ast_assert(0 < interval_ms);
+
+ nanosecs = interval_ms * 1000000L;
+ while (0 < ms) {
+ ast_queue_frame(chan, &ast_null_frame);
+
+ if (interval_ms < ms) {
+ ms -= interval_ms;
+ } else {
+ nanosecs = ms * 1000000L;
+ ms = 0;
+ }
+ test_nanosleep(0, nanosecs);
+ }
+}
+
+AST_TEST_DEFINE(test_bridging_deferred_queue)
+{
+ RAII_VAR(struct ast_channel *, chan_alice, NULL, safe_channel_release);
+ struct test_bridging_chan_pvt *alice_pvt;
+ struct ast_control_t38_parameters t38_parameters = {
+ .request_response = AST_T38_REQUEST_NEGOTIATE,
+ };
+ struct ast_frame frame = {
+ .frametype = AST_FRAME_CONTROL,
+ .subclass.integer = AST_CONTROL_T38_PARAMETERS,
+ .data.ptr = &t38_parameters,
+ .datalen = sizeof(t38_parameters),
+ };
+ RAII_VAR(struct ast_channel *, chan_bob, NULL, safe_channel_release);
+ struct test_bridging_chan_pvt *bob_pvt;
+ RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
+
+ switch (cmd) {
+ case TEST_INIT:
+ info->name = __func__;
+ info->category = TEST_CATEGORY;
+ info->summary = "Test that deferred frames from a channel in a bridge get written";
+ info->description =
+ "This test creates two channels, queues a deferrable frame on one, places it into\n"
+ "a bridge, confirms the frame was read by the bridge, adds the second channel to the\n"
+ "bridge, and makes sure the deferred frame is written to it.";
+ return AST_TEST_NOT_RUN;
+ case TEST_EXECUTE:
+ break;
+ }
+
+ /* Create the bridges */
+ bridge1 = ast_bridge_basic_new();
+ ast_test_validate(test, bridge1 != NULL);
+
+ /* Create channels that will go into the bridge */
+ START_ALICE(chan_alice, alice_pvt);
+ START_BOB(chan_bob, bob_pvt);
+ bob_pvt->condition = AST_CONTROL_T38_PARAMETERS;
+
+ /* Bridge alice and wait for the frame to be deferred */
+ ast_test_validate(test, !ast_bridge_impart(bridge1, chan_alice, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
+ wait_for_bridged(chan_alice);
+ ast_queue_frame(chan_alice, &frame);
+ wait_for_empty_queue(chan_alice);
+
+ /* Bridge bob for a second so it can receive the deferred T.38 request negotiate frame */
+ ast_test_validate(test, !ast_bridge_impart(bridge1, chan_bob, NULL, NULL, AST_BRIDGE_IMPART_CHAN_DEPARTABLE));
+ wait_for_bridged(chan_bob);
+ stream_periodic_frames(chan_alice, 1000, 20);
+ ast_test_validate(test, !ast_bridge_depart(chan_bob));
+ wait_for_unbridged(chan_bob);
+
+ /* Ensure that we received the expected indications while it was in there (request to negotiate, and to terminate) */
+ ast_test_validate(test, bob_pvt->indicated == 2);
+
+ /* Now remove alice since we are done */
+ ast_test_validate(test, !ast_bridge_depart(chan_alice));
+ wait_for_unbridged(chan_alice);
+
+ /* Hangup the channels */
+ HANGUP_CHANNEL(chan_alice);
+ HANGUP_CHANNEL(chan_bob);
+
+ return AST_TEST_PASS;
+}
+
+static int unload_module(void)
+{
+ AST_TEST_UNREGISTER(test_bridging_deferred_queue);
+
+ ast_channel_unregister(&test_bridging_chan_tech);
+ ao2_cleanup(test_bridging_chan_tech.capabilities);
+ test_bridging_chan_tech.capabilities = NULL;
+
+ return 0;
+}
+
+static int load_module(void)
+{
+ test_bridging_chan_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+ if (!test_bridging_chan_tech.capabilities) {
+ return AST_MODULE_LOAD_DECLINE;
+ }
+ ast_format_cap_append(test_bridging_chan_tech.capabilities, TEST_CHANNEL_FORMAT, 0);
+ ast_channel_register(&test_bridging_chan_tech);
+
+ AST_TEST_REGISTER(test_bridging_deferred_queue);
+
+ return AST_MODULE_LOAD_SUCCESS;
+}
+
+AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Bridging Unit Tests");