<commit_before>#include <kms-core.h>

#define KMS_SDP_SESSION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), KMS_TYPE_SDP_SESSION, KmsSdpSessionPriv))

#define LOCK(obj) (g_static_mutex_lock(&(KMS_SDP_SESSION(obj)->priv->mutex)))
#define UNLOCK(obj) (g_static_mutex_unlock(&(KMS_SDP_SESSION(obj)->priv->mutex)))

struct _KmsSdpSessionPriv {
	GStaticMutex mutex;
	gchar *addr;
	GValueArray *medias;
	gchar *name;
	glong id;
	glong version;
	gint sdp_version;
	gchar *remote_handler;
	gchar *username;
};

enum {
	PROP_0,

	PROP_ADDR,
	PROP_MEDIAS,
	PROP_NAME,
	PROP_ID,
	PROP_VERSION,
	PROP_SDP_VERSION,
	PROP_REMOTE_HANDLER,
	PROP_USERNAME
};

G_DEFINE_TYPE(KmsSdpSession, kms_sdp_session, G_TYPE_OBJECT)

static void
free_addr(KmsSdpSession *self) {
	if (self->priv->addr != NULL) {
		g_free(self->priv->addr);
		self->priv->addr = NULL;
	}
}

static void
free_name(KmsSdpSession *self) {
	if (self->priv->name != NULL) {
		g_free(self->priv->name);
		self->priv->name = NULL;
	}
}

static void
free_remote_handler(KmsSdpSession *self) {
	if (self->priv->remote_handler != NULL) {
		g_free(self->priv->remote_handler);
		self->priv->remote_handler = NULL;
	}
}

static void
free_username(KmsSdpSession *self) {
	if (self->priv->username != NULL) {
		g_free(self->priv->username);
		self->priv->username = NULL;
	}
}

static void
free_medias(KmsSdpSession *self) {
	if (self->priv->medias != NULL) {
		g_value_array_free(self->priv->medias);
		self->priv->medias = NULL;
	}
}

static gchar*
medias_to_string(GValueArray *medias) {
	GString *str = g_string_sized_new(50);
	gchar *ret;
	gint i;

	for (i = 0; i < medias->n_values; i++) {
		gchar *media_str;
		GValue *val;
		KmsSdpMedia *media;

		val = g_value_array_get_nth(medias, i);
		media = g_value_get_object(val);

		media_str = kms_sdp_media_to_string(media);

		g_string_append(str, media_str);
		g_free(media_str);
	}

	ret = str->str;
	g_string_free(str, FALSE);

	return ret;
}

gchar*
kms_sdp_session_to_string(KmsSdpSession *self) {
	gchar *str;
	gchar *medias_desc = "";

	LOCK(self);
	medias_desc = medias_to_string(self->priv->medias);

	str = g_strdup_printf("v=%d\r\n"
				"o=%s %ld %ld IN IPV4 %s\r\n"
				"s=%s\r\n"
				"c=IN IPV4 %s\r\n"
				"t=0 0\r\n"
				"%s",
				self->priv->sdp_version,
				self->priv->username,
				self->priv->id,
				self->priv->version,
				self->priv->addr,
				self->priv->name,
				self->priv->remote_handler,
				medias_desc);
	UNLOCK(self);

	g_free(medias_desc);

	return str;
}

static gboolean
compare_media_types(KmsSdpMedia *answerer, KmsSdpMedia *offerer) {
	KmsMediaType a_type, o_type;

	g_object_get(answerer, "type", &a_type, NULL);
	g_object_get(offerer, "type", &o_type, NULL);

	return (a_type == o_type);
}

