MT#56128 SDP manipulations: mode `substitute` to GHashTable

For efficiency reasons `substitute` kind of SDP manipulations
commands have been moved to the `GHashTable` instead of the `GQueue`.

Change-Id: Iec6c44109ae912ba0de440bbed6ecaee6a238b97
pull/1657/head
Donat Zenichev 2 years ago
parent 4e908d8128
commit 643c12e0b9

@ -601,6 +601,8 @@ INLINE void ng_sdp_attr_manipulations(struct sdp_manipulations_common ** sm_ptr,
bencode_item_t *command_action = it->sibling ? it->sibling : NULL; bencode_item_t *command_action = it->sibling ? it->sibling : NULL;
enum media_type media; enum media_type media;
str media_type; str media_type;
GQueue * q_ptr = NULL;
GHashTable ** ht = NULL;
if (!command_action) /* if no action, makes no sense to continue */ if (!command_action) /* if no action, makes no sense to continue */
continue; continue;
@ -624,29 +626,30 @@ INLINE void ng_sdp_attr_manipulations(struct sdp_manipulations_common ** sm_ptr,
if (!bencode_get_str(it_c, &command_type)) if (!bencode_get_str(it_c, &command_type))
continue; continue;
GQueue * q_ptr = NULL;
switch (__csh_lookup(&command_type)) { switch (__csh_lookup(&command_type)) {
/* CMD_ADD / CMD_SUBST commands */ /* CMD_ADD / CMD_SUBST commands */
case CSH_LOOKUP("substitute"):; case CSH_LOOKUP("substitute"):;
struct sdp_substitute_attr * subst_command = NULL;
switch (media) { switch (media) {
case MT_UNKNOWN: case MT_UNKNOWN:
q_ptr = &(*sm_ptr)->subst_commands_glob; ht = &(*sm_ptr)->subst_commands_glob;
break; break;
case MT_AUDIO: case MT_AUDIO:
q_ptr = &(*sm_ptr)->subst_commands_audio; ht = &(*sm_ptr)->subst_commands_audio;
break; break;
case MT_VIDEO: case MT_VIDEO:
q_ptr = &(*sm_ptr)->subst_commands_video; ht = &(*sm_ptr)->subst_commands_video;
break; break;
default: default:
ilog(LOG_WARN, "SDP manipulations: unspported SDP section targeted."); ilog(LOG_WARN, "SDP manipulations: unsupported SDP section targeted.");
continue; continue;
} }
/* a table can already be allocated by similar commands in previous iterations */
if (!*ht)
*ht = g_hash_table_new_full(str_case_hash, str_case_equal, free, free);
for (bencode_item_t *it_v = command_value->child; it_v; it_v = it_v->sibling) for (bencode_item_t *it_v = command_value->child; it_v; it_v = it_v->sibling)
{ {
str from; str from;
@ -666,11 +669,7 @@ INLINE void ng_sdp_attr_manipulations(struct sdp_manipulations_common ** sm_ptr,
continue; continue;
} }
subst_command = g_slice_alloc0(sizeof(*subst_command)); g_hash_table_replace(*ht, s_copy_from, s_copy_to);
subst_command->value_a = s_copy_from;
subst_command->value_b = s_copy_to;
g_queue_push_tail(q_ptr, subst_command);
} }
break; break;
@ -704,8 +703,6 @@ INLINE void ng_sdp_attr_manipulations(struct sdp_manipulations_common ** sm_ptr,
/* CMD_REM commands */ /* CMD_REM commands */
case CSH_LOOKUP("remove"):; case CSH_LOOKUP("remove"):;
GHashTable ** ht = NULL;
switch (media) { switch (media) {
case MT_UNKNOWN: case MT_UNKNOWN:
ht = &(*sm_ptr)->rem_commands_glob; ht = &(*sm_ptr)->rem_commands_glob;
@ -1817,20 +1814,7 @@ static void call_ng_process_flags(struct sdp_ng_flags *out, bencode_item_t *inpu
call_ng_dict_iter(out, input, call_ng_main_flags); call_ng_dict_iter(out, input, call_ng_main_flags);
} }
static void free_subst_attr(void *p) {
struct sdp_substitute_attr *attr = p;
if (attr->value_a)
free(attr->value_a);
if (attr->value_b)
free(attr->value_b);
g_slice_free1(sizeof(*attr), attr);
}
static void ng_sdp_attr_manipulations_free(struct sdp_manipulations_common * sdp_manipulations) { static void ng_sdp_attr_manipulations_free(struct sdp_manipulations_common * sdp_manipulations) {
GQueue *cpq_subst_glob = &sdp_manipulations->subst_commands_glob;
GQueue *cpq_subst_audio = &sdp_manipulations->subst_commands_audio;
GQueue *cpq_subst_video = &sdp_manipulations->subst_commands_video;
if (sdp_manipulations->rem_commands_glob) if (sdp_manipulations->rem_commands_glob)
g_hash_table_destroy(sdp_manipulations->rem_commands_glob); g_hash_table_destroy(sdp_manipulations->rem_commands_glob);
if (sdp_manipulations->rem_commands_audio) if (sdp_manipulations->rem_commands_audio)
@ -1838,17 +1822,17 @@ static void ng_sdp_attr_manipulations_free(struct sdp_manipulations_common * sdp
if (sdp_manipulations->rem_commands_video) if (sdp_manipulations->rem_commands_video)
g_hash_table_destroy(sdp_manipulations->rem_commands_video); g_hash_table_destroy(sdp_manipulations->rem_commands_video);
if (sdp_manipulations->subst_commands_glob)
g_hash_table_destroy(sdp_manipulations->subst_commands_glob);
if (sdp_manipulations->subst_commands_audio)
g_hash_table_destroy(sdp_manipulations->subst_commands_audio);
if (sdp_manipulations->subst_commands_video)
g_hash_table_destroy(sdp_manipulations->subst_commands_video);
g_queue_clear_full(&sdp_manipulations->add_commands_glob, free); g_queue_clear_full(&sdp_manipulations->add_commands_glob, free);
g_queue_clear_full(&sdp_manipulations->add_commands_audio, free); g_queue_clear_full(&sdp_manipulations->add_commands_audio, free);
g_queue_clear_full(&sdp_manipulations->add_commands_video, free); g_queue_clear_full(&sdp_manipulations->add_commands_video, free);
if (cpq_subst_glob && cpq_subst_glob->head)
g_queue_clear_full(cpq_subst_glob, free_subst_attr);
if (cpq_subst_audio && cpq_subst_audio->head)
g_queue_clear_full(cpq_subst_audio, free_subst_attr);
if (cpq_subst_video && cpq_subst_video->head)
g_queue_clear_full(cpq_subst_video, free_subst_attr);
g_slice_free1(sizeof(*sdp_manipulations), sdp_manipulations); g_slice_free1(sizeof(*sdp_manipulations), sdp_manipulations);
} }

@ -291,15 +291,6 @@ static void attr_free(void *p);
static void attr_insert(struct sdp_attributes *attrs, struct sdp_attribute *attr); static void attr_insert(struct sdp_attributes *attrs, struct sdp_attribute *attr);
INLINE void chopper_append_c(struct sdp_chopper *c, const char *s); INLINE void chopper_append_c(struct sdp_chopper *c, const char *s);
/**
* Helper, which compares a substitute command for SDP manipulations.
*/
static int sdp_manipulate_subst_cmp(gconstpointer a, gconstpointer b) {
const struct sdp_substitute_attr * subst = a;
const struct sdp_substitute_attr * subst_lookup = b;
return str_cmp_str(subst->value_a, subst_lookup->value_a);
}
/** /**
* Checks whether a given type of SDP manipulation exists for a given session level. * Checks whether a given type of SDP manipulation exists for a given session level.
*/ */
@ -320,32 +311,32 @@ static int sdp_manipulate_check(enum command_type command_type,
} }
GQueue * q_ptr = NULL; GQueue * q_ptr = NULL;
GHashTable * ht = NULL;
switch (command_type) { switch (command_type) {
case CMD_SUBST:; case CMD_SUBST:;
q_ptr = NULL; ht = NULL;
if (!attr_name || !attr_name->len) if (!attr_name || !attr_name->len)
break; break;
struct sdp_substitute_attr fictitious = {attr_name, NULL};
switch (media_type) { switch (media_type) {
case MT_AUDIO: case MT_AUDIO:
q_ptr = &sdp_manipulations->subst_commands_audio; ht = sdp_manipulations->subst_commands_audio;
break; break;
case MT_VIDEO: case MT_VIDEO:
q_ptr = &sdp_manipulations->subst_commands_video; ht = sdp_manipulations->subst_commands_video;
break; break;
default: /* MT_UNKNOWN */ default: /* MT_UNKNOWN */
q_ptr = &sdp_manipulations->subst_commands_glob; ht = sdp_manipulations->subst_commands_glob;
} }
if (q_ptr && q_ptr->head &&
g_queue_find_custom(q_ptr, &fictitious, sdp_manipulate_subst_cmp)) str * l = g_hash_table_lookup(ht, attr_name);
if (l)
return 1; return 1;
break; break;
case CMD_ADD:; case CMD_ADD:;
q_ptr = NULL;
switch (media_type) { switch (media_type) {
case MT_AUDIO: case MT_AUDIO:
q_ptr = &sdp_manipulations->add_commands_audio; q_ptr = &sdp_manipulations->add_commands_audio;
@ -361,7 +352,7 @@ static int sdp_manipulate_check(enum command_type command_type,
break; break;
case CMD_REM:; case CMD_REM:;
GHashTable *ht = NULL; ht = NULL;
if (!attr_name || !attr_name->len) if (!attr_name || !attr_name->len)
break; break;
@ -424,28 +415,26 @@ static void sdp_manipulations_subst(struct sdp_chopper *chop,
struct sdp_manipulations_common * sdp_manipulations, struct sdp_manipulations_common * sdp_manipulations,
enum media_type media_type, str * attr_name) { enum media_type media_type, str * attr_name) {
GQueue * q_ptr = NULL; GHashTable * ht = NULL;
switch (media_type) { switch (media_type) {
case MT_AUDIO: case MT_AUDIO:
q_ptr = &sdp_manipulations->subst_commands_audio; ht = sdp_manipulations->subst_commands_audio;
break; break;
case MT_VIDEO: case MT_VIDEO:
q_ptr = &sdp_manipulations->subst_commands_video; ht = sdp_manipulations->subst_commands_video;
break; break;
default: /* MT_UNKNOWN */ default: /* MT_UNKNOWN */
q_ptr = &sdp_manipulations->subst_commands_glob; ht = sdp_manipulations->subst_commands_glob;
} }
for (GList *l = q_ptr->head; l; l = l->next) str * cmd_subst_value = g_hash_table_lookup(ht, attr_name);
{ if (!cmd_subst_value)
struct sdp_substitute_attr * attr_value = l->data; return;
if (!str_cmp_str(attr_name, attr_value->value_a)) { chopper_append_c(chop, "a=");
chopper_append_c(chop, "a="); chopper_append_c(chop, cmd_subst_value->s);
chopper_append_c(chop, attr_value->value_b->s); chopper_append_c(chop, "\r\n");
chopper_append_c(chop, "\r\n");
}
}
} }
static void append_attr_to_gstring(GString *s, char * name, const str * value, static void append_attr_to_gstring(GString *s, char * name, const str * value,

@ -13,12 +13,6 @@ enum command_type {
CMD_SUBST, CMD_SUBST,
}; };
/* */
struct sdp_substitute_attr {
str * value_a; /* from */
str * value_b; /* to */
};
/* A structure for SDP arbitrary manipulations on all levels of SDP: /* A structure for SDP arbitrary manipulations on all levels of SDP:
* session (global), media (audio/video). Works only on `a=` lines. * session (global), media (audio/video). Works only on `a=` lines.
*/ */
@ -31,9 +25,9 @@ struct sdp_manipulations_common {
GHashTable * rem_commands_audio; GHashTable * rem_commands_audio;
GHashTable * rem_commands_video; GHashTable * rem_commands_video;
GQueue subst_commands_glob; GHashTable * subst_commands_glob;
GQueue subst_commands_audio; GHashTable * subst_commands_audio;
GQueue subst_commands_video; GHashTable * subst_commands_video;
}; };
struct ice_candidate; struct ice_candidate;

Loading…
Cancel
Save