webrtcbin: an element that handles the transport aspects of webrtc connections
[platform/upstream/gst-plugins-bad.git] / 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 webrtc_transceiver_parent_class parent_class
29 G_DEFINE_TYPE (WebRTCTransceiver, webrtc_transceiver,
30     GST_TYPE_WEBRTC_RTP_TRANSCEIVER);
31
32 enum
33 {
34   PROP_0,
35   PROP_WEBRTC,
36 };
37
38 void
39 webrtc_transceiver_set_transport (WebRTCTransceiver * trans,
40     TransportStream * stream)
41 {
42   GstWebRTCRTPTransceiver *rtp_trans;
43
44   g_return_if_fail (WEBRTC_IS_TRANSCEIVER (trans));
45
46   rtp_trans = GST_WEBRTC_RTP_TRANSCEIVER (trans);
47
48   gst_object_replace ((GstObject **) & trans->stream, (GstObject *) stream);
49
50   if (rtp_trans->sender)
51     gst_object_replace ((GstObject **) & rtp_trans->sender->transport,
52         (GstObject *) stream->transport);
53   if (rtp_trans->receiver)
54     gst_object_replace ((GstObject **) & rtp_trans->receiver->transport,
55         (GstObject *) stream->transport);
56
57   if (rtp_trans->sender)
58     gst_object_replace ((GstObject **) & rtp_trans->sender->rtcp_transport,
59         (GstObject *) stream->rtcp_transport);
60   if (rtp_trans->receiver)
61     gst_object_replace ((GstObject **) & rtp_trans->receiver->rtcp_transport,
62         (GstObject *) stream->rtcp_transport);
63 }
64
65 static void
66 webrtc_transceiver_set_property (GObject * object, guint prop_id,
67     const GValue * value, GParamSpec * pspec)
68 {
69   WebRTCTransceiver *trans = WEBRTC_TRANSCEIVER (object);
70
71   switch (prop_id) {
72     case PROP_WEBRTC:
73       gst_object_set_parent (GST_OBJECT (trans), g_value_get_object (value));
74       break;
75   }
76
77   GST_OBJECT_LOCK (trans);
78   switch (prop_id) {
79     case PROP_WEBRTC:
80       break;
81     default:
82       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
83       break;
84   }
85   GST_OBJECT_UNLOCK (trans);
86 }
87
88 static void
89 webrtc_transceiver_get_property (GObject * object, guint prop_id,
90     GValue * value, GParamSpec * pspec)
91 {
92   WebRTCTransceiver *trans = WEBRTC_TRANSCEIVER (object);
93
94   GST_OBJECT_LOCK (trans);
95   switch (prop_id) {
96     default:
97       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
98       break;
99   }
100   GST_OBJECT_UNLOCK (trans);
101 }
102
103 static void
104 webrtc_transceiver_finalize (GObject * object)
105 {
106   WebRTCTransceiver *trans = WEBRTC_TRANSCEIVER (object);
107
108   if (trans->stream)
109     gst_object_unref (trans->stream);
110   trans->stream = NULL;
111
112   G_OBJECT_CLASS (parent_class)->finalize (object);
113 }
114
115 static void
116 webrtc_transceiver_class_init (WebRTCTransceiverClass * klass)
117 {
118   GObjectClass *gobject_class = (GObjectClass *) klass;
119
120   gobject_class->get_property = webrtc_transceiver_get_property;
121   gobject_class->set_property = webrtc_transceiver_set_property;
122   gobject_class->finalize = webrtc_transceiver_finalize;
123
124   /* some acrobatics are required to set the parent before _constructed()
125    * has been called */
126   g_object_class_install_property (gobject_class,
127       PROP_WEBRTC,
128       g_param_spec_object ("webrtc", "Parent webrtcbin",
129           "Parent webrtcbin",
130           GST_TYPE_WEBRTC_BIN,
131           G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
132 }
133
134 static void
135 webrtc_transceiver_init (WebRTCTransceiver * trans)
136 {
137 }
138
139 WebRTCTransceiver *
140 webrtc_transceiver_new (GstWebRTCBin * webrtc, GstWebRTCRTPSender * sender,
141     GstWebRTCRTPReceiver * receiver)
142 {
143   WebRTCTransceiver *trans;
144
145   trans = g_object_new (webrtc_transceiver_get_type (), "sender", sender,
146       "receiver", receiver, "webrtc", webrtc, NULL);
147
148   return trans;
149 }