[svn-commits] qwell: branch qwell/ari_channel_mute r393829 - in /team/qwell/ari_channel_mut...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Mon Jul 8 13:39:55 CDT 2013


Author: qwell
Date: Mon Jul  8 13:39:51 2013
New Revision: 393829

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=393829
Log:
Address (most) review feedback.

Modified:
    team/qwell/ari_channel_mute/include/asterisk/channel.h
    team/qwell/ari_channel_mute/include/asterisk/stasis_app.h
    team/qwell/ari_channel_mute/main/channel.c
    team/qwell/ari_channel_mute/res/stasis/control.c
    team/qwell/ari_channel_mute/res/stasis_http/resource_channels.c

Modified: team/qwell/ari_channel_mute/include/asterisk/channel.h
URL: http://svnview.digium.com/svn/asterisk/team/qwell/ari_channel_mute/include/asterisk/channel.h?view=diff&rev=393829&r1=393828&r2=393829
==============================================================================
--- team/qwell/ari_channel_mute/include/asterisk/channel.h (original)
+++ team/qwell/ari_channel_mute/include/asterisk/channel.h Mon Jul  8 13:39:51 2013
@@ -4375,10 +4375,8 @@
 */
 const char *ast_channel_oldest_linkedid(const char *a, const char *b);
 
-enum ast_mute_direction {
-	AST_MUTE_DIRECTION_READ,
-	AST_MUTE_DIRECTION_WRITE,
-};
+#define AST_MUTE_DIRECTION_READ (1 << 0)
+#define AST_MUTE_DIRECTION_WRITE (1 << 1)
 
 /*!
  * \brief Mute a stream on a channel
@@ -4390,7 +4388,8 @@
  * \retval 0 Success
  * \retval -1 Failure
  */
-int ast_channel_mute(struct ast_channel *chan, enum ast_mute_direction direction, int frametype);
+int ast_channel_mute(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype);
+
 /*!
  * \brief Unmute a stream on a channel
  *
@@ -4401,6 +4400,6 @@
  * \retval 0 Success
  * \retval -1 Failure
  */
-int ast_channel_unmute(struct ast_channel *chan, enum ast_mute_direction direction, int frametype);
+int ast_channel_unmute(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype);
 
 #endif /* _ASTERISK_CHANNEL_H */

Modified: team/qwell/ari_channel_mute/include/asterisk/stasis_app.h
URL: http://svnview.digium.com/svn/asterisk/team/qwell/ari_channel_mute/include/asterisk/stasis_app.h?view=diff&rev=393829&r1=393828&r2=393829
==============================================================================
--- team/qwell/ari_channel_mute/include/asterisk/stasis_app.h (original)
+++ team/qwell/ari_channel_mute/include/asterisk/stasis_app.h Mon Jul  8 13:39:51 2013
@@ -175,7 +175,7 @@
  * \return 0 for success
  * \return -1 for error.
  */
-int stasis_app_control_mute(struct stasis_app_control *control, enum ast_mute_direction direction, int frametype);
+int stasis_app_control_mute(struct stasis_app_control *control, unsigned int direction, enum ast_frame_type frametype);
 
 /*!
  * \brief Unmute the channel associated with this control.
@@ -187,7 +187,7 @@
  * \return 0 for success
  * \return -1 for error.
  */
