2 * GStreamer - GStreamer SRTP decoder
4 * Copyright 2009-2011 Collabora Ltd.
5 * @author: Gabriel Millaire <gabriel.millaire@collabora.co.uk>
6 * @author: Olivier Crete <olivier.crete@collabora.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
26 * Alternatively, the contents of this file may be used under the
27 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
28 * which case the following provisions apply instead of the ones
31 * This library is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU Library General Public
33 * License as published by the Free Software Foundation; either
34 * version 2 of the License, or (at your option) any later version.
36 * This library is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * Library General Public License for more details.
41 * You should have received a copy of the GNU Library General Public
42 * License along with this library; if not, write to the
43 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
44 * Boston, MA 02111-1307, USA.
48 * SECTION:element-srtpdec
52 * gstrtpdec acts as a decoder that removes security from SRTP and SRTCP
53 * packets (encryption and authentication) and out RTP and RTCP. It
54 * receives packet of type 'application/x-srtp' or 'application/x-srtcp'
55 * on its sink pad, and outs packets of type 'application/x-rtp' or
56 * 'application/x-rtcp' on its source pad.
58 * For each packet received, it checks if the internal SSRC is in the list
59 * of streams already in use. If this is not the case, it sends a signal to
60 * the user to get the needed parameters to create a new stream : master
61 * key, encryption and authentication mechanisms for both RTP and RTCP. If
62 * the user can't provide those parameters, the buffer is dropped and a
65 * This element uses libsrtp library. The encryption and authentication
66 * mechanisms available are :
69 * - AES_ICM 256 bits (maximum security)
70 * - AES_ICM 128 bits (default)
74 * - HMAC_SHA1 80 bits (default, maximum protection)
78 * Note that for SRTP protection, authentication is mandatory (non-null)
79 * if encryption is used (non-null).
81 * Each packet received is first analysed (checked for valid SSRC) then
82 * its buffer is unprotected with libsrtp, then pushed on the source pad.
83 * If protection failed or the stream could not be created, the buffer
84 * is dropped and a warning is emitted.
86 * When the maximum usage of the master key is reached, a soft-limit
87 * signal is sent to the user, and new parameters (master key) are needed
88 * in return. If the hard limit is reached, a flag is set and every
89 * subsequent packet is dropped, until a new key is set and the stream
92 * If a stream is to be shared between multiple clients the SRTP
93 * rollover counter for a given SSRC must be set in the caps "roc" field
94 * when the request-key signal is emitted by the decoder. The rollover
95 * counters should have been transmitted by a signaling protocol by some
96 * other means. If no rollover counter is provided by the user, 0 is
99 * It is possible to receive a stream protected by multiple master keys, each buffer
100 * then contains a Master Key Identifier (MKI) to identify which key was used for this
101 * buffer. If multiple keys are needed, the first key can be specified in the caps as
102 * "srtp-key=(buffer)key1data, mki=(buffer)mki1data", then the second one can be given in
103 * the same caps as "srtp-key2=(buffer)key2data, mki2=(buffer)mki2data", and more can
106 * ## Example pipelines
108 * gst-launch-1.0 udpsrc port=5004 caps='application/x-srtp, payload=(int)8, ssrc=(uint)1356955624, srtp-key=(buffer)012345678901234567890123456789012345678901234567890123456789, srtp-cipher=(string)aes-128-icm, srtp-auth=(string)hmac-sha1-80, srtcp-cipher=(string)aes-128-icm, srtcp-auth=(string)hmac-sha1-80' ! srtpdec ! rtppcmadepay ! alawdec ! pulsesink
109 * ]| Receive PCMA SRTP packets through UDP using caps to specify
110 * master key and protection.
112 * gst-launch-1.0 audiotestsrc ! alawenc ! rtppcmapay ! 'application/x-rtp, payload=(int)8, ssrc=(uint)1356955624' ! srtpenc key="012345678901234567890123456789012345678901234567890123456789" ! udpsink port=5004
113 * ]| Send PCMA SRTP packets through UDP, nothing how the SSRC is forced so
114 * that the receiver will recognize it.
118 #include "gstsrtpelements.h"
119 #include "gstsrtpdec.h"
120 #include <gst/rtp/gstrtpbuffer.h>
123 GST_DEBUG_CATEGORY_STATIC (gst_srtp_dec_debug);
124 #define GST_CAT_DEFAULT gst_srtp_dec_debug
126 #define DEFAULT_REPLAY_WINDOW_SIZE 128
128 /* Filter signals and args */
131 SIGNAL_REQUEST_KEY = 1,
142 PROP_REPLAY_WINDOW_SIZE,
146 /* the capabilities of the inputs and outputs.
148 * describe the real formats here.
150 static GstStaticPadTemplate rtp_sink_template =
151 GST_STATIC_PAD_TEMPLATE ("rtp_sink",
154 GST_STATIC_CAPS ("application/x-srtp")
157 static GstStaticPadTemplate rtp_src_template =
158 GST_STATIC_PAD_TEMPLATE ("rtp_src",
161 GST_STATIC_CAPS ("application/x-rtp")
164 static GstStaticPadTemplate rtcp_sink_template =
165 GST_STATIC_PAD_TEMPLATE ("rtcp_sink",
168 GST_STATIC_CAPS ("application/x-srtcp")
171 static GstStaticPadTemplate rtcp_src_template =
172 GST_STATIC_PAD_TEMPLATE ("rtcp_src",
175 GST_STATIC_CAPS ("application/x-rtcp")
178 static guint gst_srtp_dec_signals[LAST_SIGNAL] = { 0 };
180 G_DEFINE_TYPE_WITH_CODE (GstSrtpDec, gst_srtp_dec, GST_TYPE_ELEMENT,
181 GST_DEBUG_CATEGORY_INIT (gst_srtp_dec_debug, "srtpdec", 0, "SRTP dec");
183 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (srtpdec, "srtpdec", GST_RANK_NONE,
184 GST_TYPE_SRTP_DEC, srtp_element_init (plugin));
186 static void gst_srtp_dec_set_property (GObject * object, guint prop_id,
187 const GValue * value, GParamSpec * pspec);
188 static void gst_srtp_dec_get_property (GObject * object, guint prop_id,
189 GValue * value, GParamSpec * pspec);
191 static void gst_srtp_dec_clear_streams (GstSrtpDec * filter);
192 static void gst_srtp_dec_remove_stream (GstSrtpDec * filter, guint ssrc);
194 static gboolean gst_srtp_dec_sink_event_rtp (GstPad * pad, GstObject * parent,
196 static gboolean gst_srtp_dec_sink_event_rtcp (GstPad * pad, GstObject * parent,
199 static gboolean gst_srtp_dec_sink_query_rtp (GstPad * pad, GstObject * parent,
201 static gboolean gst_srtp_dec_sink_query_rtcp (GstPad * pad,
202 GstObject * parent, GstQuery * query);
205 static GstIterator *gst_srtp_dec_iterate_internal_links_rtp (GstPad * pad,
207 static GstIterator *gst_srtp_dec_iterate_internal_links_rtcp (GstPad * pad,
210 static GstFlowReturn gst_srtp_dec_chain_rtp (GstPad * pad,
211 GstObject * parent, GstBuffer * buf);
212 static GstFlowReturn gst_srtp_dec_chain_rtcp (GstPad * pad,
213 GstObject * parent, GstBuffer * buf);
215 static GstStateChangeReturn gst_srtp_dec_change_state (GstElement * element,
216 GstStateChange transition);
218 static GstSrtpDecSsrcStream *request_key_with_signal (GstSrtpDec * filter,
219 guint32 ssrc, gint signal);
221 struct _GstSrtpDecSsrcStream
227 GstSrtpCipherType rtp_cipher;
228 GstSrtpAuthType rtp_auth;
229 GstSrtpCipherType rtcp_cipher;
230 GstSrtpAuthType rtcp_auth;
242 #define STREAM_HAS_CRYPTO(stream) \
243 (stream->rtp_cipher != GST_SRTP_CIPHER_NULL || \
244 stream->rtcp_cipher != GST_SRTP_CIPHER_NULL || \
245 stream->rtp_auth != GST_SRTP_AUTH_NULL || \
246 stream->rtcp_auth != GST_SRTP_AUTH_NULL)
249 /* initialize the srtpdec's class */
251 gst_srtp_dec_class_init (GstSrtpDecClass * klass)
253 GObjectClass *gobject_class;
254 GstElementClass *gstelement_class;
256 gobject_class = (GObjectClass *) klass;
257 gstelement_class = (GstElementClass *) klass;
259 gobject_class->set_property = gst_srtp_dec_set_property;
260 gobject_class->get_property = gst_srtp_dec_get_property;
262 gst_element_class_add_static_pad_template (gstelement_class,
264 gst_element_class_add_static_pad_template (gstelement_class,
266 gst_element_class_add_static_pad_template (gstelement_class,
268 gst_element_class_add_static_pad_template (gstelement_class,
269 &rtcp_sink_template);
271 gst_element_class_set_static_metadata (gstelement_class, "SRTP decoder",
272 "Filter/Network/SRTP",
273 "A SRTP and SRTCP decoder",
274 "Gabriel Millaire <millaire.gabriel@collabora.com>");
276 /* Install callbacks */
277 gstelement_class->change_state =
278 GST_DEBUG_FUNCPTR (gst_srtp_dec_change_state);
280 klass->clear_streams = GST_DEBUG_FUNCPTR (gst_srtp_dec_clear_streams);
281 klass->remove_stream = GST_DEBUG_FUNCPTR (gst_srtp_dec_remove_stream);
283 /* Install properties */
284 g_object_class_install_property (gobject_class, PROP_REPLAY_WINDOW_SIZE,
285 g_param_spec_uint ("replay-window-size", "Replay window size",
286 "Size of the replay protection window",
287 64, 0x8000, DEFAULT_REPLAY_WINDOW_SIZE,
288 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (gobject_class, PROP_STATS,
290 g_param_spec_boxed ("stats", "Statistics", "Various statistics",
291 GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
293 /* Install signals */
295 * GstSrtpDec::request-key:
296 * @gstsrtpdec: the element on which the signal is emitted
297 * @ssrc: The unique SSRC of the stream
299 * Signal emitted to get the parameters relevant to stream
300 * with @ssrc. User should provide the key and the RTP and
301 * RTCP encryption ciphers and authentication, and return
302 * them wrapped in a GstCaps.
304 gst_srtp_dec_signals[SIGNAL_REQUEST_KEY] =
305 g_signal_new ("request-key", G_TYPE_FROM_CLASS (klass),
306 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
309 * GstSrtpDec::clear-keys:
310 * @gstsrtpdec: the element on which the signal is emitted
312 * Clear the internal list of streams
314 gst_srtp_dec_signals[SIGNAL_CLEAR_KEYS] =
315 g_signal_new ("clear-keys", G_TYPE_FROM_CLASS (klass),
316 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
317 G_STRUCT_OFFSET (GstSrtpDecClass, clear_streams), NULL, NULL, NULL,
318 G_TYPE_NONE, 0, G_TYPE_NONE);
321 * GstSrtpDec::soft-limit:
322 * @gstsrtpdec: the element on which the signal is emitted
323 * @ssrc: The unique SSRC of the stream
325 * Signal emitted when the stream with @ssrc has reached the
326 * soft limit of utilisation of it's master encryption key.
327 * User should provide a new key and new RTP and RTCP encryption
328 * ciphers and authentication, and return them wrapped in a
331 gst_srtp_dec_signals[SIGNAL_SOFT_LIMIT] =
332 g_signal_new ("soft-limit", G_TYPE_FROM_CLASS (klass),
333 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
336 * GstSrtpDec::hard-limit:
337 * @gstsrtpdec: the element on which the signal is emitted
338 * @ssrc: The unique SSRC of the stream
340 * Signal emitted when the stream with @ssrc has reached the
341 * hard limit of utilisation of it's master encryption key.
342 * User should provide a new key and new RTP and RTCP encryption
343 * ciphers and authentication, and return them wrapped in a
344 * GstCaps. If user could not provide those parameters or signal
345 * is not answered, the buffers of this stream will be dropped.
347 gst_srtp_dec_signals[SIGNAL_HARD_LIMIT] =
348 g_signal_new ("hard-limit", G_TYPE_FROM_CLASS (klass),
349 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
352 * GstSrtpDec::remove-key:
353 * @gstsrtpdec: the element on which the signal is emitted
354 * @ssrc: The SSRC for which to remove the key.
356 * Removes keys for a specific SSRC
358 gst_srtp_dec_signals[SIGNAL_REMOVE_KEY] =
359 g_signal_new ("remove-key", G_TYPE_FROM_CLASS (klass),
360 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
361 G_STRUCT_OFFSET (GstSrtpDecClass, remove_stream), NULL, NULL, NULL,
362 G_TYPE_NONE, 1, G_TYPE_UINT);
366 /* initialize the new element
367 * instantiate pads and add them to element
368 * set pad callback functions
369 * initialize instance structure
372 gst_srtp_dec_init (GstSrtpDec * filter)
374 filter->replay_window_size = DEFAULT_REPLAY_WINDOW_SIZE;
376 filter->rtp_sinkpad =
377 gst_pad_new_from_static_template (&rtp_sink_template, "rtp_sink");
378 gst_pad_set_event_function (filter->rtp_sinkpad,
379 GST_DEBUG_FUNCPTR (gst_srtp_dec_sink_event_rtp));
380 gst_pad_set_query_function (filter->rtp_sinkpad,
381 GST_DEBUG_FUNCPTR (gst_srtp_dec_sink_query_rtp));
382 gst_pad_set_iterate_internal_links_function (filter->rtp_sinkpad,
383 GST_DEBUG_FUNCPTR (gst_srtp_dec_iterate_internal_links_rtp));
384 gst_pad_set_chain_function (filter->rtp_sinkpad,
385 GST_DEBUG_FUNCPTR (gst_srtp_dec_chain_rtp));
388 gst_pad_new_from_static_template (&rtp_src_template, "rtp_src");
389 gst_pad_set_iterate_internal_links_function (filter->rtp_srcpad,
390 GST_DEBUG_FUNCPTR (gst_srtp_dec_iterate_internal_links_rtp));
392 gst_pad_set_element_private (filter->rtp_sinkpad, filter->rtp_srcpad);
393 gst_pad_set_element_private (filter->rtp_srcpad, filter->rtp_sinkpad);
395 gst_element_add_pad (GST_ELEMENT (filter), filter->rtp_sinkpad);
396 gst_element_add_pad (GST_ELEMENT (filter), filter->rtp_srcpad);
399 filter->rtcp_sinkpad =
400 gst_pad_new_from_static_template (&rtcp_sink_template, "rtcp_sink");
401 gst_pad_set_event_function (filter->rtcp_sinkpad,
402 GST_DEBUG_FUNCPTR (gst_srtp_dec_sink_event_rtcp));
403 gst_pad_set_query_function (filter->rtcp_sinkpad,
404 GST_DEBUG_FUNCPTR (gst_srtp_dec_sink_query_rtcp));
405 gst_pad_set_iterate_internal_links_function (filter->rtcp_sinkpad,
406 GST_DEBUG_FUNCPTR (gst_srtp_dec_iterate_internal_links_rtcp));
407 gst_pad_set_chain_function (filter->rtcp_sinkpad,
408 GST_DEBUG_FUNCPTR (gst_srtp_dec_chain_rtcp));
410 filter->rtcp_srcpad =
411 gst_pad_new_from_static_template (&rtcp_src_template, "rtcp_src");
412 gst_pad_set_iterate_internal_links_function (filter->rtcp_srcpad,
413 GST_DEBUG_FUNCPTR (gst_srtp_dec_iterate_internal_links_rtcp));
415 gst_pad_set_element_private (filter->rtcp_sinkpad, filter->rtcp_srcpad);
416 gst_pad_set_element_private (filter->rtcp_srcpad, filter->rtcp_sinkpad);
418 gst_element_add_pad (GST_ELEMENT (filter), filter->rtcp_sinkpad);
419 gst_element_add_pad (GST_ELEMENT (filter), filter->rtcp_srcpad);
421 filter->first_session = TRUE;
424 static GstStructure *
425 gst_srtp_dec_create_stats (GstSrtpDec * filter)
428 GValue va = G_VALUE_INIT;
429 GValue v = G_VALUE_INIT;
431 s = gst_structure_new_empty ("application/x-srtp-decoder-stats");
433 g_value_init (&va, GST_TYPE_ARRAY);
434 g_value_init (&v, GST_TYPE_STRUCTURE);
436 if (filter->session) {
440 g_hash_table_iter_init (&iter, filter->streams);
441 while (g_hash_table_iter_next (&iter, &key, NULL)) {
443 guint32 ssrc = GPOINTER_TO_UINT (key);
444 srtp_err_status_t status;
447 status = srtp_get_stream_roc (filter->session, ssrc, &roc);
448 if (status != srtp_err_status_ok) {
452 ss = gst_structure_new ("application/x-srtp-stream",
453 "ssrc", G_TYPE_UINT, ssrc, "roc", G_TYPE_UINT, roc, NULL);
455 g_value_take_boxed (&v, ss);
456 gst_value_array_append_value (&va, &v);
460 gst_structure_take_value (s, "streams", &va);
467 gst_srtp_dec_set_property (GObject * object, guint prop_id,
468 const GValue * value, GParamSpec * pspec)
470 GstSrtpDec *filter = GST_SRTP_DEC (object);
472 GST_OBJECT_LOCK (filter);
475 case PROP_REPLAY_WINDOW_SIZE:
476 filter->replay_window_size = g_value_get_uint (value);
479 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
483 GST_OBJECT_UNLOCK (filter);
487 gst_srtp_dec_get_property (GObject * object, guint prop_id,
488 GValue * value, GParamSpec * pspec)
490 GstSrtpDec *filter = GST_SRTP_DEC (object);
492 GST_OBJECT_LOCK (filter);
495 case PROP_REPLAY_WINDOW_SIZE:
496 g_value_set_uint (value, filter->replay_window_size);
499 g_value_take_boxed (value, gst_srtp_dec_create_stats (filter));
502 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
506 GST_OBJECT_UNLOCK (filter);
510 gst_srtp_dec_remove_stream (GstSrtpDec * filter, guint ssrc)
512 GstSrtpDecSsrcStream *stream = NULL;
514 if (filter->streams == NULL)
517 stream = g_hash_table_lookup (filter->streams, GUINT_TO_POINTER (ssrc));
520 srtp_remove_stream (filter->session, ssrc);
521 g_hash_table_remove (filter->streams, GUINT_TO_POINTER (ssrc));
525 static GstSrtpDecSsrcStream *
526 find_stream_by_ssrc (GstSrtpDec * filter, guint32 ssrc)
528 return g_hash_table_lookup (filter->streams, GUINT_TO_POINTER (ssrc));
533 clear_key (gpointer data)
535 struct GstSrtpDecKey *key = data;
537 gst_clear_buffer (&key->mki);
538 gst_clear_buffer (&key->key);
543 /* get info from buffer caps
545 static GstSrtpDecSsrcStream *
546 get_stream_from_caps (GstSrtpDec * filter, GstCaps * caps, guint32 ssrc)
548 GstSrtpDecSsrcStream *stream;
551 const gchar *rtp_cipher, *rtp_auth, *rtcp_cipher, *rtcp_auth;
553 /* Create new stream structure and set default values */
554 stream = g_slice_new0 (GstSrtpDecSsrcStream);
558 /* Get info from caps */
559 s = gst_caps_get_structure (caps, 0);
563 rtp_cipher = gst_structure_get_string (s, "srtp-cipher");
564 rtp_auth = gst_structure_get_string (s, "srtp-auth");
565 rtcp_cipher = gst_structure_get_string (s, "srtcp-cipher");
566 rtcp_auth = gst_structure_get_string (s, "srtcp-auth");
567 if (!rtp_cipher || !rtp_auth || !rtcp_cipher || !rtcp_auth)
570 gst_structure_get_uint (s, "roc", &stream->roc);
572 stream->rtp_cipher = enum_value_from_nick (GST_TYPE_SRTP_CIPHER_TYPE,
574 stream->rtp_auth = enum_value_from_nick (GST_TYPE_SRTP_AUTH_TYPE, rtp_auth);
575 stream->rtcp_cipher = enum_value_from_nick (GST_TYPE_SRTP_CIPHER_TYPE,
577 stream->rtcp_auth = enum_value_from_nick (GST_TYPE_SRTP_AUTH_TYPE, rtcp_auth);
579 if ((gint) stream->rtp_cipher == -1 || (gint) stream->rtp_auth == -1 ||
580 (gint) stream->rtcp_cipher == -1 || (gint) stream->rtcp_auth == -1) {
581 GST_WARNING_OBJECT (filter, "Invalid caps for stream,"
582 " unknown cipher or auth type");
586 /* RFC 3711 says in "3. SRTP Framework" that SRTCP message authentication
587 * is MANDATORY. In case of GCM let the pipeline handle any errors.
589 if (stream->rtcp_cipher != GST_SRTP_CIPHER_AES_128_GCM
590 && stream->rtcp_cipher != GST_SRTP_CIPHER_AES_256_GCM
591 && stream->rtcp_cipher != GST_SRTP_CIPHER_NULL
592 && stream->rtcp_auth == GST_SRTP_AUTH_NULL) {
593 GST_WARNING_OBJECT (filter,
594 "Cannot have SRTP NULL authentication with a not-NULL encryption"
599 if (gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL) || !buf) {
601 GstBuffer *mki = NULL;
606 GST_DEBUG_OBJECT (filter, "Got key [%p] for SSRC %u", buf, ssrc);
609 if (gst_structure_get (s, "mki", GST_TYPE_BUFFER, &mki, NULL) && mki) {
610 struct GstSrtpDecKey key = {.mki = mki,.key = buf };
612 mki_size = gst_buffer_get_size (mki);
613 if (mki_size > SRTP_MAX_MKI_LEN) {
614 GST_WARNING_OBJECT (filter, "MKI is longer than allowed (%"
615 G_GSIZE_FORMAT " > %d).", mki_size, SRTP_MAX_MKI_LEN);
616 gst_buffer_unref (mki);
617 gst_buffer_unref (buf);
622 g_array_sized_new (FALSE, TRUE, sizeof (struct GstSrtpDecKey), 2);
623 g_array_set_clear_func (stream->keys, clear_key);
625 g_array_append_val (stream->keys, key);
627 /* Append more MKIs */
628 for (i = 1; i < SRTP_MAX_NUM_MASTER_KEYS; i++) {
631 g_snprintf (mki_id, 16, "mki%d", i + 1);
632 g_snprintf (key_id, 16, "srtp-key%d", i + 1);
634 if (gst_structure_get (s, mki_id, GST_TYPE_BUFFER, &mki,
635 key_id, GST_TYPE_BUFFER, &buf, NULL)) {
636 if (gst_buffer_get_size (mki) != mki_size) {
637 GST_WARNING_OBJECT (filter,
638 "MKIs need to all have the same size (first was %"
639 G_GSIZE_FORMAT ", current is %" G_GSIZE_FORMAT ").",
640 mki_size, gst_buffer_get_size (mki));
641 gst_buffer_unref (mki);
642 gst_buffer_unref (buf);
647 g_array_append_val (stream->keys, key);
657 } else if (STREAM_HAS_CRYPTO (stream)) {
664 g_slice_free (GstSrtpDecSsrcStream, stream);
668 /* Get SRTP params by signal
671 signal_get_srtp_params (GstSrtpDec * filter, guint32 ssrc, gint signal)
673 GstCaps *caps = NULL;
675 g_signal_emit (filter, gst_srtp_dec_signals[signal], 0, ssrc, &caps);
678 GST_DEBUG_OBJECT (filter, "Caps received");
683 /* Create a stream in the session
685 static srtp_err_status_t
686 init_session_stream (GstSrtpDec * filter, guint32 ssrc,
687 GstSrtpDecSsrcStream * stream)
689 srtp_err_status_t ret;
690 srtp_policy_t policy;
694 GstMapInfo *key_maps = NULL;
695 GstMapInfo *mki_maps = NULL;
698 memset (&policy, 0, sizeof (srtp_policy_t));
701 return srtp_err_status_bad_param;
703 GST_INFO_OBJECT (filter, "Setting RTP policy...");
704 set_crypto_policy_cipher_auth (stream->rtp_cipher, stream->rtp_auth,
706 GST_INFO_OBJECT (filter, "Setting RTCP policy...");
707 set_crypto_policy_cipher_auth (stream->rtcp_cipher, stream->rtcp_auth,
713 srtp_master_key_t *keys;
715 keys = g_alloca (sizeof (srtp_master_key_t) * stream->keys->len);
716 policy.keys = g_alloca (sizeof (gpointer) * stream->keys->len);
717 key_maps = g_alloca (sizeof (GstMapInfo) * stream->keys->len);
718 mki_maps = g_alloca (sizeof (GstMapInfo) * stream->keys->len);
720 for (i = 0; i < stream->keys->len; i++) {
721 struct GstSrtpDecKey *key =
722 &g_array_index (stream->keys, struct GstSrtpDecKey, i);
723 policy.keys[i] = &keys[i];
725 gst_buffer_map (key->mki, &mki_maps[i], GST_MAP_READ);
726 gst_buffer_map (key->key, &key_maps[i], GST_MAP_READ);
728 policy.keys[i]->key = (guchar *) key_maps[i].data;
729 policy.keys[i]->mki_id = (guchar *) mki_maps[i].data;
730 policy.keys[i]->mki_size = mki_maps[i].size;
732 policy.num_master_keys = stream->keys->len;
736 gst_buffer_map (stream->key, &map, GST_MAP_READ);
737 policy.key = (guchar *) map.data;
742 policy.ssrc.value = ssrc;
743 policy.ssrc.type = ssrc_specific;
744 policy.window_size = filter->replay_window_size;
747 /* If it is the first stream, create the session
748 * If not, add the stream policy to the session
750 if (filter->first_session)
751 ret = srtp_create (&filter->session, &policy);
753 ret = srtp_add_stream (filter->session, &policy);
756 gst_buffer_unmap (stream->key, &map);
762 for (i = 0; i < stream->keys->len; i++) {
763 struct GstSrtpDecKey *key = &g_array_index (stream->keys,
764 struct GstSrtpDecKey, i);
765 gst_buffer_unmap (key->mki, &mki_maps[i]);
766 gst_buffer_unmap (key->key, &key_maps[i]);
772 if (ret == srtp_err_status_ok) {
773 srtp_err_status_t status;
775 status = srtp_set_stream_roc (filter->session, ssrc, stream->roc);
777 (void) status; /* Ignore unused variable */
779 if (status == srtp_err_status_ok) {
780 /* Here, we just set the ROC, but we also need to set the initial
781 * RTP sequence number later, otherwise libsrtp will not be able
782 * to get the right packet index. */
783 g_hash_table_add (filter->streams_roc_changed, GUINT_TO_POINTER (ssrc));
787 filter->first_session = FALSE;
788 g_hash_table_insert (filter->streams, GUINT_TO_POINTER (stream->ssrc),
795 /* Return a stream structure for a given buffer
797 static GstSrtpDecSsrcStream *
798 validate_buffer (GstSrtpDec * filter, GstBuffer * buf, guint32 * ssrc,
801 GstSrtpDecSsrcStream *stream = NULL;
802 GstRTPBuffer rtpbuf = GST_RTP_BUFFER_INIT;
804 if (gst_rtp_buffer_map (buf,
805 GST_MAP_READ | GST_RTP_BUFFER_MAP_FLAG_SKIP_PADDING, &rtpbuf)) {
806 if (gst_rtp_buffer_get_payload_type (&rtpbuf) < 64
807 || gst_rtp_buffer_get_payload_type (&rtpbuf) > 80) {
808 *ssrc = gst_rtp_buffer_get_ssrc (&rtpbuf);
810 gst_rtp_buffer_unmap (&rtpbuf);
814 gst_rtp_buffer_unmap (&rtpbuf);
817 if (rtcp_buffer_get_ssrc (buf, ssrc)) {
820 GST_WARNING_OBJECT (filter, "No SSRC found in buffer");
826 stream = find_stream_by_ssrc (filter, *ssrc);
831 return request_key_with_signal (filter, *ssrc, SIGNAL_REQUEST_KEY);
835 free_stream (GstSrtpDecSsrcStream * stream)
838 gst_buffer_unref (stream->key);
840 g_array_free (stream->keys, TRUE);
841 g_slice_free (GstSrtpDecSsrcStream, stream);
845 buffers_are_equal (GstBuffer * a, GstBuffer * b)
852 if (a == NULL || b == NULL)
855 if (gst_buffer_get_size (a) != gst_buffer_get_size (b))
858 if (gst_buffer_map (a, &info, GST_MAP_READ)) {
861 equal = (gst_buffer_memcmp (b, 0, info.data, info.size) == 0);
862 gst_buffer_unmap (a, &info);
871 keys_are_equal (GArray * a, GArray * b)
879 if (a == NULL || b == NULL)
882 if (a->len != b->len)
885 for (i = 0; i < a->len; i++) {
886 struct GstSrtpDecKey *key_a = &g_array_index (a,
887 struct GstSrtpDecKey, i);
888 struct GstSrtpDecKey *key_b = &g_array_index (b,
889 struct GstSrtpDecKey, i);
891 if (!buffers_are_equal (key_a->mki, key_b->mki))
894 if (!buffers_are_equal (key_a->key, key_b->key))
904 /* Create new stream from params in caps
906 static GstSrtpDecSsrcStream *
907 update_session_stream_from_caps (GstSrtpDec * filter, guint32 ssrc,
910 GstSrtpDecSsrcStream *stream = NULL;
911 GstSrtpDecSsrcStream *old_stream = NULL;
912 srtp_err_status_t err;
914 g_return_val_if_fail (GST_IS_SRTP_DEC (filter), NULL);
915 g_return_val_if_fail (GST_IS_CAPS (caps), NULL);
917 stream = get_stream_from_caps (filter, caps, ssrc);
919 old_stream = find_stream_by_ssrc (filter, ssrc);
920 if (stream && old_stream &&
921 stream->rtp_cipher == old_stream->rtp_cipher &&
922 stream->rtcp_cipher == old_stream->rtcp_cipher &&
923 stream->rtp_auth == old_stream->rtp_auth &&
924 stream->rtcp_auth == old_stream->rtcp_auth &&
925 ((stream->keys && keys_are_equal (stream->keys, old_stream->keys)) ||
926 buffers_are_equal (stream->key, old_stream->key))) {
927 free_stream (stream);
931 /* Remove existing stream, if any */
932 gst_srtp_dec_remove_stream (filter, ssrc);
935 /* Create new session stream */
936 err = init_session_stream (filter, ssrc, stream);
938 if (err != srtp_err_status_ok) {
939 GST_WARNING_OBJECT (filter, "Failed to create the stream (err: %d)", err);
941 gst_buffer_unref (stream->key);
942 g_slice_free (GstSrtpDecSsrcStream, stream);
951 remove_yes (gpointer key, gpointer value, gpointer user_data)
956 /* Clear the policy list
959 gst_srtp_dec_clear_streams (GstSrtpDec * filter)
963 GST_OBJECT_LOCK (filter);
965 if (!filter->first_session) {
966 srtp_dealloc (filter->session);
967 filter->session = NULL;
971 nb = g_hash_table_foreach_remove (filter->streams, remove_yes, NULL);
973 filter->first_session = TRUE;
975 GST_OBJECT_UNLOCK (filter);
977 GST_DEBUG_OBJECT (filter, "Cleared %d streams", nb);
982 static GstSrtpDecSsrcStream *
983 request_key_with_signal (GstSrtpDec * filter, guint32 ssrc, gint signal)
986 GstSrtpDecSsrcStream *stream = NULL;
988 caps = signal_get_srtp_params (filter, ssrc, signal);
991 stream = update_session_stream_from_caps (filter, ssrc, caps);
993 GST_DEBUG_OBJECT (filter, "New stream set with SSRC %u", ssrc);
995 GST_WARNING_OBJECT (filter, "Could not set stream with SSRC %u", ssrc);
996 gst_caps_unref (caps);
998 GST_WARNING_OBJECT (filter, "Could not get caps for stream with SSRC %u",
1006 gst_srtp_dec_sink_setcaps (GstPad * pad, GstObject * parent,
1007 GstCaps * caps, gboolean is_rtcp)
1009 GstSrtpDec *filter = GST_SRTP_DEC (parent);
1012 gboolean ret = FALSE;
1014 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
1016 ps = gst_caps_get_structure (caps, 0);
1018 if (gst_structure_has_field_typed (ps, "ssrc", G_TYPE_UINT) &&
1019 gst_structure_has_field_typed (ps, "srtp-cipher", G_TYPE_STRING) &&
1020 gst_structure_has_field_typed (ps, "srtp-auth", G_TYPE_STRING) &&
1021 gst_structure_has_field_typed (ps, "srtcp-cipher", G_TYPE_STRING) &&
1022 gst_structure_has_field_typed (ps, "srtcp-auth", G_TYPE_STRING)) {
1025 gst_structure_get_uint (ps, "ssrc", &ssrc);
1027 if (!update_session_stream_from_caps (filter, ssrc, caps)) {
1028 GST_WARNING_OBJECT (pad, "Could not create session from pad caps: %"
1029 GST_PTR_FORMAT, caps);
1034 caps = gst_caps_copy (caps);
1035 ps = gst_caps_get_structure (caps, 0);
1036 gst_structure_remove_fields (ps, "srtp-key", "srtp-cipher", "srtp-auth",
1037 "srtcp-cipher", "srtcp-auth", "mki", NULL);
1040 gst_structure_set_name (ps, "application/x-rtcp");
1042 gst_structure_set_name (ps, "application/x-rtp");
1044 otherpad = gst_pad_get_element_private (pad);
1046 ret = gst_pad_set_caps (otherpad, caps);
1048 gst_caps_unref (caps);
1054 gst_srtp_dec_sink_event_rtp (GstPad * pad, GstObject * parent, GstEvent * event)
1058 GstSrtpDec *filter = GST_SRTP_DEC (parent);
1060 switch (GST_EVENT_TYPE (event)) {
1061 case GST_EVENT_CAPS:
1062 gst_event_parse_caps (event, &caps);
1063 ret = gst_srtp_dec_sink_setcaps (pad, parent, caps, FALSE);
1064 gst_event_unref (event);
1066 case GST_EVENT_SEGMENT:
1067 /* Make sure to send a caps event downstream before the segment event,
1068 * even if upstream didn't */
1069 if (!gst_pad_has_current_caps (filter->rtp_srcpad)) {
1070 GstCaps *caps = gst_caps_new_empty_simple ("application/x-rtp");
1072 gst_pad_set_caps (filter->rtp_srcpad, caps);
1073 gst_caps_unref (caps);
1075 filter->rtp_has_segment = TRUE;
1077 case GST_EVENT_FLUSH_STOP:
1078 filter->rtp_has_segment = FALSE;
1084 return gst_pad_event_default (pad, parent, event);
1088 gst_srtp_dec_sink_event_rtcp (GstPad * pad, GstObject * parent,
1093 GstSrtpDec *filter = GST_SRTP_DEC (parent);
1095 switch (GST_EVENT_TYPE (event)) {
1096 case GST_EVENT_CAPS:
1097 gst_event_parse_caps (event, &caps);
1098 ret = gst_srtp_dec_sink_setcaps (pad, parent, caps, TRUE);
1099 gst_event_unref (event);
1101 case GST_EVENT_SEGMENT:
1102 /* Make sure to send a caps event downstream before the segment event,
1103 * even if upstream didn't */
1104 if (!gst_pad_has_current_caps (filter->rtcp_srcpad)) {
1105 GstCaps *caps = gst_caps_new_empty_simple ("application/x-rtcp");
1107 gst_pad_set_caps (filter->rtcp_srcpad, caps);
1108 gst_caps_unref (caps);
1110 filter->rtcp_has_segment = TRUE;
1112 case GST_EVENT_FLUSH_STOP:
1113 filter->rtcp_has_segment = FALSE;
1119 return gst_pad_event_default (pad, parent, event);
1123 gst_srtp_dec_sink_query (GstPad * pad, GstObject * parent, GstQuery * query,
1126 switch (GST_QUERY_TYPE (query)) {
1127 case GST_QUERY_CAPS:
1129 GstCaps *filter = NULL;
1130 GstCaps *other_filter = NULL;
1131 GstCaps *template_caps;
1133 GstCaps *other_caps;
1137 gst_query_parse_caps (query, &filter);
1139 otherpad = (GstPad *) gst_pad_get_element_private (pad);
1142 other_filter = gst_caps_copy (filter);
1144 for (i = 0; i < gst_caps_get_size (other_filter); i++) {
1145 GstStructure *ps = gst_caps_get_structure (other_filter, i);
1147 gst_structure_set_name (ps, "application/x-rtcp");
1149 gst_structure_set_name (ps, "application/x-rtp");
1150 gst_structure_remove_fields (ps, "srtp-key", "srtp-cipher",
1151 "srtp-auth", "srtcp-cipher", "srtcp-auth", "mki", NULL);
1156 other_caps = gst_pad_peer_query_caps (otherpad, other_filter);
1158 gst_caps_unref (other_filter);
1160 goto return_template;
1163 template_caps = gst_pad_get_pad_template_caps (otherpad);
1164 ret = gst_caps_intersect_full (other_caps, template_caps,
1165 GST_CAPS_INTERSECT_FIRST);
1166 gst_caps_unref (other_caps);
1167 gst_caps_unref (template_caps);
1169 ret = gst_caps_make_writable (ret);
1171 for (i = 0; i < gst_caps_get_size (ret); i++) {
1172 GstStructure *ps = gst_caps_get_structure (ret, i);
1174 gst_structure_set_name (ps, "application/x-srtcp");
1176 gst_structure_set_name (ps, "application/x-srtp");
1182 tmp = gst_caps_intersect (ret, filter);
1183 gst_caps_unref (ret);
1187 gst_query_set_caps_result (query, ret);
1188 gst_caps_unref (ret);
1193 ret = gst_pad_get_pad_template_caps (pad);
1194 gst_query_set_caps_result (query, ret);
1195 gst_caps_unref (ret);
1199 return gst_pad_query_default (pad, parent, query);
1204 gst_srtp_dec_sink_query_rtp (GstPad * pad, GstObject * parent, GstQuery * query)
1206 return gst_srtp_dec_sink_query (pad, parent, query, FALSE);
1210 gst_srtp_dec_sink_query_rtcp (GstPad * pad, GstObject * parent,
1213 return gst_srtp_dec_sink_query (pad, parent, query, TRUE);
1216 static GstIterator *
1217 gst_srtp_dec_iterate_internal_links (GstPad * pad, GstObject * parent,
1220 GstSrtpDec *filter = GST_SRTP_DEC (parent);
1221 GstPad *otherpad = NULL;
1222 GstIterator *it = NULL;
1224 otherpad = (GstPad *) gst_pad_get_element_private (pad);
1229 g_value_init (&val, GST_TYPE_PAD);
1230 g_value_set_object (&val, otherpad);
1231 it = gst_iterator_new_single (GST_TYPE_PAD, &val);
1232 g_value_unset (&val);
1234 GST_ELEMENT_ERROR (GST_ELEMENT_CAST (filter), CORE, PAD, (NULL),
1235 ("Unable to get linked pad"));
1241 static GstIterator *
1242 gst_srtp_dec_iterate_internal_links_rtp (GstPad * pad, GstObject * parent)
1244 return gst_srtp_dec_iterate_internal_links (pad, parent, FALSE);
1247 static GstIterator *
1248 gst_srtp_dec_iterate_internal_links_rtcp (GstPad * pad, GstObject * parent)
1250 return gst_srtp_dec_iterate_internal_links (pad, parent, TRUE);
1254 gst_srtp_dec_push_early_events (GstSrtpDec * filter, GstPad * pad,
1255 GstPad * otherpad, gboolean is_rtcp)
1257 GstEvent *otherev, *ev;
1259 ev = gst_pad_get_sticky_event (pad, GST_EVENT_STREAM_START, 0);
1261 gst_event_unref (ev);
1263 gchar *new_stream_id;
1265 otherev = gst_pad_get_sticky_event (otherpad, GST_EVENT_STREAM_START, 0);
1268 const gchar *other_stream_id;
1270 gst_event_parse_stream_start (otherev, &other_stream_id);
1272 new_stream_id = g_strdup_printf ("%s/%s", other_stream_id,
1273 is_rtcp ? "rtcp" : "rtp");
1274 gst_event_unref (otherev);
1276 new_stream_id = gst_pad_create_stream_id (pad, GST_ELEMENT (filter),
1277 is_rtcp ? "rtcp" : "rtp");
1280 ev = gst_event_new_stream_start (new_stream_id);
1281 g_free (new_stream_id);
1283 gst_pad_push_event (pad, ev);
1286 ev = gst_pad_get_sticky_event (pad, GST_EVENT_CAPS, 0);
1288 gst_event_unref (ev);
1293 caps = gst_caps_new_empty_simple ("application/x-rtcp");
1295 caps = gst_caps_new_empty_simple ("application/x-rtp");
1297 gst_pad_set_caps (pad, caps);
1298 gst_caps_unref (caps);
1301 ev = gst_pad_get_sticky_event (pad, GST_EVENT_SEGMENT, 0);
1303 gst_event_unref (ev);
1305 ev = gst_pad_get_sticky_event (otherpad, GST_EVENT_SEGMENT, 0);
1308 gst_pad_push_event (pad, ev);
1312 filter->rtcp_has_segment = TRUE;
1314 filter->rtp_has_segment = TRUE;
1319 * This function should be called while holding the filter lock
1322 gst_srtp_dec_decode_buffer (GstSrtpDec * filter, GstPad * pad, GstBuffer * buf,
1323 gboolean is_rtcp, guint32 ssrc)
1326 srtp_err_status_t err;
1329 GST_LOG_OBJECT (pad, "Received %s buffer of size %" G_GSIZE_FORMAT
1330 " with SSRC = %u", is_rtcp ? "RTCP" : "RTP", gst_buffer_get_size (buf),
1333 /* Change buffer to remove protection */
1334 buf = gst_buffer_make_writable (buf);
1336 gst_buffer_map (buf, &map, GST_MAP_READWRITE);
1341 gst_srtp_init_event_reporter ();
1345 GstSrtpDecSsrcStream *stream = find_stream_by_ssrc (filter, ssrc);
1347 err = srtp_unprotect_rtcp_mki (filter->session, map.data, &size,
1348 stream && stream->keys);
1350 err = srtp_unprotect_rtcp (filter->session, map.data, &size);
1354 /* If ROC has changed, we know we need to set the initial RTP
1355 * sequence number too. */
1356 if (g_hash_table_contains (filter->streams_roc_changed,
1357 GUINT_TO_POINTER (ssrc))) {
1358 srtp_stream_t stream;
1360 stream = srtp_get_stream (filter->session, htonl (ssrc));
1364 GstRTPBuffer rtpbuf = GST_RTP_BUFFER_INIT;
1366 gst_rtp_buffer_map (buf,
1367 GST_MAP_READ | GST_RTP_BUFFER_MAP_FLAG_SKIP_PADDING, &rtpbuf);
1368 seqnum = gst_rtp_buffer_get_seq (&rtpbuf);
1369 gst_rtp_buffer_unmap (&rtpbuf);
1371 /* We finally add the RTP sequence number to the current
1372 * rollover counter. */
1373 stream->rtp_rdbx.index &= ~0xFFFF;
1374 stream->rtp_rdbx.index |= seqnum;
1377 g_hash_table_remove (filter->streams_roc_changed,
1378 GUINT_TO_POINTER (ssrc));
1384 GstSrtpDecSsrcStream *stream = find_stream_by_ssrc (filter, ssrc);
1386 err = srtp_unprotect_mki (filter->session, map.data, &size,
1387 stream && stream->keys);
1390 err = srtp_unprotect (filter->session, map.data, &size);
1394 /* Signal user depending on type of error */
1396 case srtp_err_status_ok:
1399 case srtp_err_status_replay_fail:
1400 GST_DEBUG_OBJECT (filter,
1401 "Dropping replayed packet, probably retransmission");
1403 case srtp_err_status_replay_old:
1404 GST_DEBUG_OBJECT (filter,
1405 "Dropping replayed old packet, probably retransmission");
1407 case srtp_err_status_key_expired:{
1408 GstSrtpDecSsrcStream *stream;
1410 /* Check we have an existing stream to rekey */
1411 stream = find_stream_by_ssrc (filter, ssrc);
1412 if (stream == NULL) {
1413 GST_WARNING_OBJECT (filter, "Could not find matching stream, dropping");
1417 GST_OBJECT_UNLOCK (filter);
1418 stream = request_key_with_signal (filter, ssrc, SIGNAL_HARD_LIMIT);
1419 GST_OBJECT_LOCK (filter);
1421 /* Check the key request created a new stream */
1422 if (stream == NULL) {
1423 GST_WARNING_OBJECT (filter, "Hard limit reached, no new key, dropping");
1429 case srtp_err_status_auth_fail:
1430 GST_WARNING_OBJECT (filter, "Error authentication packet, dropping");
1432 case srtp_err_status_cipher_fail:
1433 GST_WARNING_OBJECT (filter, "Error while decrypting packet, dropping");
1436 GST_WARNING_OBJECT (pad,
1437 "Unable to unprotect buffer (unprotect failed code %d)", err);
1441 gst_buffer_unmap (buf, &map);
1442 gst_buffer_set_size (buf, size);
1446 gst_buffer_unmap (buf, &map);
1450 static GstFlowReturn
1451 gst_srtp_dec_chain (GstPad * pad, GstObject * parent, GstBuffer * buf,
1454 GstSrtpDec *filter = GST_SRTP_DEC (parent);
1456 GstSrtpDecSsrcStream *stream = NULL;
1457 GstFlowReturn ret = GST_FLOW_OK;
1460 GST_OBJECT_LOCK (filter);
1462 /* Check if this stream exists, if not create a new stream */
1464 if (!(stream = validate_buffer (filter, buf, &ssrc, &is_rtcp))) {
1465 GST_OBJECT_UNLOCK (filter);
1466 GST_WARNING_OBJECT (filter, "Invalid buffer, dropping");
1470 if (!STREAM_HAS_CRYPTO (stream)) {
1471 GST_OBJECT_UNLOCK (filter);
1475 if (!gst_srtp_dec_decode_buffer (filter, pad, buf, is_rtcp, ssrc)) {
1476 GST_OBJECT_UNLOCK (filter);
1480 GST_OBJECT_UNLOCK (filter);
1482 /* If all is well, we may have reached soft limit */
1483 if (gst_srtp_get_soft_limit_reached ())
1484 request_key_with_signal (filter, ssrc, SIGNAL_SOFT_LIMIT);
1487 /* Push buffer to source pad */
1489 otherpad = filter->rtcp_srcpad;
1490 if (!filter->rtcp_has_segment)
1491 gst_srtp_dec_push_early_events (filter, filter->rtcp_srcpad,
1492 filter->rtp_srcpad, TRUE);
1494 otherpad = filter->rtp_srcpad;
1495 if (!filter->rtp_has_segment)
1496 gst_srtp_dec_push_early_events (filter, filter->rtp_srcpad,
1497 filter->rtcp_srcpad, FALSE);
1499 ret = gst_pad_push (otherpad, buf);
1504 /* Drop buffer, except if gst_pad_push returned OK or an error */
1506 gst_buffer_unref (buf);
1511 static GstFlowReturn
1512 gst_srtp_dec_chain_rtp (GstPad * pad, GstObject * parent, GstBuffer * buf)
1514 return gst_srtp_dec_chain (pad, parent, buf, FALSE);
1517 static GstFlowReturn
1518 gst_srtp_dec_chain_rtcp (GstPad * pad, GstObject * parent, GstBuffer * buf)
1520 return gst_srtp_dec_chain (pad, parent, buf, TRUE);
1523 static GstStateChangeReturn
1524 gst_srtp_dec_change_state (GstElement * element, GstStateChange transition)
1526 GstStateChangeReturn res;
1529 filter = GST_SRTP_DEC (element);
1530 GST_OBJECT_LOCK (filter);
1532 switch (transition) {
1533 case GST_STATE_CHANGE_READY_TO_PAUSED:
1534 filter->streams = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1535 NULL, (GDestroyNotify) free_stream);
1538 filter->streams_roc_changed =
1539 g_hash_table_new (g_direct_hash, g_direct_equal);
1542 filter->rtp_has_segment = FALSE;
1543 filter->rtcp_has_segment = FALSE;
1545 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1551 GST_OBJECT_UNLOCK (filter);
1553 res = GST_ELEMENT_CLASS (gst_srtp_dec_parent_class)->change_state (element,
1556 switch (transition) {
1557 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1559 case GST_STATE_CHANGE_PAUSED_TO_READY:
1560 gst_srtp_dec_clear_streams (filter);
1561 g_hash_table_unref (filter->streams);
1562 filter->streams = NULL;
1565 g_hash_table_unref (filter->streams_roc_changed);
1566 filter->streams_roc_changed = NULL;
1570 case GST_STATE_CHANGE_READY_TO_NULL: