2013-03-12 23:47:51 +00:00
|
|
|
/*
|
|
|
|
* GStreamer - GStreamer SRTP encoder
|
|
|
|
*
|
|
|
|
* Copyright 2009-2011 Collabora Ltd.
|
|
|
|
* @author: Gabriel Millaire <gabriel.millaire@collabora.com>
|
|
|
|
* @author: Olivier Crete <olivier.crete@collabora.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the
|
|
|
|
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
|
|
|
|
* which case the following provisions apply instead of the ones
|
|
|
|
* mentioned above:
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-03-08 18:01:13 +00:00
|
|
|
* SECTION:element-srtpenc
|
|
|
|
* @title: srtpenc
|
2013-03-25 18:18:24 +00:00
|
|
|
* @see_also: srtpdec
|
2013-03-12 23:47:51 +00:00
|
|
|
*
|
|
|
|
* gstrtpenc acts as an encoder that adds security to RTP and RTCP
|
|
|
|
* packets in the form of encryption and authentication. It outs SRTP
|
|
|
|
* and SRTCP.
|
|
|
|
*
|
|
|
|
* An application can request multiple RTP and RTCP pads to protect,
|
|
|
|
* but every sink pad requested must receive packets from the same
|
|
|
|
* source (identical SSRC). If a packet received contains a different
|
2019-09-02 19:08:44 +00:00
|
|
|
* SSRC, a warning is emitted and the valid SSRC is forced on the packet.
|
2013-03-12 23:47:51 +00:00
|
|
|
*
|
|
|
|
* This element uses libsrtp library. When receiving the first packet,
|
|
|
|
* the library is initialized with a new stream (based on the SSRC). It
|
|
|
|
* uses the default RTP and RTCP encryption and authentication mechanisms,
|
|
|
|
* unless the user has set the relevant properties first. It also uses
|
|
|
|
* a master key that MUST be set by property (key) at the beginning. The
|
2013-12-13 22:16:49 +00:00
|
|
|
* master key must be of a maximum length of 46 characters (14 characters
|
2019-03-15 09:46:56 +00:00
|
|
|
* for the salt plus the key). The encryption and authentication mechanisms
|
2013-12-13 22:16:49 +00:00
|
|
|
* available are :
|
2013-03-12 23:47:51 +00:00
|
|
|
*
|
|
|
|
* Encryption (properties rtp-cipher and rtcp-cipher)
|
2013-12-13 22:16:49 +00:00
|
|
|
* - AES_ICM 256 bits (maximum security)
|
|
|
|
* - AES_ICM 128 bits (default)
|
2013-03-12 23:47:51 +00:00
|
|
|
* - NULL
|
|
|
|
*
|
|
|
|
* Authentication (properties rtp-auth and rtcp-auth)
|
2013-12-13 22:16:49 +00:00
|
|
|
* - HMAC_SHA1 80 bits (default, maximum protection)
|
|
|
|
* - HMAC_SHA1 32 bits
|
2013-03-12 23:47:51 +00:00
|
|
|
* - NULL
|
|
|
|
*
|
|
|
|
* Note that for SRTP protection, authentication is mandatory (non-null)
|
|
|
|
* if encryption is used (non-null).
|
|
|
|
*
|
|
|
|
* When requested to create a sink pad, a linked source pad is created.
|
|
|
|
* Each packet received is first analysed (checked for valid SSRC) then
|
|
|
|
* its buffer is protected with libsrtp, then pushed on the source pad.
|
|
|
|
* If protection failed or the stream could not be created, the buffer
|
|
|
|
* is dropped and a warning is emitted. The packets pushed on the source
|
|
|
|
* pad are of type 'application/x-srtp' or 'application/x-srtcp'.
|
|
|
|
*
|
|
|
|
* When the maximum usage of the master key is reached, a soft-limit
|
|
|
|
* signal is sent to the user. The user must then set a new master key
|
|
|
|
* by property. If the hard limit is reached, a flag is set and every
|
|
|
|
* subsequent packet is dropped, until a new key is set and the stream
|
|
|
|
* has been updated.
|
|
|
|
*
|
2014-03-22 05:16:41 +00:00
|
|
|
* If a stream is to be shared between multiple clients it is also
|
|
|
|
* possible to request the internal SRTP rollover counter for a given
|
|
|
|
* SSRC. The rollover counter should be then transmitted and used by the
|
|
|
|
* clients to authenticate and decrypt the packets. Failing to do that
|
|
|
|
* the clients will start with a rollover counter of 0 which will
|
|
|
|
* probably be incorrect if the stream has been transmitted for a
|
|
|
|
* while to other clients.
|
2018-10-17 22:30:10 +00:00
|
|
|
*
|
|
|
|
* This element supports sending with a single Master Key, it is possible to set the
|
|
|
|
* Master Key Identifier (MKI) using the "mki" property. If this property is set, the MKI
|
|
|
|
* will be added to every buffer.
|
2013-03-12 23:47:51 +00:00
|
|
|
*/
|
|
|
|
|
2018-01-26 15:25:33 +00:00
|
|
|
#include "gstsrtpenc.h"
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
#include <gst/rtp/gstrtpbuffer.h>
|
|
|
|
#include <gst/rtp/gstrtcpbuffer.h>
|
|
|
|
#include <string.h>
|
2018-01-26 15:25:33 +00:00
|
|
|
#include <stdio.h>
|
2014-03-22 05:16:41 +00:00
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_CATEGORY_STATIC (gst_srtp_enc_debug);
|
|
|
|
#define GST_CAT_DEFAULT gst_srtp_enc_debug
|
|
|
|
|
2013-12-13 22:16:49 +00:00
|
|
|
/* 128 bit key size: 14 (salt) + 16 */
|
|
|
|
#define MASTER_128_KEY_SIZE 30
|
|
|
|
|
|
|
|
/* 256 bit key size: 14 (salt) + 16 + 16 */
|
|
|
|
#define MASTER_256_KEY_SIZE 46
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
/* Properties default values */
|
|
|
|
#define DEFAULT_MASTER_KEY NULL
|
|
|
|
#define DEFAULT_RTP_CIPHER GST_SRTP_CIPHER_AES_128_ICM
|
|
|
|
#define DEFAULT_RTP_AUTH GST_SRTP_AUTH_HMAC_SHA1_80
|
|
|
|
#define DEFAULT_RTCP_CIPHER DEFAULT_RTP_CIPHER
|
|
|
|
#define DEFAULT_RTCP_AUTH DEFAULT_RTP_AUTH
|
|
|
|
#define DEFAULT_RANDOM_KEY FALSE
|
2014-05-02 21:27:56 +00:00
|
|
|
#define DEFAULT_REPLAY_WINDOW_SIZE 128
|
2014-06-13 09:15:25 +00:00
|
|
|
#define DEFAULT_ALLOW_REPEAT_TX FALSE
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2013-06-29 00:15:08 +00:00
|
|
|
#define HAS_CRYPTO(filter) (filter->rtp_cipher != GST_SRTP_CIPHER_NULL || \
|
|
|
|
filter->rtcp_cipher != GST_SRTP_CIPHER_NULL || \
|
|
|
|
filter->rtp_auth != GST_SRTP_AUTH_NULL || \
|
|
|
|
filter->rtcp_auth != GST_SRTP_AUTH_NULL)
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
/* Filter signals and args */
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
SIGNAL_SOFT_LIMIT,
|
2014-03-22 05:16:41 +00:00
|
|
|
SIGNAL_GET_ROLLOVER_COUNTER,
|
2013-03-12 23:47:51 +00:00
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_MKEY,
|
|
|
|
PROP_RTP_CIPHER,
|
|
|
|
PROP_RTP_AUTH,
|
|
|
|
PROP_RTCP_CIPHER,
|
|
|
|
PROP_RTCP_AUTH,
|
2014-05-02 21:27:56 +00:00
|
|
|
PROP_RANDOM_KEY,
|
2014-06-13 09:15:25 +00:00
|
|
|
PROP_REPLAY_WINDOW_SIZE,
|
2016-05-30 12:10:23 +00:00
|
|
|
PROP_ALLOW_REPEAT_TX,
|
2018-10-17 22:30:10 +00:00
|
|
|
PROP_STATS,
|
|
|
|
PROP_MKI
|
2013-03-12 23:47:51 +00:00
|
|
|
};
|
|
|
|
|
2015-03-19 15:17:58 +00:00
|
|
|
typedef struct ProcessBufferItData
|
|
|
|
{
|
|
|
|
GstSrtpEnc *filter;
|
|
|
|
GstPad *pad;
|
|
|
|
GstBufferList *out_list;
|
2018-05-04 15:59:53 +00:00
|
|
|
GstFlowReturn flowret;
|
2015-03-19 15:17:58 +00:00
|
|
|
gboolean is_rtcp;
|
|
|
|
} ProcessBufferItData;
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
/* the capabilities of the inputs and outputs.
|
|
|
|
*
|
|
|
|
* describe the real formats here.
|
|
|
|
*/
|
|
|
|
static GstStaticPadTemplate rtp_sink_template =
|
2014-05-06 02:25:08 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("rtp_sink_%u",
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_REQUEST,
|
|
|
|
GST_STATIC_CAPS ("application/x-rtp")
|
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate rtp_src_template =
|
2014-05-06 02:25:08 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("rtp_src_%u",
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_SOMETIMES,
|
|
|
|
GST_STATIC_CAPS ("application/x-srtp")
|
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate rtcp_sink_template =
|
2014-05-06 02:25:08 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("rtcp_sink_%u",
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_REQUEST,
|
|
|
|
GST_STATIC_CAPS ("application/x-rtcp")
|
|
|
|
);
|
|
|
|
|
|
|
|
static GstStaticPadTemplate rtcp_src_template =
|
2014-05-06 02:25:08 +00:00
|
|
|
GST_STATIC_PAD_TEMPLATE ("rtcp_src_%u",
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_SOMETIMES,
|
|
|
|
GST_STATIC_CAPS ("application/x-srtcp")
|
|
|
|
);
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GstSrtpEnc, gst_srtp_enc, GST_TYPE_ELEMENT);
|
|
|
|
|
|
|
|
static guint gst_srtp_enc_signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
|
|
|
static void gst_srtp_enc_dispose (GObject * object);
|
|
|
|
|
|
|
|
static void gst_srtp_enc_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void gst_srtp_enc_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
|
|
|
static gboolean gst_srtp_enc_sink_query_rtp (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query);
|
|
|
|
static gboolean gst_srtp_enc_sink_query_rtcp (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query);
|
|
|
|
|
|
|
|
static GstIterator *gst_srtp_enc_iterate_internal_links_rtp (GstPad * pad,
|
|
|
|
GstObject * parent);
|
|
|
|
static GstIterator *gst_srtp_enc_iterate_internal_links_rtcp (GstPad * pad,
|
|
|
|
GstObject * parent);
|
|
|
|
|
|
|
|
static GstFlowReturn gst_srtp_enc_chain_rtp (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buf);
|
|
|
|
static GstFlowReturn gst_srtp_enc_chain_rtcp (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buf);
|
2015-03-18 09:16:41 +00:00
|
|
|
static GstFlowReturn gst_srtp_enc_chain_list_rtp (GstPad * pad,
|
|
|
|
GstObject * parent, GstBufferList * buf);
|
|
|
|
static GstFlowReturn gst_srtp_enc_chain_list_rtcp (GstPad * pad,
|
|
|
|
GstObject * parent, GstBufferList * buf);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
static gboolean gst_srtp_enc_sink_event_rtp (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
|
|
|
static gboolean gst_srtp_enc_sink_event_rtcp (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
|
|
|
|
|
|
|
static GstStateChangeReturn gst_srtp_enc_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
|
|
|
|
|
|
|
static GstPad *gst_srtp_enc_request_new_pad (GstElement * element,
|
|
|
|
GstPadTemplate * templ, const gchar * name, const GstCaps * caps);
|
|
|
|
|
|
|
|
static void gst_srtp_enc_release_pad (GstElement * element, GstPad * pad);
|
|
|
|
|
2014-03-22 05:16:41 +00:00
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
/* initialize the srtpenc's class
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
gst_srtp_enc_class_init (GstSrtpEncClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
|
2016-03-04 06:50:26 +00:00
|
|
|
gst_element_class_add_static_pad_template (gstelement_class,
|
|
|
|
&rtp_src_template);
|
|
|
|
gst_element_class_add_static_pad_template (gstelement_class,
|
|
|
|
&rtp_sink_template);
|
|
|
|
gst_element_class_add_static_pad_template (gstelement_class,
|
|
|
|
&rtcp_src_template);
|
|
|
|
gst_element_class_add_static_pad_template (gstelement_class,
|
|
|
|
&rtcp_sink_template);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
gst_element_class_set_static_metadata (gstelement_class, "SRTP encoder",
|
|
|
|
"Filter/Network/SRTP",
|
|
|
|
"A SRTP and SRTCP encoder",
|
|
|
|
"Gabriel Millaire <millaire.gabriel@collabora.com>");
|
|
|
|
|
|
|
|
|
|
|
|
/* Install callbacks */
|
|
|
|
gobject_class->set_property = gst_srtp_enc_set_property;
|
|
|
|
gobject_class->get_property = gst_srtp_enc_get_property;
|
|
|
|
gobject_class->dispose = gst_srtp_enc_dispose;
|
|
|
|
gstelement_class->request_new_pad =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_request_new_pad);
|
|
|
|
gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_srtp_enc_release_pad);
|
|
|
|
gstelement_class->change_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_change_state);
|
|
|
|
|
|
|
|
/* Install properties */
|
|
|
|
g_object_class_install_property (gobject_class, PROP_MKEY,
|
2013-12-13 22:16:49 +00:00
|
|
|
g_param_spec_boxed ("key", "Key", "Master key (minimum of "
|
|
|
|
G_STRINGIFY (MASTER_128_KEY_SIZE) " and maximum of "
|
|
|
|
G_STRINGIFY (MASTER_256_KEY_SIZE) " bytes)",
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_TYPE_BUFFER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
|
|
|
g_object_class_install_property (gobject_class, PROP_RTP_CIPHER,
|
|
|
|
g_param_spec_enum ("rtp-cipher", "RTP Cipher", "RTP Cipher",
|
|
|
|
GST_TYPE_SRTP_CIPHER_TYPE, DEFAULT_RTP_CIPHER,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
g_object_class_install_property (gobject_class, PROP_RTP_AUTH,
|
|
|
|
g_param_spec_enum ("rtp-auth", "RTP Authentication",
|
|
|
|
"RTP Authentication", GST_TYPE_SRTP_AUTH_TYPE, DEFAULT_RTP_AUTH,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
g_object_class_install_property (gobject_class, PROP_RTCP_CIPHER,
|
|
|
|
g_param_spec_enum ("rtcp-cipher", "RTCP Cipher",
|
|
|
|
"RTCP Cipher", GST_TYPE_SRTP_CIPHER_TYPE, DEFAULT_RTCP_CIPHER,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
g_object_class_install_property (gobject_class, PROP_RTCP_AUTH,
|
|
|
|
g_param_spec_enum ("rtcp-auth", "RTCP Authentication",
|
|
|
|
"RTCP Authentication", GST_TYPE_SRTP_AUTH_TYPE, DEFAULT_RTCP_AUTH,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
g_object_class_install_property (gobject_class, PROP_RANDOM_KEY,
|
|
|
|
g_param_spec_boolean ("random-key", "Generate random key",
|
|
|
|
"Generate a random key if TRUE",
|
|
|
|
DEFAULT_RANDOM_KEY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2014-05-02 21:27:56 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_REPLAY_WINDOW_SIZE,
|
|
|
|
g_param_spec_uint ("replay-window-size", "Replay window size",
|
|
|
|
"Size of the replay protection window",
|
|
|
|
64, 0x8000, DEFAULT_REPLAY_WINDOW_SIZE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2014-06-13 09:15:25 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_ALLOW_REPEAT_TX,
|
2014-06-18 13:36:09 +00:00
|
|
|
g_param_spec_boolean ("allow-repeat-tx",
|
|
|
|
"Allow repeat packets transmission",
|
|
|
|
"Whether retransmissions of packets with the same sequence number are allowed"
|
|
|
|
"(Note that such repeated transmissions must have the same RTP payload, "
|
|
|
|
"or a severe security weakness is introduced!)",
|
2014-06-13 09:15:25 +00:00
|
|
|
DEFAULT_ALLOW_REPEAT_TX, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2016-05-30 12:10:23 +00:00
|
|
|
g_object_class_install_property (gobject_class, PROP_STATS,
|
|
|
|
g_param_spec_boxed ("stats", "Statistics", "Various statistics",
|
|
|
|
GST_TYPE_STRUCTURE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
|
2018-10-17 22:30:10 +00:00
|
|
|
#ifdef HAVE_SRTP2
|
|
|
|
g_object_class_install_property (gobject_class, PROP_MKI,
|
|
|
|
g_param_spec_boxed ("mki", "MKI",
|
|
|
|
"Master key Identifier (NULL means no MKI)", GST_TYPE_BUFFER,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
|
|
|
|
GST_PARAM_MUTABLE_PLAYING));
|
|
|
|
#endif
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GstSrtpEnc::soft-limit:
|
|
|
|
* @gstsrtpenc: the element on which the signal is emitted
|
|
|
|
*
|
2019-09-02 19:08:44 +00:00
|
|
|
* Signal emitted when the stream with @ssrc has reached the soft
|
2013-03-12 23:47:51 +00:00
|
|
|
* limit of utilisation of it's master encryption key. User should
|
|
|
|
* provide a new key by setting the #GstSrtpEnc:key property.
|
|
|
|
*/
|
|
|
|
gst_srtp_enc_signals[SIGNAL_SOFT_LIMIT] =
|
|
|
|
g_signal_new ("soft-limit", G_TYPE_FROM_CLASS (klass),
|
2014-05-02 21:13:54 +00:00
|
|
|
G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* initialize the new element
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
gst_srtp_enc_init (GstSrtpEnc * filter)
|
|
|
|
{
|
|
|
|
filter->key_changed = TRUE;
|
|
|
|
filter->first_session = TRUE;
|
|
|
|
filter->key = DEFAULT_MASTER_KEY;
|
|
|
|
filter->rtp_cipher = DEFAULT_RTP_CIPHER;
|
|
|
|
filter->rtp_auth = DEFAULT_RTP_AUTH;
|
|
|
|
filter->rtcp_cipher = DEFAULT_RTCP_CIPHER;
|
|
|
|
filter->rtcp_auth = DEFAULT_RTCP_AUTH;
|
2014-05-02 21:27:56 +00:00
|
|
|
filter->replay_window_size = DEFAULT_REPLAY_WINDOW_SIZE;
|
2014-06-13 09:15:25 +00:00
|
|
|
filter->allow_repeat_tx = DEFAULT_ALLOW_REPEAT_TX;
|
2018-01-26 15:25:33 +00:00
|
|
|
filter->ssrcs_set = g_hash_table_new (g_direct_hash, g_direct_equal);
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
2013-12-13 22:16:49 +00:00
|
|
|
static guint
|
|
|
|
max_cipher_key_size (GstSrtpEnc * filter)
|
|
|
|
{
|
|
|
|
guint rtp_size, rtcp_size;
|
|
|
|
|
|
|
|
rtp_size = cipher_key_size (filter->rtp_cipher);
|
|
|
|
rtcp_size = cipher_key_size (filter->rtcp_cipher);
|
|
|
|
|
|
|
|
return (rtp_size > rtcp_size) ? rtp_size : rtcp_size;
|
|
|
|
}
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
/* Create stream
|
2015-03-18 08:39:49 +00:00
|
|
|
*
|
|
|
|
* Should be called with the filter locked
|
2013-03-12 23:47:51 +00:00
|
|
|
*/
|
2018-01-26 15:25:33 +00:00
|
|
|
static srtp_err_status_t
|
2014-05-02 21:07:21 +00:00
|
|
|
gst_srtp_enc_create_session (GstSrtpEnc * filter)
|
2013-03-12 23:47:51 +00:00
|
|
|
{
|
2018-01-26 15:25:33 +00:00
|
|
|
srtp_err_status_t ret;
|
2013-03-12 23:47:51 +00:00
|
|
|
srtp_policy_t policy;
|
|
|
|
GstMapInfo map;
|
2013-06-29 00:15:08 +00:00
|
|
|
guchar tmp[1];
|
2018-10-17 22:30:10 +00:00
|
|
|
#ifdef HAVE_SRTP2
|
|
|
|
srtp_master_key_t mkey;
|
|
|
|
srtp_master_key_t *mkey_ptr = &mkey;
|
|
|
|
gboolean has_mki = FALSE;
|
|
|
|
GstMapInfo mki_map;
|
|
|
|
#endif
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
memset (&policy, 0, sizeof (srtp_policy_t));
|
|
|
|
|
2013-06-29 00:15:08 +00:00
|
|
|
if (HAS_CRYPTO (filter)) {
|
2014-03-25 16:39:18 +00:00
|
|
|
guint expected;
|
|
|
|
gsize keysize;
|
|
|
|
|
2013-06-29 00:15:08 +00:00
|
|
|
if (filter->key == NULL) {
|
2016-09-06 09:02:06 +00:00
|
|
|
GST_OBJECT_UNLOCK (filter);
|
2013-06-29 00:15:08 +00:00
|
|
|
GST_ELEMENT_ERROR (filter, LIBRARY, SETTINGS,
|
|
|
|
("Cipher is not NULL, key must be set"),
|
|
|
|
("Cipher is not NULL, key must be set"));
|
2016-09-06 09:02:06 +00:00
|
|
|
GST_OBJECT_LOCK (filter);
|
2018-01-26 15:25:33 +00:00
|
|
|
return srtp_err_status_fail;
|
2013-06-29 00:15:08 +00:00
|
|
|
}
|
2014-03-25 16:39:18 +00:00
|
|
|
|
|
|
|
expected = max_cipher_key_size (filter);
|
|
|
|
keysize = gst_buffer_get_size (filter->key);
|
|
|
|
|
|
|
|
if (expected != keysize) {
|
2016-09-06 09:02:06 +00:00
|
|
|
GST_OBJECT_UNLOCK (filter);
|
2014-03-25 16:39:18 +00:00
|
|
|
GST_ELEMENT_ERROR (filter, LIBRARY, SETTINGS,
|
|
|
|
("Master key size is wrong"),
|
|
|
|
("Expected master key of %d bytes, but received %" G_GSIZE_FORMAT
|
|
|
|
" bytes", expected, keysize));
|
2016-09-06 09:02:06 +00:00
|
|
|
GST_OBJECT_LOCK (filter);
|
2018-01-26 15:25:33 +00:00
|
|
|
return srtp_err_status_fail;
|
2013-06-29 00:15:08 +00:00
|
|
|
}
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (filter, "Setting RTP/RTCP policy to %d / %d",
|
|
|
|
filter->rtp_cipher, filter->rtcp_cipher);
|
|
|
|
set_crypto_policy_cipher_auth (filter->rtp_cipher, filter->rtp_auth,
|
|
|
|
&policy.rtp);
|
|
|
|
set_crypto_policy_cipher_auth (filter->rtcp_cipher, filter->rtcp_auth,
|
|
|
|
&policy.rtcp);
|
|
|
|
|
2013-06-29 00:15:08 +00:00
|
|
|
if (HAS_CRYPTO (filter)) {
|
|
|
|
gst_buffer_map (filter->key, &map, GST_MAP_READ);
|
|
|
|
policy.key = (guchar *) map.data;
|
|
|
|
} else {
|
|
|
|
policy.key = tmp;
|
|
|
|
}
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2018-10-17 22:30:10 +00:00
|
|
|
#ifdef HAVE_SRTP2
|
|
|
|
if (filter->mki) {
|
|
|
|
if (!gst_buffer_map (filter->mki, &mki_map, GST_MAP_READ)) {
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
GST_ELEMENT_ERROR (filter, LIBRARY, SETTINGS, ("Could not map MKI"),
|
|
|
|
(NULL));
|
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
|
|
|
|
ret = srtp_err_status_fail;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
has_mki = TRUE;
|
|
|
|
|
|
|
|
policy.num_master_keys = 1;
|
|
|
|
policy.keys = &mkey_ptr;
|
|
|
|
mkey.key = policy.key;
|
|
|
|
policy.key = NULL;
|
|
|
|
|
|
|
|
mkey.mki_id = (guchar *) mki_map.data;
|
|
|
|
mkey.mki_size = mki_map.size;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-05-02 21:07:21 +00:00
|
|
|
policy.ssrc.value = 0;
|
|
|
|
policy.ssrc.type = ssrc_any_outbound;
|
2013-03-12 23:47:51 +00:00
|
|
|
policy.next = NULL;
|
|
|
|
|
2014-05-02 21:27:56 +00:00
|
|
|
policy.window_size = filter->replay_window_size;
|
2014-06-13 09:15:25 +00:00
|
|
|
policy.allow_repeat_tx = filter->allow_repeat_tx;
|
2014-05-02 21:27:56 +00:00
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
/* If it is the first stream, create the session
|
|
|
|
* If not, add the stream to the session
|
|
|
|
*/
|
2014-05-02 21:07:21 +00:00
|
|
|
ret = srtp_create (&filter->session, &policy);
|
2013-11-20 01:18:48 +00:00
|
|
|
filter->first_session = FALSE;
|
|
|
|
|
2018-10-17 22:30:10 +00:00
|
|
|
#ifdef HAVE_SRTP2
|
|
|
|
done:
|
|
|
|
|
|
|
|
if (has_mki)
|
|
|
|
gst_buffer_unmap (filter->mki, &mki_map);
|
|
|
|
#endif
|
|
|
|
|
2013-06-29 00:15:08 +00:00
|
|
|
if (HAS_CRYPTO (filter))
|
|
|
|
gst_buffer_unmap (filter->key, &map);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2018-10-17 22:30:10 +00:00
|
|
|
|
2014-05-02 21:07:21 +00:00
|
|
|
return ret;
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 19:08:44 +00:00
|
|
|
/* Release resources and set default values
|
2013-03-12 23:47:51 +00:00
|
|
|
*/
|
|
|
|
static void
|
2015-03-18 08:39:49 +00:00
|
|
|
gst_srtp_enc_reset_no_lock (GstSrtpEnc * filter)
|
2013-03-12 23:47:51 +00:00
|
|
|
{
|
2017-08-24 08:00:41 +00:00
|
|
|
if (!filter->first_session) {
|
2019-03-06 06:36:44 +00:00
|
|
|
if (filter->session) {
|
|
|
|
srtp_dealloc (filter->session);
|
|
|
|
filter->session = NULL;
|
|
|
|
}
|
2018-01-26 15:25:33 +00:00
|
|
|
|
|
|
|
g_hash_table_remove_all (filter->ssrcs_set);
|
2017-08-24 08:00:41 +00:00
|
|
|
}
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
filter->first_session = TRUE;
|
|
|
|
filter->key_changed = FALSE;
|
2015-03-18 08:39:49 +00:00
|
|
|
}
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2015-03-18 08:39:49 +00:00
|
|
|
static void
|
|
|
|
gst_srtp_enc_reset (GstSrtpEnc * filter)
|
|
|
|
{
|
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
gst_srtp_enc_reset_no_lock (filter);
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create sinkpad to receive RTP packets from encers
|
|
|
|
* and a srcpad for the RTP packets
|
|
|
|
*/
|
|
|
|
static GstPad *
|
|
|
|
create_rtp_sink (GstSrtpEnc * filter, const gchar * name)
|
|
|
|
{
|
2014-05-02 21:19:21 +00:00
|
|
|
GstPad *sinkpad, *srcpad;
|
2013-03-12 23:47:51 +00:00
|
|
|
gchar *sinkpadname, *srcpadname;
|
2014-05-06 02:25:08 +00:00
|
|
|
guint nb = 0;
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (filter, "creating RTP sink pad");
|
2014-05-02 21:19:21 +00:00
|
|
|
sinkpad = gst_pad_new_from_static_template (&rtp_sink_template, name);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
sinkpadname = gst_pad_get_name (sinkpad);
|
2014-05-06 02:25:08 +00:00
|
|
|
sscanf (sinkpadname, "rtp_sink_%u", &nb);
|
|
|
|
srcpadname = g_strdup_printf ("rtp_src_%u", nb);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (filter, "creating RTP source pad");
|
2014-05-02 21:19:21 +00:00
|
|
|
srcpad = gst_pad_new_from_static_template (&rtp_src_template, srcpadname);
|
2013-03-12 23:47:51 +00:00
|
|
|
g_free (srcpadname);
|
|
|
|
g_free (sinkpadname);
|
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_element_private (sinkpad, srcpad);
|
|
|
|
gst_pad_set_element_private (srcpad, sinkpad);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_query_function (sinkpad,
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_sink_query_rtp));
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_iterate_internal_links_function (sinkpad,
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_iterate_internal_links_rtp));
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_chain_function (sinkpad,
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_chain_rtp));
|
2015-03-18 09:16:41 +00:00
|
|
|
gst_pad_set_chain_list_function (sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_chain_list_rtp));
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_event_function (sinkpad,
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_sink_event_rtp));
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_active (sinkpad, TRUE);
|
|
|
|
gst_element_add_pad (GST_ELEMENT_CAST (filter), sinkpad);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_iterate_internal_links_function (srcpad,
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_iterate_internal_links_rtp));
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_active (srcpad, TRUE);
|
|
|
|
gst_element_add_pad (GST_ELEMENT_CAST (filter), srcpad);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
return sinkpad;
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create sinkpad to receive RTCP packets from encers
|
|
|
|
* and a srcpad for the RTCP packets
|
|
|
|
*/
|
|
|
|
static GstPad *
|
|
|
|
create_rtcp_sink (GstSrtpEnc * filter, const gchar * name)
|
|
|
|
{
|
2014-05-02 21:19:21 +00:00
|
|
|
GstPad *srcpad, *sinkpad;
|
2013-03-12 23:47:51 +00:00
|
|
|
gchar *sinkpadname, *srcpadname;
|
2014-05-06 02:25:08 +00:00
|
|
|
guint nb = 0;
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (filter, "creating RTCP sink pad");
|
2014-05-02 21:19:21 +00:00
|
|
|
sinkpad = gst_pad_new_from_static_template (&rtcp_sink_template, name);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
sinkpadname = gst_pad_get_name (sinkpad);
|
2014-05-06 02:25:08 +00:00
|
|
|
sscanf (sinkpadname, "rtcp_sink_%u", &nb);
|
|
|
|
srcpadname = g_strdup_printf ("rtcp_src_%u", nb);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (filter, "creating RTCP source pad");
|
2014-05-02 21:19:21 +00:00
|
|
|
srcpad = gst_pad_new_from_static_template (&rtcp_src_template, srcpadname);
|
2013-03-12 23:47:51 +00:00
|
|
|
g_free (srcpadname);
|
|
|
|
g_free (sinkpadname);
|
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_element_private (sinkpad, srcpad);
|
|
|
|
gst_pad_set_element_private (srcpad, sinkpad);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_query_function (sinkpad,
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_sink_query_rtcp));
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_iterate_internal_links_function (sinkpad,
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_iterate_internal_links_rtcp));
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_chain_function (sinkpad,
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_chain_rtcp));
|
2015-03-18 09:16:41 +00:00
|
|
|
gst_pad_set_chain_list_function (sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_chain_list_rtcp));
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_event_function (sinkpad,
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_sink_event_rtcp));
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_active (sinkpad, TRUE);
|
|
|
|
gst_element_add_pad (GST_ELEMENT_CAST (filter), sinkpad);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_iterate_internal_links_function (srcpad,
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_DEBUG_FUNCPTR (gst_srtp_enc_iterate_internal_links_rtcp));
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_active (srcpad, TRUE);
|
|
|
|
gst_element_add_pad (GST_ELEMENT_CAST (filter), srcpad);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
return sinkpad;
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handling new pad request
|
|
|
|
*/
|
|
|
|
static GstPad *
|
|
|
|
gst_srtp_enc_request_new_pad (GstElement * element,
|
|
|
|
GstPadTemplate * templ, const gchar * name, const GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstElementClass *klass;
|
|
|
|
GstSrtpEnc *filter;
|
|
|
|
|
|
|
|
filter = GST_SRTP_ENC (element);
|
|
|
|
klass = GST_ELEMENT_GET_CLASS (element);
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (element, "New pad requested");
|
|
|
|
|
2014-05-06 02:25:08 +00:00
|
|
|
if (templ == gst_element_class_get_pad_template (klass, "rtp_sink_%u"))
|
2013-03-12 23:47:51 +00:00
|
|
|
return create_rtp_sink (filter, name);
|
|
|
|
|
2014-05-06 02:25:08 +00:00
|
|
|
if (templ == gst_element_class_get_pad_template (klass, "rtcp_sink_%u"))
|
2013-03-12 23:47:51 +00:00
|
|
|
return create_rtcp_sink (filter, name);
|
|
|
|
|
|
|
|
GST_ERROR_OBJECT (element, "Could not find specified template");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dispose
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
gst_srtp_enc_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
GstSrtpEnc *filter = GST_SRTP_ENC (object);
|
|
|
|
GstIterator *it;
|
|
|
|
GValue val = { 0 };
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (object, "Dispose...");
|
|
|
|
|
|
|
|
it = gst_element_iterate_sink_pads (GST_ELEMENT_CAST (object));
|
|
|
|
while (gst_iterator_next (it, &val) == GST_ITERATOR_OK) {
|
|
|
|
gst_srtp_enc_release_pad (GST_ELEMENT_CAST (object),
|
|
|
|
g_value_get_object (&val));
|
|
|
|
g_value_unset (&val);
|
|
|
|
gst_iterator_resync (it);
|
|
|
|
}
|
|
|
|
gst_iterator_free (it);
|
|
|
|
|
2018-10-17 22:30:10 +00:00
|
|
|
gst_buffer_replace (&filter->key, NULL);
|
|
|
|
gst_buffer_replace (&filter->mki, NULL);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2018-01-26 15:25:33 +00:00
|
|
|
if (filter->ssrcs_set)
|
|
|
|
g_hash_table_unref (filter->ssrcs_set);
|
|
|
|
filter->ssrcs_set = NULL;
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
G_OBJECT_CLASS (gst_srtp_enc_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
2016-05-30 12:10:23 +00:00
|
|
|
static GstStructure *
|
|
|
|
gst_srtp_enc_create_stats (GstSrtpEnc * filter)
|
|
|
|
{
|
|
|
|
GstStructure *s;
|
|
|
|
GValue va = G_VALUE_INIT;
|
|
|
|
GValue v = G_VALUE_INIT;
|
|
|
|
|
|
|
|
s = gst_structure_new_empty ("application/x-srtp-encoder-stats");
|
|
|
|
|
|
|
|
g_value_init (&va, GST_TYPE_ARRAY);
|
|
|
|
g_value_init (&v, GST_TYPE_STRUCTURE);
|
|
|
|
|
|
|
|
if (filter->session) {
|
2018-01-26 15:25:33 +00:00
|
|
|
GHashTableIter iter;
|
|
|
|
gpointer key;
|
|
|
|
|
|
|
|
g_hash_table_iter_init (&iter, filter->ssrcs_set);
|
|
|
|
while (g_hash_table_iter_next (&iter, &key, NULL)) {
|
2016-05-30 12:10:23 +00:00
|
|
|
GstStructure *ss;
|
2018-01-26 15:25:33 +00:00
|
|
|
guint32 ssrc = GPOINTER_TO_UINT (key);
|
|
|
|
srtp_err_status_t status;
|
|
|
|
guint32 roc;
|
|
|
|
|
|
|
|
status = srtp_get_stream_roc (filter->session, ssrc, &roc);
|
|
|
|
if (status != srtp_err_status_ok) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-30 12:10:23 +00:00
|
|
|
|
|
|
|
ss = gst_structure_new ("application/x-srtp-stream",
|
|
|
|
"ssrc", G_TYPE_UINT, ssrc, "roc", G_TYPE_UINT, roc, NULL);
|
|
|
|
|
|
|
|
g_value_take_boxed (&v, ss);
|
|
|
|
gst_value_array_append_value (&va, &v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_structure_take_value (s, "streams", &va);
|
|
|
|
g_value_unset (&v);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
static void
|
|
|
|
gst_srtp_enc_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstSrtpEnc *filter = GST_SRTP_ENC (object);
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_MKEY:
|
2018-10-17 22:30:10 +00:00
|
|
|
gst_clear_buffer (&filter->key);
|
2013-03-12 23:47:51 +00:00
|
|
|
filter->key = g_value_dup_boxed (value);
|
|
|
|
filter->key_changed = TRUE;
|
|
|
|
GST_INFO_OBJECT (object, "Set property: key=[%p]", filter->key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_RTP_CIPHER:
|
|
|
|
filter->rtp_cipher = g_value_get_enum (value);
|
|
|
|
GST_INFO_OBJECT (object, "Set property: rtp cipher=%d",
|
|
|
|
filter->rtp_cipher);
|
|
|
|
break;
|
|
|
|
case PROP_RTP_AUTH:
|
|
|
|
filter->rtp_auth = g_value_get_enum (value);
|
|
|
|
GST_INFO_OBJECT (object, "Set property: rtp auth=%d", filter->rtp_auth);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_RTCP_CIPHER:
|
|
|
|
filter->rtcp_cipher = g_value_get_enum (value);
|
|
|
|
GST_INFO_OBJECT (object, "Set property: rtcp cipher=%d",
|
|
|
|
filter->rtcp_cipher);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_RTCP_AUTH:
|
|
|
|
filter->rtcp_auth = g_value_get_enum (value);
|
|
|
|
GST_INFO_OBJECT (object, "Set property: rtcp auth=%d", filter->rtcp_auth);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_RANDOM_KEY:
|
|
|
|
filter->random_key = g_value_get_boolean (value);
|
|
|
|
break;
|
|
|
|
|
2014-05-02 21:27:56 +00:00
|
|
|
case PROP_REPLAY_WINDOW_SIZE:
|
|
|
|
filter->replay_window_size = g_value_get_uint (value);
|
|
|
|
break;
|
|
|
|
|
2014-06-13 09:15:25 +00:00
|
|
|
case PROP_ALLOW_REPEAT_TX:
|
|
|
|
filter->allow_repeat_tx = g_value_get_boolean (value);
|
|
|
|
break;
|
2018-10-17 22:30:10 +00:00
|
|
|
#ifdef HAVE_SRTP2
|
|
|
|
case PROP_MKI:
|
|
|
|
gst_clear_buffer (&filter->mki);
|
|
|
|
filter->mki = g_value_dup_boxed (value);
|
|
|
|
filter->key_changed = TRUE;
|
|
|
|
GST_INFO_OBJECT (object, "Set property: mki=[%p]", filter->mki);
|
|
|
|
break;
|
|
|
|
#endif
|
2013-03-12 23:47:51 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_srtp_enc_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstSrtpEnc *filter = GST_SRTP_ENC (object);
|
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_MKEY:
|
|
|
|
if (filter->key)
|
|
|
|
g_value_set_boxed (value, filter->key);
|
|
|
|
break;
|
|
|
|
case PROP_RTP_CIPHER:
|
|
|
|
g_value_set_enum (value, filter->rtp_cipher);
|
|
|
|
break;
|
|
|
|
case PROP_RTCP_CIPHER:
|
|
|
|
g_value_set_enum (value, filter->rtcp_cipher);
|
|
|
|
break;
|
|
|
|
case PROP_RTP_AUTH:
|
|
|
|
g_value_set_enum (value, filter->rtp_auth);
|
|
|
|
break;
|
|
|
|
case PROP_RTCP_AUTH:
|
|
|
|
g_value_set_enum (value, filter->rtcp_auth);
|
|
|
|
break;
|
|
|
|
case PROP_RANDOM_KEY:
|
|
|
|
g_value_set_boolean (value, filter->random_key);
|
|
|
|
break;
|
2014-05-02 21:27:56 +00:00
|
|
|
case PROP_REPLAY_WINDOW_SIZE:
|
|
|
|
g_value_set_uint (value, filter->replay_window_size);
|
|
|
|
break;
|
2014-06-13 09:15:25 +00:00
|
|
|
case PROP_ALLOW_REPEAT_TX:
|
|
|
|
g_value_set_boolean (value, filter->allow_repeat_tx);
|
|
|
|
break;
|
2016-05-30 12:10:23 +00:00
|
|
|
case PROP_STATS:
|
|
|
|
g_value_take_boxed (value, gst_srtp_enc_create_stats (filter));
|
|
|
|
break;
|
2018-10-17 22:30:10 +00:00
|
|
|
#ifdef HAVE_SRTP2
|
|
|
|
case PROP_MKI:
|
|
|
|
if (filter->mki)
|
|
|
|
g_value_set_boxed (value, filter->mki);
|
|
|
|
break;
|
|
|
|
#endif
|
2013-03-12 23:47:51 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns the source pad linked with the sink pad
|
|
|
|
*/
|
|
|
|
static GstPad *
|
|
|
|
get_rtp_other_pad (GstPad * pad)
|
|
|
|
{
|
2014-05-02 21:19:21 +00:00
|
|
|
return GST_PAD (gst_pad_get_element_private (pad));
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
2019-10-24 23:42:23 +00:00
|
|
|
static void
|
|
|
|
gst_srtp_enc_add_ssrc (GstSrtpEnc * filter, guint ssrc)
|
|
|
|
{
|
|
|
|
gboolean is_added =
|
|
|
|
g_hash_table_add (filter->ssrcs_set, GUINT_TO_POINTER (ssrc));
|
|
|
|
if (is_added) {
|
|
|
|
GST_DEBUG_OBJECT (filter, "Added ssrc %u", ssrc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
/* Release a sink pad and it's linked source pad
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
gst_srtp_enc_release_pad (GstElement * element, GstPad * sinkpad)
|
|
|
|
{
|
2014-05-02 21:19:21 +00:00
|
|
|
GstPad *srcpad;
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
GST_INFO_OBJECT (element, "Releasing pad %s:%s",
|
|
|
|
GST_DEBUG_PAD_NAME (sinkpad));
|
|
|
|
|
2014-05-02 21:19:21 +00:00
|
|
|
srcpad = GST_PAD (gst_pad_get_element_private (sinkpad));
|
2013-03-12 23:47:51 +00:00
|
|
|
gst_pad_set_element_private (sinkpad, NULL);
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_element_private (srcpad, NULL);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
/* deactivate from source to sink */
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_pad_set_active (srcpad, FALSE);
|
|
|
|
gst_pad_set_active (sinkpad, FALSE);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
/* remove pads */
|
2014-05-02 21:19:21 +00:00
|
|
|
gst_element_remove_pad (element, srcpad);
|
|
|
|
gst_element_remove_pad (element, sinkpad);
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Common setcaps function
|
|
|
|
* Handles the link with other elements
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
gst_srtp_enc_sink_setcaps (GstPad * pad, GstSrtpEnc * filter,
|
|
|
|
GstCaps * caps, gboolean is_rtcp)
|
|
|
|
{
|
|
|
|
GstPad *otherpad = NULL;
|
|
|
|
GstStructure *ps = NULL;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
|
|
|
|
|
|
|
|
caps = gst_caps_copy (caps);
|
|
|
|
|
|
|
|
ps = gst_caps_get_structure (caps, 0);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pad, "Sink caps: %" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
if (is_rtcp)
|
|
|
|
gst_structure_set_name (ps, "application/x-srtcp");
|
|
|
|
else
|
|
|
|
gst_structure_set_name (ps, "application/x-srtp");
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
|
2018-01-26 15:25:33 +00:00
|
|
|
if (gst_structure_has_field_typed (ps, "ssrc", G_TYPE_UINT)) {
|
|
|
|
guint ssrc;
|
|
|
|
gst_structure_get_uint (ps, "ssrc", &ssrc);
|
2019-10-24 23:42:23 +00:00
|
|
|
gst_srtp_enc_add_ssrc (filter, ssrc);
|
2018-01-26 15:25:33 +00:00
|
|
|
}
|
|
|
|
|
2013-06-29 00:15:08 +00:00
|
|
|
if (HAS_CRYPTO (filter))
|
|
|
|
gst_structure_set (ps, "srtp-key", GST_TYPE_BUFFER, filter->key, NULL);
|
|
|
|
|
2018-10-17 22:30:10 +00:00
|
|
|
#ifdef HAVE_SRTP2
|
|
|
|
if (filter->mki)
|
|
|
|
gst_structure_set (ps, "mki", GST_TYPE_BUFFER, filter->mki, NULL);
|
|
|
|
#endif
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
/* Add srtp-specific params to source caps */
|
2013-06-29 00:15:08 +00:00
|
|
|
gst_structure_set (ps,
|
2013-03-12 23:47:51 +00:00
|
|
|
"srtp-cipher", G_TYPE_STRING,
|
|
|
|
enum_nick_from_value (GST_TYPE_SRTP_CIPHER_TYPE, filter->rtp_cipher),
|
|
|
|
"srtp-auth", G_TYPE_STRING,
|
|
|
|
enum_nick_from_value (GST_TYPE_SRTP_AUTH_TYPE, filter->rtp_auth),
|
|
|
|
"srtcp-cipher", G_TYPE_STRING,
|
|
|
|
enum_nick_from_value (GST_TYPE_SRTP_CIPHER_TYPE, filter->rtcp_cipher),
|
|
|
|
"srtcp-auth", G_TYPE_STRING,
|
|
|
|
enum_nick_from_value (GST_TYPE_SRTP_AUTH_TYPE, filter->rtcp_auth), NULL);
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pad, "Source caps: %" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
/* Set caps on source pad */
|
2014-05-02 21:19:21 +00:00
|
|
|
otherpad = get_rtp_other_pad (pad);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
ret = gst_pad_set_caps (otherpad, caps);
|
|
|
|
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_srtp_enc_sink_query (GstPad * pad, GstObject * parent, GstQuery * query,
|
|
|
|
gboolean is_rtcp)
|
|
|
|
{
|
|
|
|
switch (GST_QUERY_TYPE (query)) {
|
|
|
|
case GST_QUERY_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps *filter = NULL;
|
|
|
|
GstCaps *other_filter = NULL;
|
|
|
|
GstPad *otherpad;
|
|
|
|
GstCaps *other_caps;
|
|
|
|
GstCaps *ret;
|
|
|
|
GstCaps *template_caps;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
otherpad = get_rtp_other_pad (pad);
|
|
|
|
|
|
|
|
gst_query_parse_caps (query, &filter);
|
|
|
|
if (filter) {
|
|
|
|
other_filter = gst_caps_copy (filter);
|
|
|
|
|
|
|
|
for (i = 0; i < gst_caps_get_size (other_filter); i++) {
|
|
|
|
GstStructure *ps = gst_caps_get_structure (other_filter, i);
|
|
|
|
if (is_rtcp)
|
|
|
|
gst_structure_set_name (ps, "application/x-srtcp");
|
|
|
|
else
|
|
|
|
gst_structure_set_name (ps, "application/x-srtp");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
other_caps = gst_pad_peer_query_caps (otherpad, other_filter);
|
|
|
|
|
|
|
|
if (other_filter)
|
|
|
|
gst_caps_unref (other_filter);
|
|
|
|
|
|
|
|
if (!other_caps)
|
|
|
|
goto return_template;
|
|
|
|
|
|
|
|
template_caps = gst_pad_get_pad_template_caps (otherpad);
|
|
|
|
ret = gst_caps_intersect_full (other_caps, template_caps,
|
|
|
|
GST_CAPS_INTERSECT_FIRST);
|
|
|
|
gst_caps_unref (other_caps);
|
|
|
|
gst_caps_unref (template_caps);
|
|
|
|
|
|
|
|
ret = gst_caps_make_writable (ret);
|
|
|
|
|
|
|
|
for (i = 0; i < gst_caps_get_size (ret); i++) {
|
|
|
|
GstStructure *ps = gst_caps_get_structure (ret, i);
|
|
|
|
if (is_rtcp)
|
|
|
|
gst_structure_set_name (ps, "application/x-rtcp");
|
|
|
|
else
|
|
|
|
gst_structure_set_name (ps, "application/x-rtp");
|
|
|
|
gst_structure_remove_fields (ps, "srtp-key", "srtp-cipher", "srtp-auth",
|
2018-10-17 22:30:10 +00:00
|
|
|
"srtcp-cipher", "srtcp-auth", "mki", NULL);
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gst_query_set_caps_result (query, ret);
|
2014-02-28 19:16:27 +00:00
|
|
|
gst_caps_unref (ret);
|
2013-03-12 23:47:51 +00:00
|
|
|
return TRUE;
|
|
|
|
return_template:
|
|
|
|
|
|
|
|
ret = gst_pad_get_pad_template_caps (pad);
|
|
|
|
gst_query_set_caps_result (query, ret);
|
|
|
|
gst_caps_unref (ret);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return gst_pad_query_default (pad, parent, query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_srtp_enc_sink_query_rtp (GstPad * pad, GstObject * parent, GstQuery * query)
|
|
|
|
{
|
|
|
|
return gst_srtp_enc_sink_query (pad, parent, query, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_srtp_enc_sink_query_rtcp (GstPad * pad, GstObject * parent,
|
|
|
|
GstQuery * query)
|
|
|
|
{
|
|
|
|
return gst_srtp_enc_sink_query (pad, parent, query, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstIterator *
|
|
|
|
gst_srtp_enc_iterate_internal_links (GstPad * pad, GstObject * parent,
|
|
|
|
gboolean is_rtcp)
|
|
|
|
{
|
|
|
|
GstSrtpEnc *filter = GST_SRTP_ENC (parent);
|
|
|
|
GstPad *otherpad = NULL;
|
|
|
|
GstIterator *it = NULL;
|
|
|
|
|
|
|
|
otherpad = get_rtp_other_pad (pad);
|
|
|
|
|
|
|
|
if (otherpad) {
|
|
|
|
GValue val = { 0 };
|
|
|
|
|
|
|
|
g_value_init (&val, GST_TYPE_PAD);
|
|
|
|
g_value_set_object (&val, otherpad);
|
|
|
|
it = gst_iterator_new_single (GST_TYPE_PAD, &val);
|
|
|
|
g_value_unset (&val);
|
|
|
|
} else {
|
|
|
|
GST_ELEMENT_ERROR (GST_ELEMENT_CAST (filter), CORE, PAD, (NULL),
|
|
|
|
("Unable to get linked pad"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstIterator *
|
|
|
|
gst_srtp_enc_iterate_internal_links_rtp (GstPad * pad, GstObject * parent)
|
|
|
|
{
|
|
|
|
return gst_srtp_enc_iterate_internal_links (pad, parent, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstIterator *
|
|
|
|
gst_srtp_enc_iterate_internal_links_rtcp (GstPad * pad, GstObject * parent)
|
|
|
|
{
|
|
|
|
return gst_srtp_enc_iterate_internal_links (pad, parent, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_srtp_enc_replace_random_key (GstSrtpEnc * filter)
|
|
|
|
{
|
|
|
|
guint i;
|
2013-12-13 22:16:49 +00:00
|
|
|
guint key_size;
|
2013-03-12 23:47:51 +00:00
|
|
|
GstMapInfo map;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (filter, "Generating random key");
|
|
|
|
|
|
|
|
if (filter->key)
|
|
|
|
gst_buffer_unref (filter->key);
|
|
|
|
|
2013-12-13 22:16:49 +00:00
|
|
|
key_size = max_cipher_key_size (filter);
|
|
|
|
|
|
|
|
filter->key = gst_buffer_new_allocate (NULL, key_size, NULL);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
gst_buffer_map (filter->key, &map, GST_MAP_WRITE);
|
|
|
|
for (i = 0; i < map.size; i += 4)
|
|
|
|
GST_WRITE_UINT32_BE (map.data + i, g_random_int ());
|
|
|
|
gst_buffer_unmap (filter->key, &map);
|
|
|
|
|
|
|
|
filter->key_changed = TRUE;
|
|
|
|
}
|
|
|
|
|
2015-03-18 08:17:31 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_srtp_enc_check_set_caps (GstSrtpEnc * filter, GstPad * pad,
|
|
|
|
gboolean is_rtcp)
|
|
|
|
{
|
|
|
|
gboolean do_setcaps = FALSE;
|
|
|
|
|
2015-03-18 08:39:49 +00:00
|
|
|
GST_OBJECT_LOCK (filter);
|
2015-03-18 08:17:31 +00:00
|
|
|
|
2015-03-18 08:39:49 +00:00
|
|
|
if (filter->key_changed) {
|
|
|
|
gst_srtp_enc_reset_no_lock (filter);
|
|
|
|
do_setcaps = TRUE;
|
2015-03-18 08:17:31 +00:00
|
|
|
}
|
|
|
|
|
2014-05-02 21:07:21 +00:00
|
|
|
if (filter->first_session) {
|
2018-01-26 15:25:33 +00:00
|
|
|
srtp_err_status_t status = gst_srtp_enc_create_session (filter);
|
2015-03-18 08:17:31 +00:00
|
|
|
|
2018-01-26 15:25:33 +00:00
|
|
|
if (status != srtp_err_status_ok) {
|
2015-03-18 08:39:49 +00:00
|
|
|
GST_OBJECT_UNLOCK (filter);
|
2014-05-02 21:07:21 +00:00
|
|
|
GST_ELEMENT_ERROR (filter, LIBRARY, INIT,
|
|
|
|
("Could not initialize SRTP encoder"),
|
|
|
|
("Failed to add stream to SRTP encoder (err: %d)", status));
|
2015-03-18 08:17:31 +00:00
|
|
|
return GST_FLOW_ERROR;
|
2014-05-02 21:07:21 +00:00
|
|
|
}
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 08:39:49 +00:00
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
/* Update source caps if asked */
|
|
|
|
if (do_setcaps) {
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
caps = gst_pad_get_current_caps (pad);
|
|
|
|
if (!gst_srtp_enc_sink_setcaps (pad, filter, caps, is_rtcp)) {
|
|
|
|
gst_caps_unref (caps);
|
2015-03-18 08:17:31 +00:00
|
|
|
return GST_FLOW_NOT_NEGOTIATED;
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
gst_caps_unref (caps);
|
|
|
|
}
|
|
|
|
|
2015-03-18 08:17:31 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
2013-08-12 16:27:33 +00:00
|
|
|
|
2019-10-24 23:42:23 +00:00
|
|
|
static void
|
|
|
|
gst_srtp_enc_ensure_ssrc (GstSrtpEnc * filter, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstRTPBuffer rtpbuf = GST_RTP_BUFFER_INIT;
|
|
|
|
if (gst_rtp_buffer_map (buf,
|
|
|
|
GST_MAP_READ | GST_RTP_BUFFER_MAP_FLAG_SKIP_PADDING, &rtpbuf)) {
|
|
|
|
guint32 ssrc = gst_rtp_buffer_get_ssrc (&rtpbuf);
|
|
|
|
gst_srtp_enc_add_ssrc (filter, ssrc);
|
|
|
|
gst_rtp_buffer_unmap (&rtpbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 15:59:53 +00:00
|
|
|
static GstFlowReturn
|
2015-03-18 08:17:31 +00:00
|
|
|
gst_srtp_enc_process_buffer (GstSrtpEnc * filter, GstPad * pad,
|
2018-05-04 15:59:53 +00:00
|
|
|
GstBuffer * buf, gboolean is_rtcp, GstBuffer ** outbuf_ptr)
|
2015-03-18 08:17:31 +00:00
|
|
|
{
|
2018-05-04 15:59:53 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2015-03-18 08:17:31 +00:00
|
|
|
gint size_max, size;
|
|
|
|
GstBuffer *bufout = NULL;
|
|
|
|
GstMapInfo mapout;
|
2018-01-26 15:25:33 +00:00
|
|
|
srtp_err_status_t err;
|
2013-08-12 16:27:33 +00:00
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
/* Create a bigger buffer to add protection */
|
2015-03-17 15:49:54 +00:00
|
|
|
size = gst_buffer_get_size (buf);
|
|
|
|
size_max = size + SRTP_MAX_TRAILER_LEN + 10;
|
2013-03-12 23:47:51 +00:00
|
|
|
bufout = gst_buffer_new_allocate (NULL, size_max, NULL);
|
|
|
|
|
|
|
|
gst_buffer_map (bufout, &mapout, GST_MAP_READWRITE);
|
|
|
|
|
2015-03-17 15:49:54 +00:00
|
|
|
gst_buffer_extract (buf, 0, mapout.data, size);
|
2013-03-12 23:47:51 +00:00
|
|
|
|
2015-03-18 08:17:31 +00:00
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
gst_srtp_init_event_reporter ();
|
|
|
|
|
2018-05-04 15:59:53 +00:00
|
|
|
if (filter->session == NULL) {
|
|
|
|
/* The rtcp session disappeared (element shutting down) */
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
ret = GST_FLOW_FLUSHING;
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-10-24 23:42:23 +00:00
|
|
|
|
|
|
|
gst_srtp_enc_ensure_ssrc (filter, buf);
|
|
|
|
|
2018-10-17 22:30:10 +00:00
|
|
|
#ifdef HAVE_SRTP2
|
|
|
|
if (is_rtcp)
|
|
|
|
err = srtp_protect_rtcp_mki (filter->session, mapout.data, &size,
|
|
|
|
(filter->mki != NULL), 0);
|
|
|
|
else
|
|
|
|
err = srtp_protect_mki (filter->session, mapout.data, &size,
|
|
|
|
(filter->mki != NULL), 0);
|
|
|
|
#else
|
2013-03-12 23:47:51 +00:00
|
|
|
if (is_rtcp)
|
|
|
|
err = srtp_protect_rtcp (filter->session, mapout.data, &size);
|
|
|
|
else
|
|
|
|
err = srtp_protect (filter->session, mapout.data, &size);
|
2018-10-17 22:30:10 +00:00
|
|
|
#endif
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
|
2015-03-18 08:17:31 +00:00
|
|
|
gst_buffer_unmap (bufout, &mapout);
|
|
|
|
|
2018-01-26 15:25:33 +00:00
|
|
|
if (err == srtp_err_status_ok) {
|
2013-03-12 23:47:51 +00:00
|
|
|
/* Buffer protected */
|
|
|
|
gst_buffer_set_size (bufout, size);
|
|
|
|
gst_buffer_copy_into (bufout, buf, GST_BUFFER_COPY_METADATA, 0, -1);
|
|
|
|
|
2015-03-17 15:50:41 +00:00
|
|
|
GST_LOG_OBJECT (pad, "Encoding %s buffer of size %d",
|
2013-03-12 23:47:51 +00:00
|
|
|
is_rtcp ? "RTCP" : "RTP", size);
|
|
|
|
|
2018-01-26 15:25:33 +00:00
|
|
|
} else if (err == srtp_err_status_key_expired) {
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
GST_ELEMENT_ERROR (GST_ELEMENT_CAST (filter), STREAM, ENCODE,
|
|
|
|
("Key usage limit has been reached"),
|
|
|
|
("Unable to protect buffer (hard key usage limit reached)"));
|
2018-05-04 15:59:53 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2013-03-12 23:47:51 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* srtp_protect failed */
|
|
|
|
GST_ELEMENT_ERROR (filter, LIBRARY, FAILED, (NULL),
|
|
|
|
("Unable to protect buffer (protect failed) code %d", err));
|
2018-05-04 15:59:53 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2013-03-12 23:47:51 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-05-04 15:59:53 +00:00
|
|
|
*outbuf_ptr = bufout;
|
|
|
|
return ret;
|
2015-03-18 08:17:31 +00:00
|
|
|
|
|
|
|
fail:
|
|
|
|
gst_buffer_unref (bufout);
|
2018-05-04 15:59:53 +00:00
|
|
|
return ret;
|
2015-03-18 08:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_srtp_enc_chain (GstPad * pad, GstObject * parent, GstBuffer * buf,
|
|
|
|
gboolean is_rtcp)
|
|
|
|
{
|
|
|
|
GstSrtpEnc *filter = GST_SRTP_ENC (parent);
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstPad *otherpad;
|
2015-03-18 09:16:41 +00:00
|
|
|
GstBuffer *bufout = NULL;
|
2015-03-18 08:17:31 +00:00
|
|
|
|
|
|
|
if ((ret = gst_srtp_enc_check_set_caps (filter, pad, is_rtcp)) != GST_FLOW_OK) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
|
|
|
|
if (!HAS_CRYPTO (filter)) {
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
otherpad = get_rtp_other_pad (pad);
|
|
|
|
return gst_pad_push (otherpad, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
|
2018-05-04 15:59:53 +00:00
|
|
|
ret = gst_srtp_enc_process_buffer (filter, pad, buf, is_rtcp, &bufout);
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto out;
|
2015-03-18 08:17:31 +00:00
|
|
|
|
2018-05-04 15:59:53 +00:00
|
|
|
/* Push buffer to source pad */
|
|
|
|
otherpad = get_rtp_other_pad (pad);
|
|
|
|
ret = gst_pad_push (otherpad, bufout);
|
|
|
|
bufout = NULL;
|
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK)
|
|
|
|
goto out;
|
2015-03-18 08:17:31 +00:00
|
|
|
|
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
if (gst_srtp_get_soft_limit_reached ()) {
|
2015-03-18 08:39:49 +00:00
|
|
|
GST_OBJECT_UNLOCK (filter);
|
2014-05-02 21:13:54 +00:00
|
|
|
g_signal_emit (filter, gst_srtp_enc_signals[SIGNAL_SOFT_LIMIT], 0);
|
2015-03-18 08:39:49 +00:00
|
|
|
GST_OBJECT_LOCK (filter);
|
2013-03-12 23:47:51 +00:00
|
|
|
if (filter->random_key && !filter->key_changed)
|
|
|
|
gst_srtp_enc_replace_random_key (filter);
|
|
|
|
}
|
|
|
|
|
2015-03-18 08:17:31 +00:00
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
out:
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-19 15:17:58 +00:00
|
|
|
static gboolean
|
|
|
|
process_buffer_it (GstBuffer ** buffer, guint index, gpointer user_data)
|
|
|
|
{
|
|
|
|
ProcessBufferItData *data = user_data;
|
|
|
|
GstBuffer *bufout;
|
2018-05-04 15:59:53 +00:00
|
|
|
GstFlowReturn ret;
|
2015-03-19 15:17:58 +00:00
|
|
|
|
2018-05-04 15:59:53 +00:00
|
|
|
ret = gst_srtp_enc_process_buffer (data->filter, data->pad, *buffer,
|
|
|
|
data->is_rtcp, &bufout);
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
data->flowret = ret;
|
|
|
|
return FALSE;
|
2015-03-19 15:17:58 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 15:59:53 +00:00
|
|
|
gst_buffer_list_add (data->out_list, bufout);
|
|
|
|
|
2015-03-19 15:17:58 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:16:41 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_srtp_enc_chain_list (GstPad * pad, GstObject * parent,
|
|
|
|
GstBufferList * buf_list, gboolean is_rtcp)
|
|
|
|
{
|
|
|
|
GstSrtpEnc *filter = GST_SRTP_ENC (parent);
|
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
|
|
|
GstPad *otherpad;
|
|
|
|
GstBufferList *out_list = NULL;
|
2015-03-19 15:17:58 +00:00
|
|
|
ProcessBufferItData process_data;
|
2015-03-18 09:16:41 +00:00
|
|
|
|
2015-03-19 15:17:58 +00:00
|
|
|
GST_LOG_OBJECT (pad, "Buffer chain with list of %d",
|
|
|
|
gst_buffer_list_length (buf_list));
|
2015-03-18 09:16:41 +00:00
|
|
|
|
2015-03-19 15:17:58 +00:00
|
|
|
if (!gst_buffer_list_length (buf_list))
|
|
|
|
goto out;
|
2015-03-18 09:16:41 +00:00
|
|
|
|
2015-03-19 15:17:58 +00:00
|
|
|
if ((ret = gst_srtp_enc_check_set_caps (filter, pad, is_rtcp)) != GST_FLOW_OK)
|
2015-03-18 09:16:41 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
|
|
|
|
if (!HAS_CRYPTO (filter)) {
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
otherpad = get_rtp_other_pad (pad);
|
|
|
|
return gst_pad_push_list (otherpad, buf_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
|
|
|
|
out_list = gst_buffer_list_new ();
|
|
|
|
|
2015-03-19 15:17:58 +00:00
|
|
|
process_data.filter = filter;
|
|
|
|
process_data.pad = pad;
|
|
|
|
process_data.is_rtcp = is_rtcp;
|
|
|
|
process_data.out_list = out_list;
|
2018-05-04 15:59:53 +00:00
|
|
|
process_data.flowret = GST_FLOW_OK;
|
2015-03-18 09:16:41 +00:00
|
|
|
|
2018-05-04 15:59:53 +00:00
|
|
|
if (!gst_buffer_list_foreach (buf_list, process_buffer_it, &process_data)) {
|
|
|
|
ret = process_data.flowret;
|
|
|
|
goto out;
|
|
|
|
}
|
2015-03-19 15:17:58 +00:00
|
|
|
|
|
|
|
if (!gst_buffer_list_length (out_list)) {
|
|
|
|
gst_buffer_list_unref (out_list);
|
|
|
|
ret = GST_FLOW_OK;
|
|
|
|
goto out;
|
2015-03-18 09:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Push buffer to source pad */
|
|
|
|
otherpad = get_rtp_other_pad (pad);
|
2015-03-19 15:17:58 +00:00
|
|
|
GST_LOG_OBJECT (pad, "Pushing buffer chain of %d",
|
|
|
|
gst_buffer_list_length (buf_list));
|
2015-03-18 09:16:41 +00:00
|
|
|
ret = gst_pad_push_list (otherpad, out_list);
|
|
|
|
|
|
|
|
if (ret != GST_FLOW_OK) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
|
|
|
|
if (gst_srtp_get_soft_limit_reached ()) {
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
g_signal_emit (filter, gst_srtp_enc_signals[SIGNAL_SOFT_LIMIT], 0);
|
|
|
|
GST_OBJECT_LOCK (filter);
|
|
|
|
if (filter->random_key && !filter->key_changed)
|
|
|
|
gst_srtp_enc_replace_random_key (filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
|
|
|
gst_buffer_list_unref (buf_list);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_srtp_enc_chain_rtp (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
return gst_srtp_enc_chain (pad, parent, buf, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_srtp_enc_chain_rtcp (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
return gst_srtp_enc_chain (pad, parent, buf, TRUE);
|
|
|
|
}
|
|
|
|
|
2015-03-18 09:16:41 +00:00
|
|
|
static GstFlowReturn
|
|
|
|
gst_srtp_enc_chain_list_rtp (GstPad * pad, GstObject * parent,
|
|
|
|
GstBufferList * buf_list)
|
|
|
|
{
|
|
|
|
return gst_srtp_enc_chain_list (pad, parent, buf_list, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_srtp_enc_chain_list_rtcp (GstPad * pad, GstObject * parent,
|
|
|
|
GstBufferList * buf_list)
|
|
|
|
{
|
|
|
|
return gst_srtp_enc_chain_list (pad, parent, buf_list, TRUE);
|
|
|
|
}
|
|
|
|
|
2013-03-12 23:47:51 +00:00
|
|
|
|
|
|
|
/* Change state
|
|
|
|
*/
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_srtp_enc_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstStateChangeReturn res;
|
|
|
|
GstSrtpEnc *filter;
|
|
|
|
|
|
|
|
filter = GST_SRTP_ENC (element);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
2013-06-29 00:15:08 +00:00
|
|
|
if (filter->rtp_cipher != GST_SRTP_CIPHER_NULL ||
|
|
|
|
filter->rtcp_cipher != GST_SRTP_CIPHER_NULL ||
|
|
|
|
filter->rtp_auth != GST_SRTP_AUTH_NULL ||
|
|
|
|
filter->rtcp_auth != GST_SRTP_AUTH_NULL) {
|
|
|
|
if (!filter->key) {
|
|
|
|
if (filter->random_key) {
|
|
|
|
gst_srtp_enc_replace_random_key (filter);
|
|
|
|
} else {
|
|
|
|
GST_ERROR_OBJECT (element, "Need a key to get to READY");
|
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
|
|
|
}
|
2013-03-12 23:47:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 06:36:44 +00:00
|
|
|
|
|
|
|
/* RFC 3711 says in "3. SRTP Framework" that SRTCP message authentication
|
|
|
|
* is MANDATORY. In case of GCM let the pipeline handle any errors.
|
|
|
|
*/
|
|
|
|
if (filter->rtcp_cipher != GST_SRTP_CIPHER_AES_128_GCM
|
|
|
|
&& filter->rtcp_cipher != GST_SRTP_CIPHER_AES_256_GCM
|
|
|
|
&& filter->rtcp_cipher != GST_SRTP_CIPHER_NULL
|
|
|
|
&& filter->rtcp_auth == GST_SRTP_AUTH_NULL) {
|
2013-03-12 23:47:51 +00:00
|
|
|
GST_ERROR_OBJECT (filter,
|
|
|
|
"RTCP authentication can't be NULL if encryption is not NULL.");
|
|
|
|
return GST_STATE_CHANGE_FAILURE;
|
|
|
|
}
|
2015-03-18 08:39:49 +00:00
|
|
|
GST_OBJECT_LOCK (filter);
|
2013-03-12 23:47:51 +00:00
|
|
|
if (!filter->first_session)
|
2015-03-18 08:39:49 +00:00
|
|
|
gst_srtp_enc_reset_no_lock (filter);
|
|
|
|
GST_OBJECT_UNLOCK (filter);
|
2013-03-12 23:47:51 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = GST_ELEMENT_CLASS (gst_srtp_enc_parent_class)->change_state (element,
|
|
|
|
transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
gst_srtp_enc_reset (filter);
|
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_srtp_enc_sink_event (GstPad * pad, GstObject * parent, GstEvent * event,
|
|
|
|
gboolean is_rtcp)
|
|
|
|
{
|
|
|
|
GstSrtpEnc *filter = GST_SRTP_ENC (parent);
|
|
|
|
gboolean ret;
|
|
|
|
GstPad *otherpad;
|
|
|
|
|
|
|
|
otherpad = get_rtp_other_pad (pad);
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
GST_DEBUG_OBJECT (pad, "Encing event Flush stop (%d)",
|
|
|
|
GST_EVENT_TYPE (event));
|
|
|
|
gst_srtp_enc_reset (filter);
|
|
|
|
ret = gst_pad_push_event (otherpad, event);
|
|
|
|
break;
|
|
|
|
case GST_EVENT_CAPS:
|
|
|
|
{
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
gst_event_parse_caps (event, &caps);
|
|
|
|
ret = gst_srtp_enc_sink_setcaps (pad, filter, caps, is_rtcp);
|
2014-06-17 09:48:12 +00:00
|
|
|
gst_event_unref (event);
|
2013-03-12 23:47:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
GST_DEBUG_OBJECT (pad, "Encing event default (%d)",
|
|
|
|
GST_EVENT_TYPE (event));
|
|
|
|
ret = gst_pad_event_default (pad, parent, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_srtp_enc_sink_event_rtp (GstPad * pad, GstObject * parent, GstEvent * event)
|
|
|
|
{
|
|
|
|
return gst_srtp_enc_sink_event (pad, parent, event, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_srtp_enc_sink_event_rtcp (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event)
|
|
|
|
{
|
|
|
|
return gst_srtp_enc_sink_event (pad, parent, event, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* entry point to initialize the plug-in
|
|
|
|
* initialize the plug-in itself
|
|
|
|
* register the element factories and other features
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
gst_srtp_enc_plugin_init (GstPlugin * srtpenc)
|
|
|
|
{
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_srtp_enc_debug, "srtpenc", 0, "SRTP Enc");
|
|
|
|
|
|
|
|
return gst_element_register (srtpenc, "srtpenc", GST_RANK_NONE,
|
|
|
|
GST_TYPE_SRTP_ENC);
|
|
|
|
}
|