-int stasis_app_control_unmute(struct stasis_app_control *control, enum ast_mute_direction direction, int frametype);
+int stasis_app_control_unmute(struct stasis_app_control *control, unsigned int direction, enum ast_frame_type frametype);
 
 /*!
  * \brief Answer the channel associated with this control.

Modified: team/qwell/ari_channel_mute/main/channel.c
URL: http://svnview.digium.com/svn/asterisk/team/qwell/ari_channel_mute/main/channel.c?view=diff&rev=393829&r1=393828&r2=393829
==============================================================================
--- team/qwell/ari_channel_mute/main/channel.c (original)
+++ team/qwell/ari_channel_mute/main/channel.c Mon Jul  8 13:39:51 2013
@@ -10535,46 +10535,24 @@
 
 static void mute_datastore_destroy_cb(void *data)
 {
-	ast_free(data);
-}
-
-static const struct ast_datastore_info mute_datastore_voice_read = {
-	.type = "mutevoiceread",
+	ao2_cleanup(data);
+}
+
+static const struct ast_datastore_info mute_datastore_voice = {
+	.type = "mutevoice",
 	.destroy = mute_datastore_destroy_cb
 };
 
-static const struct ast_datastore_info mute_datastore_voice_write = {
-	.type = "mutevoicewrite",
-	.destroy = mute_datastore_destroy_cb
+static void mute_framehook_destroy_cb(void *data)
+{
+	ao2_cleanup(data);
+}
+
+struct mute_data {
+	enum ast_frame_type frametype;
+	unsigned int direction;
+	int framehook_id;
 };
-
-static void mute_framehook_destroy_cb(void *data)
-{
-	ast_free(data);
-}
-
-struct mute_data {
-	int frametype;
-	enum ast_mute_direction direction;
-};
-
-static const struct ast_datastore_info *mute_get_datastore_information(enum ast_mute_direction direction, int frametype)
-{
-	switch (direction) {
-	case AST_MUTE_DIRECTION_READ:
-		if (frametype == AST_FRAME_VOICE) {
-			return &mute_datastore_voice_read;
-		}
-		break;
-	case AST_MUTE_DIRECTION_WRITE:
-		if (frametype == AST_FRAME_VOICE) {
-			return &mute_datastore_voice_write;
-		}
-		break;
-	}
-
-	return NULL;
-}
 
 static struct ast_frame *mute_framehook_event_cb(struct ast_channel *chan, struct ast_frame *frame, enum ast_framehook_event event, void *data)
 {
@@ -10589,9 +10567,9 @@
 		return frame;
 	}
 
-	if (event == AST_FRAMEHOOK_EVENT_READ && mute->direction == AST_MUTE_DIRECTION_READ) {
+	if (event == AST_FRAMEHOOK_EVENT_READ && (mute->direction & AST_MUTE_DIRECTION_READ)) {
 		mute_frame = 1;
-	} else if (event == AST_FRAMEHOOK_EVENT_WRITE && mute->direction == AST_MUTE_DIRECTION_WRITE) {
+	} else if (event == AST_FRAMEHOOK_EVENT_WRITE && (mute->direction & AST_MUTE_DIRECTION_WRITE)) {
 		mute_frame = 1;
 	}
 
@@ -10608,73 +10586,90 @@
 	return frame;
 }
 
-int ast_channel_mute(struct ast_channel *chan, enum ast_mute_direction direction, int frametype)
+static const struct ast_datastore_info *mute_get_datastore_information(enum ast_frame_type frametype)
+{
+	switch (frametype) {
+	case AST_FRAME_VOICE:
+		return &mute_datastore_voice;
+	default:
+		return NULL;
+	}
+}
+
+int ast_channel_mute(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype)
 {
 	SCOPED_CHANNELLOCK(lockvar, chan);
-	struct mute_data *mute;
+
+	RAII_VAR(struct mute_data *, mute, NULL, ao2_cleanup);
+	const struct ast_datastore_info *datastore_info = NULL;
+	struct ast_datastore *datastore = NULL;
 	struct ast_framehook_interface interface = {
 		.version = AST_FRAMEHOOK_INTERFACE_VERSION,
 		.event_cb = mute_framehook_event_cb,
 		.destroy_cb = mute_framehook_destroy_cb,
 	};
-	int hook_id;
+	int framehook_id;
+
+	if (!(datastore_info = mute_get_datastore_information(frametype))) {
+		ast_log(LOG_WARNING, "Attempted to mute an unsupported frame type (%d).\n", frametype);
+		return -1;
+	}
+
+	if ((datastore = ast_channel_datastore_find(chan, datastore_info, NULL))) {
+		mute = datastore->data;
+		ao2_ref(mute, +1);
+
+		mute->direction |= direction;
+
+		return 0;
+	}
+
+	if (!(mute = ao2_alloc(sizeof(*mute), NULL))) {
+		ast_log(LOG_WARNING, "Failed to allocate data while attempting to mute channel.\n");
+		return -1;
+	}
+
+	mute->frametype = frametype;
+	mute->direction |= direction;
+
+	interface.data = mute;
+
+	framehook_id = ast_framehook_attach(chan, &interface);
+	if (framehook_id < 0) {
+		/* Hook attach failed.  Get rid of the evidence. */
+		ast_log(LOG_WARNING, "Failed to attach framehook while attempting to mute channel.\n");
+		return -1;
+	}
+
+	/* One ref for the framehook */
+	ao2_ref(mute, +1);
+
+	mute->framehook_id = framehook_id;
+
+	if (!(datastore = ast_datastore_alloc(datastore_info, NULL))) {
+		ast_log(LOG_WARNING, "Failed to allocate datastore while attempting to mute channel.\n");
+		ast_framehook_detach(chan, framehook_id);
+		return -1;
+	}
+
+	datastore->data = mute;
+
+	ast_channel_datastore_add(chan, datastore);
+
+	/* and another ref for the datastore */
+	ao2_ref(mute, +1);
+
+	return 0;
+}
+
+int ast_channel_unmute(struct ast_channel *chan, unsigned int direction, enum ast_frame_type frametype)
+{
+	const struct ast_datastore_info *datastore_info = NULL;
 	struct ast_datastore *datastore = NULL;
-	const struct ast_datastore_info *datastore_info = NULL;
-
-	if (!(datastore_info = mute_get_datastore_information(direction, frametype))) {
-		ast_log(LOG_NOTICE, "Attempted to mute an unsupported frame type (%d).\n", frametype);
-		return -1;
-	}
-
-	if (!(mute = ast_calloc(1, sizeof(*mute)))) {
-		ast_log(LOG_NOTICE, "Failed to allocate data while attempting to mute channel.\n");
-		return -1;
-	}
-
-	mute->frametype = frametype;
-	mute->direction = direction;
-
-	interface.data = mute;
-
-	hook_id = ast_framehook_attach(chan, &interface);
-	if (hook_id < 0) {
-		/* Hook attach failed.  Get rid of the evidence. */
-		ast_log(LOG_NOTICE, "Failed to attach framehook while attempting to mute channel.\n");
-		ast_free(mute);
-		return -1;
-	}
-
-	if ((datastore = ast_channel_datastore_find(chan, datastore_info, NULL))) {
-		/* A datastore/audiohook already existed for this type of mute.  Kill them. */
-		ast_framehook_detach(chan, *(int *)datastore->data);
-		ast_channel_datastore_remove(chan, datastore);
-	}
-
-	if (!(datastore = ast_datastore_alloc(datastore_info, NULL))) {
-		ast_log(LOG_NOTICE, "Failed to allocate datastore while attempting to mute channel.\n");
-		ast_framehook_detach(chan, hook_id);
-		return -1;
-	}
-
-	if (!(datastore->data = ast_calloc(1, sizeof(int)))) {
-		ast_datastore_free(datastore);
-		ast_framehook_detach(chan, hook_id);
-	}
-
-	*(int *)datastore->data = hook_id;
-
-	ast_channel_datastore_add(chan, datastore);
-
-	return 0;
-}
-
-int ast_channel_unmute(struct ast_channel *chan, enum ast_mute_direction direction, int frametype)
-{
-	struct ast_datastore *datastore = NULL;
-	const struct ast_datastore_info *datastore_info = NULL;
-
-	if (!(datastore_info = mute_get_datastore_information(direction, frametype))) {
-		ast_log(LOG_NOTICE, "Attempted to unmute an unsupported frame type (%d).\n", frametype);
+	struct mute_data *mute;
+
+	if (!(datastore_info = mute_get_datastore_information(frametype))) {
+		ast_log(LOG_WARNING, "Attempted to unmute an unsupported frame type (%d).\n", frametype);
 		return -1;
 	}
 
@@ -10683,8 +10678,15 @@
 		return 0;
 	}
 
-	ast_framehook_detach(chan, *(int *)datastore->data);
-	ast_channel_datastore_remove(chan, datastore);
+	mute = datastore->data;
+
+	mute->direction &= ~(direction);
+
+	if (mute->direction == 0) {
+		/* Nothing left to mute.  Bye! */
+		ast_framehook_detach(chan, mute->framehook_id);
+		ast_channel_datastore_remove(chan, datastore);
+	}
 
 	return 0;
 }