void
kms_sdp_session_intersect(KmsSdpSession *answerer, KmsSdpSession *offerer,
						KmsSdpSession **neg_answ,
						KmsSdpSession **neg_off) {
	GValueArray *offerer_medias, *answerer_medias;
	GValueArray *new_answerer_medias, *new_offerer_medias;
	GSList *used = NULL;
	gint i, ii;

	g_return_if_fail(KMS_IS_SDP_SESSION(answerer));
	g_return_if_fail(KMS_IS_SDP_SESSION(offerer));

	answerer_medias = answerer->priv->medias;
	offerer_medias = offerer->priv->medias;

	new_answerer_medias = g_value_array_new(answerer_medias->n_prealloced);
	new_offerer_medias = g_value_array_new(offerer_medias->n_prealloced);

	for (i = 0; i < offerer_medias->n_values; i++) {
		KmsSdpMedia *o_media;
		KmsSdpMedia *new_a_media = NULL, *new_o_media;
		GValue aux = G_VALUE_INIT;

		o_media = g_value_get_object(g_value_array_get_nth(
							offerer_medias, i));

		for (ii = 0; ii < answerer_medias->n_values; ii++) {
			KmsSdpMedia *a_media;

			a_media = g_value_get_object(g_value_array_get_nth(
							answerer_medias, ii));

			if (!compare_media_types(o_media, a_media) ||
					g_slist_find(used, a_media) != NULL)
				continue;

			/* TODO: intersect medias
			kms_sdp_media_intersect(a_media, o_media,
						&new_a_media, &new_o_media);
			*/

			if (new_a_media != NULL) {
				used = g_slist_prepend(used, a_media);
				break;
			}
		}<commit_msg>Use sdp-media intersection to intersect sdp-session<commit_after>#include <kms-core.h>

#define KMS_SDP_SESSION_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), KMS_TYPE_SDP_SESSION, KmsSdpSessionPriv))

#define LOCK(obj) (g_static_mutex_lock(&(KMS_SDP_SESSION(obj)->priv->mutex)))
#define UNLOCK(obj) (g_static_mutex_unlock(&(KMS_SDP_SESSION(obj)->priv->mutex)))

struct _KmsSdpSessionPriv {
	GStaticMutex mutex;
	gchar *addr;
	GValueArray *medias;
	gchar *name;
	glong id;
	glong version;
	gint sdp_version;
	gchar *remote_handler;
	gchar *username;
};

enum {
	PROP_0,

	PROP_ADDR,
	PROP_MEDIAS,
	PROP_NAME,
	PROP_ID,
	PROP_VERSION,
	PROP_SDP_VERSION,
	PROP_REMOTE_HANDLER,
	PROP_USERNAME
};

G_DEFINE_TYPE(KmsSdpSession, kms_sdp_session, G_TYPE_OBJECT)

static void
free_addr(KmsSdpSession *self) {
	if (self->priv->addr != NULL) {
		g_free(self->priv->addr);
		self->priv->addr = NULL;
	}
}

static void
free_name(KmsSdpSession *self) {
	if (self->priv->name != NULL) {
		g_free(self->priv->name);
		self->priv->name = NULL;
	}
}

static void
free_remote_handler(KmsSdpSession *self) {
	if (self->priv->remote_handler != NULL) {
		g_free(self->priv->remote_handler);
		self->priv->remote_handler = NULL;
	}
}

static void
free_username(KmsSdpSession *self) {
	if (self->priv->username != NULL) {
		g_free(self->priv->username);
		self->priv->username = NULL;
	}
}

static void
free_medias(KmsSdpSession *self) {
	if (self->priv->medias != NULL) {
		g_value_array_free(self->priv->medias);
		self->priv->medias = NULL;
	}
}

static gchar*
medias_to_string(GValueArray *medias) {
	GString *str = g_string_sized_new(50);
	gchar *ret;
	gint i;

	for (i = 0; i < medias->n_values; i++) {
		gchar *media_str;
		GValue *val;
		KmsSdpMedia *media;

		val = g_value_array_get_nth(medias, i);
		media = g_value_get_object(val);

		media_str = kms_sdp_media_to_string(media);

		g_string_append(str, media_str);
		g_free(media_str);
	}

	ret = str->str;
	g_string_free(str, FALSE);

	return ret;
}

gchar*
kms_sdp_session_to_string(KmsSdpSession *self) {
	gchar *str;
	gchar *medias_desc = "";

	LOCK(self);
	medias_desc = medias_to_string(self->priv->medias);

	str = g_strdup_printf("v=%d\r\n"
				"o=%s %ld %ld IN IPV4 %s\r\n"
				"s=%s\r\n"
				"c=IN IPV4 %s\r\n"
				"t=0 0\r\n"
				"%s",
				self->priv->sdp_version,
				self->priv->username,
				self->priv->id,
				self->priv->version,
				self->priv->addr,
				self->priv->name,
				self->priv->remote_handler,
				medias_desc);
	UNLOCK(self);

	g_free(medias_desc);

	return str;
}

static gboolean
compare_media_types(KmsSdpMedia *answerer, KmsSdpMedia *offerer) {
	KmsMediaType a_type, o_type;

	g_object_get(answerer, "type", &a_type, NULL);
	g_object_get(offerer, "type", &o_type, NULL);

	return (a_type == o_type);
}

void
kms_sdp_session_intersect(KmsSdpSession *answerer, KmsSdpSession *offerer,
						KmsSdpSession **neg_answ,
						KmsSdpSession **neg_off) {
	GValueArray *offerer_medias, *answerer_medias;
	GValueArray *new_answerer_medias, *new_offerer_medias;
	GSList *used = NULL;
	gint i, ii;

	g_return_if_fail(KMS_IS_SDP_SESSION(answerer));
	g_return_if_fail(KMS_IS_SDP_SESSION(offerer));

	answerer_medias = answerer->priv->medias;
	offerer_medias = offerer->priv->medias;

	new_answerer_medias = g_value_array_new(answerer_medias->n_prealloced);
	new_offerer_medias = g_value_array_new(offerer_medias->n_prealloced);

	for (i = 0; i < offerer_medias->n_values; i++) {
		KmsSdpMedia *o_media;
		KmsSdpMedia *new_a_media = NULL, *new_o_media;
		GValue aux = G_VALUE_INIT;

		o_media = g_value_get_object(g_value_array_get_nth(
							offerer_medias, i));

		for (ii = 0; ii < answerer_medias->n_values; ii++) {
			KmsSdpMedia *a_media;

			a_media = g_value_get_object(g_value_array_get_nth(
							answerer_medias, ii));

			if (!compare_media_types(o_media, a_media) ||
					g_slist_find(used, a_media) != NULL)
				continue;

			kms_sdp_media_intersect(a_media, o_media,
						&new_a_media, &new_o_media);

			if (new_a_media != NULL) {
				used = g_slist_prepend(used, a_media);
				break;
			}
		}
{'fraction_non_alphanumeric': 0.13896725131853305, 'fraction_numerical': 0.0024530847540782533, 'mean_word_length': 4.870410367170626, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1045652', 'n_tokens_mistral': 3787, 'n_tokens_neox': 3503, 'n_words': 554}