2021-02-17 22:48:33 +00:00
|
|
|
/*
|
2022-02-26 20:09:04 +00:00
|
|
|
* Copyright (C) 2021-2022 Purism SPC
|
2021-02-17 22:48:33 +00:00
|
|
|
*
|
|
|
|
* This file is part of Calls.
|
|
|
|
*
|
|
|
|
* Calls is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calls 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
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calls. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Evangelos Ribeiro Tzaras <evangelos.tzaras@puri.sm>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define G_LOG_DOMAIN "CallsSipMediaPipeline"
|
|
|
|
|
2022-12-18 10:19:21 +00:00
|
|
|
#include "calls-config.h"
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
#include "calls-media-pipeline-enums.h"
|
2021-02-17 22:48:33 +00:00
|
|
|
#include "calls-sip-media-pipeline.h"
|
2022-04-25 07:47:24 +00:00
|
|
|
#include "calls-srtp-utils.h"
|
2023-01-21 15:42:39 +00:00
|
|
|
#include "calls-util.h"
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2022-04-07 16:56:24 +00:00
|
|
|
#include <glib-unix.h>
|
2021-02-17 22:48:33 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
#include <gio/gio.h>
|
|
|
|
|
2022-02-26 20:09:04 +00:00
|
|
|
#define MAKE_ELEMENT(var, element, name) \
|
|
|
|
self->var = gst_element_factory_make (element, name); \
|
|
|
|
if (!self->var) { \
|
|
|
|
if (error) \
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, \
|
|
|
|
"Could not create '%s' element of type %s", \
|
|
|
|
name ? : "unnamed", element); \
|
|
|
|
return FALSE; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-10 22:25:13 +00:00
|
|
|
/**
|
|
|
|
* SECTION:sip-media-pipeline
|
|
|
|
* @short_description:
|
|
|
|
* @Title:
|
|
|
|
*
|
|
|
|
* #CallsSipMediaPipeline is responsible for building Gstreamer pipelines.
|
|
|
|
* Usually a sender and receiver pipeline is employed.
|
|
|
|
*
|
|
|
|
* The sender pipeline records audio and uses RTP to send it out over the network
|
|
|
|
* to the specified host.
|
|
|
|
* The receiver pipeline receives RTP from the network and plays the audio
|
|
|
|
* on the system.
|
|
|
|
*
|
|
|
|
* Both pipelines are using RTCP.
|
|
|
|
*/
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
|
|
|
|
/* The following defines are used to set/reset bitmaps of playing/paused/stop state */
|
2022-04-06 08:30:34 +00:00
|
|
|
#define EL_PIPELINE (1<<0)
|
|
|
|
#define EL_RTPBIN (1<<1)
|
|
|
|
#define EL_RTP_SRC (1<<2)
|
|
|
|
#define EL_RTP_SINK (1<<3)
|
|
|
|
#define EL_RTCP_SRC (1<<4)
|
|
|
|
#define EL_RTCP_SINK (1<<5)
|
|
|
|
|
|
|
|
#define EL_SRTP_ENCODER (1<<6)
|
|
|
|
#define EL_SRTP_DECODER (1<<7)
|
|
|
|
|
|
|
|
#define EL_AUDIO_SRC (1<<8)
|
|
|
|
#define EL_AUDIO_SINK (1<<9)
|
|
|
|
|
|
|
|
#define EL_PAYLOADER (1<<10)
|
|
|
|
#define EL_DEPAYLOADER (1<<11)
|
|
|
|
|
|
|
|
#define EL_ENCODER (1<<12)
|
|
|
|
#define EL_DECODER (1<<13)
|
|
|
|
|
|
|
|
#define EL_SENDING \
|
|
|
|
(EL_AUDIO_SRC | EL_ENCODER | EL_PAYLOADER | \
|
|
|
|
EL_RTPBIN | EL_RTP_SINK | EL_RTCP_SINK)
|
|
|
|
|
|
|
|
#define EL_ALL_RTP \
|
|
|
|
(EL_PIPELINE | EL_RTPBIN | \
|
|
|
|
EL_RTP_SRC | EL_RTP_SINK | EL_RTCP_SRC | EL_RTCP_SINK | \
|
|
|
|
EL_AUDIO_SRC | EL_AUDIO_SINK | \
|
|
|
|
EL_ENCODER | EL_DECODER | EL_PAYLOADER | EL_DEPAYLOADER)
|
|
|
|
|
|
|
|
#define EL_ALL_SRTP (EL_ALL_RTP | EL_SRTP_ENCODER | EL_SRTP_DECODER)
|
2022-02-28 10:18:36 +00:00
|
|
|
|
|
|
|
|
2021-04-10 22:25:13 +00:00
|
|
|
enum {
|
|
|
|
PROP_0,
|
|
|
|
PROP_CODEC,
|
|
|
|
PROP_REMOTE,
|
|
|
|
PROP_RPORT_RTP,
|
|
|
|
PROP_RPORT_RTCP,
|
|
|
|
PROP_DEBUG,
|
2022-02-28 10:18:36 +00:00
|
|
|
PROP_STATE,
|
2021-04-10 22:25:13 +00:00
|
|
|
PROP_LAST_PROP,
|
|
|
|
};
|
2022-02-28 10:18:36 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
SENDING_STARTED,
|
|
|
|
N_SIGNALS
|
|
|
|
};
|
|
|
|
|
2021-04-10 22:25:13 +00:00
|
|
|
static GParamSpec *props[PROP_LAST_PROP];
|
2022-02-28 10:18:36 +00:00
|
|
|
static uint signals[N_SIGNALS];
|
|
|
|
|
2021-04-10 22:25:13 +00:00
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
struct _CallsSipMediaPipeline {
|
2022-04-25 07:47:53 +00:00
|
|
|
GObject parent;
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2022-04-25 07:47:53 +00:00
|
|
|
MediaCodecInfo *codec;
|
|
|
|
gboolean debug;
|
2022-02-28 10:18:36 +00:00
|
|
|
|
2022-04-25 07:47:53 +00:00
|
|
|
CallsMediaPipelineState state;
|
|
|
|
uint element_map_playing;
|
|
|
|
uint element_map_paused;
|
|
|
|
uint element_map_stopped;
|
|
|
|
gboolean emitted_sending_signal;
|
2022-04-06 08:30:34 +00:00
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
/* Connection details */
|
2022-04-25 07:47:53 +00:00
|
|
|
char *remote;
|
|
|
|
gint rport_rtp;
|
|
|
|
gint rport_rtcp;
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2022-04-25 07:47:53 +00:00
|
|
|
GstElement *pipeline;
|
|
|
|
GstElement *rtpbin;
|
2022-04-06 08:30:34 +00:00
|
|
|
|
2022-04-25 07:47:53 +00:00
|
|
|
GstElement *rtp_src;
|
|
|
|
GstElement *rtp_sink;
|
|
|
|
GstElement *rtcp_sink;
|
|
|
|
GstElement *rtcp_src;
|
2022-04-06 08:30:34 +00:00
|
|
|
|
2022-04-25 07:47:53 +00:00
|
|
|
GstElement *audio_src;
|
|
|
|
GstElement *payloader;
|
|
|
|
GstElement *encoder;
|
2022-04-06 08:30:34 +00:00
|
|
|
|
2022-04-25 07:47:53 +00:00
|
|
|
GstElement *audio_sink;
|
|
|
|
GstElement *depayloader;
|
|
|
|
GstElement *decoder;
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2022-04-07 09:42:53 +00:00
|
|
|
/* SRTP */
|
2022-04-25 07:47:53 +00:00
|
|
|
gboolean use_srtp;
|
2022-04-25 07:47:24 +00:00
|
|
|
calls_srtp_crypto_attribute *crypto_own;
|
|
|
|
calls_srtp_crypto_attribute *crypto_theirs;
|
2022-04-07 09:42:53 +00:00
|
|
|
|
2022-04-25 07:47:53 +00:00
|
|
|
GstElement *srtpenc;
|
|
|
|
GstElement *srtpdec;
|
2022-04-07 09:42:53 +00:00
|
|
|
|
2022-04-25 07:47:53 +00:00
|
|
|
gulong request_rtpbin_rtp_decoder_id;
|
|
|
|
gulong request_rtpbin_rtp_encoder_id;
|
|
|
|
gulong request_rtpbin_rtcp_encoder_id;
|
|
|
|
gulong request_rtpbin_rtcp_decoder_id;
|
2022-04-07 09:42:53 +00:00
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
/* Gstreamer busses */
|
2022-04-25 07:47:53 +00:00
|
|
|
GstBus *bus;
|
|
|
|
guint bus_watch_id;
|
2021-02-17 22:48:33 +00:00
|
|
|
};
|
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
#if GLIB_CHECK_VERSION (2, 70, 0)
|
2022-02-28 13:02:39 +00:00
|
|
|
G_DEFINE_FINAL_TYPE (CallsSipMediaPipeline, calls_sip_media_pipeline, G_TYPE_OBJECT)
|
|
|
|
#else
|
|
|
|
G_DEFINE_TYPE (CallsSipMediaPipeline, calls_sip_media_pipeline, G_TYPE_OBJECT)
|
|
|
|
#endif
|
2021-02-17 22:48:33 +00:00
|
|
|
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
set_state (CallsSipMediaPipeline *self,
|
|
|
|
CallsMediaPipelineState state)
|
|
|
|
{
|
2022-04-03 08:49:50 +00:00
|
|
|
g_autoptr (GEnumClass) enum_class = NULL;
|
|
|
|
GEnumValue *enum_val;
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
g_autofree char *fname = NULL;
|
2022-04-03 08:49:50 +00:00
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
g_assert (CALLS_SIP_MEDIA_PIPELINE (self));
|
|
|
|
|
|
|
|
if (self->state == state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self->state = state;
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_STATE]);
|
|
|
|
|
|
|
|
self->emitted_sending_signal = FALSE;
|
2022-04-03 08:49:50 +00:00
|
|
|
|
|
|
|
if (state == CALLS_MEDIA_PIPELINE_STATE_INITIALIZING)
|
|
|
|
return;
|
|
|
|
|
|
|
|
enum_class = g_type_class_ref (CALLS_TYPE_MEDIA_PIPELINE_STATE);
|
|
|
|
enum_val = g_enum_get_value (enum_class, state);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
fname = g_strdup_printf ("calls-%s", enum_val->value_nick);
|
2022-04-03 08:49:50 +00:00
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (self->pipeline),
|
2022-04-03 08:49:50 +00:00
|
|
|
GST_DEBUG_GRAPH_SHOW_ALL,
|
2022-04-06 08:30:34 +00:00
|
|
|
fname);
|
2022-02-28 10:18:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
check_element_maps (CallsSipMediaPipeline *self)
|
|
|
|
{
|
2022-04-07 13:29:39 +00:00
|
|
|
uint all_rtp_elements;
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
g_assert (CALLS_IS_SIP_MEDIA_PIPELINE (self));
|
|
|
|
|
2022-04-07 13:29:39 +00:00
|
|
|
all_rtp_elements = self->use_srtp ? EL_ALL_SRTP : EL_ALL_RTP;
|
|
|
|
|
|
|
|
if (self->element_map_playing == all_rtp_elements) {
|
2022-02-28 10:18:36 +00:00
|
|
|
g_debug ("All pipeline elements are playing");
|
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_PLAYING);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-07 13:29:39 +00:00
|
|
|
if (self->element_map_paused == all_rtp_elements) {
|
2022-02-28 10:18:36 +00:00
|
|
|
g_debug ("All pipeline elements are paused");
|
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_PAUSED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-07 13:29:39 +00:00
|
|
|
if (self->element_map_stopped == all_rtp_elements) {
|
2022-02-28 10:18:36 +00:00
|
|
|
g_debug ("All pipeline elements are stopped");
|
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_STOPPED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
if ((self->element_map_playing & (EL_SENDING)) == (EL_SENDING) &&
|
2022-02-28 10:18:36 +00:00
|
|
|
!self->emitted_sending_signal) {
|
|
|
|
g_debug ("Sender pipeline is sending data to %s RTP/RTCP %d/%d",
|
|
|
|
self->remote, self->rport_rtp, self->rport_rtcp);
|
|
|
|
g_signal_emit (self, signals[SENDING_STARTED], 0);
|
|
|
|
self->emitted_sending_signal = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
/* rtpbin adds a pad once the payload is verified */
|
|
|
|
static void
|
2021-04-10 22:25:13 +00:00
|
|
|
on_pad_added (GstElement *rtpbin,
|
|
|
|
GstPad *srcpad,
|
2021-02-17 22:48:33 +00:00
|
|
|
GstElement *depayloader)
|
|
|
|
{
|
|
|
|
GstPad *sinkpad;
|
|
|
|
|
|
|
|
g_debug ("pad added: %s", GST_PAD_NAME (srcpad));
|
|
|
|
|
|
|
|
sinkpad = gst_element_get_static_pad (depayloader, "sink");
|
2022-02-26 20:09:04 +00:00
|
|
|
g_debug ("linking to %s", GST_PAD_NAME (sinkpad));
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK)
|
2021-08-25 18:36:01 +00:00
|
|
|
g_warning ("Failed to link rtpbin to depayloader");
|
2021-02-17 22:48:33 +00:00
|
|
|
|
|
|
|
gst_object_unref (sinkpad);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2021-04-10 22:25:13 +00:00
|
|
|
on_bus_message (GstBus *bus,
|
|
|
|
GstMessage *message,
|
|
|
|
gpointer data)
|
2021-02-17 22:48:33 +00:00
|
|
|
{
|
2022-02-28 10:18:36 +00:00
|
|
|
CallsSipMediaPipeline *self = CALLS_SIP_MEDIA_PIPELINE (data);
|
2021-02-17 22:48:33 +00:00
|
|
|
|
|
|
|
switch (GST_MESSAGE_TYPE (message)) {
|
|
|
|
case GST_MESSAGE_ERROR:
|
2022-04-24 10:24:55 +00:00
|
|
|
{
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
g_autofree char *msg = NULL;
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
gst_message_parse_error (message, &error, &msg);
|
|
|
|
g_warning ("Error on the message bus: %s (%s)", error->message, msg);
|
|
|
|
break;
|
|
|
|
}
|
2021-02-17 22:48:33 +00:00
|
|
|
|
|
|
|
case GST_MESSAGE_WARNING:
|
2022-04-24 10:24:55 +00:00
|
|
|
{
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
g_autofree char *msg = NULL;
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
gst_message_parse_warning (message, &error, &msg);
|
|
|
|
g_warning ("Warning on the message bus: %s (%s)", error->message, msg);
|
|
|
|
break;
|
|
|
|
}
|
2021-02-17 22:48:33 +00:00
|
|
|
|
|
|
|
case GST_MESSAGE_EOS:
|
|
|
|
g_debug ("Received end of stream");
|
2022-02-28 10:18:36 +00:00
|
|
|
calls_sip_media_pipeline_stop (self);
|
2021-02-17 22:48:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GST_MESSAGE_STATE_CHANGED:
|
2022-04-24 10:24:55 +00:00
|
|
|
{
|
|
|
|
GstState oldstate;
|
|
|
|
GstState newstate;
|
|
|
|
uint element_id = 0;
|
|
|
|
uint unset_element_id;
|
|
|
|
|
|
|
|
gst_message_parse_state_changed (message, &oldstate, &newstate, NULL);
|
|
|
|
|
|
|
|
g_debug ("Element %s has changed state from %s to %s",
|
|
|
|
GST_OBJECT_NAME (message->src),
|
|
|
|
gst_element_state_get_name (oldstate),
|
|
|
|
gst_element_state_get_name (newstate));
|
|
|
|
|
|
|
|
if (message->src == GST_OBJECT (self->pipeline))
|
|
|
|
element_id = EL_PIPELINE;
|
|
|
|
else if (message->src == GST_OBJECT (self->rtpbin))
|
|
|
|
element_id = EL_RTPBIN;
|
|
|
|
|
|
|
|
else if (message->src == GST_OBJECT (self->rtp_src))
|
|
|
|
element_id = EL_RTP_SRC;
|
|
|
|
else if (message->src == GST_OBJECT (self->rtp_sink))
|
|
|
|
element_id = EL_RTP_SINK;
|
|
|
|
|
|
|
|
else if (message->src == GST_OBJECT (self->rtcp_src))
|
|
|
|
element_id = EL_RTCP_SRC;
|
|
|
|
else if (message->src == GST_OBJECT (self->rtcp_sink))
|
|
|
|
element_id = EL_RTCP_SINK;
|
|
|
|
|
2022-04-07 13:29:39 +00:00
|
|
|
else if (message->src == GST_OBJECT (self->srtpenc))
|
|
|
|
element_id = EL_SRTP_ENCODER;
|
|
|
|
else if (message->src == GST_OBJECT (self->srtpdec))
|
|
|
|
element_id = EL_SRTP_DECODER;
|
2022-04-24 10:24:55 +00:00
|
|
|
|
|
|
|
else if (message->src == GST_OBJECT (self->audio_src))
|
|
|
|
element_id = EL_AUDIO_SRC;
|
|
|
|
else if (message->src == GST_OBJECT (self->audio_sink))
|
|
|
|
element_id = EL_AUDIO_SINK;
|
|
|
|
|
|
|
|
else if (message->src == GST_OBJECT (self->payloader))
|
|
|
|
element_id = EL_PAYLOADER;
|
|
|
|
else if (message->src == GST_OBJECT (self->depayloader))
|
|
|
|
element_id = EL_DEPAYLOADER;
|
|
|
|
|
|
|
|
else if (message->src == GST_OBJECT (self->encoder))
|
|
|
|
element_id = EL_ENCODER;
|
|
|
|
else if (message->src == GST_OBJECT (self->decoder))
|
|
|
|
element_id = EL_DECODER;
|
|
|
|
|
|
|
|
unset_element_id = G_MAXUINT ^ element_id;
|
|
|
|
|
|
|
|
if (newstate == GST_STATE_PLAYING) {
|
|
|
|
self->element_map_playing |= element_id;
|
|
|
|
self->element_map_paused &= unset_element_id;
|
|
|
|
self->element_map_stopped &= unset_element_id;
|
|
|
|
} else if (newstate == GST_STATE_PAUSED) {
|
|
|
|
self->element_map_paused |= element_id;
|
|
|
|
self->element_map_playing &= unset_element_id;
|
|
|
|
self->element_map_stopped &= unset_element_id;
|
|
|
|
} else if (newstate == GST_STATE_NULL) {
|
|
|
|
self->element_map_stopped |= element_id;
|
|
|
|
self->element_map_playing &= unset_element_id;
|
|
|
|
self->element_map_paused &= unset_element_id;
|
2021-02-17 22:48:33 +00:00
|
|
|
}
|
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
check_element_maps (self);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-26 20:09:04 +00:00
|
|
|
default:
|
2022-02-28 10:18:36 +00:00
|
|
|
if (self->debug)
|
2022-02-26 20:09:04 +00:00
|
|
|
g_debug ("Got unhandled %s message", GST_MESSAGE_TYPE_NAME (message));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* keep watching for messages on the bus */
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-07 09:42:53 +00:00
|
|
|
/* SRTP setup */
|
|
|
|
|
|
|
|
static GstCaps *
|
|
|
|
on_srtpdec_request_key (GstElement *srtpdec,
|
|
|
|
guint ssrc,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2022-04-25 07:47:24 +00:00
|
|
|
CallsSipMediaPipeline *self = CALLS_SIP_MEDIA_PIPELINE (user_data);
|
|
|
|
|
|
|
|
GstCaps *caps;
|
|
|
|
|
|
|
|
const char *srtp_cipher = "null";
|
|
|
|
const char *srtcp_cipher = "null";
|
|
|
|
const char *srtp_auth = "null";
|
|
|
|
const char *srtcp_auth = "null";
|
|
|
|
|
|
|
|
gboolean need_mki;
|
|
|
|
|
|
|
|
if (!calls_srtp_crypto_get_srtpdec_params (self->crypto_theirs,
|
|
|
|
&srtp_cipher,
|
|
|
|
&srtp_auth,
|
|
|
|
&srtcp_cipher,
|
|
|
|
&srtcp_auth))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (self->crypto_theirs->n_key_params == 0 ||
|
|
|
|
self->crypto_theirs->n_key_params > 16) {
|
|
|
|
g_warning ("Got %u key parameters, but can only handle between 1 and 16",
|
|
|
|
self->crypto_theirs->n_key_params);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
need_mki = self->crypto_theirs->n_key_params > 1;
|
|
|
|
|
|
|
|
if (self->crypto_theirs->n_key_params == 1) {
|
|
|
|
/* g_autofree guchar *key_salt = NULL; */
|
|
|
|
guchar *key_salt = NULL;
|
|
|
|
gsize key_salt_length;
|
|
|
|
g_autoptr (GstBuffer) key_buffer = NULL;
|
|
|
|
|
|
|
|
key_salt = g_base64_decode (self->crypto_theirs->key_params[0].b64_keysalt,
|
|
|
|
&key_salt_length);
|
|
|
|
key_buffer = gst_buffer_new_wrapped (key_salt, key_salt_length);
|
|
|
|
|
|
|
|
/* TODO Setting up MKI buffer not implemented yet */
|
|
|
|
if (self->crypto_theirs->key_params[0].mki) {
|
|
|
|
g_warning ("Using MKI is not implemented yet");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gst_caps_new_simple ("application/x-srtp",
|
|
|
|
"srtp-key", GST_TYPE_BUFFER, key_buffer,
|
|
|
|
"srtp-cipher", G_TYPE_STRING, srtp_cipher,
|
|
|
|
"srtcp-cipher", G_TYPE_STRING, srtcp_cipher,
|
|
|
|
"srtp-auth", G_TYPE_STRING, srtp_auth,
|
|
|
|
"srtcp-auth", G_TYPE_STRING, srtcp_auth,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO Setting up MKI buffer not implemented yet */
|
|
|
|
g_warning ("Using MKI is not implemented yet");
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
caps = gst_caps_new_simple ("application/x-srtp",
|
|
|
|
"srtp-cipher", G_TYPE_STRING, srtp_cipher,
|
|
|
|
"srtcp-cipher", G_TYPE_STRING, srtcp_cipher,
|
|
|
|
"srtp-auth", G_TYPE_STRING, srtp_auth,
|
|
|
|
"srtcp-auth", G_TYPE_STRING, srtcp_auth,
|
2022-04-07 09:42:53 +00:00
|
|
|
NULL);
|
2022-04-25 07:47:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
for (guint i = 0; i < self->crypto_theirs->n_key_params; i++) {
|
|
|
|
GstStructure *structure;
|
|
|
|
|
|
|
|
g_autofree char *structure_name = g_strdup_printf ("key-%u", i);
|
|
|
|
|
|
|
|
guchar *key_salt = NULL;
|
|
|
|
gsize key_salt_length;
|
|
|
|
g_autoptr (GstBuffer) key_buffer = NULL;
|
|
|
|
g_autoptr (GstBuffer) mki_buffer = NULL;
|
|
|
|
|
|
|
|
key_salt = g_base64_decode (self->crypto_theirs->key_params[0].b64_keysalt,
|
|
|
|
&key_salt_length);
|
|
|
|
key_buffer = gst_buffer_new_wrapped (key_salt, key_salt_length);
|
|
|
|
|
|
|
|
|
|
|
|
if (i == 0 && need_mki) {
|
|
|
|
structure = gst_structure_new (structure_name,
|
|
|
|
"srtp-key", GST_TYPE_BUFFER, key_buffer,
|
|
|
|
"mki", GST_TYPE_BUFFER, mki_buffer,
|
|
|
|
NULL);
|
|
|
|
} else if (i == 0 && !need_mki) {
|
|
|
|
structure = gst_structure_new (structure_name,
|
|
|
|
"srtp-key", GST_TYPE_BUFFER, key_buffer,
|
|
|
|
NULL);
|
|
|
|
} else {
|
|
|
|
g_autofree char *key_field_name = g_strdup_printf ("srtp-key%u", i+1);
|
|
|
|
g_autofree char *mki_field_name = g_strdup_printf ("mki%u", i+1);
|
|
|
|
|
|
|
|
structure = gst_structure_new (structure_name,
|
|
|
|
key_field_name, GST_TYPE_BUFFER, key_buffer,
|
|
|
|
mki_field_name, GST_TYPE_BUFFER, mki_buffer,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
gst_caps_append_structure (caps, structure);
|
|
|
|
}
|
|
|
|
|
|
|
|
return caps;
|
2022-04-07 09:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GstElement *
|
|
|
|
on_rtpbin_request_decoder (GstElement *rtpbin,
|
|
|
|
guint session_id,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
CallsSipMediaPipeline *self = CALLS_SIP_MEDIA_PIPELINE (user_data);
|
|
|
|
|
|
|
|
if (!self->use_srtp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return gst_object_ref (self->srtpdec);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static GstElement *
|
|
|
|
on_rtpbin_request_encoder (GstElement *rtpbin,
|
|
|
|
guint session_id,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
CallsSipMediaPipeline *self = CALLS_SIP_MEDIA_PIPELINE (user_data);
|
|
|
|
|
|
|
|
if (!self->use_srtp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return gst_object_ref (self->srtpenc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
/* Pipeline setup */
|
2022-02-26 20:09:04 +00:00
|
|
|
|
|
|
|
static gboolean
|
2022-04-06 08:30:34 +00:00
|
|
|
setup_socket_reuse (CallsSipMediaPipeline *self,
|
|
|
|
GError **error)
|
2022-02-26 20:09:04 +00:00
|
|
|
{
|
2022-04-06 08:30:34 +00:00
|
|
|
g_autoptr (GSocket) rtp_sock = NULL;
|
|
|
|
g_autoptr (GSocket) rtcp_sock = NULL;
|
2022-02-26 20:09:04 +00:00
|
|
|
|
2022-04-22 15:46:50 +00:00
|
|
|
/* set rtp element ready and lock it's state so it doesn't get stopped */
|
|
|
|
gst_element_set_locked_state (self->rtp_src, TRUE);
|
|
|
|
gst_element_set_state (self->rtp_src, GST_STATE_READY);
|
2022-02-26 20:09:04 +00:00
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
g_object_get (self->rtp_src, "used-socket", &rtp_sock, NULL);
|
|
|
|
if (!rtp_sock) {
|
2022-02-26 20:09:04 +00:00
|
|
|
if (error)
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
2022-04-06 08:30:34 +00:00
|
|
|
"Could not retrieve used socket from RTP udpsrc element");
|
2022-02-26 20:09:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-04-22 15:46:50 +00:00
|
|
|
/* configure socket and don't close it, since it belongs to rtp_src */
|
|
|
|
g_object_set (self->rtp_sink,
|
|
|
|
"socket", rtp_sock,
|
|
|
|
"close-socket", FALSE,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* set rtcp element ready and lock it's state so it doesn't get stopped */
|
|
|
|
gst_element_set_locked_state (self->rtcp_src, TRUE);
|
|
|
|
gst_element_set_state (self->rtcp_src, GST_STATE_READY);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
g_object_get (self->rtcp_src, "used-socket", &rtcp_sock, NULL);
|
|
|
|
if (!rtcp_sock) {
|
2022-02-26 20:09:04 +00:00
|
|
|
if (error)
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
2022-04-06 08:30:34 +00:00
|
|
|
"Could not retrieve used socket from RTCP udpsrc element");
|
2022-02-26 20:09:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-22 15:46:50 +00:00
|
|
|
/* configure socket and don't close it, since it belongs to rtcp_src */
|
2022-04-06 08:30:34 +00:00
|
|
|
g_object_set (self->rtcp_sink,
|
|
|
|
"socket", rtcp_sock,
|
|
|
|
"close-socket", FALSE,
|
|
|
|
NULL);
|
2022-02-26 20:09:04 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2022-04-06 08:30:34 +00:00
|
|
|
pipeline_init (CallsSipMediaPipeline *self,
|
|
|
|
GError **error)
|
2022-02-26 20:09:04 +00:00
|
|
|
{
|
2022-04-07 09:42:53 +00:00
|
|
|
GstPad *tmppad;
|
2022-02-26 20:09:04 +00:00
|
|
|
const char *env_var;
|
|
|
|
|
|
|
|
g_assert (CALLS_SIP_MEDIA_PIPELINE (self));
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
self->pipeline = gst_pipeline_new ("media-pipeline");
|
2022-02-26 20:09:04 +00:00
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
if (!self->pipeline) {
|
2022-02-26 20:09:04 +00:00
|
|
|
if (error)
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
2022-04-06 08:30:34 +00:00
|
|
|
"Could not create media pipeline");
|
2022-02-26 20:09:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
gst_object_ref_sink (self->pipeline);
|
2022-02-26 20:09:04 +00:00
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
/* Audio source*/
|
2022-02-26 20:09:04 +00:00
|
|
|
env_var = g_getenv ("CALLS_AUDIOSRC");
|
|
|
|
if (!STR_IS_NULL_OR_EMPTY (env_var)) {
|
2022-04-06 08:30:34 +00:00
|
|
|
MAKE_ELEMENT (audio_src, env_var, "audiosource");
|
2022-02-26 20:09:04 +00:00
|
|
|
} else {
|
|
|
|
g_autoptr (GstStructure) gst_props = NULL;
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
MAKE_ELEMENT (audio_src, "pulsesrc", "audiosource");
|
2022-02-26 20:09:04 +00:00
|
|
|
|
|
|
|
/* enable echo cancellation and set buffer size to 40ms */
|
|
|
|
gst_props = gst_structure_new ("props",
|
|
|
|
"media.role", G_TYPE_STRING, "phone",
|
|
|
|
"filter.want", G_TYPE_STRING, "echo-cancel",
|
|
|
|
NULL);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
g_object_set (self->audio_src,
|
2022-02-26 20:09:04 +00:00
|
|
|
"buffer-time", (gint64) 40000,
|
|
|
|
"stream-properties", gst_props,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
/* Audio sink */
|
|
|
|
env_var = g_getenv ("CALLS_AUDIOSINK");
|
|
|
|
if (!STR_IS_NULL_OR_EMPTY (env_var)) {
|
|
|
|
MAKE_ELEMENT (audio_sink, env_var, "audiosink");
|
|
|
|
} else {
|
|
|
|
g_autoptr (GstStructure) gst_props = NULL;
|
|
|
|
|
|
|
|
MAKE_ELEMENT (audio_sink, "pulsesink", "audiosink");
|
|
|
|
|
|
|
|
/* enable echo cancellation and set buffer size to 40ms */
|
|
|
|
gst_props = gst_structure_new ("props",
|
|
|
|
"media.role", G_TYPE_STRING, "phone",
|
|
|
|
"filter.want", G_TYPE_STRING, "echo-cancel",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_object_set (self->audio_sink,
|
|
|
|
"buffer-time", (gint64) 40000,
|
|
|
|
"stream-properties", gst_props,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* rtpbin */
|
|
|
|
MAKE_ELEMENT (rtpbin, "rtpbin", "rtpbin");
|
|
|
|
|
2022-04-07 09:42:53 +00:00
|
|
|
/* srtp elements */
|
|
|
|
MAKE_ELEMENT (srtpdec, "srtpdec", "srtpdec");
|
|
|
|
g_signal_connect (self->srtpdec,
|
|
|
|
"request-key",
|
|
|
|
G_CALLBACK (on_srtpdec_request_key),
|
|
|
|
self);
|
|
|
|
|
|
|
|
MAKE_ELEMENT (srtpenc, "srtpenc", "srtpenc");
|
|
|
|
|
|
|
|
#if GST_CHECK_VERSION (1, 20, 0)
|
|
|
|
tmppad = gst_element_request_pad_simple (self->srtpenc, "rtp_sink_0");
|
|
|
|
#else
|
|
|
|
tmppad = gst_element_get_request_pad (self->srtpenc, "rtp_sink_0");
|
|
|
|
#endif
|
|
|
|
gst_object_unref (tmppad);
|
|
|
|
|
|
|
|
#if GST_CHECK_VERSION (1, 20, 0)
|
|
|
|
tmppad = gst_element_request_pad_simple (self->srtpenc, "rtcp_sink_0");
|
|
|
|
#else
|
|
|
|
tmppad = gst_element_get_request_pad (self->srtpenc, "rtcp_sink_0");
|
|
|
|
#endif
|
|
|
|
gst_object_unref (tmppad);
|
|
|
|
|
|
|
|
|
|
|
|
self->request_rtpbin_rtp_encoder_id =
|
|
|
|
g_signal_connect (self->rtpbin,
|
|
|
|
"request-rtp-encoder",
|
|
|
|
G_CALLBACK (on_rtpbin_request_encoder),
|
|
|
|
self);
|
|
|
|
|
|
|
|
self->request_rtpbin_rtp_decoder_id =
|
|
|
|
g_signal_connect (self->rtpbin,
|
|
|
|
"request-rtp-decoder",
|
|
|
|
G_CALLBACK (on_rtpbin_request_decoder),
|
|
|
|
self);
|
|
|
|
|
|
|
|
self->request_rtpbin_rtcp_encoder_id =
|
|
|
|
g_signal_connect (self->rtpbin,
|
|
|
|
"request-rtcp-encoder",
|
|
|
|
G_CALLBACK (on_rtpbin_request_encoder),
|
|
|
|
self);
|
|
|
|
|
|
|
|
self->request_rtpbin_rtcp_decoder_id =
|
|
|
|
g_signal_connect (self->rtpbin,
|
|
|
|
"request-rtcp-decoder",
|
|
|
|
G_CALLBACK (on_rtpbin_request_decoder),
|
|
|
|
self);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
/* UDP sources and sinks for RTP and RTCP */
|
|
|
|
MAKE_ELEMENT (rtp_src, "udpsrc", "rtp-udp-src");
|
2022-02-26 20:09:04 +00:00
|
|
|
MAKE_ELEMENT (rtp_sink, "udpsink", "rtp-udp-sink");
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
MAKE_ELEMENT (rtcp_src, "udpsrc", "rtcp-udp-src");
|
|
|
|
MAKE_ELEMENT (rtcp_sink, "udpsink", "rtcp-udp-sink");
|
|
|
|
|
|
|
|
/* port 0 means letting the OS allocate */
|
2022-10-11 11:10:19 +00:00
|
|
|
g_object_set (self->rtp_src, "port", 0, "address", "::", NULL);
|
2022-04-06 08:30:34 +00:00
|
|
|
|
2022-10-11 11:10:19 +00:00
|
|
|
g_object_set (self->rtcp_src, "port", 0, "address", "::", NULL);
|
2022-02-26 20:09:04 +00:00
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
g_object_set (self->rtp_sink, "async", FALSE, "sync", FALSE, NULL);
|
|
|
|
g_object_set (self->rtcp_sink, "async", FALSE, "sync", FALSE, NULL);
|
|
|
|
|
2022-02-26 20:09:04 +00:00
|
|
|
g_object_bind_property (self, "rport-rtp",
|
|
|
|
self->rtp_sink, "port",
|
|
|
|
G_BINDING_BIDIRECTIONAL);
|
|
|
|
|
|
|
|
g_object_bind_property (self, "remote",
|
|
|
|
self->rtp_sink, "host",
|
|
|
|
G_BINDING_BIDIRECTIONAL);
|
|
|
|
|
|
|
|
g_object_bind_property (self, "rport-rtcp",
|
2022-04-06 08:30:34 +00:00
|
|
|
self->rtcp_sink, "port",
|
2022-02-26 20:09:04 +00:00
|
|
|
G_BINDING_BIDIRECTIONAL);
|
|
|
|
|
|
|
|
g_object_bind_property (self, "remote",
|
2022-04-06 08:30:34 +00:00
|
|
|
self->rtcp_sink, "host",
|
2022-02-26 20:09:04 +00:00
|
|
|
G_BINDING_BIDIRECTIONAL);
|
|
|
|
|
2022-02-28 21:04:23 +00:00
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
/* Add all elements to the pipeline */
|
|
|
|
gst_bin_add_many (GST_BIN (self->pipeline),
|
|
|
|
self->audio_src, self->audio_sink,
|
|
|
|
self->rtpbin,
|
|
|
|
self->rtp_src, self->rtp_sink,
|
|
|
|
self->rtcp_src, self->rtcp_sink,
|
|
|
|
NULL);
|
2022-02-28 21:04:23 +00:00
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
/* Setup bus watch */
|
|
|
|
self->bus = gst_pipeline_get_bus (GST_PIPELINE (self->pipeline));
|
|
|
|
self->bus_watch_id = gst_bus_add_watch (self->bus, on_bus_message, self);
|
2022-02-28 21:04:23 +00:00
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
if (!setup_socket_reuse (self, error))
|
2022-02-28 21:04:23 +00:00
|
|
|
return FALSE;
|
2022-02-26 20:09:04 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2022-04-06 08:30:34 +00:00
|
|
|
pipeline_link_elements (CallsSipMediaPipeline *self,
|
|
|
|
GError **error)
|
2022-02-26 20:09:04 +00:00
|
|
|
{
|
|
|
|
g_autoptr (GstPad) srcpad = NULL;
|
|
|
|
g_autoptr (GstPad) sinkpad = NULL;
|
2022-04-07 09:42:53 +00:00
|
|
|
GstPadLinkReturn ret;
|
2022-02-26 20:09:04 +00:00
|
|
|
|
|
|
|
g_assert (CALLS_IS_SIP_MEDIA_PIPELINE (self));
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
/* link to payloader */
|
|
|
|
|
|
|
|
#if GST_CHECK_VERSION (1, 20, 0)
|
|
|
|
sinkpad = gst_element_request_pad_simple (self->rtpbin, "send_rtp_sink_0");
|
|
|
|
#else
|
|
|
|
sinkpad = gst_element_get_request_pad (self->rtpbin, "send_rtp_sink_0");
|
|
|
|
#endif
|
|
|
|
srcpad = gst_element_get_static_pad (self->payloader, "src");
|
|
|
|
if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
|
|
|
|
if (error)
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
"Failed to link payloader to rtpbin");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Transmitter pads */
|
|
|
|
|
2022-02-26 20:09:04 +00:00
|
|
|
srcpad = gst_element_get_static_pad (self->rtp_src, "src");
|
|
|
|
#if GST_CHECK_VERSION (1, 20, 0)
|
2022-04-06 08:30:34 +00:00
|
|
|
sinkpad = gst_element_request_pad_simple (self->rtpbin, "recv_rtp_sink_0");
|
2022-02-26 20:09:04 +00:00
|
|
|
#else
|
2022-04-06 08:30:34 +00:00
|
|
|
sinkpad = gst_element_get_request_pad (self->rtpbin, "recv_rtp_sink_0");
|
2022-02-26 20:09:04 +00:00
|
|
|
#endif
|
2022-04-07 09:42:53 +00:00
|
|
|
ret = gst_pad_link (srcpad, sinkpad);
|
|
|
|
if (ret != GST_PAD_LINK_OK) {
|
2022-02-26 20:09:04 +00:00
|
|
|
if (error)
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
"Failed to link rtpsrc to rtpbin");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_object_unref (srcpad);
|
|
|
|
gst_object_unref (sinkpad);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
srcpad = gst_element_get_static_pad (self->rtpbin, "send_rtp_src_0");
|
|
|
|
sinkpad = gst_element_get_static_pad (self->rtp_sink, "sink");
|
2022-02-26 20:09:04 +00:00
|
|
|
if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
|
|
|
|
if (error)
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
2022-04-06 08:30:34 +00:00
|
|
|
"Failed to link rtpbin to rtpsink");
|
2022-02-26 20:09:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_object_unref (srcpad);
|
|
|
|
gst_object_unref (sinkpad);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
srcpad = gst_element_get_static_pad (self->rtcp_src, "src");
|
2022-04-24 10:24:55 +00:00
|
|
|
#if GST_CHECK_VERSION (1, 20, 0)
|
2022-04-06 08:30:34 +00:00
|
|
|
sinkpad = gst_element_request_pad_simple (self->rtpbin, "recv_rtcp_sink_0");
|
2022-02-26 20:09:04 +00:00
|
|
|
#else
|
2022-04-06 08:30:34 +00:00
|
|
|
sinkpad = gst_element_get_request_pad (self->rtpbin, "recv_rtcp_sink_0");
|
2022-02-26 20:09:04 +00:00
|
|
|
#endif
|
|
|
|
if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
|
|
|
|
if (error)
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
2022-04-06 08:30:34 +00:00
|
|
|
"Failed to link rtcpsrc to rtpbin");
|
2022-02-26 20:09:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
gst_object_unref (srcpad);
|
|
|
|
gst_object_unref (sinkpad);
|
2022-04-06 08:30:34 +00:00
|
|
|
|
|
|
|
#if GST_CHECK_VERSION (1, 20, 0)
|
2022-04-24 10:24:55 +00:00
|
|
|
srcpad = gst_element_request_pad_simple (self->rtpbin, "send_rtcp_src_0");
|
2022-04-06 08:30:34 +00:00
|
|
|
#else
|
2022-04-24 10:24:55 +00:00
|
|
|
srcpad = gst_element_get_request_pad (self->rtpbin, "send_rtcp_src_0");
|
2022-04-06 08:30:34 +00:00
|
|
|
#endif
|
2022-04-24 10:24:55 +00:00
|
|
|
sinkpad = gst_element_get_static_pad (self->rtcp_sink, "sink");
|
|
|
|
if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
|
|
|
|
if (error)
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
"Failed to link rtpbin to rtcpsink");
|
|
|
|
return FALSE;
|
|
|
|
}
|
2022-04-06 08:30:34 +00:00
|
|
|
|
|
|
|
/* can only link to depayloader after RTP payload has been verified */
|
|
|
|
g_signal_connect (self->rtpbin, "pad-added", G_CALLBACK (on_pad_added), self->depayloader);
|
2022-02-26 20:09:04 +00:00
|
|
|
|
2022-04-07 09:42:53 +00:00
|
|
|
/* request-encoder and request-decoder signals have been emitted after linking pads from rtpbin */
|
|
|
|
if (self->request_rtpbin_rtp_decoder_id)
|
|
|
|
g_signal_handler_disconnect (self->rtpbin, self->request_rtpbin_rtp_decoder_id);
|
|
|
|
|
|
|
|
if (self->request_rtpbin_rtp_encoder_id)
|
|
|
|
g_signal_handler_disconnect (self->rtpbin, self->request_rtpbin_rtp_encoder_id);
|
|
|
|
|
|
|
|
if (self->request_rtpbin_rtcp_decoder_id)
|
|
|
|
g_signal_handler_disconnect (self->rtpbin, self->request_rtpbin_rtcp_decoder_id);
|
|
|
|
|
|
|
|
if (self->request_rtpbin_rtcp_encoder_id)
|
|
|
|
g_signal_handler_disconnect (self->rtpbin, self->request_rtpbin_rtcp_encoder_id);
|
|
|
|
|
2022-02-26 20:09:04 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gboolean
|
2022-04-06 08:30:34 +00:00
|
|
|
pipeline_setup_codecs (CallsSipMediaPipeline *self,
|
|
|
|
MediaCodecInfo *codec,
|
|
|
|
GError **error)
|
2022-02-26 20:09:04 +00:00
|
|
|
{
|
|
|
|
g_autoptr (GstCaps) caps = NULL;
|
|
|
|
g_autofree char *caps_string = NULL;
|
|
|
|
|
|
|
|
g_assert (CALLS_IS_SIP_MEDIA_PIPELINE (self));
|
|
|
|
g_assert (codec);
|
|
|
|
|
|
|
|
MAKE_ELEMENT (decoder, codec->gst_decoder_name, "decoder");
|
|
|
|
MAKE_ELEMENT (depayloader, codec->gst_depayloader_name, "depayloader");
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
MAKE_ELEMENT (encoder, codec->gst_encoder_name, "encoder");
|
|
|
|
MAKE_ELEMENT (payloader, codec->gst_payloader_name, "payloader");
|
|
|
|
|
|
|
|
gst_bin_add_many (GST_BIN (self->pipeline),
|
|
|
|
self->depayloader, self->decoder,
|
|
|
|
self->payloader, self->encoder,
|
|
|
|
NULL);
|
2022-02-26 20:09:04 +00:00
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
if (!gst_element_link_many (self->audio_src, self->encoder, self->payloader, NULL)) {
|
|
|
|
if (error)
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
"Failed to link audiosrc encoder and payloader");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gst_element_link_many (self->depayloader, self->decoder, self->audio_sink, NULL)) {
|
2022-02-26 20:09:04 +00:00
|
|
|
if (error)
|
|
|
|
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
"Failed to link depayloader decoder and audiosink");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* UDP src capabilities */
|
2022-04-07 09:42:53 +00:00
|
|
|
caps_string = media_codec_get_gst_capabilities (codec, self->use_srtp);
|
2022-02-26 20:09:04 +00:00
|
|
|
g_debug ("Capabilities:\n%s", caps_string);
|
|
|
|
|
|
|
|
caps = gst_caps_from_string (caps_string);
|
|
|
|
|
|
|
|
/* set udp sinks and sources for RTP and RTCP */
|
|
|
|
g_object_set (self->rtp_src,
|
|
|
|
"caps", caps,
|
|
|
|
NULL);
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2022-02-28 12:09:26 +00:00
|
|
|
calls_sip_media_pipeline_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2021-02-17 22:48:33 +00:00
|
|
|
{
|
|
|
|
CallsSipMediaPipeline *self = CALLS_SIP_MEDIA_PIPELINE (object);
|
|
|
|
|
|
|
|
switch (property_id) {
|
|
|
|
case PROP_CODEC:
|
|
|
|
g_value_set_pointer (value, self->codec);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_REMOTE:
|
|
|
|
g_value_set_string (value, self->remote);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_RPORT_RTP:
|
|
|
|
g_value_set_uint (value, self->rport_rtp);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_RPORT_RTCP:
|
|
|
|
g_value_set_uint (value, self->rport_rtcp);
|
|
|
|
break;
|
|
|
|
|
2021-02-27 11:06:22 +00:00
|
|
|
case PROP_DEBUG:
|
|
|
|
g_value_set_boolean (value, self->debug);
|
|
|
|
break;
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
case PROP_STATE:
|
|
|
|
g_value_set_enum (value, calls_sip_media_pipeline_get_state (self));
|
|
|
|
break;
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2022-02-28 12:09:26 +00:00
|
|
|
calls_sip_media_pipeline_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
2021-02-17 22:48:33 +00:00
|
|
|
{
|
|
|
|
CallsSipMediaPipeline *self = CALLS_SIP_MEDIA_PIPELINE (object);
|
|
|
|
|
|
|
|
switch (property_id) {
|
|
|
|
case PROP_CODEC:
|
2022-02-27 11:49:18 +00:00
|
|
|
calls_sip_media_pipeline_set_codec (self, g_value_get_pointer (value));
|
2021-02-17 22:48:33 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_REMOTE:
|
|
|
|
g_free (self->remote);
|
|
|
|
self->remote = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_RPORT_RTP:
|
|
|
|
self->rport_rtp = g_value_get_uint (value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROP_RPORT_RTCP:
|
|
|
|
self->rport_rtcp = g_value_get_uint (value);
|
|
|
|
break;
|
|
|
|
|
2021-02-27 11:06:22 +00:00
|
|
|
case PROP_DEBUG:
|
|
|
|
self->debug = g_value_get_boolean (value);
|
|
|
|
break;
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-28 13:02:39 +00:00
|
|
|
static void
|
|
|
|
calls_sip_media_pipeline_constructed (GObject *object)
|
|
|
|
{
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
CallsSipMediaPipeline *self = CALLS_SIP_MEDIA_PIPELINE (object);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (calls_sip_media_pipeline_parent_class)->constructed (object);
|
|
|
|
|
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_INITIALIZING);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
if (!pipeline_init (self, &error)) {
|
|
|
|
g_warning ("Could not create pipeline: %s", error->message);
|
2022-02-28 13:02:39 +00:00
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_NEED_CODEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-01 16:43:21 +00:00
|
|
|
static void
|
2022-02-28 12:09:26 +00:00
|
|
|
calls_sip_media_pipeline_finalize (GObject *object)
|
2021-04-01 16:43:21 +00:00
|
|
|
{
|
|
|
|
CallsSipMediaPipeline *self = CALLS_SIP_MEDIA_PIPELINE (object);
|
|
|
|
|
|
|
|
calls_sip_media_pipeline_stop (self);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
gst_object_unref (self->pipeline);
|
|
|
|
gst_bus_remove_watch (self->bus);
|
|
|
|
gst_object_unref (self->bus);
|
2022-04-07 09:42:53 +00:00
|
|
|
gst_object_unref (self->srtpenc);
|
|
|
|
gst_object_unref (self->srtpdec);
|
2021-04-01 16:43:21 +00:00
|
|
|
|
|
|
|
g_free (self->remote);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (calls_sip_media_pipeline_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
static void
|
|
|
|
calls_sip_media_pipeline_class_init (CallsSipMediaPipelineClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2022-02-28 12:09:26 +00:00
|
|
|
object_class->set_property = calls_sip_media_pipeline_set_property;
|
2022-02-28 13:02:39 +00:00
|
|
|
object_class->constructed = calls_sip_media_pipeline_constructed;
|
2022-02-28 12:09:26 +00:00
|
|
|
object_class->get_property = calls_sip_media_pipeline_get_property;
|
|
|
|
object_class->finalize = calls_sip_media_pipeline_finalize;
|
2021-02-17 22:48:33 +00:00
|
|
|
|
|
|
|
/* Maybe we want to turn Codec into a GObject later */
|
|
|
|
props[PROP_CODEC] = g_param_spec_pointer ("codec",
|
|
|
|
"Codec",
|
|
|
|
"Media codec",
|
2022-02-27 11:49:18 +00:00
|
|
|
G_PARAM_READWRITE);
|
2021-02-17 22:48:33 +00:00
|
|
|
|
|
|
|
props[PROP_REMOTE] = g_param_spec_string ("remote",
|
|
|
|
"Remote",
|
|
|
|
"Remote host",
|
|
|
|
NULL,
|
|
|
|
G_PARAM_READWRITE);
|
|
|
|
|
|
|
|
props[PROP_RPORT_RTP] = g_param_spec_uint ("rport-rtp",
|
|
|
|
"rport-rtp",
|
|
|
|
"remote rtp port",
|
|
|
|
1025, 65535, 5002,
|
|
|
|
G_PARAM_READWRITE);
|
|
|
|
|
|
|
|
props[PROP_RPORT_RTCP] = g_param_spec_uint ("rport-rtcp",
|
|
|
|
"rport-rtcp",
|
|
|
|
"remote rtcp port",
|
|
|
|
1025, 65535, 5003,
|
|
|
|
G_PARAM_READWRITE);
|
|
|
|
|
2021-02-27 11:06:22 +00:00
|
|
|
props[PROP_DEBUG] = g_param_spec_boolean ("debug",
|
|
|
|
"Debug",
|
|
|
|
"Enable debugging information",
|
|
|
|
FALSE,
|
|
|
|
G_PARAM_READWRITE);
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
props[PROP_STATE] = g_param_spec_enum ("state",
|
|
|
|
"State",
|
|
|
|
"The state of the media pipeline",
|
|
|
|
CALLS_TYPE_MEDIA_PIPELINE_STATE,
|
|
|
|
CALLS_MEDIA_PIPELINE_STATE_UNKNOWN,
|
|
|
|
G_PARAM_READABLE);
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
|
2022-02-28 10:18:36 +00:00
|
|
|
|
|
|
|
signals[SENDING_STARTED] =
|
|
|
|
g_signal_new ("sending-started",
|
|
|
|
G_TYPE_FROM_CLASS (klass),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0, NULL, NULL, NULL,
|
|
|
|
G_TYPE_NONE, 0);
|
2021-02-17 22:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-12 11:28:10 +00:00
|
|
|
static void
|
|
|
|
on_dump_data_written (GObject *source_object,
|
|
|
|
GAsyncResult *res,
|
|
|
|
gpointer userdata)
|
|
|
|
{
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
g_autofree char *dot_data = userdata;
|
|
|
|
GOutputStream *stream = G_OUTPUT_STREAM (source_object);
|
|
|
|
gsize n_expected = GPOINTER_TO_SIZE (dot_data);
|
|
|
|
gsize n_written;
|
|
|
|
|
|
|
|
n_written = g_output_stream_write_finish (stream, res, &error);
|
|
|
|
if (n_written != n_expected) {
|
|
|
|
/* this is not an error
|
|
|
|
* TODO write the rest
|
|
|
|
* note: maybe easier with GBytes because of keeping track of
|
|
|
|
* dot_data reference ? */
|
|
|
|
g_warning ("Expected to write %" G_GSIZE_FORMAT
|
|
|
|
" but only wrote %" G_GSIZE_FORMAT " bytes",
|
|
|
|
n_expected,
|
|
|
|
n_written);
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
g_warning ("Error while trying to dump dot graph to file: %s",
|
|
|
|
error->message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_dump_file_created (GObject *source_object,
|
|
|
|
GAsyncResult *res,
|
|
|
|
gpointer userdata)
|
|
|
|
{
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
g_autoptr (GFileOutputStream) stream = NULL;
|
|
|
|
GFile *file = G_FILE (source_object);
|
|
|
|
char *dot_data = userdata;
|
|
|
|
|
|
|
|
|
|
|
|
stream = g_file_create_finish (G_FILE (source_object), res, &error);
|
|
|
|
if (!stream) {
|
|
|
|
g_autofree char *path = g_file_get_path (file);
|
|
|
|
|
|
|
|
if (error->code == G_IO_ERROR_EXISTS) {
|
|
|
|
/* we could potentially also g_file_replace() here,
|
|
|
|
* but no immediate need to bother right now */
|
|
|
|
}
|
|
|
|
|
|
|
|
g_warning ("Cannot create file %s: %s", path, error->message);
|
|
|
|
|
|
|
|
g_free (dot_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_output_stream_write_async (G_OUTPUT_STREAM (stream),
|
|
|
|
dot_data,
|
|
|
|
strlen (dot_data),
|
|
|
|
G_PRIORITY_DEFAULT,
|
|
|
|
NULL,
|
|
|
|
on_dump_data_written,
|
|
|
|
dot_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* we need dump_pipeline_graph_to_path() because both
|
|
|
|
* GST_DEBUG_BIN_TO_DOT_FILE*() and gst_debug_bin_to_dot_file*()
|
|
|
|
* require GStreamer being initialized with environment variable
|
|
|
|
* GST_DEBUG_DOT_DIR set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
dump_pipeline_graph_to_path (GstBin *bin,
|
|
|
|
const char *full_path)
|
|
|
|
{
|
2022-12-18 10:19:21 +00:00
|
|
|
#ifdef CALLS_GST_DEBUG
|
2022-10-12 11:28:10 +00:00
|
|
|
g_autoptr (GFile) file = NULL;
|
|
|
|
char *dot_data;
|
|
|
|
|
|
|
|
g_print ("Dumping pipeline graph to '%s'", full_path);
|
|
|
|
|
|
|
|
dot_data = gst_debug_bin_to_dot_data (bin,
|
|
|
|
GST_DEBUG_GRAPH_SHOW_VERBOSE);
|
|
|
|
|
|
|
|
file = g_file_new_for_path (full_path);
|
|
|
|
|
|
|
|
/* try creating, and if it fails with IO_ERROR_EXISTS try replacing the file */
|
|
|
|
g_file_create_async (file,
|
|
|
|
G_PRIORITY_DEFAULT,
|
|
|
|
G_FILE_CREATE_PRIVATE,
|
|
|
|
NULL,
|
|
|
|
on_dump_file_created,
|
|
|
|
dot_data);
|
2022-12-18 10:19:21 +00:00
|
|
|
#endif
|
2022-10-12 11:28:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-07 16:56:24 +00:00
|
|
|
static gboolean
|
|
|
|
usr2_handler (CallsSipMediaPipeline *self)
|
|
|
|
{
|
2022-10-12 11:28:10 +00:00
|
|
|
g_autofree char *tmp_dir = NULL;
|
|
|
|
g_autofree char *file_path = NULL;
|
|
|
|
|
2022-04-07 16:56:24 +00:00
|
|
|
g_print ("playing: %d\n"
|
|
|
|
"paused: %d\n"
|
|
|
|
"stopped: %d\n"
|
|
|
|
"target map: %d\n"
|
|
|
|
"current state: %d\n",
|
|
|
|
self->element_map_playing,
|
|
|
|
self->element_map_paused,
|
|
|
|
self->element_map_stopped,
|
2022-04-07 09:42:53 +00:00
|
|
|
self->use_srtp ? EL_ALL_SRTP : EL_ALL_RTP,
|
2022-04-07 16:56:24 +00:00
|
|
|
self->state);
|
|
|
|
|
2022-10-12 11:28:10 +00:00
|
|
|
/* TODO once we require GLib >= 2.74
|
|
|
|
* we can open a temp file more easily with g_file_new_tmp_async/finish()
|
|
|
|
|
|
|
|
g_file_new_tmp_async ("calls-pipeline-XXXXXX",
|
|
|
|
G_PRIORITY_DEFAULT,
|
|
|
|
NULL,
|
|
|
|
on_dump_file_created,
|
|
|
|
dot_data);
|
|
|
|
*/
|
|
|
|
tmp_dir = g_mkdtemp ("calls-pipeline-XXXXXX");
|
|
|
|
file_path = g_strconcat (tmp_dir, G_DIR_SEPARATOR_S, "usr2-debug.dot", NULL);
|
|
|
|
|
|
|
|
dump_pipeline_graph_to_path (GST_BIN (self->pipeline), file_path);
|
2022-04-07 16:56:24 +00:00
|
|
|
|
|
|
|
return G_SOURCE_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
static void
|
|
|
|
calls_sip_media_pipeline_init (CallsSipMediaPipeline *self)
|
|
|
|
{
|
2022-03-01 11:51:11 +00:00
|
|
|
if (!gst_is_initialized ())
|
|
|
|
gst_init (NULL, NULL);
|
2022-04-07 16:56:24 +00:00
|
|
|
|
|
|
|
/* Pipeline debugging */
|
|
|
|
g_unix_signal_add (SIGUSR2,
|
|
|
|
(GSourceFunc) usr2_handler,
|
|
|
|
self);
|
2021-02-17 22:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CallsSipMediaPipeline*
|
|
|
|
calls_sip_media_pipeline_new (MediaCodecInfo *codec)
|
|
|
|
{
|
|
|
|
CallsSipMediaPipeline *pipeline;
|
|
|
|
|
2022-02-28 13:02:39 +00:00
|
|
|
pipeline = g_object_new (CALLS_TYPE_SIP_MEDIA_PIPELINE, NULL);
|
2022-02-27 11:49:18 +00:00
|
|
|
|
2022-02-28 13:02:39 +00:00
|
|
|
if (codec)
|
|
|
|
g_object_set (pipeline, "codec", codec, NULL);
|
2021-02-17 22:48:33 +00:00
|
|
|
|
|
|
|
return pipeline;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-02-27 11:49:18 +00:00
|
|
|
void
|
|
|
|
calls_sip_media_pipeline_set_codec (CallsSipMediaPipeline *self,
|
|
|
|
MediaCodecInfo *codec)
|
|
|
|
{
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
|
|
|
|
g_return_if_fail (CALLS_IS_SIP_MEDIA_PIPELINE (self));
|
|
|
|
g_return_if_fail (codec);
|
|
|
|
|
|
|
|
if (self->codec == codec)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (self->codec) {
|
|
|
|
g_warning ("Cannot change codec of a pipeline. Use a new pipeline instead.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-28 07:39:47 +00:00
|
|
|
if (!media_codec_available_in_gst (codec)) {
|
|
|
|
g_warning ("Cannot setup pipeline with codec '%s' because it's not available in GStreamer",
|
|
|
|
codec->name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
if (!pipeline_setup_codecs (self, codec, &error)) {
|
|
|
|
g_warning ("Error trying to setup codecs for pipeline: %s",
|
2022-02-27 11:49:18 +00:00
|
|
|
error->message);
|
2022-02-28 10:18:36 +00:00
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_ERROR);
|
2022-02-27 11:49:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
if (!pipeline_link_elements (self, &error)) {
|
|
|
|
g_warning ("Not all pads could be linked: %s",
|
2022-02-27 11:49:18 +00:00
|
|
|
error->message);
|
2022-02-28 10:18:36 +00:00
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_ERROR);
|
2022-02-27 11:49:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->codec = codec;
|
|
|
|
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_CODEC]);
|
2022-02-28 10:18:36 +00:00
|
|
|
|
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_READY);
|
2022-02-27 11:49:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
|
2022-04-25 07:47:24 +00:00
|
|
|
void
|
|
|
|
calls_sip_media_pipeline_set_crypto (CallsSipMediaPipeline *self,
|
|
|
|
calls_srtp_crypto_attribute *crypto_own,
|
|
|
|
calls_srtp_crypto_attribute *crypto_theirs)
|
|
|
|
{
|
|
|
|
guchar *key_salt = NULL;
|
|
|
|
gsize key_salt_length;
|
|
|
|
GstSrtpCipherType srtp_cipher;
|
|
|
|
GstSrtpAuthType srtp_auth;
|
|
|
|
GstSrtpCipherType srtcp_cipher;
|
|
|
|
GstSrtpAuthType srtcp_auth;
|
|
|
|
|
|
|
|
g_autoptr (GstBuffer) key_buffer = NULL;
|
|
|
|
|
|
|
|
g_return_if_fail (CALLS_IS_SIP_MEDIA_PIPELINE (self));
|
|
|
|
g_return_if_fail (crypto_own);
|
|
|
|
g_return_if_fail (crypto_theirs);
|
|
|
|
g_return_if_fail (crypto_own->crypto_suite == crypto_theirs->crypto_suite);
|
|
|
|
g_return_if_fail (crypto_own->tag == crypto_theirs->tag);
|
|
|
|
|
|
|
|
if (self->use_srtp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
self->use_srtp = TRUE;
|
|
|
|
self->crypto_own = crypto_own;
|
|
|
|
self->crypto_theirs = crypto_theirs;
|
|
|
|
|
|
|
|
if (!calls_srtp_crypto_get_srtpenc_params (crypto_own,
|
|
|
|
&srtp_cipher,
|
|
|
|
&srtp_auth,
|
|
|
|
&srtcp_cipher,
|
|
|
|
&srtcp_auth)) {
|
|
|
|
g_autofree char *attr_str =
|
|
|
|
calls_srtp_print_sdp_crypto_attribute (crypto_own, NULL);
|
|
|
|
g_warning ("Could not get srtpenc parameters from attribute: %s", attr_str);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO MKI stuff */
|
|
|
|
|
|
|
|
key_salt = g_base64_decode (crypto_own->key_params[0].b64_keysalt,
|
|
|
|
&key_salt_length);
|
|
|
|
key_buffer = gst_buffer_new_wrapped (key_salt, key_salt_length);
|
|
|
|
|
|
|
|
g_object_set (self->srtpenc,
|
|
|
|
"key", key_buffer,
|
|
|
|
"rtp-cipher", srtp_cipher,
|
|
|
|
"rtp-auth", srtp_auth,
|
|
|
|
"rtcp-cipher", srtcp_cipher,
|
|
|
|
"rtcp-auth", srtcp_auth,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-27 11:06:22 +00:00
|
|
|
static void
|
|
|
|
diagnose_used_ports_in_socket (GSocket *socket)
|
|
|
|
{
|
|
|
|
g_autoptr (GSocketAddress) local_addr = NULL;
|
|
|
|
g_autoptr (GSocketAddress) remote_addr = NULL;
|
|
|
|
guint16 local_port;
|
|
|
|
guint16 remote_port;
|
|
|
|
|
|
|
|
local_addr = g_socket_get_local_address (socket, NULL);
|
|
|
|
remote_addr = g_socket_get_remote_address (socket, NULL);
|
|
|
|
if (!local_addr) {
|
|
|
|
g_warning ("Could not get local address of socket");
|
|
|
|
return;
|
|
|
|
}
|
2021-03-09 22:58:51 +00:00
|
|
|
g_assert (G_IS_INET_SOCKET_ADDRESS (local_addr));
|
2021-02-27 11:06:22 +00:00
|
|
|
|
|
|
|
local_port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (local_addr));
|
|
|
|
g_debug ("Using local port %d", local_port);
|
|
|
|
|
|
|
|
if (!remote_addr) {
|
|
|
|
g_warning ("Could not get remote address of socket");
|
|
|
|
return;
|
|
|
|
}
|
2021-03-09 22:58:51 +00:00
|
|
|
g_assert (G_IS_INET_SOCKET_ADDRESS (remote_addr));
|
2021-02-27 11:06:22 +00:00
|
|
|
|
|
|
|
remote_port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (remote_addr));
|
|
|
|
g_debug ("Using remote port %d", remote_port);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
diagnose_ports_in_use (CallsSipMediaPipeline *self)
|
|
|
|
{
|
|
|
|
GSocket *socket_in;
|
|
|
|
GSocket *socket_out;
|
|
|
|
gboolean same_socket = FALSE;
|
|
|
|
|
2021-03-09 22:58:51 +00:00
|
|
|
g_assert (CALLS_IS_SIP_MEDIA_PIPELINE (self));
|
2022-02-28 10:18:36 +00:00
|
|
|
|
|
|
|
if (self->state != CALLS_MEDIA_PIPELINE_STATE_PLAYING &&
|
|
|
|
self->state != CALLS_MEDIA_PIPELINE_STATE_PAUSED) {
|
|
|
|
g_warning ("Cannot diagnose ports when pipeline is not active");
|
|
|
|
return;
|
|
|
|
}
|
2021-02-27 11:06:22 +00:00
|
|
|
|
|
|
|
g_object_get (self->rtp_src, "used-socket", &socket_in, NULL);
|
|
|
|
g_object_get (self->rtp_sink, "used-socket", &socket_out, NULL);
|
|
|
|
|
|
|
|
if (socket_in == NULL || socket_out == NULL) {
|
|
|
|
g_warning ("Could not get used socket");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
same_socket = socket_in == socket_out;
|
|
|
|
|
|
|
|
if (same_socket) {
|
|
|
|
g_debug ("Diagnosing bidirectional socket...");
|
|
|
|
diagnose_used_ports_in_socket (socket_in);
|
2022-04-24 10:24:55 +00:00
|
|
|
} else {
|
2021-02-27 11:06:22 +00:00
|
|
|
g_debug ("Diagnosing server socket...");
|
|
|
|
diagnose_used_ports_in_socket (socket_in);
|
|
|
|
g_debug ("Diagnosing client socket...");
|
|
|
|
diagnose_used_ports_in_socket (socket_out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
void
|
|
|
|
calls_sip_media_pipeline_start (CallsSipMediaPipeline *self)
|
|
|
|
{
|
|
|
|
g_return_if_fail (CALLS_IS_SIP_MEDIA_PIPELINE (self));
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
if (self->state != CALLS_MEDIA_PIPELINE_STATE_READY) {
|
|
|
|
g_warning ("Cannot start pipeline because it's not ready");
|
2022-02-28 08:47:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
g_debug ("Starting media pipeline");
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
g_debug ("RTP/RTCP port before starting pipeline: %d/%d",
|
|
|
|
calls_sip_media_pipeline_get_rtp_port (self),
|
|
|
|
calls_sip_media_pipeline_get_rtcp_port (self));
|
|
|
|
|
2022-04-22 15:46:50 +00:00
|
|
|
/* unlock the state of our udp sources, see setup_socket_reuse() */
|
|
|
|
gst_element_set_locked_state (self->rtp_src, FALSE);
|
|
|
|
gst_element_set_locked_state (self->rtcp_src, FALSE);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
gst_element_set_state (self->pipeline, GST_STATE_PLAYING);
|
2021-02-27 11:06:22 +00:00
|
|
|
|
2022-02-28 21:04:23 +00:00
|
|
|
g_debug ("RTP/RTCP port after starting pipeline: %d/%d",
|
|
|
|
calls_sip_media_pipeline_get_rtp_port (self),
|
|
|
|
calls_sip_media_pipeline_get_rtcp_port (self));
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_PLAY_PENDING);
|
|
|
|
|
2021-02-27 11:06:22 +00:00
|
|
|
if (self->debug)
|
|
|
|
diagnose_ports_in_use (self);
|
2021-02-17 22:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
calls_sip_media_pipeline_stop (CallsSipMediaPipeline *self)
|
|
|
|
{
|
|
|
|
g_return_if_fail (CALLS_IS_SIP_MEDIA_PIPELINE (self));
|
|
|
|
|
|
|
|
g_debug ("Stopping media pipeline");
|
|
|
|
|
2022-04-22 15:46:50 +00:00
|
|
|
gst_element_set_locked_state (self->rtp_src, FALSE);
|
|
|
|
gst_element_set_locked_state (self->rtcp_src, FALSE);
|
|
|
|
gst_element_set_locked_state (self->rtp_sink, FALSE);
|
|
|
|
gst_element_set_locked_state (self->rtcp_sink, FALSE);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
gst_element_set_state (self->pipeline, GST_STATE_NULL);
|
2022-02-28 10:18:36 +00:00
|
|
|
|
|
|
|
set_state (self, CALLS_MEDIA_PIPELINE_STATE_STOP_PENDING);
|
2021-02-17 22:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2022-02-28 10:24:33 +00:00
|
|
|
calls_sip_media_pipeline_pause (CallsSipMediaPipeline *self,
|
|
|
|
gboolean pause)
|
2021-02-17 22:48:33 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (CALLS_IS_SIP_MEDIA_PIPELINE (self));
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
if (pause &&
|
|
|
|
(self->state == CALLS_MEDIA_PIPELINE_STATE_PAUSED ||
|
|
|
|
self->state == CALLS_MEDIA_PIPELINE_STATE_PAUSE_PENDING))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!pause &&
|
|
|
|
(self->state == CALLS_MEDIA_PIPELINE_STATE_PLAYING ||
|
|
|
|
self->state == CALLS_MEDIA_PIPELINE_STATE_PLAY_PENDING))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (self->state != CALLS_MEDIA_PIPELINE_STATE_PLAYING &&
|
|
|
|
self->state != CALLS_MEDIA_PIPELINE_STATE_PLAY_PENDING &&
|
|
|
|
self->state != CALLS_MEDIA_PIPELINE_STATE_PAUSED &&
|
|
|
|
self->state != CALLS_MEDIA_PIPELINE_STATE_PAUSE_PENDING) {
|
|
|
|
g_warning ("Cannot pause or unpause pipeline because it's not currently active");
|
2022-02-28 10:24:33 +00:00
|
|
|
return;
|
2022-02-28 10:18:36 +00:00
|
|
|
}
|
2022-02-28 10:24:33 +00:00
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
g_debug ("%s media pipeline", pause ?
|
2022-02-28 10:24:33 +00:00
|
|
|
"Pausing" :
|
|
|
|
"Unpausing");
|
2022-02-28 10:18:36 +00:00
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
|
2022-04-22 15:46:50 +00:00
|
|
|
/* leave udpsrc running to prevent timeouts */
|
|
|
|
gst_element_set_locked_state (self->rtp_src, pause);
|
|
|
|
gst_element_set_locked_state (self->rtcp_src, pause);
|
|
|
|
gst_element_set_locked_state (self->rtp_sink, pause);
|
|
|
|
gst_element_set_locked_state (self->rtcp_sink, pause);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
gst_element_set_state (self->pipeline, pause ?
|
2022-02-28 10:24:33 +00:00
|
|
|
GST_STATE_PAUSED :
|
|
|
|
GST_STATE_PLAYING);
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
set_state (self, pause ?
|
|
|
|
CALLS_MEDIA_PIPELINE_STATE_PAUSE_PENDING :
|
|
|
|
CALLS_MEDIA_PIPELINE_STATE_PLAY_PENDING);
|
2021-02-17 22:48:33 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 20:09:04 +00:00
|
|
|
|
2022-02-28 08:49:43 +00:00
|
|
|
int
|
|
|
|
calls_sip_media_pipeline_get_rtp_port (CallsSipMediaPipeline *self)
|
|
|
|
{
|
|
|
|
int port;
|
|
|
|
|
|
|
|
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_PIPELINE (self), 0);
|
|
|
|
|
|
|
|
g_object_get (self->rtp_src, "port", &port, NULL);
|
|
|
|
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
calls_sip_media_pipeline_get_rtcp_port (CallsSipMediaPipeline *self)
|
|
|
|
{
|
|
|
|
int port;
|
|
|
|
|
|
|
|
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_PIPELINE (self), 0);
|
|
|
|
|
2022-04-06 08:30:34 +00:00
|
|
|
g_object_get (self->rtcp_src, "port", &port, NULL);
|
2022-02-28 08:49:43 +00:00
|
|
|
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:18:36 +00:00
|
|
|
|
|
|
|
CallsMediaPipelineState
|
|
|
|
calls_sip_media_pipeline_get_state (CallsSipMediaPipeline *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_PIPELINE (self),
|
|
|
|
CALLS_MEDIA_PIPELINE_STATE_UNKNOWN);
|
|
|
|
|
|
|
|
return self->state;
|
|
|
|
}
|