gstreamer/ext/dtls/gstdtlsdec.c

649 lines
18 KiB
C
Raw Normal View History

/*
* Copyright (c) 2014, Ericsson AB. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gstdtlsdec.h"
#include "gstdtlscertificate.h"
2015-03-16 16:34:05 +00:00
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("application/x-dtls")
);
2015-03-16 16:34:05 +00:00
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
GST_PAD_SRC,
GST_PAD_REQUEST,
GST_STATIC_CAPS_ANY);
2015-03-16 16:34:05 +00:00
GST_DEBUG_CATEGORY_STATIC (er_dtls_dec_debug);
#define GST_CAT_DEFAULT er_dtls_dec_debug
#define gst_er_dtls_dec_parent_class parent_class
2015-03-16 16:34:05 +00:00
G_DEFINE_TYPE_WITH_CODE (GstErDtlsDec, gst_er_dtls_dec, GST_TYPE_ELEMENT,
GST_DEBUG_CATEGORY_INIT (er_dtls_dec_debug, "erdtlsdec", 0,
"Ericsson DTLS Decoder"));
#define UNUSED(param) while (0) { (void)(param); }
2015-03-16 16:34:05 +00:00
enum
{
SIGNAL_ON_KEY_RECEIVED,
NUM_SIGNALS
};
static guint signals[NUM_SIGNALS];
2015-03-16 16:34:05 +00:00
enum
{
PROP_0,
PROP_CONNECTION_ID,
PROP_PEM,
PROP_PEER_PEM,
PROP_DECODER_KEY,
PROP_SRTP_CIPHER,
PROP_SRTP_AUTH,
NUM_PROPERTIES
};
static GParamSpec *properties[NUM_PROPERTIES];
#define DEFAULT_CONNECTION_ID NULL
#define DEFAULT_PEM NULL
#define DEFAULT_PEER_PEM NULL
#define DEFAULT_DECODER_KEY NULL
#define DEFAULT_SRTP_CIPHER 0
#define DEFAULT_SRTP_AUTH 0
2015-03-16 16:34:05 +00:00
static void gst_er_dtls_dec_finalize (GObject *);
static void gst_er_dtls_dec_dispose (GObject *);
static void gst_er_dtls_dec_set_property (GObject *, guint prop_id,
const GValue *, GParamSpec *);
static void gst_er_dtls_dec_get_property (GObject *, guint prop_id, GValue *,
GParamSpec *);
static GstStateChangeReturn gst_er_dtls_dec_change_state (GstElement *,
GstStateChange);
static GstPad *gst_er_dtls_dec_request_new_pad (GstElement *, GstPadTemplate *,
const gchar * name, const GstCaps *);
static void gst_er_dtls_dec_release_pad (GstElement *, GstPad *);
static void on_key_received (ErDtlsConnection *, gpointer key, guint cipher,
guint auth, GstErDtlsDec *);
static gboolean on_peer_certificate_received (ErDtlsConnection *, gchar * pem,
GstErDtlsDec *);
static GstFlowReturn sink_chain (GstPad *, GstObject * parent, GstBuffer *);
static ErDtlsAgent *get_agent_by_pem (const gchar * pem);
static void agent_weak_ref_notify (gchar * pem, ErDtlsAgent *);
static void create_connection (GstErDtlsDec *, gchar * id);
static void connection_weak_ref_notify (gchar * id, ErDtlsConnection *);
static void
gst_er_dtls_dec_class_init (GstErDtlsDecClass * klass)
{
2015-03-16 16:34:05 +00:00
GObjectClass *gobject_class;
GstElementClass *element_class;
gobject_class = (GObjectClass *) klass;
element_class = (GstElementClass *) klass;
gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_er_dtls_dec_finalize);
gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_er_dtls_dec_dispose);
gobject_class->set_property =
GST_DEBUG_FUNCPTR (gst_er_dtls_dec_set_property);
gobject_class->get_property =
GST_DEBUG_FUNCPTR (gst_er_dtls_dec_get_property);
element_class->change_state =
GST_DEBUG_FUNCPTR (gst_er_dtls_dec_change_state);
element_class->request_new_pad =
GST_DEBUG_FUNCPTR (gst_er_dtls_dec_request_new_pad);
element_class->release_pad = GST_DEBUG_FUNCPTR (gst_er_dtls_dec_release_pad);
signals[SIGNAL_ON_KEY_RECEIVED] =
g_signal_new ("on-key-received", G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST, 0, NULL, NULL,
g_cclosure_marshal_generic, G_TYPE_NONE, 0);
properties[PROP_CONNECTION_ID] =
g_param_spec_string ("connection-id",
"Connection id",
"Every encoder/decoder pair should have the same, unique, connection-id",
DEFAULT_CONNECTION_ID, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
properties[PROP_PEM] =
g_param_spec_string ("pem",
"PEM string",
"A string containing a X509 certificate and RSA private key in PEM format",
DEFAULT_PEM, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
properties[PROP_PEER_PEM] =
g_param_spec_string ("peer-pem",
"Peer PEM string",
"The X509 certificate received in the DTLS handshake, in PEM format",
DEFAULT_PEER_PEM, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
properties[PROP_DECODER_KEY] =
g_param_spec_boxed ("decoder-key",
"Decoder key",
"SRTP key that should be used by the decider",
GST_TYPE_CAPS, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
properties[PROP_SRTP_CIPHER] =
g_param_spec_uint ("srtp-cipher",
"SRTP cipher",
"The SRTP cipher selected in the DTLS handshake. "
"The value will be set to an ErDtlsSrtpCipher.",
0, ER_DTLS_SRTP_CIPHER_AES_128_ICM, DEFAULT_SRTP_CIPHER,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
properties[PROP_SRTP_AUTH] =
g_param_spec_uint ("srtp-auth",
"SRTP authentication",
"The SRTP authentication selected in the DTLS handshake. "
"The value will be set to an ErDtlsSrtpAuth.",
0, ER_DTLS_SRTP_AUTH_HMAC_SHA1_80, DEFAULT_SRTP_AUTH,
G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&src_template));
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sink_template));
gst_element_class_set_static_metadata (element_class,
"DTLS Decoder",
"Decoder/Network/DTLS",
"Decodes DTLS packets", "Patrik Oldsberg patrik.oldsberg@ericsson.com");
}
2015-03-16 16:34:05 +00:00
static void
gst_er_dtls_dec_init (GstErDtlsDec * self)
{
2015-03-16 16:34:05 +00:00
GstPad *sink;
self->agent = get_agent_by_pem (NULL);
self->connection_id = NULL;
self->connection = NULL;
self->peer_pem = NULL;
2015-03-16 16:34:05 +00:00
self->decoder_key = NULL;
self->srtp_cipher = DEFAULT_SRTP_CIPHER;
self->srtp_auth = DEFAULT_SRTP_AUTH;
2015-03-16 16:34:05 +00:00
g_mutex_init (&self->src_mutex);
2015-03-16 16:34:05 +00:00
self->src = NULL;
sink = gst_pad_new_from_static_template (&sink_template, "sink");
g_return_if_fail (sink);
2015-03-16 16:34:05 +00:00
gst_pad_set_chain_function (sink, GST_DEBUG_FUNCPTR (sink_chain));
2015-03-16 16:34:05 +00:00
gst_element_add_pad (GST_ELEMENT (self), sink);
}
2015-03-16 16:34:05 +00:00
static void
gst_er_dtls_dec_finalize (GObject * object)
{
2015-03-16 16:34:05 +00:00
GstErDtlsDec *self = GST_ER_DTLS_DEC (object);
2015-03-16 16:34:05 +00:00
if (self->decoder_key) {
gst_buffer_unref (self->decoder_key);
self->decoder_key = NULL;
}
2015-03-16 16:34:05 +00:00
g_free (self->connection_id);
self->connection_id = NULL;
2015-03-16 16:34:05 +00:00
g_free (self->peer_pem);
self->peer_pem = NULL;
2015-03-16 16:34:05 +00:00
g_mutex_clear (&self->src_mutex);
2015-03-16 16:34:05 +00:00
GST_LOG_OBJECT (self, "finalized");
2015-03-16 16:34:05 +00:00
G_OBJECT_CLASS (parent_class)->finalize (object);
}
2015-03-16 16:34:05 +00:00
static void
gst_er_dtls_dec_dispose (GObject * object)
{
2015-03-16 16:34:05 +00:00
GstErDtlsDec *self = GST_ER_DTLS_DEC (object);
2015-03-16 16:34:05 +00:00
if (self->agent) {
g_object_unref (self->agent);
self->agent = NULL;
}
2015-03-16 16:34:05 +00:00
if (self->connection) {
g_object_unref (self->connection);
self->connection = NULL;
}
}
2015-03-16 16:34:05 +00:00
static void
gst_er_dtls_dec_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
2015-03-16 16:34:05 +00:00
GstErDtlsDec *self = GST_ER_DTLS_DEC (object);
2015-03-16 16:34:05 +00:00
switch (prop_id) {
case PROP_CONNECTION_ID:
2015-03-16 16:34:05 +00:00
g_free (self->connection_id);
self->connection_id = g_value_dup_string (value);
g_return_if_fail (self->agent);
create_connection (self, self->connection_id);
break;
case PROP_PEM:
2015-03-16 16:34:05 +00:00
if (self->agent) {
g_object_unref (self->agent);
}
self->agent = get_agent_by_pem (g_value_get_string (value));
if (self->connection_id) {
create_connection (self, self->connection_id);
}
break;
default:
2015-03-16 16:34:05 +00:00
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
}
}
2015-03-16 16:34:05 +00:00
static void
gst_er_dtls_dec_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
2015-03-16 16:34:05 +00:00
GstErDtlsDec *self = GST_ER_DTLS_DEC (object);
2015-03-16 16:34:05 +00:00
switch (prop_id) {
case PROP_CONNECTION_ID:
2015-03-16 16:34:05 +00:00
g_value_set_string (value, self->connection_id);
break;
case PROP_PEM:
2015-03-16 16:34:05 +00:00
g_value_take_string (value,
er_dtls_agent_get_certificate_pem (self->agent));
break;
case PROP_PEER_PEM:
2015-03-16 16:34:05 +00:00
g_value_set_string (value, self->peer_pem);
break;
case PROP_DECODER_KEY:
2015-03-16 16:34:05 +00:00
g_value_set_boxed (value, self->decoder_key);
break;
case PROP_SRTP_CIPHER:
2015-03-16 16:34:05 +00:00
g_value_set_uint (value, self->srtp_cipher);
break;
case PROP_SRTP_AUTH:
2015-03-16 16:34:05 +00:00
g_value_set_uint (value, self->srtp_auth);
break;
default:
2015-03-16 16:34:05 +00:00
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
}
}
2015-03-16 16:34:05 +00:00
static GstStateChangeReturn
gst_er_dtls_dec_change_state (GstElement * element, GstStateChange transition)
{
2015-03-16 16:34:05 +00:00
GstErDtlsDec *self = GST_ER_DTLS_DEC (element);
GstStateChangeReturn ret;
2015-03-16 16:34:05 +00:00
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
2015-03-16 16:34:05 +00:00
if (self->connection) {
g_signal_connect_object (self->connection,
"on-decoder-key", G_CALLBACK (on_key_received), self, 0);
g_signal_connect_object (self->connection,
"on-peer-certificate", G_CALLBACK (on_peer_certificate_received),
self, 0);
} else {
GST_WARNING_OBJECT (self,
"trying to change state to ready without connection id and pem");
return GST_STATE_CHANGE_FAILURE;
}
break;
default:
2015-03-16 16:34:05 +00:00
break;
}
2015-03-16 16:34:05 +00:00
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2015-03-16 16:34:05 +00:00
return ret;
}
2015-03-16 16:34:05 +00:00
static GstPad *
gst_er_dtls_dec_request_new_pad (GstElement * element,
GstPadTemplate * tmpl, const gchar * name, const GstCaps * caps)
{
2015-03-16 16:34:05 +00:00
GstErDtlsDec *self = GST_ER_DTLS_DEC (element);
2015-03-16 16:34:05 +00:00
GST_DEBUG_OBJECT (element, "requesting pad");
2015-03-16 16:34:05 +00:00
g_return_val_if_fail (!self->src, NULL);
g_return_val_if_fail (tmpl->direction == GST_PAD_SRC, NULL);
2015-03-16 16:34:05 +00:00
g_mutex_lock (&self->src_mutex);
2015-03-16 16:34:05 +00:00
self->src = gst_pad_new_from_template (tmpl, name);
g_return_val_if_fail (self->src, NULL);
2015-03-16 16:34:05 +00:00
if (caps) {
g_object_set (self->src, "caps", caps, NULL);
}
2015-03-16 16:34:05 +00:00
gst_pad_set_active (self->src, TRUE);
gst_element_add_pad (element, self->src);
2015-03-16 16:34:05 +00:00
g_mutex_unlock (&self->src_mutex);
2015-03-16 16:34:05 +00:00
return self->src;
}
2015-03-16 16:34:05 +00:00
static void
gst_er_dtls_dec_release_pad (GstElement * element, GstPad * pad)
{
2015-03-16 16:34:05 +00:00
GstErDtlsDec *self = GST_ER_DTLS_DEC (element);
2015-03-16 16:34:05 +00:00
g_mutex_lock (&self->src_mutex);
2015-03-16 16:34:05 +00:00
g_return_if_fail (self->src == pad);
gst_element_remove_pad (element, self->src);
self->src = NULL;
2015-03-16 16:34:05 +00:00
GST_DEBUG_OBJECT (self, "releasing src pad");
2015-03-16 16:34:05 +00:00
g_mutex_unlock (&self->src_mutex);
2015-03-16 16:34:05 +00:00
GST_ELEMENT_GET_CLASS (element)->release_pad (element, pad);
}
2015-03-16 16:34:05 +00:00
static void
on_key_received (ErDtlsConnection * connection, gpointer key, guint cipher,
guint auth, GstErDtlsDec * self)
{
2015-03-16 16:34:05 +00:00
gpointer key_dup;
gchar *key_str;
2015-03-16 16:34:05 +00:00
UNUSED (connection);
g_return_if_fail (GST_IS_ER_DTLS_DEC (self));
2015-03-16 16:34:05 +00:00
self->srtp_cipher = cipher;
self->srtp_auth = auth;
2015-03-16 16:34:05 +00:00
key_dup = g_memdup (key, ER_DTLS_SRTP_MASTER_KEY_LENGTH);
self->decoder_key =
gst_buffer_new_wrapped (key_dup, ER_DTLS_SRTP_MASTER_KEY_LENGTH);
2015-03-16 16:34:05 +00:00
key_str = g_base64_encode (key, ER_DTLS_SRTP_MASTER_KEY_LENGTH);
GST_INFO_OBJECT (self, "received key: %s", key_str);
g_free (key_str);
2015-03-16 16:34:05 +00:00
g_signal_emit (self, signals[SIGNAL_ON_KEY_RECEIVED], 0);
}
2015-03-16 16:34:05 +00:00
static gboolean
signal_peer_certificate_received (GWeakRef * ref)
{
2015-03-16 16:34:05 +00:00
GstErDtlsDec *self;
2015-03-16 16:34:05 +00:00
self = g_weak_ref_get (ref);
g_weak_ref_clear (ref);
g_free (ref);
ref = NULL;
2015-03-16 16:34:05 +00:00
if (self) {
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PEER_PEM]);
g_object_unref (self);
self = NULL;
}
2015-03-16 16:34:05 +00:00
return FALSE;
}
2015-03-16 16:34:05 +00:00
static gboolean
on_peer_certificate_received (ErDtlsConnection * connection, gchar * pem,
GstErDtlsDec * self)
{
2015-03-16 16:34:05 +00:00
GWeakRef *ref;
2015-03-16 16:34:05 +00:00
UNUSED (connection);
g_return_val_if_fail (GST_IS_ER_DTLS_DEC (self), TRUE);
2015-03-16 16:34:05 +00:00
GST_DEBUG_OBJECT (self, "Received peer certificate PEM: \n%s", pem);
2015-03-16 16:34:05 +00:00
self->peer_pem = g_strdup (pem);
2015-03-16 16:34:05 +00:00
ref = g_new (GWeakRef, 1);
g_weak_ref_init (ref, self);
2015-03-16 16:34:05 +00:00
g_idle_add ((GSourceFunc) signal_peer_certificate_received, ref);
2015-03-16 16:34:05 +00:00
return TRUE;
}
2015-03-16 16:34:05 +00:00
static GstFlowReturn
sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
{
2015-03-16 16:34:05 +00:00
GstErDtlsDec *self = GST_ER_DTLS_DEC (parent);
GstFlowReturn ret = GST_FLOW_OK;
GstMapInfo map_info = GST_MAP_INFO_INIT;
gint size;
2015-03-16 16:34:05 +00:00
if (!self->agent) {
gst_buffer_unref (buffer);
return GST_FLOW_OK;
}
2015-03-16 16:34:05 +00:00
GST_DEBUG_OBJECT (self, "received buffer from %s with length %zd",
self->connection_id, gst_buffer_get_size (buffer));
2015-03-16 16:34:05 +00:00
gst_buffer_map (buffer, &map_info, GST_MAP_READWRITE);
2015-03-16 16:34:05 +00:00
if (!map_info.size) {
gst_buffer_unmap (buffer, &map_info);
return GST_FLOW_OK;
}
2015-03-16 16:34:05 +00:00
size =
er_dtls_connection_process (self->connection, map_info.data,
map_info.size);
gst_buffer_unmap (buffer, &map_info);
2015-03-16 16:34:05 +00:00
if (size <= 0) {
gst_buffer_unref (buffer);
2015-03-16 16:34:05 +00:00
return GST_FLOW_OK;
}
2015-03-16 16:34:05 +00:00
g_mutex_lock (&self->src_mutex);
if (self->src) {
gst_buffer_set_size (buffer, size);
GST_LOG_OBJECT (self, "decoded buffer with length %d, pushing", size);
ret = gst_pad_push (self->src, buffer);
} else {
GST_LOG_OBJECT (self, "dropped buffer with length %d, not linked", size);
gst_buffer_unref (buffer);
}
2015-03-16 16:34:05 +00:00
g_mutex_unlock (&self->src_mutex);
2015-03-16 16:34:05 +00:00
return ret;
}
static GHashTable *agent_table = NULL;
2015-03-16 16:34:05 +00:00
G_LOCK_DEFINE_STATIC (agent_table);
static ErDtlsAgent *generated_cert_agent = NULL;
2015-03-16 16:34:05 +00:00
static ErDtlsAgent *
get_agent_by_pem (const gchar * pem)
{
2015-03-16 16:34:05 +00:00
ErDtlsAgent *agent;
2015-03-16 16:34:05 +00:00
if (!pem) {
if (g_once_init_enter (&generated_cert_agent)) {
ErDtlsAgent *new_agent;
2015-03-16 16:34:05 +00:00
new_agent = g_object_new (ER_TYPE_DTLS_AGENT, "certificate",
g_object_new (ER_TYPE_DTLS_CERTIFICATE, NULL), NULL);
2015-03-16 16:34:05 +00:00
GST_DEBUG_OBJECT (generated_cert_agent,
"no agent with generated cert found, creating new");
g_once_init_leave (&generated_cert_agent, new_agent);
} else {
2015-03-16 16:34:05 +00:00
GST_DEBUG_OBJECT (generated_cert_agent,
"using agent with generated cert");
}
2015-03-16 16:34:05 +00:00
agent = generated_cert_agent;
g_object_ref (agent);
} else {
G_LOCK (agent_table);
2015-03-16 16:34:05 +00:00
if (!agent_table) {
agent_table =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}
2015-03-16 16:34:05 +00:00
agent = ER_DTLS_AGENT (g_hash_table_lookup (agent_table, pem));
2015-03-16 16:34:05 +00:00
if (!agent) {
agent = g_object_new (ER_TYPE_DTLS_AGENT,
"certificate", g_object_new (ER_TYPE_DTLS_CERTIFICATE, "pem", pem,
NULL), NULL);
2015-03-16 16:34:05 +00:00
g_object_weak_ref (G_OBJECT (agent), (GWeakNotify) agent_weak_ref_notify,
(gpointer) g_strdup (pem));
2015-03-16 16:34:05 +00:00
g_hash_table_insert (agent_table, g_strdup (pem), agent);
2015-03-16 16:34:05 +00:00
GST_DEBUG_OBJECT (agent, "no agent found, created new");
} else {
g_object_ref (agent);
GST_DEBUG_OBJECT (agent, "agent found");
}
2015-03-16 16:34:05 +00:00
G_UNLOCK (agent_table);
}
2015-03-16 16:34:05 +00:00
return agent;
}
2015-03-16 16:34:05 +00:00
static void
agent_weak_ref_notify (gchar * pem, ErDtlsAgent * agent)
{
2015-03-16 16:34:05 +00:00
UNUSED (agent);
2015-03-16 16:34:05 +00:00
G_LOCK (agent_table);
g_hash_table_remove (agent_table, pem);
G_UNLOCK (agent_table);
2015-03-16 16:34:05 +00:00
g_free (pem);
pem = NULL;
}
static GHashTable *connection_table = NULL;
2015-03-16 16:34:05 +00:00
G_LOCK_DEFINE_STATIC (connection_table);
2015-03-16 16:34:05 +00:00
ErDtlsConnection *
gst_er_dtls_dec_fetch_connection (gchar * id)
{
2015-03-16 16:34:05 +00:00
ErDtlsConnection *connection;
g_return_val_if_fail (id, NULL);
2015-03-16 16:34:05 +00:00
GST_DEBUG ("fetching '%s' from connection table, size is %d",
id, g_hash_table_size (connection_table));
2015-03-16 16:34:05 +00:00
G_LOCK (connection_table);
2015-03-16 16:34:05 +00:00
connection = g_hash_table_lookup (connection_table, id);
2015-03-16 16:34:05 +00:00
if (connection) {
g_object_ref (connection);
g_hash_table_remove (connection_table, id);
} else {
GST_WARNING ("no connection with id '%s' found", id);
}
2015-03-16 16:34:05 +00:00
G_UNLOCK (connection_table);
2015-03-16 16:34:05 +00:00
return connection;
}
2015-03-16 16:34:05 +00:00
static void
create_connection (GstErDtlsDec * self, gchar * id)
{
2015-03-16 16:34:05 +00:00
g_return_if_fail (GST_IS_ER_DTLS_DEC (self));
g_return_if_fail (ER_IS_DTLS_AGENT (self->agent));
2015-03-16 16:34:05 +00:00
if (self->connection) {
g_object_unref (self->connection);
self->connection = NULL;
}
2015-03-16 16:34:05 +00:00
G_LOCK (connection_table);
2015-03-16 16:34:05 +00:00
if (!connection_table) {
connection_table =
g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}
2015-03-16 16:34:05 +00:00
if (g_hash_table_contains (connection_table, id)) {
G_UNLOCK (connection_table);
2015-03-16 16:34:05 +00:00
g_return_if_reached ();
}
2015-03-16 16:34:05 +00:00
self->connection =
g_object_new (ER_TYPE_DTLS_CONNECTION, "agent", self->agent, NULL);
2015-03-16 16:34:05 +00:00
g_object_weak_ref (G_OBJECT (self->connection),
(GWeakNotify) connection_weak_ref_notify, g_strdup (id));
2015-03-16 16:34:05 +00:00
g_hash_table_insert (connection_table, g_strdup (id), self->connection);
2015-03-16 16:34:05 +00:00
G_UNLOCK (connection_table);
}
2015-03-16 16:34:05 +00:00
static void
connection_weak_ref_notify (gchar * id, ErDtlsConnection * connection)
{
2015-03-16 16:34:05 +00:00
UNUSED (connection);
2015-03-16 16:34:05 +00:00
G_LOCK (connection_table);
g_hash_table_remove (connection_table, id);
G_UNLOCK (connection_table);
2015-03-16 16:34:05 +00:00
g_free (id);
id = NULL;
}