Modified: team/qwell/ari_channel_mute/res/stasis/control.c
URL: http://svnview.digium.com/svn/asterisk/team/qwell/ari_channel_mute/res/stasis/control.c?view=diff&rev=393829&r1=393828&r2=393829
==============================================================================
--- team/qwell/ari_channel_mute/res/stasis/control.c (original)
+++ team/qwell/ari_channel_mute/res/stasis/control.c Mon Jul  8 13:39:51 2013
@@ -35,6 +35,7 @@
 #include "asterisk/bridging.h"
 #include "asterisk/bridging_basic.h"
 #include "asterisk/bridging_features.h"
+#include "asterisk/frame.h"
 #include "asterisk/pbx.h"
 
 struct stasis_app_control {
@@ -208,8 +209,8 @@
 }
 
 struct stasis_app_control_mute_data {
-	int frametype;
-	enum ast_mute_direction direction;
+	enum ast_frame_type frametype;
+	unsigned int direction;
 };
 
 static void *app_control_mute(struct stasis_app_control *control,
@@ -222,7 +223,7 @@
 	return NULL;
 }
 
-int stasis_app_control_mute(struct stasis_app_control *control, enum ast_mute_direction direction, int frametype)
+int stasis_app_control_mute(struct stasis_app_control *control, unsigned int direction, enum ast_frame_type frametype)
 {
 	struct stasis_app_control_mute_data *mute_data;
 
@@ -248,7 +249,7 @@
 	return NULL;
 }
 
