webrtcbin: an element that handles the transport aspects of webrtc connections
[platform/upstream/gst-plugins-bad.git] / ext / webrtc / utils.c
1 /* GStreamer
2  * Copyright (C) 2017 Matthew Waters <matthew@centricular.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
23
24 #include "utils.h"
25 #include "gstwebrtcbin.h"
26
27 GstPadTemplate *
28 _find_pad_template (GstElement * element, GstPadDirection direction,
29     GstPadPresence presence, const gchar * name)
30 {
31   GstElementClass *element_class = GST_ELEMENT_GET_CLASS (element);
32   const GList *l = gst_element_class_get_pad_template_list (element_class);
33   GstPadTemplate *templ = NULL;
34
35   for (; l; l = l->next) {
36     templ = l->data;
37     if (templ->direction != direction)
38       continue;
39     if (templ->presence != presence)
40       continue;
41     if (g_strcmp0 (templ->name_template, name) == 0) {
42       return templ;
43     }
44   }
45
46   return NULL;
47 }
48
49 GstSDPMessage *
50 _get_latest_sdp (GstWebRTCBin * webrtc)
51 {
52   if (webrtc->current_local_description &&
53       webrtc->current_local_description->type == GST_WEBRTC_SDP_TYPE_ANSWER) {
54     return webrtc->current_local_description->sdp;
55   }
56   if (webrtc->current_remote_description &&
57       webrtc->current_remote_description->type == GST_WEBRTC_SDP_TYPE_ANSWER) {
58     return webrtc->current_remote_description->sdp;
59   }
60   if (webrtc->current_local_description &&
61       webrtc->current_local_description->type == GST_WEBRTC_SDP_TYPE_OFFER) {
62     return webrtc->current_local_description->sdp;
63   }
64   if (webrtc->current_remote_description &&
65       webrtc->current_remote_description->type == GST_WEBRTC_SDP_TYPE_OFFER) {
66     return webrtc->current_remote_description->sdp;
67   }
68
69   return NULL;
70 }
71
72 struct pad_block *
73 _create_pad_block (GstElement * element, GstPad * pad, gulong block_id,
74     gpointer user_data, GDestroyNotify notify)
75 {
76   struct pad_block *ret = g_new0 (struct pad_block, 1);
77
78   ret->element = gst_object_ref (element);
79   ret->pad = gst_object_ref (pad);
80   ret->block_id = block_id;
81   ret->user_data = user_data;
82   ret->notify = notify;
83
84   return ret;
85 }
86
87 void
88 _free_pad_block (struct pad_block *block)
89 {
90   if (!block)
91     return;
92
93   if (block->block_id)
94     gst_pad_remove_probe (block->pad, block->block_id);
95   gst_object_unref (block->element);
96   gst_object_unref (block->pad);
97   if (block->notify)
98     block->notify (block->user_data);
99   g_free (block);
100 }
101
102 gchar *
103 _enum_value_to_string (GType type, guint value)
104 {
105   GEnumClass *enum_class;
106   GEnumValue *enum_value;
107   gchar *str = NULL;
108
109   enum_class = g_type_class_ref (type);
110   enum_value = g_enum_get_value (enum_class, value);
111
112   if (enum_value)
113     str = g_strdup (enum_value->value_nick);
114
115   g_type_class_unref (enum_class);
116
117   return str;
118 }
119
120 const gchar *
121 _g_checksum_to_webrtc_string (GChecksumType type)
122 {
123   switch (type) {
124     case G_CHECKSUM_SHA1:
125       return "sha-1";
126     case G_CHECKSUM_SHA256:
127       return "sha-256";
128 #ifdef G_CHECKSUM_SHA384
129     case G_CHECKSUM_SHA384:
130       return "sha-384";
131 #endif
132     case G_CHECKSUM_SHA512:
133       return "sha-512";
134     default:
135       g_warning ("unknown GChecksumType!");
136       return NULL;
137   }
138 }