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 "gstdtlselements.h"
31 #include "gstdtlssrtpdemux.h"
32
33 #define PACKET_IS_DTLS(b) (b > 0x13 && b < 0x40)
34 #define PACKET_IS_RTP(b) (b > 0x7f && b < 0xc0)
35
36 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
37 GST_PAD_SINK,
38 GST_PAD_ALWAYS,
39 GST_STATIC_CAPS_ANY);
40
41 static GstStaticPadTemplate rtp_src_template =
42 GST_STATIC_PAD_TEMPLATE ("rtp_src",
43 GST_PAD_SRC,
44 GST_PAD_ALWAYS,
45 GST_STATIC_CAPS ("application/x-rtp;"
46 "application/x-rtcp;" "application/x-srtp;" "application/x-srtcp")
47 );
48
49 static GstStaticPadTemplate dtls_src_template =
50 GST_STATIC_PAD_TEMPLATE ("dtls_src",
51 GST_PAD_SRC,
52 GST_PAD_ALWAYS,
53 GST_STATIC_CAPS ("application/x-dtls")
54 );
55
56 GST_DEBUG_CATEGORY_STATIC (gst_gst_dtls_srtp_demux_debug);
57 #define GST_CAT_DEFAULT gst_gst_dtls_srtp_demux_debug
58
59 #define gst_dtls_srtp_demux_parent_class parent_class
60 G_DEFINE_TYPE_WITH_CODE (GstDtlsSrtpDemux, gst_dtls_srtp_demux,
61 GST_TYPE_ELEMENT, GST_DEBUG_CATEGORY_INIT (gst_gst_dtls_srtp_demux_debug,
62 "dtlssrtpdemux", 0, "DTLS SRTP Demultiplexer"));
63 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (dtlssrtpdemux, "dtlssrtpdemux",
64 GST_RANK_NONE, GST_TYPE_DTLS_SRTP_DEMUX, dtls_element_init (plugin));
65
66
67 static GstFlowReturn sink_chain (GstPad *, GstObject * self, GstBuffer *);
68
69 static void
gst_dtls_srtp_demux_class_init(GstDtlsSrtpDemuxClass * klass)70 gst_dtls_srtp_demux_class_init (GstDtlsSrtpDemuxClass * klass)
71 {
72 GstElementClass *element_class;
73
74 element_class = (GstElementClass *) klass;
75
76 gst_element_class_add_static_pad_template (element_class, &sink_template);
77 gst_element_class_add_static_pad_template (element_class, &rtp_src_template);
78 gst_element_class_add_static_pad_template (element_class, &dtls_src_template);
79
80 gst_element_class_set_static_metadata (element_class,
81 "DTLS SRTP Demultiplexer",
82 "DTLS/SRTP/Demux",
83 "Demultiplexes DTLS and SRTP packets",
84 "Patrik Oldsberg patrik.oldsberg@ericsson.com");
85 }
86
87 static void
gst_dtls_srtp_demux_init(GstDtlsSrtpDemux * self)88 gst_dtls_srtp_demux_init (GstDtlsSrtpDemux * self)
89 {
90 GstPad *sink;
91
92 sink = gst_pad_new_from_static_template (&sink_template, "sink");
93 self->rtp_src =
94 gst_pad_new_from_static_template (&rtp_src_template, "rtp_src");
95 self->dtls_src =
96 gst_pad_new_from_static_template (&dtls_src_template, "dtls_src");
97 g_return_if_fail (sink);
98 g_return_if_fail (self->rtp_src);
99 g_return_if_fail (self->dtls_src);
100
101 gst_pad_set_chain_function (sink, GST_DEBUG_FUNCPTR (sink_chain));
102
103 gst_element_add_pad (GST_ELEMENT (self), sink);
104 gst_element_add_pad (GST_ELEMENT (self), self->rtp_src);
105 gst_element_add_pad (GST_ELEMENT (self), self->dtls_src);
106 }
107
108 static GstFlowReturn
sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)109 sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
110 {
111 GstDtlsSrtpDemux *self = GST_DTLS_SRTP_DEMUX (parent);
112 guint8 first_byte;
113
114 if (gst_buffer_get_size (buffer) == 0) {
115 GST_LOG_OBJECT (self, "received buffer with size 0");
116 gst_buffer_unref (buffer);
117 return GST_FLOW_OK;
118 }
119
120 if (gst_buffer_extract (buffer, 0, &first_byte, 1) != 1) {
121 GST_WARNING_OBJECT (self, "could not extract first byte from buffer");
122 gst_buffer_unref (buffer);
123 return GST_FLOW_OK;
124 }
125
126 if (PACKET_IS_DTLS (first_byte)) {
127 GST_LOG_OBJECT (self, "pushing dtls packet");
128
129 return gst_pad_push (self->dtls_src, buffer);
130 }
131
132 if (PACKET_IS_RTP (first_byte)) {
133 GST_LOG_OBJECT (self, "pushing rtp packet");
134
135 return gst_pad_push (self->rtp_src, buffer);
136 }
137
138 GST_WARNING_OBJECT (self, "received invalid buffer: %x", first_byte);
139 gst_buffer_unref (buffer);
140 return GST_FLOW_OK;
141 }
142