Move files from gst-plugins-bad into the "subprojects/gst-plugins-bad/" subdir
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / ext / webrtc / webrtctransceiver.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 "gstwebrtcbin.h"
25 #include "utils.h"
26 #include "webrtctransceiver.h"
27
28 #define GST_CAT_DEFAULT webrtc_transceiver_debug
29 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
30
31 #define webrtc_transceiver_parent_class parent_class
32 G_DEFINE_TYPE_WITH_CODE (WebRTCTransceiver, webrtc_transceiver,
33     GST_TYPE_WEBRTC_RTP_TRANSCEIVER,
34     GST_DEBUG_CATEGORY_INIT (webrtc_transceiver_debug,
35         "webrtctransceiver", 0, "webrtctransceiver"););
36
37 #define DEFAULT_FEC_TYPE GST_WEBRTC_FEC_TYPE_NONE
38 #define DEFAULT_DO_NACK FALSE
39 #define DEFAULT_FEC_PERCENTAGE 100
40
41 enum
42 {
43   PROP_0,
44   PROP_WEBRTC,
45   PROP_FEC_TYPE,
46   PROP_FEC_PERCENTAGE,
47   PROP_DO_NACK,
48 };
49
50 void
51 webrtc_transceiver_set_transport (WebRTCTransceiver * trans,
52     TransportStream * stream)
53 {
54   GstWebRTCRTPTransceiver *rtp_trans;
55
56   g_return_if_fail (WEBRTC_IS_TRANSCEIVER (trans));
57
58   rtp_trans = GST_WEBRTC_RTP_TRANSCEIVER (trans);
59
60   gst_object_replace ((GstObject **) & trans->stream, (GstObject *) stream);
61
62   if (rtp_trans->sender) {
63     gst_object_replace ((GstObject **) & rtp_trans->sender->transport,
64         (GstObject *) stream->transport);
65     g_object_notify (G_OBJECT (rtp_trans->sender), "transport");
66   }
67
68   if (rtp_trans->receiver) {
69     gst_object_replace ((GstObject **) & rtp_trans->receiver->transport,
70         (GstObject *) stream->transport);
71     g_object_notify (G_OBJECT (rtp_trans->receiver), "transport");
72   }
73 }
74
75 GstWebRTCDTLSTransport *
76 webrtc_transceiver_get_dtls_transport (GstWebRTCRTPTransceiver * trans)
77 {
78   g_return_val_if_fail (WEBRTC_IS_TRANSCEIVER (trans), NULL);
79
80   if (trans->sender) {
81     return trans->sender->transport;
82   } else if (trans->receiver) {
83     return trans->receiver->transport;
84   }
85
86   return NULL;
87 }
88
89 static void
90 webrtc_transceiver_set_property (GObject * object, guint prop_id,
91     const GValue * value, GParamSpec * pspec)
92 {
93   WebRTCTransceiver *trans = WEBRTC_TRANSCEIVER (object);
94
95   switch (prop_id) {
96     case PROP_WEBRTC:
97       gst_object_set_parent (GST_OBJECT (trans), g_value_get_object (value));
98       break;
99   }
100
101   GST_OBJECT_LOCK (trans);
102   switch (prop_id) {
103     case PROP_WEBRTC:
104       break;
105     case PROP_FEC_TYPE:
106       trans->fec_type = g_value_get_enum (value);
107       break;
108     case PROP_DO_NACK:
109       trans->do_nack = g_value_get_boolean (value);
110       break;
111     case PROP_FEC_PERCENTAGE:
112       trans->fec_percentage = g_value_get_uint (value);
113       break;
114     default:
115       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
116       break;
117   }
118   GST_OBJECT_UNLOCK (trans);
119 }
120
121 static void
122 webrtc_transceiver_get_property (GObject * object, guint prop_id,
123     GValue * value, GParamSpec * pspec)
124 {
125   WebRTCTransceiver *trans = WEBRTC_TRANSCEIVER (object);
126
127   GST_OBJECT_LOCK (trans);
128   switch (prop_id) {
129     case PROP_FEC_TYPE:
130       g_value_set_enum (value, trans->fec_type);
131       break;
132     case PROP_DO_NACK:
133       g_value_set_boolean (value, trans->do_nack);
134       break;
135     case PROP_FEC_PERCENTAGE:
136       g_value_set_uint (value, trans->fec_percentage);
137       break;
138     default:
139       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140       break;
141   }
142   GST_OBJECT_UNLOCK (trans);
143 }
144
145 static void
146 webrtc_transceiver_finalize (GObject * object)
147 {
148   WebRTCTransceiver *trans = WEBRTC_TRANSCEIVER (object);
149
150   if (trans->stream)
151     gst_object_unref (trans->stream);
152   trans->stream = NULL;
153
154   if (trans->local_rtx_ssrc_map)
155     gst_structure_free (trans->local_rtx_ssrc_map);
156   trans->local_rtx_ssrc_map = NULL;
157
158   gst_caps_replace (&trans->last_configured_caps, NULL);
159
160   if (trans->ssrc_event)
161     gst_event_unref (trans->ssrc_event);
162   trans->ssrc_event = NULL;
163
164   G_OBJECT_CLASS (parent_class)->finalize (object);
165 }
166
167 static void
168 webrtc_transceiver_class_init (WebRTCTransceiverClass * klass)
169 {
170   GObjectClass *gobject_class = (GObjectClass *) klass;
171
172   gobject_class->get_property = webrtc_transceiver_get_property;
173   gobject_class->set_property = webrtc_transceiver_set_property;
174   gobject_class->finalize = webrtc_transceiver_finalize;
175
176   /* some acrobatics are required to set the parent before _constructed()
177    * has been called */
178   g_object_class_install_property (gobject_class,
179       PROP_WEBRTC,
180       g_param_spec_object ("webrtc", "Parent webrtcbin",
181           "Parent webrtcbin",
182           GST_TYPE_WEBRTC_BIN,
183           G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
184
185   g_object_class_install_property (gobject_class,
186       PROP_FEC_TYPE,
187       g_param_spec_enum ("fec-type", "FEC type",
188           "The type of Forward Error Correction to use",
189           GST_TYPE_WEBRTC_FEC_TYPE,
190           DEFAULT_FEC_TYPE,
191           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192
193   g_object_class_install_property (gobject_class,
194       PROP_DO_NACK,
195       g_param_spec_boolean ("do-nack", "Do nack",
196           "Whether to send negative acknowledgements for feedback",
197           DEFAULT_DO_NACK,
198           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199
200   g_object_class_install_property (gobject_class,
201       PROP_FEC_PERCENTAGE,
202       g_param_spec_uint ("fec-percentage", "FEC percentage",
203           "The amount of Forward Error Correction to apply",
204           0, 100, DEFAULT_FEC_PERCENTAGE,
205           G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206 }
207
208 static void
209 webrtc_transceiver_init (WebRTCTransceiver * trans)
210 {
211 }
212
213 WebRTCTransceiver *
214 webrtc_transceiver_new (GstWebRTCBin * webrtc, GstWebRTCRTPSender * sender,
215     GstWebRTCRTPReceiver * receiver)
216 {
217   WebRTCTransceiver *trans;
218
219   trans = g_object_new (webrtc_transceiver_get_type (), "sender", sender,
220       "receiver", receiver, "webrtc", webrtc, NULL);
221
222   return trans;
223 }