2021-02-17 22:48:33 +00:00
|
|
|
/*
|
2022-02-28 10:37:26 +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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-04-09 15:41:14 +00:00
|
|
|
#define G_LOG_DOMAIN "CallsSipMediaManager"
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2021-11-23 14:04:18 +00:00
|
|
|
#include "calls-settings.h"
|
2021-02-17 22:48:33 +00:00
|
|
|
#include "calls-sip-media-manager.h"
|
2022-02-28 10:37:26 +00:00
|
|
|
#include "calls-sip-media-pipeline.h"
|
2022-05-03 10:11:24 +00:00
|
|
|
#include "calls-srtp-utils.h"
|
2021-04-10 22:25:13 +00:00
|
|
|
#include "gst-rfc3551.h"
|
2023-01-21 15:42:39 +00:00
|
|
|
#include "calls-util.h"
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2022-02-28 10:37:26 +00:00
|
|
|
#include <gio/gio.h>
|
2021-02-17 22:48:33 +00:00
|
|
|
#include <gst/gst.h>
|
|
|
|
|
2022-02-25 07:35:41 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
|
2021-04-10 22:25:13 +00:00
|
|
|
/**
|
|
|
|
* SECTION:sip-media-manager
|
|
|
|
* @short_description: The media manager singleton
|
|
|
|
* @Title: CallsSipMediaManager
|
|
|
|
*
|
|
|
|
* #CallsSipMediaManager is mainly responsible for generating appropriate
|
2022-02-28 10:37:26 +00:00
|
|
|
* SDP messages for the set of supported codecs. It also holds a list of
|
|
|
|
* #CallsSipMediaPipeline objects that are ready to be used.
|
2021-04-10 22:25:13 +00:00
|
|
|
*/
|
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
typedef struct _CallsSipMediaManager {
|
|
|
|
GObject parent;
|
2021-03-31 07:46:44 +00:00
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
CallsSettings *settings;
|
|
|
|
GList *preferred_codecs;
|
|
|
|
GListStore *pipelines;
|
2021-02-17 22:48:33 +00:00
|
|
|
} CallsSipMediaManager;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (CallsSipMediaManager, calls_sip_media_manager, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
|
2021-11-23 14:04:18 +00:00
|
|
|
static void
|
|
|
|
on_notify_preferred_audio_codecs (CallsSipMediaManager *self)
|
|
|
|
{
|
|
|
|
GList *supported_codecs;
|
2022-04-24 10:24:55 +00:00
|
|
|
|
2021-11-23 14:04:18 +00:00
|
|
|
g_auto (GStrv) settings_codec_preference = NULL;
|
|
|
|
|
|
|
|
g_assert (CALLS_IS_SIP_MEDIA_MANAGER (self));
|
|
|
|
|
|
|
|
g_clear_list (&self->preferred_codecs, NULL);
|
|
|
|
supported_codecs = media_codecs_get_candidates ();
|
|
|
|
|
|
|
|
if (!supported_codecs) {
|
|
|
|
g_warning ("There aren't any supported codecs installed on your system");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
settings_codec_preference = calls_settings_get_preferred_audio_codecs (self->settings);
|
|
|
|
|
|
|
|
if (!settings_codec_preference) {
|
|
|
|
g_debug ("No audio codec preference set. Using all supported codecs");
|
|
|
|
self->preferred_codecs = supported_codecs;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (guint i = 0; settings_codec_preference[i] != NULL; i++) {
|
|
|
|
MediaCodecInfo *codec = media_codec_by_name (settings_codec_preference[i]);
|
|
|
|
|
|
|
|
if (!codec) {
|
|
|
|
g_debug ("Did not find audio codec %s", settings_codec_preference[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (media_codec_available_in_gst (codec))
|
|
|
|
self->preferred_codecs = g_list_append (self->preferred_codecs, codec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!self->preferred_codecs) {
|
|
|
|
g_warning ("Cannot satisfy audio codec preference, "
|
|
|
|
"falling back to all supported codecs");
|
|
|
|
self->preferred_codecs = supported_codecs;
|
|
|
|
} else {
|
|
|
|
g_list_free (supported_codecs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 07:33:38 +00:00
|
|
|
|
2022-02-28 10:37:26 +00:00
|
|
|
static void
|
|
|
|
add_new_pipeline (CallsSipMediaManager *self)
|
|
|
|
{
|
|
|
|
CallsSipMediaPipeline *pipeline;
|
|
|
|
|
|
|
|
g_assert (CALLS_IS_SIP_MEDIA_MANAGER (self));
|
|
|
|
|
|
|
|
pipeline = calls_sip_media_pipeline_new (NULL);
|
|
|
|
g_list_store_append (self->pipelines, pipeline);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
static void
|
|
|
|
calls_sip_media_manager_finalize (GObject *object)
|
|
|
|
{
|
2021-09-24 07:33:38 +00:00
|
|
|
CallsSipMediaManager *self = CALLS_SIP_MEDIA_MANAGER (object);
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2021-11-23 14:04:18 +00:00
|
|
|
g_list_free (self->preferred_codecs);
|
2022-02-28 10:37:26 +00:00
|
|
|
g_object_unref (self->pipelines);
|
2021-03-31 07:46:44 +00:00
|
|
|
|
2021-02-17 22:48:33 +00:00
|
|
|
G_OBJECT_CLASS (calls_sip_media_manager_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
calls_sip_media_manager_class_init (CallsSipMediaManagerClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = calls_sip_media_manager_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
calls_sip_media_manager_init (CallsSipMediaManager *self)
|
|
|
|
{
|
2022-04-24 10:24:55 +00:00
|
|
|
if (!gst_is_initialized ())
|
2022-03-01 11:51:40 +00:00
|
|
|
gst_init (NULL, NULL);
|
2021-03-31 07:46:44 +00:00
|
|
|
|
2022-05-09 20:57:41 +00:00
|
|
|
self->settings = calls_settings_get_default ();
|
2021-11-23 14:04:18 +00:00
|
|
|
g_signal_connect_swapped (self->settings,
|
|
|
|
"notify::preferred-audio-codecs",
|
|
|
|
G_CALLBACK (on_notify_preferred_audio_codecs),
|
|
|
|
self);
|
|
|
|
on_notify_preferred_audio_codecs (self);
|
2021-11-22 23:58:08 +00:00
|
|
|
|
2022-02-28 10:37:26 +00:00
|
|
|
self->pipelines = g_list_store_new (CALLS_TYPE_SIP_MEDIA_PIPELINE);
|
|
|
|
|
|
|
|
add_new_pipeline (self);
|
2021-02-17 22:48:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Public functions */
|
|
|
|
|
|
|
|
CallsSipMediaManager *
|
2021-06-02 18:18:50 +00:00
|
|
|
calls_sip_media_manager_default (void)
|
2021-02-17 22:48:33 +00:00
|
|
|
{
|
|
|
|
static CallsSipMediaManager *instance = NULL;
|
|
|
|
|
|
|
|
if (instance == NULL) {
|
|
|
|
g_debug ("Creating CallsSipMediaManager");
|
|
|
|
instance = g_object_new (CALLS_TYPE_SIP_MEDIA_MANAGER, NULL);
|
|
|
|
g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *) &instance);
|
|
|
|
}
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-10 06:54:20 +00:00
|
|
|
/* helper for calls_sip_media_manager_get_capabilities() */
|
|
|
|
static char *
|
|
|
|
get_connection_line (const char *ip)
|
|
|
|
{
|
|
|
|
int af_family;
|
|
|
|
|
|
|
|
if (STR_IS_NULL_OR_EMPTY(ip))
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
af_family = get_address_family_for_ip (ip, TRUE);
|
|
|
|
if (af_family == AF_UNSPEC)
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
return g_strdup_printf ("c=IN %s %s\r\n",
|
|
|
|
af_family == AF_INET ? "IP4" : "IP6",
|
|
|
|
ip);
|
|
|
|
|
|
|
|
invalid:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-08 14:43:13 +00:00
|
|
|
/* calls_sip_media_manager_get_capabilities:
|
2021-02-17 22:48:33 +00:00
|
|
|
*
|
2021-04-08 14:43:13 +00:00
|
|
|
* @self: A #CallsSipMediaManager
|
2021-02-17 22:48:33 +00:00
|
|
|
* @port: Should eventually come from the ICE stack
|
2022-05-03 10:11:24 +00:00
|
|
|
* @crypto_attributes: A #GList of #calls_srtp_crypto_attribute
|
2021-04-08 14:43:13 +00:00
|
|
|
* @supported_codecs: A #GList of #MediaCodecInfo
|
2021-02-17 22:48:33 +00:00
|
|
|
*
|
2021-04-26 08:29:24 +00:00
|
|
|
* Returns: (transfer full): string describing capabilities
|
2021-02-17 22:48:33 +00:00
|
|
|
* to be used in the session description (SDP)
|
|
|
|
*/
|
|
|
|
char *
|
2021-04-08 14:43:13 +00:00
|
|
|
calls_sip_media_manager_get_capabilities (CallsSipMediaManager *self,
|
2021-11-22 23:58:08 +00:00
|
|
|
const char *own_ip,
|
2022-02-28 10:38:06 +00:00
|
|
|
gint rtp_port,
|
|
|
|
gint rtcp_port,
|
2022-05-03 10:11:24 +00:00
|
|
|
GList *crypto_attributes,
|
2021-04-08 14:43:13 +00:00
|
|
|
GList *supported_codecs)
|
2021-02-17 22:48:33 +00:00
|
|
|
{
|
2022-05-03 10:11:24 +00:00
|
|
|
char *payload_type = crypto_attributes ? "SAVP" : "AVP";
|
2022-04-24 10:24:55 +00:00
|
|
|
|
2021-03-31 07:46:44 +00:00
|
|
|
g_autoptr (GString) media_line = NULL;
|
|
|
|
g_autoptr (GString) attribute_lines = NULL;
|
2022-10-10 06:54:20 +00:00
|
|
|
g_autofree char *connection_line = NULL;
|
2021-03-31 07:46:44 +00:00
|
|
|
GList *node;
|
2021-02-17 22:48:33 +00:00
|
|
|
|
|
|
|
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_MANAGER (self), NULL);
|
|
|
|
|
2021-03-31 07:46:44 +00:00
|
|
|
media_line = g_string_new (NULL);
|
|
|
|
attribute_lines = g_string_new (NULL);
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2021-04-08 14:43:13 +00:00
|
|
|
if (supported_codecs == NULL) {
|
2021-03-31 07:46:44 +00:00
|
|
|
g_warning ("No supported codecs found. Can't build meaningful SDP message");
|
2021-04-09 15:41:32 +00:00
|
|
|
g_string_append_printf (media_line, "m=audio 0 RTP/AVP");
|
2021-03-31 07:46:44 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2021-03-31 07:46:44 +00:00
|
|
|
/* media lines look f.e like "audio 31337 RTP/AVP 9 8 0" */
|
|
|
|
g_string_append_printf (media_line,
|
2022-02-28 10:38:06 +00:00
|
|
|
"m=audio %d RTP/%s", rtp_port, payload_type);
|
2021-02-17 22:48:33 +00:00
|
|
|
|
2021-04-08 14:43:13 +00:00
|
|
|
for (node = supported_codecs; node != NULL; node = node->next) {
|
2021-03-31 07:46:44 +00:00
|
|
|
MediaCodecInfo *codec = node->data;
|
|
|
|
|
2021-04-07 21:53:34 +00:00
|
|
|
g_string_append_printf (media_line, " %u", codec->payload_id);
|
2021-03-31 07:46:44 +00:00
|
|
|
g_string_append_printf (attribute_lines,
|
2021-04-07 21:53:34 +00:00
|
|
|
"a=rtpmap:%u %s/%u%s",
|
2021-03-31 07:46:44 +00:00
|
|
|
codec->payload_id,
|
|
|
|
codec->name,
|
|
|
|
codec->clock_rate,
|
|
|
|
"\r\n");
|
|
|
|
}
|
|
|
|
|
2022-05-03 10:11:24 +00:00
|
|
|
for (node = crypto_attributes; node != NULL; node = node->next) {
|
|
|
|
calls_srtp_crypto_attribute *attr = node->data;
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
g_autofree char *crypto_line =
|
2022-10-10 06:54:20 +00:00
|
|
|
calls_srtp_print_sdp_crypto_attribute (attr, &error);
|
2022-05-03 10:11:24 +00:00
|
|
|
|
|
|
|
if (!crypto_line) {
|
|
|
|
g_warning ("Could not print SDP crypto line for tag %d: %s", attr->tag, error->message);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
g_string_append_printf (attribute_lines, "%s\r\n", crypto_line);
|
|
|
|
}
|
|
|
|
|
2022-02-28 10:38:06 +00:00
|
|
|
g_string_append_printf (attribute_lines, "a=rtcp:%d\r\n", rtcp_port);
|
2021-03-31 07:46:44 +00:00
|
|
|
|
2022-04-24 10:24:55 +00:00
|
|
|
done:
|
2022-10-10 06:54:20 +00:00
|
|
|
connection_line = get_connection_line (own_ip);
|
|
|
|
|
|
|
|
return g_strdup_printf ("v=0\r\n"
|
|
|
|
"%s"
|
|
|
|
"%s\r\n"
|
|
|
|
"%s\r\n",
|
|
|
|
connection_line ?: "",
|
|
|
|
media_line->str,
|
|
|
|
attribute_lines->str);
|
2021-02-17 22:48:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 16:15:24 +00:00
|
|
|
|
2021-04-08 14:43:13 +00:00
|
|
|
/* calls_sip_media_manager_static_capabilities:
|
|
|
|
*
|
|
|
|
* @self: A #CallsSipMediaManager
|
2022-02-28 10:38:06 +00:00
|
|
|
* @rtp_port: Port to use for RTP. Should eventually come from the ICE stack
|
|
|
|
* @rtcp_port: Port to use for RTCP.Should eventually come from the ICE stack
|
2022-05-03 10:11:24 +00:00
|
|
|
* @crypto_attributes: A #GList of #calls_srtp_crypto_attribute
|
2021-04-08 14:43:13 +00:00
|
|
|
*
|
2021-04-26 08:29:24 +00:00
|
|
|
* Returns: (transfer full): string describing capabilities
|
2021-04-08 14:43:13 +00:00
|
|
|
* to be used in the session description (SDP)
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
calls_sip_media_manager_static_capabilities (CallsSipMediaManager *self,
|
2021-11-22 23:58:08 +00:00
|
|
|
const char *own_ip,
|
2022-02-28 10:38:06 +00:00
|
|
|
gint rtp_port,
|
|
|
|
gint rtcp_port,
|
2022-05-03 10:11:24 +00:00
|
|
|
GList *crypto_attributes)
|
2021-04-08 14:43:13 +00:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_MANAGER (self), NULL);
|
|
|
|
|
|
|
|
return calls_sip_media_manager_get_capabilities (self,
|
2021-11-22 23:58:08 +00:00
|
|
|
own_ip,
|
2022-02-28 10:38:06 +00:00
|
|
|
rtp_port,
|
|
|
|
rtcp_port,
|
2022-05-03 10:11:24 +00:00
|
|
|
crypto_attributes,
|
2021-11-23 14:04:18 +00:00
|
|
|
self->preferred_codecs);
|
2021-04-08 14:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* calls_sip_media_manager_codec_candiates:
|
|
|
|
*
|
|
|
|
* @self: A #CallsSipMediaManager
|
|
|
|
*
|
2021-04-26 08:29:24 +00:00
|
|
|
* Returns: (transfer none): A #GList of supported
|
2021-04-08 14:43:13 +00:00
|
|
|
* #MediaCodecInfo
|
|
|
|
*/
|
|
|
|
GList *
|
|
|
|
calls_sip_media_manager_codec_candidates (CallsSipMediaManager *self)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_MANAGER (self), NULL);
|
|
|
|
|
2021-11-23 14:04:18 +00:00
|
|
|
return self->preferred_codecs;
|
2021-04-08 14:43:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* calls_sip_media_manager_get_codecs_from_sdp
|
|
|
|
*
|
|
|
|
* @self: A #CallsSipMediaManager
|
|
|
|
* @sdp: A #sdp_media_t media description
|
|
|
|
*
|
2021-09-17 13:48:49 +00:00
|
|
|
* Returns: (transfer container): A #GList of codecs found in the
|
2021-04-08 14:43:13 +00:00
|
|
|
* SDP message
|
|
|
|
*/
|
|
|
|
GList *
|
|
|
|
calls_sip_media_manager_get_codecs_from_sdp (CallsSipMediaManager *self,
|
|
|
|
sdp_media_t *sdp_media)
|
|
|
|
{
|
|
|
|
GList *codecs = NULL;
|
|
|
|
sdp_rtpmap_t *rtpmap = NULL;
|
|
|
|
|
|
|
|
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_MANAGER (self), NULL);
|
|
|
|
g_return_val_if_fail (sdp_media, NULL);
|
|
|
|
|
|
|
|
if (sdp_media->m_type != sdp_media_audio) {
|
|
|
|
g_warning ("Only the 'audio' media type is supported");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (rtpmap = sdp_media->m_rtpmaps; rtpmap != NULL; rtpmap = rtpmap->rm_next) {
|
|
|
|
MediaCodecInfo *codec = media_codec_by_payload_id (rtpmap->rm_pt);
|
|
|
|
if (codec)
|
|
|
|
codecs = g_list_append (codecs, codec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sdp_media->m_next != NULL)
|
|
|
|
g_warning ("Currently only a single media session is supported");
|
|
|
|
|
|
|
|
if (codecs == NULL)
|
|
|
|
g_warning ("Did not find any common codecs");
|
|
|
|
|
|
|
|
return codecs;
|
|
|
|
}
|
2021-09-24 07:33:38 +00:00
|
|
|
|
2022-02-28 10:37:26 +00:00
|
|
|
/**
|
|
|
|
* calls_sip_media_manager_get_pipeline:
|
|
|
|
* @self: A #CallsSipMediaManager
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): A #CallsSipMediaPipeline
|
|
|
|
*/
|
|
|
|
CallsSipMediaPipeline *
|
|
|
|
calls_sip_media_manager_get_pipeline (CallsSipMediaManager *self)
|
|
|
|
{
|
|
|
|
g_autoptr (CallsSipMediaPipeline) pipeline = NULL;
|
|
|
|
|
|
|
|
g_return_val_if_fail (CALLS_IS_SIP_MEDIA_MANAGER (self), NULL);
|
|
|
|
|
|
|
|
pipeline = g_list_model_get_item (G_LIST_MODEL (self->pipelines), 0);
|
|
|
|
|
|
|
|
g_list_store_remove (self->pipelines, 0);
|
2021-09-24 07:33:38 +00:00
|
|
|
|
2022-02-28 10:37:26 +00:00
|
|
|
/* add a pipeline for the one we just removed */
|
|
|
|
add_new_pipeline (self);
|
|
|
|
|
|
|
|
return pipeline;
|
|
|
|
}
|