1 /*
2 * Copyright (c) 2014, Ericsson AB. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or other
12 * materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
23 * OF SUCH DAMAGE.
24 */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "gstdtlssrtpdemux.h"
31
32 #define PACKET_IS_DTLS(b) (b > 0x13 && b < 0x40)
33 #define PACKET_IS_RTP(b) (b > 0x7f && b < 0xc0)
34
35 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
36 GST_PAD_SINK,
37 GST_PAD_ALWAYS,
38 GST_STATIC_CAPS_ANY);
39
40 static GstStaticPadTemplate rtp_src_template =
41 GST_STATIC_PAD_TEMPLATE ("rtp_src",
42 GST_PAD_SRC,
43 GST_PAD_ALWAYS,
44 GST_STATIC_CAPS ("application/x-rtp;"
45 "application/x-rtcp;" "application/x-srtp;" "application/x-srtcp")
46 );
47
48 static GstStaticPadTemplate dtls_src_template =
49 GST_STATIC_PAD_TEMPLATE ("dtls_src",
50 GST_PAD_SRC,
51 GST_PAD_ALWAYS,
52 GST_STATIC_CAPS ("application/x-dtls")
53 );
54
55 GST_DEBUG_CATEGORY_STATIC (gst_gst_dtls_srtp_demux_debug);
56 #define GST_CAT_DEFAULT gst_gst_dtls_srtp_demux_debug
57
58 #define gst_dtls_srtp_demux_parent_class parent_class
59 G_DEFINE_TYPE_WITH_CODE (GstDtlsSrtpDemux, gst_dtls_srtp_demux,
60 GST_TYPE_ELEMENT, GST_DEBUG_CATEGORY_INIT (gst_gst_dtls_srtp_demux_debug,
61 "dtlssrtpdemux", 0, "DTLS SRTP Demultiplexer"));
62
63 static GstFlowReturn sink_chain (GstPad *, GstObject * self, GstBuffer *);
64
65 static void
gst_dtls_srtp_demux_class_init(GstDtlsSrtpDemuxClass * klass)66 gst_dtls_srtp_demux_class_init (GstDtlsSrtpDemuxClass * klass)
67 {
68 GstElementClass *element_class;
69
70 element_class = (GstElementClass *) klass;
71
72 gst_element_class_add_static_pad_template (element_class, &sink_template);
73 gst_element_class_add_static_pad_template (element_class, &rtp_src_template);
74 gst_element_class_add_static_pad_template (element_class, &dtls_src_template);
75
76 gst_element_class_set_static_metadata (element_class,
77 "DTLS SRTP Demultiplexer",
78 "DTLS/SRTP/Demux",
79 "Demultiplexes DTLS and SRTP packets",
80 "Patrik Oldsberg patrik.oldsberg@ericsson.com");
81 }
82
83 static void
gst_dtls_srtp_demux_init(GstDtlsSrtpDemux * self)84 gst_dtls_srtp_demux_init (GstDtlsSrtpDemux * self)
85 {
86 GstPad *sink;
87
88 sink = gst_pad_new_from_static_template (&sink_template, "sink");
89 self->rtp_src =
90 gst_pad_new_from_static_template (&rtp_src_template, "rtp_src");
91 self->dtls_src =
92 gst_pad_new_from_static_template (&dtls_src_template, "dtls_src");
93 g_return_if_fail (sink);
94 g_return_if_fail (self->rtp_src);
95 g_return_if_fail (self->dtls_src);
96
97 gst_pad_set_chain_function (sink, GST_DEBUG_FUNCPTR (sink_chain));
98
99 gst_element_add_pad (GST_ELEMENT (self), sink);
100 gst_element_add_pad (GST_ELEMENT (self), self->rtp_src);
101 gst_element_add_pad (GST_ELEMENT (self), self->dtls_src);
102 }
103
104 static GstFlowReturn
sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)105 sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
106 {
107 GstDtlsSrtpDemux *self = GST_DTLS_SRTP_DEMUX (parent);
108 guint8 first_byte;
109
110 if (gst_buffer_get_size (buffer) == 0) {
111 GST_LOG_OBJECT (self, "received buffer with size 0");
112 gst_buffer_unref (buffer);
113 return GST_FLOW_OK;
114 }
115
116 if (gst_buffer_extract (buffer, 0, &first_byte, 1) != 1) {
117 GST_WARNING_OBJECT (self, "could not extract first byte from buffer");
118 gst_buffer_unref (buffer);
119 return GST_FLOW_OK;
120 }
121
122 if (PACKET_IS_DTLS (first_byte)) {
123 GST_LOG_OBJECT (self, "pushing dtls packet");
124
125 return gst_pad_push (self->dtls_src, buffer);
126 }
127
128 if (PACKET_IS_RTP (first_byte)) {
129 GST_LOG_OBJECT (self, "pushing rtp packet");
130
131 return gst_pad_push (self->rtp_src, buffer);
132 }
133
134 GST_WARNING_OBJECT (self, "received invalid buffer: %x", first_byte);
135 gst_buffer_unref (buffer);
136 return GST_FLOW_OK;
137 }
138