-int stasis_app_control_unmute(struct stasis_app_control *control, enum ast_mute_direction direction, int frametype)
+int stasis_app_control_unmute(struct stasis_app_control *control, unsigned int direction, enum ast_frame_type frametype)
 {
 	struct stasis_app_control_mute_data *mute_data;
 

Modified: team/qwell/ari_channel_mute/res/stasis_http/resource_channels.c
URL: http://svnview.digium.com/svn/asterisk/team/qwell/ari_channel_mute/res/stasis_http/resource_channels.c?view=diff&rev=393829&r1=393828&r2=393829
==============================================================================
--- team/qwell/ari_channel_mute/res/stasis_http/resource_channels.c (original)
+++ team/qwell/ari_channel_mute/res/stasis_http/resource_channels.c Mon Jul  8 13:39:51 2013
@@ -144,9 +144,8 @@
 void stasis_http_mute_channel(struct ast_variable *headers, struct ast_mute_channel_args *args, struct stasis_http_response *response)
 {
 	RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
-	int mute_in = 0;
-	int mute_out = 0;
-	int stream_type = AST_FRAME_VOICE;
+	unsigned int direction = 0;
+	enum ast_frame_type frametype = AST_FRAME_VOICE;
 
 	control = find_control(response, args->channel_id);
 	if (control == NULL) {
@@ -154,12 +153,11 @@
 	}
 
 	if (!strcmp(args->direction, "in")) {
-		mute_in = 1;
+		direction = AST_MUTE_DIRECTION_READ;
 	} else if (!strcmp(args->direction, "out")) {
-		mute_out = 1;
+		direction = AST_MUTE_DIRECTION_WRITE;
 	} else if (!strcmp(args->direction, "both")) {
-		mute_in = 1;
-		mute_out = 1;
+		direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
 	} else {
 		stasis_http_response_error(
 			response, 400, "Bad Request",
@@ -167,13 +165,7 @@
 		return;
 	}
 
-	if (mute_in) {
-		stasis_app_control_mute(control, AST_MUTE_DIRECTION_READ, stream_type);
-	}
-
-	if (mute_out) {
-		stasis_app_control_mute(control, AST_MUTE_DIRECTION_WRITE, stream_type);
-	}
+	stasis_app_control_mute(control, direction, frametype);
 
 	stasis_http_response_no_content(response);
 }
@@ -181,9 +173,8 @@
 void stasis_http_unmute_channel(struct ast_variable *headers, struct ast_unmute_channel_args *args, struct stasis_http_response *response)
 {
 	RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
-	int unmute_in = 0;
-	int unmute_out = 0;
-	int stream_type = AST_FRAME_VOICE;
+	unsigned int direction = 0;
+	enum ast_frame_type frametype = AST_FRAME_VOICE;
 
 	control = find_control(response, args->channel_id);
 	if (control == NULL) {
@@ -191,12 +182,11 @@
 	}
 
 	if (!strcmp(args->direction, "in")) {
-		unmute_in = 1;
+		direction = AST_MUTE_DIRECTION_READ;
 	} else if (!strcmp(args->direction, "out")) {
-		unmute_out = 1;
+		direction = AST_MUTE_DIRECTION_WRITE;
 	} else if (!strcmp(args->direction, "both")) {
-		unmute_in = 1;
-		unmute_out = 1;
+		direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
 	} else {
 		stasis_http_response_error(
 			response, 400, "Bad Request",
@@ -204,13 +194,7 @@
 		return;
 	}
 
-	if (unmute_in) {
-		stasis_app_control_unmute(control, AST_MUTE_DIRECTION_READ, stream_type);
-	}
-
-	if (unmute_out) {
-		stasis_app_control_unmute(control, AST_MUTE_DIRECTION_WRITE, stream_type);
-	}
+	stasis_app_control_unmute(control, direction, frametype);
 
 	stasis_http_response_no_content(response);
 }




More information about the svn-commits mailing list