Merge "stasis_bridges: Remove silly usage of RAII_VAR."

This commit is contained in:
Jenkins2 2018-01-11 06:43:33 -06:00 committed by Gerrit Code Review
commit ae723f85ee
1 changed files with 58 additions and 44 deletions

View File

@ -233,7 +233,7 @@ static void bridge_snapshot_dtor(void *obj)
struct ast_bridge_snapshot *ast_bridge_snapshot_create(struct ast_bridge *bridge) struct ast_bridge_snapshot *ast_bridge_snapshot_create(struct ast_bridge *bridge)
{ {
RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup); struct ast_bridge_snapshot *snapshot;
struct ast_bridge_channel *bridge_channel; struct ast_bridge_channel *bridge_channel;
if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) { if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) {
@ -249,17 +249,22 @@ struct ast_bridge_snapshot *ast_bridge_snapshot_create(struct ast_bridge *bridge
if (ast_string_field_init(snapshot, 128) if (ast_string_field_init(snapshot, 128)
|| ast_string_field_init_extended(snapshot, video_source_id)) { || ast_string_field_init_extended(snapshot, video_source_id)) {
ao2_ref(snapshot, -1); ao2_ref(snapshot, -1);
return NULL; return NULL;
} }
snapshot->channels = ast_str_container_alloc(SNAPSHOT_CHANNELS_BUCKETS); snapshot->channels = ast_str_container_alloc(SNAPSHOT_CHANNELS_BUCKETS);
if (!snapshot->channels) { if (!snapshot->channels) {
ao2_ref(snapshot, -1);
return NULL; return NULL;
} }
AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) { AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
if (ast_str_container_add(snapshot->channels, if (ast_str_container_add(snapshot->channels,
ast_channel_uniqueid(bridge_channel->chan))) { ast_channel_uniqueid(bridge_channel->chan))) {
ao2_ref(snapshot, -1);
return NULL; return NULL;
} }
} }
@ -285,14 +290,13 @@ struct ast_bridge_snapshot *ast_bridge_snapshot_create(struct ast_bridge *bridge
ast_channel_uniqueid(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc)); ast_channel_uniqueid(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc));
} }
ao2_ref(snapshot, +1);
return snapshot; return snapshot;
} }
void ast_bridge_publish_state(struct ast_bridge *bridge) void ast_bridge_publish_state(struct ast_bridge *bridge)
{ {
RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup); struct ast_bridge_snapshot *snapshot;
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); struct stasis_message *msg;
if (!ast_bridge_snapshot_type()) { if (!ast_bridge_snapshot_type()) {
return; return;
@ -306,17 +310,19 @@ void ast_bridge_publish_state(struct ast_bridge *bridge)
} }
msg = stasis_message_create(ast_bridge_snapshot_type(), snapshot); msg = stasis_message_create(ast_bridge_snapshot_type(), snapshot);
ao2_ref(snapshot, -1);
if (!msg) { if (!msg) {
return; return;
} }
stasis_publish(ast_bridge_topic(bridge), msg); stasis_publish(ast_bridge_topic(bridge), msg);
ao2_ref(msg, -1);
} }
static void bridge_publish_state_from_blob(struct ast_bridge *bridge, static void bridge_publish_state_from_blob(struct ast_bridge *bridge,
struct ast_bridge_blob *obj) struct ast_bridge_blob *obj)
{ {
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); struct stasis_message *msg;
ast_assert(obj != NULL); ast_assert(obj != NULL);
@ -326,6 +332,7 @@ static void bridge_publish_state_from_blob(struct ast_bridge *bridge,
} }
stasis_publish(ast_bridge_topic(bridge), msg); stasis_publish(ast_bridge_topic(bridge), msg);
ao2_ref(msg, -1);
} }
/*! \brief Destructor for bridge merge messages */ /*! \brief Destructor for bridge merge messages */
@ -342,7 +349,7 @@ static void bridge_merge_message_dtor(void *obj)
/*! \brief Bridge merge message creation helper */ /*! \brief Bridge merge message creation helper */
static struct ast_bridge_merge_message *bridge_merge_message_create(struct ast_bridge *to, struct ast_bridge *from) static struct ast_bridge_merge_message *bridge_merge_message_create(struct ast_bridge *to, struct ast_bridge *from)
{ {
RAII_VAR(struct ast_bridge_merge_message *, msg, NULL, ao2_cleanup); struct ast_bridge_merge_message *msg;
msg = ao2_alloc(sizeof(*msg), bridge_merge_message_dtor); msg = ao2_alloc(sizeof(*msg), bridge_merge_message_dtor);
if (!msg) { if (!msg) {
@ -350,16 +357,13 @@ static struct ast_bridge_merge_message *bridge_merge_message_create(struct ast_b
} }
msg->to = ast_bridge_snapshot_create(to); msg->to = ast_bridge_snapshot_create(to);
if (!msg->to) {
return NULL;
}
msg->from = ast_bridge_snapshot_create(from); msg->from = ast_bridge_snapshot_create(from);
if (!msg->from) { if (!msg->to || !msg->from) {
ao2_ref(msg, -1);
return NULL; return NULL;
} }
ao2_ref(msg, +1);
return msg; return msg;
} }
@ -368,16 +372,17 @@ static struct ast_json *ast_bridge_merge_message_to_json(
const struct stasis_message_sanitizer *sanitize) const struct stasis_message_sanitizer *sanitize)
{ {
struct ast_bridge_merge_message *merge = stasis_message_data(msg); struct ast_bridge_merge_message *merge = stasis_message_data(msg);
RAII_VAR(struct ast_json *, json_bridge_to, struct ast_json *json_bridge_to = ast_bridge_snapshot_to_json(merge->to, sanitize);
ast_bridge_snapshot_to_json(merge->to, sanitize), ast_json_unref); struct ast_json *json_bridge_from = ast_bridge_snapshot_to_json(merge->from, sanitize);
RAII_VAR(struct ast_json *, json_bridge_from,
ast_bridge_snapshot_to_json(merge->from, sanitize), ast_json_unref);
if (!json_bridge_to || !json_bridge_from) { if (!json_bridge_to || !json_bridge_from) {
ast_json_unref(json_bridge_to);
ast_json_unref(json_bridge_from);
return NULL; return NULL;
} }
return ast_json_pack("{s: s, s: o, s: O, s: O}", return ast_json_pack("{s: s, s: o, s: o, s: o}",
"type", "BridgeMerged", "type", "BridgeMerged",
"timestamp", ast_json_timeval(*stasis_message_timestamp(msg), NULL), "timestamp", ast_json_timeval(*stasis_message_timestamp(msg), NULL),
"bridge", json_bridge_to, "bridge", json_bridge_to,
@ -386,8 +391,8 @@ static struct ast_json *ast_bridge_merge_message_to_json(
void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from) void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from)
{ {
RAII_VAR(struct ast_bridge_merge_message *, merge_msg, NULL, ao2_cleanup); struct ast_bridge_merge_message *merge_msg;
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); struct stasis_message *msg;
if (!ast_bridge_merge_message_type()) { if (!ast_bridge_merge_message_type()) {
return; return;
@ -404,11 +409,13 @@ void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from)
} }
msg = stasis_message_create(ast_bridge_merge_message_type(), merge_msg); msg = stasis_message_create(ast_bridge_merge_message_type(), merge_msg);
ao2_ref(merge_msg, -1);
if (!msg) { if (!msg) {
return; return;
} }
stasis_publish(ast_bridge_topic_all(), msg); stasis_publish(ast_bridge_topic_all(), msg);
ao2_ref(msg, -1);
} }
static void bridge_blob_dtor(void *obj) static void bridge_blob_dtor(void *obj)
@ -428,8 +435,8 @@ struct stasis_message *ast_bridge_blob_create(
struct ast_channel *chan, struct ast_channel *chan,
struct ast_json *blob) struct ast_json *blob)
{ {
RAII_VAR(struct ast_bridge_blob *, obj, NULL, ao2_cleanup); struct ast_bridge_blob *obj;
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); struct stasis_message *msg;
if (!message_type) { if (!message_type) {
return NULL; return NULL;
@ -443,6 +450,8 @@ struct stasis_message *ast_bridge_blob_create(
if (bridge) { if (bridge) {
obj->bridge = ast_bridge_snapshot_create(bridge); obj->bridge = ast_bridge_snapshot_create(bridge);
if (obj->bridge == NULL) { if (obj->bridge == NULL) {
ao2_ref(obj, -1);
return NULL; return NULL;
} }
} }
@ -450,6 +459,8 @@ struct stasis_message *ast_bridge_blob_create(
if (chan) { if (chan) {
obj->channel = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan)); obj->channel = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan));
if (obj->channel == NULL) { if (obj->channel == NULL) {
ao2_ref(obj, -1);
return NULL; return NULL;
} }
} }
@ -459,19 +470,16 @@ struct stasis_message *ast_bridge_blob_create(
} }
msg = stasis_message_create(message_type, obj); msg = stasis_message_create(message_type, obj);
if (!msg) { ao2_ref(obj, -1);
return NULL;
}
ao2_ref(msg, +1);
return msg; return msg;
} }
void ast_bridge_publish_enter(struct ast_bridge *bridge, struct ast_channel *chan, void ast_bridge_publish_enter(struct ast_bridge *bridge, struct ast_channel *chan,
struct ast_channel *swap) struct ast_channel *swap)
{ {
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); struct stasis_message *msg;
RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref); struct ast_json *blob = NULL;
if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) { if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) {
return; return;
@ -485,6 +493,7 @@ void ast_bridge_publish_enter(struct ast_bridge *bridge, struct ast_channel *cha
} }
msg = ast_bridge_blob_create(ast_channel_entered_bridge_type(), bridge, chan, blob); msg = ast_bridge_blob_create(ast_channel_entered_bridge_type(), bridge, chan, blob);
ast_json_unref(blob);
if (!msg) { if (!msg) {
return; return;
} }
@ -492,11 +501,12 @@ void ast_bridge_publish_enter(struct ast_bridge *bridge, struct ast_channel *cha
/* enter blob first, then state */ /* enter blob first, then state */
stasis_publish(ast_bridge_topic(bridge), msg); stasis_publish(ast_bridge_topic(bridge), msg);
bridge_publish_state_from_blob(bridge, stasis_message_data(msg)); bridge_publish_state_from_blob(bridge, stasis_message_data(msg));
ao2_ref(msg, -1);
} }
void ast_bridge_publish_leave(struct ast_bridge *bridge, struct ast_channel *chan) void ast_bridge_publish_leave(struct ast_bridge *bridge, struct ast_channel *chan)
{ {
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); struct stasis_message *msg;
if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) { if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_INVISIBLE)) {
return; return;
@ -509,6 +519,7 @@ void ast_bridge_publish_leave(struct ast_bridge *bridge, struct ast_channel *cha
/* state first, then leave blob (opposite of enter, preserves nesting of events) */ /* state first, then leave blob (opposite of enter, preserves nesting of events) */
bridge_publish_state_from_blob(bridge, stasis_message_data(msg)); bridge_publish_state_from_blob(bridge, stasis_message_data(msg));
stasis_publish(ast_bridge_topic(bridge), msg); stasis_publish(ast_bridge_topic(bridge), msg);
ao2_ref(msg, -1);
} }
static struct ast_json *simple_bridge_channel_event( static struct ast_json *simple_bridge_channel_event(
@ -518,16 +529,17 @@ static struct ast_json *simple_bridge_channel_event(
const struct timeval *tv, const struct timeval *tv,
const struct stasis_message_sanitizer *sanitize) const struct stasis_message_sanitizer *sanitize)
{ {
RAII_VAR(struct ast_json *, json_bridge, struct ast_json *json_bridge = ast_bridge_snapshot_to_json(bridge_snapshot, sanitize);
ast_bridge_snapshot_to_json(bridge_snapshot, sanitize), ast_json_unref); struct ast_json *json_channel = ast_channel_snapshot_to_json(channel_snapshot, sanitize);
RAII_VAR(struct ast_json *, json_channel,
ast_channel_snapshot_to_json(channel_snapshot, sanitize), ast_json_unref);
if (!json_bridge || !json_channel) { if (!json_bridge || !json_channel) {
ast_json_unref(json_bridge);
ast_json_unref(json_channel);
return NULL; return NULL;
} }
return ast_json_pack("{s: s, s: o, s: O, s: O}", return ast_json_pack("{s: s, s: o, s: o, s: o}",
"type", type, "type", type,
"timestamp", ast_json_timeval(*tv, NULL), "timestamp", ast_json_timeval(*tv, NULL),
"bridge", json_bridge, "bridge", json_bridge,
@ -557,9 +569,10 @@ struct ast_json *ast_channel_left_bridge_to_json(
static struct ast_json *container_to_json_array(struct ao2_container *items, static struct ast_json *container_to_json_array(struct ao2_container *items,
const struct stasis_message_sanitizer *sanitize) const struct stasis_message_sanitizer *sanitize)
{ {
RAII_VAR(struct ast_json *, json_items, ast_json_array_create(), ast_json_unref); struct ast_json *json_items = ast_json_array_create();
char *item; char *item;
struct ao2_iterator it; struct ao2_iterator it;
if (!json_items) { if (!json_items) {
return NULL; return NULL;
} }
@ -573,12 +586,14 @@ static struct ast_json *container_to_json_array(struct ao2_container *items,
if (ast_json_array_append(json_items, ast_json_string_create(item))) { if (ast_json_array_append(json_items, ast_json_string_create(item))) {
ao2_cleanup(item); ao2_cleanup(item);
ao2_iterator_destroy(&it); ao2_iterator_destroy(&it);
ast_json_unref(json_items);
return NULL; return NULL;
} }
} }
ao2_iterator_destroy(&it); ao2_iterator_destroy(&it);
return ast_json_ref(json_items); return json_items;
} }
static const char *capability2str(uint32_t capabilities) static const char *capability2str(uint32_t capabilities)
@ -594,7 +609,7 @@ struct ast_json *ast_bridge_snapshot_to_json(
const struct ast_bridge_snapshot *snapshot, const struct ast_bridge_snapshot *snapshot,
const struct stasis_message_sanitizer *sanitize) const struct stasis_message_sanitizer *sanitize)
{ {
RAII_VAR(struct ast_json *, json_bridge, NULL, ast_json_unref); struct ast_json *json_bridge;
struct ast_json *json_channels; struct ast_json *json_channels;
if (snapshot == NULL) { if (snapshot == NULL) {
@ -625,7 +640,7 @@ struct ast_json *ast_bridge_snapshot_to_json(
ast_json_string_create(snapshot->video_source_id)); ast_json_string_create(snapshot->video_source_id));
} }
return ast_json_ref(json_bridge); return json_bridge;
} }
/*! /*!
@ -1225,7 +1240,7 @@ int ast_attended_transfer_message_add_link(struct ast_attended_transfer_message
void ast_bridge_publish_attended_transfer(struct ast_attended_transfer_message *transfer_msg) void ast_bridge_publish_attended_transfer(struct ast_attended_transfer_message *transfer_msg)
{ {
RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup); struct stasis_message *msg;
msg = stasis_message_create(ast_attended_transfer_type(), transfer_msg); msg = stasis_message_create(ast_attended_transfer_type(), transfer_msg);
if (!msg) { if (!msg) {
@ -1233,11 +1248,12 @@ void ast_bridge_publish_attended_transfer(struct ast_attended_transfer_message *
} }
stasis_publish(ast_bridge_topic_all(), msg); stasis_publish(ast_bridge_topic_all(), msg);
ao2_ref(msg, -1);
} }
struct ast_bridge_snapshot *ast_bridge_snapshot_get_latest(const char *uniqueid) struct ast_bridge_snapshot *ast_bridge_snapshot_get_latest(const char *uniqueid)
{ {
RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup); struct stasis_message *message;
struct ast_bridge_snapshot *snapshot; struct ast_bridge_snapshot *snapshot;
ast_assert(!ast_strlen_zero(uniqueid)); ast_assert(!ast_strlen_zero(uniqueid));
@ -1249,11 +1265,9 @@ struct ast_bridge_snapshot *ast_bridge_snapshot_get_latest(const char *uniqueid)
return NULL; return NULL;
} }
snapshot = stasis_message_data(message); snapshot = ao2_bump(stasis_message_data(message));
if (!snapshot) { ao2_ref(message, -1);
return NULL;
}
ao2_ref(snapshot, +1);
return snapshot; return snapshot;
} }