[Asterisk-code-review] Dial: Add function to append already-created channel. (asterisk[master])
Mark Michelson
asteriskteam at digium.com
Wed Mar 30 17:23:47 CDT 2016
Mark Michelson has uploaded a new change for review.
https://gerrit.asterisk.org/2512
Change subject: Dial: Add function to append already-created channel.
......................................................................
Dial: Add function to append already-created channel.
The Dial API takes responsiblity for creating an outbound channel when
calling ast_dial_append(). This commit adds a new function,
ast_dial_append_channel(), which allows us to create the channel outside
the Dial API and then to append the channel to the ast_dial structure.
This is useful for situations where the channel's creation and dialing
are distinct operations. Upcoming ARI early bridge work will illustrate
its usage.
Change-Id: Id8179f64f8f99132f80dead8d5db2030fd2c0509
---
M include/asterisk/dial.h
M main/dial.c
2 files changed, 85 insertions(+), 32 deletions(-)
git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/12/2512/1
diff --git a/include/asterisk/dial.h b/include/asterisk/dial.h
index c59257c..1f6154d 100644
--- a/include/asterisk/dial.h
+++ b/include/asterisk/dial.h
@@ -76,6 +76,16 @@
*/
int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device, const struct ast_assigned_ids *assignedids);
+/*!
+ * \brief Append a channel using an actual channel object
+ *
+ * \param dial The ast_dial to add the channel to
+ * \param chan The channel to add to the dial
+ * \retval -1 Failure
+ * \retval non-zero The position of the channel in the list of dialed channels
+ */
+int ast_dial_append_channel(struct ast_dial *dial, struct ast_channel *chan);
+
/*! \brief Request all appended channels, but do not dial
* \param dial Dialing structure
* \param chan Optional dialing channel
diff --git a/main/dial.c b/main/dial.c
index 127f327..97529a6 100644
--- a/main/dial.c
+++ b/main/dial.c
@@ -248,22 +248,9 @@
return dial;
}
-/*! \brief Append a channel
- * \note Appends a channel to a dialing structure
- * \return Returns channel reference number on success, -1 on failure
- */
-int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device, const struct ast_assigned_ids *assignedids)
+static int dial_append_common(struct ast_dial *dial, struct ast_dial_channel *channel,
+ const char *tech, const char *device, const struct ast_assigned_ids *assignedids)
{
- struct ast_dial_channel *channel = NULL;
-
- /* Make sure we have required arguments */
- if (!dial || !tech || !device)
- return -1;
-
- /* Allocate new memory for dialed channel structure */
- if (!(channel = ast_calloc(1, sizeof(*channel))))
- return -1;
-
/* Record technology and device for when we actually dial */
channel->tech = ast_strdup(tech);
channel->device = ast_strdup(device);
@@ -287,6 +274,59 @@
AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
return channel->num;
+
+}
+
+/*! \brief Append a channel
+ * \note Appends a channel to a dialing structure
+ * \return Returns channel reference number on success, -1 on failure
+ */
+int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device, const struct ast_assigned_ids *assignedids)
+{
+ struct ast_dial_channel *channel = NULL;
+
+ /* Make sure we have required arguments */
+ if (!dial || !tech || !device)
+ return -1;
+
+ /* Allocate new memory for dialed channel structure */
+ if (!(channel = ast_calloc(1, sizeof(*channel))))
+ return -1;
+
+ return dial_append_common(dial, channel, tech, device, assignedids);
+}
+
+int ast_dial_append_channel(struct ast_dial *dial, struct ast_channel *chan)
+{
+ struct ast_dial_channel *channel;
+ const char *tech;
+ const char *device;
+ const char *dash;
+
+ if (!dial || !chan) {
+ return -1;
+ }
+
+ channel = ast_calloc(1, sizeof(*channel));
+ if (!channel) {
+ return -1;
+ }
+ channel->owner = chan;
+
+ tech = ast_strdupa(ast_channel_name(chan));
+
+ device = strchr(tech, '/');
+ if (!device) {
+ return -1;
+ }
+ device++;
+
+ dash = strchr(device, '-');
+ if (dash) {
+ dash = '\0';
+ }
+
+ return dial_append_common(dial, channel, tech, device, NULL);
}
/*! \brief Helper function that requests all channels */
@@ -315,27 +355,29 @@
}
}
- /* Copy device string over */
- ast_copy_string(numsubst, channel->device, sizeof(numsubst));
+ if (!channel->owner) {
+ /* Copy device string over */
+ ast_copy_string(numsubst, channel->device, sizeof(numsubst));
- if (cap && ast_format_cap_count(cap)) {
- cap_request = cap;
- } else if (requester_cap) {
- cap_request = requester_cap;
- } else {
- cap_all_audio = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- ast_format_cap_append_by_type(cap_all_audio, AST_MEDIA_TYPE_AUDIO);
- cap_request = cap_all_audio;
- }
+ if (cap && ast_format_cap_count(cap)) {
+ cap_request = cap;
+ } else if (requester_cap) {
+ cap_request = requester_cap;
+ } else {
+ cap_all_audio = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+ ast_format_cap_append_by_type(cap_all_audio, AST_MEDIA_TYPE_AUDIO);
+ cap_request = cap_all_audio;
+ }
- /* If we fail to create our owner channel bail out */
- if (!(channel->owner = ast_request(channel->tech, cap_request, &assignedids, chan, numsubst, &channel->cause))) {
+ /* If we fail to create our owner channel bail out */
+ if (!(channel->owner = ast_request(channel->tech, cap_request, &assignedids, chan, numsubst, &channel->cause))) {
+ ao2_cleanup(cap_all_audio);
+ return -1;
+ }
+ cap_request = NULL;
+ ao2_cleanup(requester_cap);
ao2_cleanup(cap_all_audio);
- return -1;
}
- cap_request = NULL;
- ao2_cleanup(requester_cap);
- ao2_cleanup(cap_all_audio);
if (chan) {
ast_channel_lock_both(chan, channel->owner);
@@ -1096,6 +1138,7 @@
}
/* Hang up channel if need be */
+ ast_log(LOG_NOTICE, "Hanging up the outbound channel...\n");
ast_hangup(channel->owner);
channel->owner = NULL;
--
To view, visit https://gerrit.asterisk.org/2512
To unsubscribe, visit https://gerrit.asterisk.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Id8179f64f8f99132f80dead8d5db2030fd2c0509
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Mark Michelson <mmichelson at digium.com>
More information about the asterisk-code-review
mailing list