aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorfiaxh <git@lightrise.org>2021-12-23 00:44:51 +0100
committerfiaxh <git@lightrise.org>2021-12-23 00:46:58 +0100
commitd02c5bc55d4325de308a592e8980139aa0634215 (patch)
tree9cb91f86056dd29f863bd8e54580cccc625af60e /plugins
parent1378224444b1862ac95783ac2bae7d25a0a8862d (diff)
parentf0c7dd0682fec8d72c644d8e54896de7bdc40ddb (diff)
downloaddino-d02c5bc55d4325de308a592e8980139aa0634215.tar.gz
dino-d02c5bc55d4325de308a592e8980139aa0634215.zip
Merge branch groupcalls
Diffstat (limited to 'plugins')
-rw-r--r--plugins/ice/src/dtls_srtp.vala86
-rw-r--r--plugins/ice/src/plugin.vala7
-rw-r--r--plugins/ice/src/transport_parameters.vala60
-rw-r--r--plugins/rtp/CMakeLists.txt4
-rw-r--r--plugins/rtp/src/codec_util.vala88
-rw-r--r--plugins/rtp/src/device.vala414
-rw-r--r--plugins/rtp/src/module.vala4
-rw-r--r--plugins/rtp/src/plugin.vala105
-rw-r--r--plugins/rtp/src/stream.vala457
-rw-r--r--plugins/rtp/src/video_widget.vala76
10 files changed, 883 insertions, 418 deletions
diff --git a/plugins/ice/src/dtls_srtp.vala b/plugins/ice/src/dtls_srtp.vala
index ca398e69..c5b2eb75 100644
--- a/plugins/ice/src/dtls_srtp.vala
+++ b/plugins/ice/src/dtls_srtp.vala
@@ -37,40 +37,30 @@ public class Handler {
this.own_fingerprint = creds.own_fingerprint;
}
- public uint8[]? process_incoming_data(uint component_id, uint8[] data) {
+ public uint8[]? process_incoming_data(uint component_id, uint8[] data) throws Crypto.Error {
if (srtp_session.has_decrypt) {
- try {
- if (component_id == 1) {
- if (data.length >= 2 && data[1] >= 192 && data[1] < 224) {
- return srtp_session.decrypt_rtcp(data);
- }
- return srtp_session.decrypt_rtp(data);
+ if (component_id == 1) {
+ if (data.length >= 2 && data[1] >= 192 && data[1] < 224) {
+ return srtp_session.decrypt_rtcp(data);
}
- if (component_id == 2) return srtp_session.decrypt_rtcp(data);
- } catch (Error e) {
- warning("%s (%d)", e.message, e.code);
- return null;
+ return srtp_session.decrypt_rtp(data);
}
+ if (component_id == 2) return srtp_session.decrypt_rtcp(data);
} else if (component_id == 1) {
on_data_rec(data);
}
return null;
}
- public uint8[]? process_outgoing_data(uint component_id, uint8[] data) {
+ public uint8[]? process_outgoing_data(uint component_id, uint8[] data) throws Crypto.Error {
if (srtp_session.has_encrypt) {
- try {
- if (component_id == 1) {
- if (data.length >= 2 && data[1] >= 192 && data[1] < 224) {
- return srtp_session.encrypt_rtcp(data);
- }
- return srtp_session.encrypt_rtp(data);
+ if (component_id == 1) {
+ if (data.length >= 2 && data[1] >= 192 && data[1] < 224) {
+ return srtp_session.encrypt_rtcp(data);
}
- if (component_id == 2) return srtp_session.encrypt_rtcp(data);
- } catch (Error e) {
- warning("%s (%d)", e.message, e.code);
- return null;
+ return srtp_session.encrypt_rtp(data);
}
+ if (component_id == 2) return srtp_session.encrypt_rtcp(data);
}
return null;
}
@@ -122,6 +112,19 @@ public class Handler {
}
public async Xmpp.Xep.Jingle.ContentEncryption? setup_dtls_connection() {
+ MainContext context = MainContext.current_source().get_context();
+ var thread = new Thread<Xmpp.Xep.Jingle.ContentEncryption>("dtls-connection", () => {
+ var res = setup_dtls_connection_thread();
+ Source source = new IdleSource();
+ source.set_callback(setup_dtls_connection.callback);
+ source.attach(context);
+ return res;
+ });
+ yield;
+ return thread.join();
+ }
+
+ private Xmpp.Xep.Jingle.ContentEncryption? setup_dtls_connection_thread() {
buffer_mutex.lock();
if (stop) {
restart = true;
@@ -156,28 +159,23 @@ public class Handler {
session.set_push_function(push_function);
session.set_verify_function(verify_function);
- Thread<int> thread = new Thread<int> (null, () => {
- DateTime maximum_time = new DateTime.now_utc().add_seconds(20);
- do {
- err = session.handshake();
+ DateTime maximum_time = new DateTime.now_utc().add_seconds(20);
+ do {
+ err = session.handshake();
+
+ DateTime current_time = new DateTime.now_utc();
+ if (maximum_time.compare(current_time) < 0) {
+ warning("DTLS handshake timeouted");
+ err = ErrorCode.APPLICATION_ERROR_MIN + 1;
+ break;
+ }
+ if (stop) {
+ debug("DTLS handshake stopped");
+ err = ErrorCode.APPLICATION_ERROR_MIN + 2;
+ break;
+ }
+ } while (err < 0 && !((ErrorCode)err).is_fatal());
- DateTime current_time = new DateTime.now_utc();
- if (maximum_time.compare(current_time) < 0) {
- warning("DTLS handshake timeouted");
- err = ErrorCode.APPLICATION_ERROR_MIN + 1;
- break;
- }
- if (stop) {
- debug("DTLS handshake stopped");
- err = ErrorCode.APPLICATION_ERROR_MIN + 2;
- break;
- }
- } while (err < 0 && !((ErrorCode)err).is_fatal());
- Idle.add(setup_dtls_connection.callback);
- return err;
- });
- yield;
- err = thread.join();
buffer_mutex.lock();
if (stop) {
stop = false;
@@ -186,7 +184,7 @@ public class Handler {
buffer_mutex.unlock();
if (restart) {
debug("Restarting DTLS handshake");
- return yield setup_dtls_connection();
+ return setup_dtls_connection_thread();
}
return null;
}
diff --git a/plugins/ice/src/plugin.vala b/plugins/ice/src/plugin.vala
index f145dd6d..4abf042c 100644
--- a/plugins/ice/src/plugin.vala
+++ b/plugins/ice/src/plugin.vala
@@ -15,7 +15,12 @@ public class Dino.Plugins.Ice.Plugin : RootInterface, Object {
list.add(new Module());
});
app.stream_interactor.stream_attached_modules.connect((account, stream) => {
- stream.get_module(Socks5Bytestreams.Module.IDENTITY).set_local_ip_address_handler(get_local_ip_addresses);
+ if (stream.get_module(Socks5Bytestreams.Module.IDENTITY) != null) {
+ stream.get_module(Socks5Bytestreams.Module.IDENTITY).set_local_ip_address_handler(get_local_ip_addresses);
+ }
+ if (stream.get_module(JingleRawUdp.Module.IDENTITY) != null) {
+ stream.get_module(JingleRawUdp.Module.IDENTITY).set_local_ip_address_handler(get_local_ip_addresses);
+ }
});
app.stream_interactor.stream_negotiated.connect(on_stream_negotiated);
}
diff --git a/plugins/ice/src/transport_parameters.vala b/plugins/ice/src/transport_parameters.vala
index 62c04906..f684e411 100644
--- a/plugins/ice/src/transport_parameters.vala
+++ b/plugins/ice/src/transport_parameters.vala
@@ -10,16 +10,14 @@ public class Dino.Plugins.Ice.TransportParameters : JingleIceUdp.IceUdpTransport
private bool remote_credentials_set;
private Map<uint8, DatagramConnection> connections = new HashMap<uint8, DatagramConnection>();
private DtlsSrtp.Handler? dtls_srtp_handler;
+ private MainContext thread_context;
+ private MainLoop thread_loop;
private class DatagramConnection : Jingle.DatagramConnection {
private Nice.Agent agent;
private DtlsSrtp.Handler? dtls_srtp_handler;
private uint stream_id;
private string? error;
- private ulong sent;
- private ulong sent_reported;
- private ulong recv;
- private ulong recv_reported;
private ulong datagram_received_id;
public DatagramConnection(Nice.Agent agent, DtlsSrtp.Handler? dtls_srtp_handler, uint stream_id, uint8 component_id) {
@@ -28,11 +26,7 @@ public class Dino.Plugins.Ice.TransportParameters : JingleIceUdp.IceUdpTransport
this.stream_id = stream_id;
this.component_id = component_id;
this.datagram_received_id = this.datagram_received.connect((datagram) => {
- recv += datagram.length;
- if (recv > recv_reported + 100000) {
- debug("Received %lu bytes via stream %u component %u", recv, stream_id, component_id);
- recv_reported = recv;
- }
+ bytes_received += datagram.length;
});
}
@@ -45,16 +39,22 @@ public class Dino.Plugins.Ice.TransportParameters : JingleIceUdp.IceUdpTransport
public override void send_datagram(Bytes datagram) {
if (this.agent != null && is_component_ready(agent, stream_id, component_id)) {
- uint8[] encrypted_data = null;
- if (dtls_srtp_handler != null) {
- encrypted_data = dtls_srtp_handler.process_outgoing_data(component_id, datagram.get_data());
- if (encrypted_data == null) return;
- }
- agent.send(stream_id, component_id, encrypted_data ?? datagram.get_data());
- sent += datagram.length;
- if (sent > sent_reported + 100000) {
- debug("Sent %lu bytes via stream %u component %u", sent, stream_id, component_id);
- sent_reported = sent;
+ try {
+ if (dtls_srtp_handler != null) {
+ uint8[] encrypted_data = dtls_srtp_handler.process_outgoing_data(component_id, datagram.get_data());
+ if (encrypted_data == null) return;
+ // TODO: Nonblocking might require certain libnice versions?
+ GLib.OutputVector[] vectors = {{ encrypted_data, encrypted_data.length }};
+ Nice.OutputMessage[] messages = {{ vectors }};
+ agent.send_messages_nonblocking(stream_id, component_id, messages);
+ } else {
+ GLib.OutputVector[] vectors = {{ datagram.get_data(), datagram.get_size() }};
+ Nice.OutputMessage[] messages = {{ vectors }};
+ agent.send_messages_nonblocking(stream_id, component_id, messages);
+ }
+ bytes_sent += datagram.length;
+ } catch (GLib.Error e) {
+ warning("%s while send_datagram stream %u component %u", e.message, stream_id, component_id);
}
}
}
@@ -93,6 +93,14 @@ public class Dino.Plugins.Ice.TransportParameters : JingleIceUdp.IceUdpTransport
agent.controlling_mode = !incoming;
stream_id = agent.add_stream(components);
+ thread_context = new MainContext();
+ new Thread<void*>(@"ice-thread-$stream_id", () => {
+ thread_context.push_thread_default();
+ thread_loop = new MainLoop(thread_context, false);
+ thread_loop.run();
+ thread_context.pop_thread_default();
+ return null;
+ });
if (turn_ip != null) {
for (uint8 component_id = 1; component_id <= components; component_id++) {
@@ -107,7 +115,7 @@ public class Dino.Plugins.Ice.TransportParameters : JingleIceUdp.IceUdpTransport
for (uint8 component_id = 1; component_id <= components; component_id++) {
// We don't properly get local candidates before this call
- agent.attach_recv(stream_id, component_id, MainContext.@default(), on_recv);
+ agent.attach_recv(stream_id, component_id, thread_context, on_recv);
}
agent.gather_candidates(stream_id);
@@ -260,8 +268,13 @@ public class Dino.Plugins.Ice.TransportParameters : JingleIceUdp.IceUdpTransport
if (stream_id != this.stream_id) return;
uint8[] decrypt_data = null;
if (dtls_srtp_handler != null) {
- decrypt_data = dtls_srtp_handler.process_incoming_data(component_id, data);
- if (decrypt_data == null) return;
+ try {
+ decrypt_data = dtls_srtp_handler.process_incoming_data(component_id, data);
+ if (decrypt_data == null) return;
+ } catch (Crypto.Error e) {
+ warning("%s while on_recv stream %u component %u", e.message, stream_id, component_id);
+ return;
+ }
}
may_consider_ready(stream_id, component_id);
if (connections.has_key((uint8) component_id)) {
@@ -341,5 +354,8 @@ public class Dino.Plugins.Ice.TransportParameters : JingleIceUdp.IceUdpTransport
agent = null;
dtls_srtp_handler = null;
connections.clear();
+ if (thread_loop != null) {
+ thread_loop.quit();
+ }
}
}
diff --git a/plugins/rtp/CMakeLists.txt b/plugins/rtp/CMakeLists.txt
index 3264e24a..fa4f367c 100644
--- a/plugins/rtp/CMakeLists.txt
+++ b/plugins/rtp/CMakeLists.txt
@@ -16,6 +16,10 @@ if(GstRtp_VERSION VERSION_GREATER "1.16")
set(RTP_DEFINITIONS GST_1_16)
endif()
+if(Vala_VERSION VERSION_GREATER "0.50")
+ set(RTP_DEFINITIONS VALA_0_50)
+endif()
+
if(WebRTCAudioProcessing_VERSION GREATER "0.4")
message(STATUS "Ignoring WebRTCAudioProcessing, only versions < 0.4 supported so far")
unset(WebRTCAudioProcessing_FOUND)
diff --git a/plugins/rtp/src/codec_util.vala b/plugins/rtp/src/codec_util.vala
index 417dc4be..6fb5e7aa 100644
--- a/plugins/rtp/src/codec_util.vala
+++ b/plugins/rtp/src/codec_util.vala
@@ -97,7 +97,7 @@ public class Dino.Plugins.Rtp.CodecUtil {
case "h264":
return new string[] {/*"msdkh264enc", */"vaapih264enc", "x264enc"};
case "vp9":
- return new string[] {/*"msdkvp9enc", */"vaapivp9enc" /*, "vp9enc" */};
+ return new string[] {/*"msdkvp9enc", */"vaapivp9enc", "vp9enc"};
case "vp8":
return new string[] {/*"msdkvp8enc", */"vaapivp8enc", "vp8enc"};
}
@@ -140,13 +140,16 @@ public class Dino.Plugins.Rtp.CodecUtil {
public static string? get_encode_args(string media, string codec, string encode, JingleRtp.PayloadType? payload_type) {
// H264
if (encode == "msdkh264enc") return @" rate-control=vbr";
- if (encode == "vaapih264enc") return @" tune=low-power";
- if (encode == "x264enc") return @" byte-stream=1 profile=baseline speed-preset=ultrafast tune=zerolatency";
+ if (encode == "vaapih264enc") return @" rate-control=vbr tune=low-power";
+ if (encode == "x264enc") return @" byte-stream=1 speed-preset=ultrafast tune=zerolatency bframes=0 cabac=false dct8x8=false";
// VP8
- if (encode == "msdkvp8enc") return " rate-control=vbr";
- if (encode == "vaapivp8enc") return " rate-control=vbr";
- if (encode == "vp8enc") return " deadline=1 error-resilient=1";
+ if (encode == "vaapivp8enc" || encode == "msdkvp8enc") return " rate-control=vbr target-percentage=90";
+ if (encode == "vp8enc") return " deadline=1 error-resilient=3 lag-in-frames=0 resize-allowed=true threads=8 dropframe-threshold=30 end-usage=vbr cpu-used=4";
+
+ // VP9
+ if (encode == "msdkvp9enc" || encode == "vaapivp9enc") return " rate-control=vbr target-percentage=90";
+ if (encode == "vp9enc") return " deadline=1 error-resilient=3 lag-in-frames=0 resize-allowed=true threads=8 dropframe-threshold=30 end-usage=vbr cpu-used=4";
// OPUS
if (encode == "opusenc") {
@@ -159,7 +162,8 @@ public class Dino.Plugins.Rtp.CodecUtil {
public static string? get_encode_suffix(string media, string codec, string encode, JingleRtp.PayloadType? payload_type) {
// H264
- if (media == "video" && codec == "h264") return " ! video/x-h264,profile=constrained-baseline ! h264parse";
+ if (media == "video" && codec == "h264") return " ! capsfilter caps=video/x-h264,profile=constrained-baseline ! h264parse";
+ if (media == "video" && codec == "vp8" && encode == "vp8enc") return " ! capsfilter caps=video/x-vp8,profile=(string)1";
return null;
}
@@ -171,26 +175,43 @@ public class Dino.Plugins.Rtp.CodecUtil {
if (encode_name == null) return 0;
Gst.Element encode = encode_bin.get_by_name(@"$(encode_bin.name)_encode");
- bitrate = uint.min(2048000, bitrate);
-
switch (encode_name) {
case "msdkh264enc":
case "vaapih264enc":
case "x264enc":
+ case "msdkvp9enc":
+ case "vaapivp9enc":
case "msdkvp8enc":
case "vaapivp8enc":
bitrate = uint.min(2048000, bitrate);
encode.set("bitrate", bitrate);
return bitrate;
+ case "vp9enc":
case "vp8enc":
bitrate = uint.min(2147483, bitrate);
- encode.set("target-bitrate", bitrate * 1000);
+ encode.set("target-bitrate", bitrate * 1024);
return bitrate;
}
return 0;
}
+ public void update_rescale_caps(Gst.Element encode_element, Gst.Caps caps) {
+ Gst.Bin? encode_bin = encode_element as Gst.Bin;
+ if (encode_bin == null) return;
+ Gst.Element rescale_caps = encode_bin.get_by_name(@"$(encode_bin.name)_rescale_caps");
+ rescale_caps.set("caps", caps);
+ }
+
+ public Gst.Caps? get_rescale_caps(Gst.Element encode_element) {
+ Gst.Bin? encode_bin = encode_element as Gst.Bin;
+ if (encode_bin == null) return null;
+ Gst.Element rescale_caps = encode_bin.get_by_name(@"$(encode_bin.name)_rescale_caps");
+ Gst.Caps caps;
+ rescale_caps.get("caps", out caps);
+ return caps;
+ }
+
public static string? get_decode_prefix(string media, string codec, string decode, JingleRtp.PayloadType? payload_type) {
return null;
}
@@ -198,6 +219,7 @@ public class Dino.Plugins.Rtp.CodecUtil {
public static string? get_decode_args(string media, string codec, string decode, JingleRtp.PayloadType? payload_type) {
if (decode == "opusdec" && payload_type != null && payload_type.parameters.has("useinbandfec", "1")) return " use-inband-fec=true";
if (decode == "vaapivp9dec" || decode == "vaapivp8dec" || decode == "vaapih264dec") return " max-errors=100";
+ if (decode == "vp8dec" || decode == "vp9dec") return " threads=8";
return null;
}
@@ -242,6 +264,7 @@ public class Dino.Plugins.Rtp.CodecUtil {
}
public string? get_decode_element_name(string media, string? codec) {
+ if (codec == "vp9") return null; // Temporary unsupport VP9
if (get_depay_element_name(media, codec) == null) return null;
foreach (string candidate in get_decode_candidates(media, codec)) {
if (is_element_supported(candidate)) return candidate;
@@ -270,7 +293,7 @@ public class Dino.Plugins.Rtp.CodecUtil {
string decode_suffix = get_decode_suffix(media, codec, decode, payload_type) ?? "";
string depay_args = get_depay_args(media, codec, decode, payload_type) ?? "";
string resample = media == "audio" ? @" ! audioresample name=$(base_name)_resample" : "";
- return @"$depay$depay_args name=$(base_name)_rtp_depay ! $decode_prefix$decode$decode_args name=$(base_name)_$(codec)_decode$decode_suffix ! $(media)convert name=$(base_name)_convert$resample";
+ return @"queue ! $depay$depay_args name=$(base_name)_rtp_depay ! $decode_prefix$decode$decode_args name=$(base_name)_$(codec)_decode$decode_suffix ! $(media)convert name=$(base_name)_convert$resample";
}
public Gst.Element? get_decode_bin(string media, JingleRtp.PayloadType payload_type, string? name = null) {
@@ -285,16 +308,29 @@ public class Dino.Plugins.Rtp.CodecUtil {
}
public string? get_encode_bin_description(string media, string? codec, JingleRtp.PayloadType? payload_type, string? element_name = null, string? name = null) {
+ string? desc1 = get_encode_bin_without_payloader_description(media, codec, payload_type, element_name, name);
+ string? desc2 = get_payloader_bin_description(media, codec, payload_type, name);
+ return @"$desc1 ! $desc2";
+ }
+
+ public string? get_payloader_bin_description(string media, string? codec, JingleRtp.PayloadType? payload_type, string? name = null) {
if (codec == null) return null;
string base_name = name ?? @"encode_$(codec)_$(Random.next_int())";
string? pay = get_pay_element_name(media, codec);
+ if (pay == null) return null;
+ return @"$pay pt=$(payload_type != null ? payload_type.id : 96) name=$(base_name)_rtp_pay";
+ }
+
+ public string? get_encode_bin_without_payloader_description(string media, string? codec, JingleRtp.PayloadType? payload_type, string? element_name = null, string? name = null) {
+ if (codec == null) return null;
+ string base_name = name ?? @"encode_$(codec)_$(Random.next_int())";
string? encode = element_name ?? get_encode_element_name(media, codec);
- if (pay == null || encode == null) return null;
+ if (encode == null) return null;
string encode_prefix = get_encode_prefix(media, codec, encode, payload_type) ?? "";
string encode_args = get_encode_args(media, codec, encode, payload_type) ?? "";
string encode_suffix = get_encode_suffix(media, codec, encode, payload_type) ?? "";
- string resample = media == "audio" ? @" ! audioresample name=$(base_name)_resample" : "";
- return @"$(media)convert name=$(base_name)_convert$resample ! $encode_prefix$encode$encode_args name=$(base_name)_encode$encode_suffix ! $pay pt=$(payload_type != null ? payload_type.id : 96) name=$(base_name)_rtp_pay";
+ string rescale = media == "audio" ? @" ! audioresample name=$(base_name)_resample" : @" ! videoscale name=$(base_name)_rescale ! capsfilter name=$(base_name)_rescale_caps";
+ return @"$(media)convert name=$(base_name)_convert$rescale ! queue ! $encode_prefix$encode$encode_args name=$(base_name)_encode$encode_suffix";
}
public Gst.Element? get_encode_bin(string media, JingleRtp.PayloadType payload_type, string? name = null) {
@@ -308,4 +344,26 @@ public class Dino.Plugins.Rtp.CodecUtil {
return bin;
}
-} \ No newline at end of file
+ public Gst.Element? get_encode_bin_without_payloader(string media, JingleRtp.PayloadType payload_type, string? name = null) {
+ string? codec = get_codec_from_payload(media, payload_type);
+ string base_name = name ?? @"encode_$(codec)_$(Random.next_int())";
+ string? desc = get_encode_bin_without_payloader_description(media, codec, payload_type, null, base_name);
+ if (desc == null) return null;
+ debug("Pipeline to encode %s %s without payloader: %s", media, codec, desc);
+ Gst.Element bin = Gst.parse_bin_from_description(desc, true);
+ bin.name = name;
+ return bin;
+ }
+
+ public Gst.Element? get_payloader_bin(string media, JingleRtp.PayloadType payload_type, string? name = null) {
+ string? codec = get_codec_from_payload(media, payload_type);
+ string base_name = name ?? @"encode_$(codec)_$(Random.next_int())";
+ string? desc = get_payloader_bin_description(media, codec, payload_type, base_name);
+ if (desc == null) return null;
+ debug("Pipeline to payload %s %s: %s", media, codec, desc);
+ Gst.Element bin = Gst.parse_bin_from_description(desc, true);
+ bin.name = name;
+ return bin;
+ }
+
+}
diff --git a/plugins/rtp/src/device.vala b/plugins/rtp/src/device.vala
index e25271b1..97258d0c 100644
--- a/plugins/rtp/src/device.vala
+++ b/plugins/rtp/src/device.vala
@@ -1,44 +1,64 @@
+using Xmpp.Xep.JingleRtp;
+using Gee;
+
public class Dino.Plugins.Rtp.Device : MediaDevice, Object {
+ private const int[] common_widths = {320, 360, 400, 480, 640, 960, 1280, 1920, 2560, 3840};
+
public Plugin plugin { get; private set; }
+ public CodecUtil codec_util { get { return plugin.codec_util; } }
public Gst.Device device { get; private set; }
- private string device_name;
- public string id { get {
- return device_name;
- }}
- private string device_display_name;
- public string display_name { get {
- return device_display_name;
- }}
+ public string id { get { return device_name; }}
+ public string display_name { get { return device_display_name; }}
public string detail_name { get {
return device.properties.get_string("alsa.card_name") ?? device.properties.get_string("alsa.id") ?? id;
}}
- public Gst.Pipeline pipe { get {
- return plugin.pipe;
- }}
+
+ public Gst.Pipeline pipe { get { return plugin.pipe; }}
public string? media { get {
- if (device.device_class.has_prefix("Audio/")) {
+ if (device.has_classes("Audio")) {
return "audio";
- } else if (device.device_class.has_prefix("Video/")) {
+ } else if (device.has_classes("Video")) {
return "video";
} else {
return null;
}
}}
- public bool is_source { get {
- return device.device_class.has_suffix("/Source");
- }}
- public bool is_sink { get {
- return device.device_class.has_suffix("/Sink");
- }}
+ public bool is_source { get { return device.has_classes("Source"); }}
+ public bool is_sink { get { return device.has_classes("Sink"); }}
+
+ private string device_name;
+ private string device_display_name;
+ private Gst.Caps device_caps;
private Gst.Element element;
private Gst.Element tee;
private Gst.Element dsp;
- private Gst.Element mixer;
+ private Gst.Base.Aggregator mixer;
private Gst.Element filter;
- private Gst.Element rate;
- private int links = 0;
+ private int links;
+
+ // Codecs
+ private Gee.Map<PayloadType, Gst.Element> codecs = new HashMap<PayloadType, Gst.Element>(PayloadType.hash_func, PayloadType.equals_func);
+ private Gee.Map<PayloadType, Gst.Element> codec_tees = new HashMap<PayloadType, Gst.Element>(PayloadType.hash_func, PayloadType.equals_func);
+
+ // Payloaders
+ private Gee.Map<PayloadType, Gee.Map<uint, Gst.Element>> payloaders = new HashMap<PayloadType, Gee.Map<uint, Gst.Element>>(PayloadType.hash_func, PayloadType.equals_func);
+ private Gee.Map<PayloadType, Gee.Map<uint, Gst.Element>> payloader_tees = new HashMap<PayloadType, Gee.Map<uint, Gst.Element>>(PayloadType.hash_func, PayloadType.equals_func);
+ private Gee.Map<PayloadType, Gee.Map<uint, uint>> payloader_links = new HashMap<PayloadType, Gee.Map<uint, uint>>(PayloadType.hash_func, PayloadType.equals_func);
+
+ // Bitrate
+ private Gee.Map<PayloadType, Gee.List<CodecBitrate>> codec_bitrates = new HashMap<PayloadType, Gee.List<CodecBitrate>>(PayloadType.hash_func, PayloadType.equals_func);
+
+ private class CodecBitrate {
+ public uint bitrate;
+ public int64 timestamp;
+
+ public CodecBitrate(uint bitrate) {
+ this.bitrate = bitrate;
+ this.timestamp = get_monotonic_time();
+ }
+ }
public Device(Plugin plugin, Gst.Device device) {
this.plugin = plugin;
@@ -57,25 +77,241 @@ public class Dino.Plugins.Rtp.Device : MediaDevice, Object {
}
public Gst.Element? link_sink() {
+ if (!is_sink) return null;
if (element == null) create();
links++;
- if (mixer != null) return mixer;
- if (is_sink && media == "audio") return filter;
+ if (mixer != null) {
+ Gst.Element rate = Gst.ElementFactory.make("audiorate", @"$(id)_rate_$(Random.next_int())");
+ pipe.add(rate);
+ rate.link(mixer);
+ return rate;
+ }
+ if (media == "audio") return filter;
return element;
}
- public Gst.Element? link_source() {
+ public Gst.Element? link_source(PayloadType? payload_type = null, uint ssrc = 0, int seqnum_offset = -1, uint32 timestamp_offset = 0) {
+ if (!is_source) return null;
if (element == null) create();
links++;
+ if (payload_type != null && ssrc != 0 && tee != null) {
+ bool new_codec = false;
+ string? codec = CodecUtil.get_codec_from_payload(media, payload_type);
+ if (!codecs.has_key(payload_type)) {
+ codecs[payload_type] = codec_util.get_encode_bin_without_payloader(media, payload_type, @"$(id)_$(codec)_encoder");
+ pipe.add(codecs[payload_type]);
+ new_codec = true;
+ }
+ if (!codec_tees.has_key(payload_type)) {
+ codec_tees[payload_type] = Gst.ElementFactory.make("tee", @"$(id)_$(codec)_tee");
+ codec_tees[payload_type].@set("allow-not-linked", true);
+ pipe.add(codec_tees[payload_type]);
+ codecs[payload_type].link(codec_tees[payload_type]);
+ }
+ if (!payloaders.has_key(payload_type)) {
+ payloaders[payload_type] = new HashMap<uint, Gst.Element>();
+ }
+ if (!payloaders[payload_type].has_key(ssrc)) {
+ payloaders[payload_type][ssrc] = codec_util.get_payloader_bin(media, payload_type, @"$(id)_$(codec)_$(ssrc)");
+ var payload = (Gst.RTP.BasePayload) ((Gst.Bin) payloaders[payload_type][ssrc]).get_by_name(@"$(id)_$(codec)_$(ssrc)_rtp_pay");
+ payload.ssrc = ssrc;
+ payload.seqnum_offset = seqnum_offset;
+ if (timestamp_offset != 0) {
+ payload.timestamp_offset = timestamp_offset;
+ }
+ pipe.add(payloaders[payload_type][ssrc]);
+ codec_tees[payload_type].link(payloaders[payload_type][ssrc]);
+ debug("Payload for %s with %s using ssrc %u, seqnum_offset %u, timestamp_offset %u", media, codec, ssrc, seqnum_offset, timestamp_offset);
+ }
+ if (!payloader_tees.has_key(payload_type)) {
+ payloader_tees[payload_type] = new HashMap<uint, Gst.Element>();
+ }
+ if (!payloader_tees[payload_type].has_key(ssrc)) {
+ payloader_tees[payload_type][ssrc] = Gst.ElementFactory.make("tee", @"$(id)_$(codec)_$(ssrc)_tee");
+ payloader_tees[payload_type][ssrc].@set("allow-not-linked", true);
+ pipe.add(payloader_tees[payload_type][ssrc]);
+ payloaders[payload_type][ssrc].link(payloader_tees[payload_type][ssrc]);
+ }
+ if (!payloader_links.has_key(payload_type)) {
+ payloader_links[payload_type] = new HashMap<uint, uint>();
+ }
+ if (!payloader_links[payload_type].has_key(ssrc)) {
+ payloader_links[payload_type][ssrc] = 1;
+ } else {
+ payloader_links[payload_type][ssrc] = payloader_links[payload_type][ssrc] + 1;
+ }
+ if (new_codec) {
+ tee.link(codecs[payload_type]);
+ }
+ return payloader_tees[payload_type][ssrc];
+ }
if (tee != null) return tee;
return element;
}
- public void unlink() {
+ private static double get_target_bitrate(Gst.Caps caps) {
+ if (caps == null || caps.get_size() == 0) return uint.MAX;
+ unowned Gst.Structure? that = caps.get_structure(0);
+ int num = 0, den = 0, width = 0, height = 0;
+ if (!that.has_field("width") || !that.get_int("width", out width)) return uint.MAX;
+ if (!that.has_field("height") || !that.get_int("height", out height)) return uint.MAX;
+ if (!that.has_field("framerate")) return uint.MAX;
+ Value framerate = that.get_value("framerate");
+ if (framerate.type() != typeof(Gst.Fraction)) return uint.MAX;
+ num = Gst.Value.get_fraction_numerator(framerate);
+ den = Gst.Value.get_fraction_denominator(framerate);
+ double pxs = ((double)num/(double)den) * (double)width * (double)height;
+ double br = Math.sqrt(Math.sqrt(pxs)) * 100.0 - 3700.0;
+ if (br < 128.0) return 128.0;
+ return br;
+ }
+
+ private Gst.Caps get_active_caps(PayloadType payload_type) {
+ return codec_util.get_rescale_caps(codecs[payload_type]) ?? device_caps;
+ }
+
+ private void apply_caps(PayloadType payload_type, Gst.Caps caps) {
+ plugin.pause();
+ debug("Set scaled caps to %s", caps.to_string());
+ codec_util.update_rescale_caps(codecs[payload_type], caps);
+ plugin.unpause();
+ }
+
+ private void apply_width(PayloadType payload_type, int new_width, uint bitrate) {
+ int device_caps_width, device_caps_height, active_caps_width, device_caps_framerate_num, device_caps_framerate_den;
+ device_caps.get_structure(0).get_int("width", out device_caps_width);
+ device_caps.get_structure(0).get_int("height", out device_caps_height);
+ device_caps.get_structure(0).get_fraction("framerate", out device_caps_framerate_num, out device_caps_framerate_den);
+ Gst.Caps active_caps = get_active_caps(payload_type);
+ if (active_caps != null && active_caps.get_size() > 0) {
+ active_caps.get_structure(0).get_int("width", out active_caps_width);
+ } else {
+ active_caps_width = device_caps_width;
+ }
+ if (new_width == active_caps_width) return;
+ int new_height = device_caps_height * new_width / device_caps_width;
+ Gst.Caps new_caps = new Gst.Caps.simple("video/x-raw", "width", typeof(int), new_width, "height", typeof(int), new_height, "framerate", typeof(Gst.Fraction), device_caps_framerate_num, device_caps_framerate_den, null);
+ double required_bitrate = get_target_bitrate(new_caps);
+ debug("Changing resolution width from %d to %d (requires bitrate %f, current target is %u)", active_caps_width, new_width, required_bitrate, bitrate);
+ if (bitrate < required_bitrate && new_width > active_caps_width) return;
+ apply_caps(payload_type, new_caps);
+ }
+
+ public void update_bitrate(PayloadType payload_type, uint bitrate) {
+ if (codecs.has_key(payload_type)) {
+ lock(codec_bitrates);
+ if (!codec_bitrates.has_key(payload_type)) {
+ codec_bitrates[payload_type] = new ArrayList<CodecBitrate>();
+ }
+ codec_bitrates[payload_type].add(new CodecBitrate(bitrate));
+ var remove = new ArrayList<CodecBitrate>();
+ foreach (CodecBitrate rate in codec_bitrates[payload_type]) {
+ if (rate.timestamp < get_monotonic_time() - 5000000L) {
+ remove.add(rate);
+ continue;
+ }
+ if (rate.bitrate < bitrate) {
+ bitrate = rate.bitrate;
+ }
+ }
+ codec_bitrates[payload_type].remove_all(remove);
+ if (media == "video") {
+ if (bitrate < 128) bitrate = 128;
+ Gst.Caps active_caps = get_active_caps(payload_type);
+ double max_bitrate = get_target_bitrate(device_caps) * 2;
+ double current_target_bitrate = get_target_bitrate(active_caps);
+ int device_caps_width, active_caps_width;
+ device_caps.get_structure(0).get_int("width", out device_caps_width);
+ if (active_caps != null && active_caps.get_size() > 0) {
+ active_caps.get_structure(0).get_int("width", out active_caps_width);
+ } else {
+ active_caps_width = device_caps_width;
+ }
+ if (bitrate < 0.75 * current_target_bitrate && active_caps_width > common_widths[0]) {
+ // Lower video resolution
+ int i = 1;
+ for(; i < common_widths.length && common_widths[i] < active_caps_width; i++);if (common_widths[i] != active_caps_width) {
+ debug("Decrease resolution to ensure target bitrate (%u) is in reach (current resolution target bitrate is %f)", bitrate, current_target_bitrate);
+ }
+ apply_width(payload_type, common_widths[i-1], bitrate);
+ } else if (bitrate > 2 * current_target_bitrate && active_caps_width < device_caps_width) {
+ // Higher video resolution
+ int i = 0;
+ for(; i < common_widths.length && common_widths[i] <= active_caps_width; i++);
+ if (common_widths[i] != active_caps_width) {
+ debug("Increase resolution to make use of available bandwidth of target bitrate (%u) (current resolution target bitrate is %f)", bitrate, current_target_bitrate);
+ }
+ if (common_widths[i] > device_caps_width) {
+ // We never scale up, so just stick with what the device gives
+ apply_width(payload_type, device_caps_width, bitrate);
+ } else if (common_widths[i] != active_caps_width) {
+ apply_width(payload_type, common_widths[i], bitrate);
+ }
+ }
+ if (bitrate > max_bitrate) bitrate = (uint) max_bitrate;
+ }
+ codec_util.update_bitrate(media, payload_type, codecs[payload_type], bitrate);
+ unlock(codec_bitrates);
+ }
+ }
+
+ public void unlink(Gst.Element? link = null) {
if (links <= 0) {
critical("Link count below zero.");
return;
}
+ if (link != null && is_source && tee != null) {
+ PayloadType payload_type = payloader_tees.first_match((entry) => entry.value.any_match((entry) => entry.value == link)).key;
+ uint ssrc = payloader_tees[payload_type].first_match((entry) => entry.value == link).key;
+ payloader_links[payload_type][ssrc] = payloader_links[payload_type][ssrc] - 1;
+ if (payloader_links[payload_type][ssrc] == 0) {
+ plugin.pause();
+
+ codec_tees[payload_type].unlink(payloaders[payload_type][ssrc]);
+ payloaders[payload_type][ssrc].set_locked_state(true);
+ payloaders[payload_type][ssrc].set_state(Gst.State.NULL);
+ payloaders[payload_type][ssrc].unlink(payloader_tees[payload_type][ssrc]);
+ pipe.remove(payloaders[payload_type][ssrc]);
+ payloaders[payload_type].unset(ssrc);
+ payloader_tees[payload_type][ssrc].set_locked_state(true);
+ payloader_tees[payload_type][ssrc].set_state(Gst.State.NULL);
+ pipe.remove(payloader_tees[payload_type][ssrc]);
+ payloader_tees[payload_type].unset(ssrc);
+
+ payloader_links[payload_type].unset(ssrc);
+ plugin.unpause();
+ }
+ if (payloader_links[payload_type].size == 0) {
+ plugin.pause();
+
+ tee.unlink(codecs[payload_type]);
+ codecs[payload_type].set_locked_state(true);
+ codecs[payload_type].set_state(Gst.State.NULL);
+ codecs[payload_type].unlink(codec_tees[payload_type]);
+ pipe.remove(codecs[payload_type]);
+ codecs.unset(payload_type);
+ codec_tees[payload_type].set_locked_state(true);
+ codec_tees[payload_type].set_state(Gst.State.NULL);
+ pipe.remove(codec_tees[payload_type]);
+ codec_tees.unset(payload_type);
+
+ payloaders.unset(payload_type);
+ payloader_tees.unset(payload_type);
+ payloader_links.unset(payload_type);
+ plugin.unpause();
+ }
+ }
+ if (link != null && is_sink && mixer != null) {
+ plugin.pause();
+ link.set_locked_state(true);
+ Gst.Base.AggregatorPad mixer_sink_pad = (Gst.Base.AggregatorPad) link.get_static_pad("src").get_peer();
+ link.get_static_pad("src").unlink(mixer_sink_pad);
+ mixer_sink_pad.set_active(false);
+ link.set_state(Gst.State.NULL);
+ pipe.remove(link);
+ mixer.release_request_pad(mixer_sink_pad);
+ plugin.unpause();
+ }
links--;
if (links == 0) {
destroy();
@@ -154,11 +390,16 @@ public class Dino.Plugins.Rtp.Device : MediaDevice, Object {
debug("Creating device %s", id);
plugin.pause();
element = device.create_element(id);
+ if (is_sink) {
+ element.@set("async", false);
+ element.@set("sync", false);
+ }
pipe.add(element);
+ device_caps = get_best_caps();
if (is_source) {
element.@set("do-timestamp", true);
filter = Gst.ElementFactory.make("capsfilter", @"caps_filter_$id");
- filter.@set("caps", get_best_caps());
+ filter.@set("caps", device_caps);
pipe.add(filter);
element.link(filter);
#if WITH_VOICE_PROCESSOR
@@ -174,22 +415,18 @@ public class Dino.Plugins.Rtp.Device : MediaDevice, Object {
pipe.add(tee);
(dsp ?? filter).link(tee);
}
- if (is_sink) {
- element.@set("async", false);
- element.@set("sync", false);
- }
if (is_sink && media == "audio") {
- filter = Gst.ElementFactory.make("capsfilter", @"caps_filter_$id");
- filter.@set("caps", get_best_caps());
- pipe.add(filter);
- if (plugin.echoprobe != null) {
- rate = Gst.ElementFactory.make("audiorate", @"rate_$id");
- rate.@set("tolerance", 100000000);
- pipe.add(rate);
- filter.link(rate);
- rate.link(plugin.echoprobe);
+ mixer = (Gst.Base.Aggregator) Gst.ElementFactory.make("audiomixer", @"mixer_$id");
+ pipe.add(mixer);
+ mixer.link(pipe);
+ if (plugin.echoprobe != null && !plugin.echoprobe.get_static_pad("src").is_linked()) {
+ mixer.link(plugin.echoprobe);
plugin.echoprobe.link(element);
} else {
+ filter = Gst.ElementFactory.make("capsfilter", @"caps_filter_$id");
+ filter.@set("caps", device_caps);
+ pipe.add(filter);
+ mixer.link(filter);
filter.link(element);
}
}
@@ -197,38 +434,25 @@ public class Dino.Plugins.Rtp.Device : MediaDevice, Object {
}
private void destroy() {
- if (mixer != null) {
- if (is_sink && media == "audio" && plugin.echoprobe != null) {
- plugin.echoprobe.unlink(mixer);
- }
- int linked_sink_pads = 0;
- mixer.foreach_sink_pad((_, pad) => {
- if (pad.is_linked()) linked_sink_pads++;
- return true;
- });
- if (linked_sink_pads > 0) {
- warning("%s-mixer still has %i sink pads while being destroyed", id, linked_sink_pads);
+ if (is_sink) {
+ if (mixer != null) {
+ int linked_sink_pads = 0;
+ mixer.foreach_sink_pad((_, pad) => {
+ if (pad.is_linked()) linked_sink_pads++;
+ return true;
+ });
+ if (linked_sink_pads > 0) {
+ warning("%s-mixer still has %i sink pads while being destroyed", id, linked_sink_pads);
+ }
+ mixer.unlink(plugin.echoprobe ?? element);
}
- mixer.set_locked_state(true);
- mixer.set_state(Gst.State.NULL);
- mixer.unlink(element);
- pipe.remove(mixer);
- mixer = null;
- } else if (is_sink && media == "audio") {
if (filter != null) {
filter.set_locked_state(true);
filter.set_state(Gst.State.NULL);
- filter.unlink(rate ?? ((Gst.Element)plugin.echoprobe) ?? element);
+ filter.unlink(element);
pipe.remove(filter);
filter = null;
}
- if (rate != null) {
- rate.set_locked_state(true);
- rate.set_state(Gst.State.NULL);
- rate.unlink(plugin.echoprobe);
- pipe.remove(rate);
- rate = null;
- }
if (plugin.echoprobe != null) {
plugin.echoprobe.unlink(element);
}
@@ -239,34 +463,42 @@ public class Dino.Plugins.Rtp.Device : MediaDevice, Object {
else if (is_source) element.unlink(tee);
pipe.remove(element);
element = null;
- if (filter != null) {
- filter.set_locked_state(true);
- filter.set_state(Gst.State.NULL);
- filter.unlink(dsp ?? tee);
- pipe.remove(filter);
- filter = null;
- }
- if (dsp != null) {
- dsp.set_locked_state(true);
- dsp.set_state(Gst.State.NULL);
- dsp.unlink(tee);
- pipe.remove(dsp);
- dsp = null;
+ if (mixer != null) {
+ mixer.set_locked_state(true);
+ mixer.set_state(Gst.State.NULL);
+ pipe.remove(mixer);
+ mixer = null;
}
- if (tee != null) {
- int linked_src_pads = 0;
- tee.foreach_src_pad((_, pad) => {
- if (pad.is_linked()) linked_src_pads++;
- return true;
- });
- if (linked_src_pads != 0) {
- warning("%s-tee still has %d src pads while being destroyed", id, linked_src_pads);
+ if (is_source) {
+ if (filter != null) {
+ filter.set_locked_state(true);
+ filter.set_state(Gst.State.NULL);
+ filter.unlink(dsp ?? tee);
+ pipe.remove(filter);
+ filter = null;
+ }
+ if (dsp != null) {
+ dsp.set_locked_state(true);
+ dsp.set_state(Gst.State.NULL);
+ dsp.unlink(tee);
+ pipe.remove(dsp);
+ dsp = null;
+ }
+ if (tee != null) {
+ int linked_src_pads = 0;
+ tee.foreach_src_pad((_, pad) => {
+ if (pad.is_linked()) linked_src_pads++;
+ return true;
+ });
+ if (linked_src_pads != 0) {
+ warning("%s-tee still has %d src pads while being destroyed", id, linked_src_pads);
+ }
+ tee.set_locked_state(true);
+ tee.set_state(Gst.State.NULL);
+ pipe.remove(tee);
+ tee = null;
}
- tee.set_locked_state(true);
- tee.set_state(Gst.State.NULL);
- pipe.remove(tee);
- tee = null;
}
debug("Destroyed device %s", id);
}
-} \ No newline at end of file
+}
diff --git a/plugins/rtp/src/module.vala b/plugins/rtp/src/module.vala
index dfe224aa..4f48b6c4 100644
--- a/plugins/rtp/src/module.vala
+++ b/plugins/rtp/src/module.vala
@@ -63,7 +63,7 @@ public class Dino.Plugins.Rtp.Module : JingleRtp.Module {
return supported;
}
- private async bool is_payload_supported(string media, JingleRtp.PayloadType payload_type) {
+ public override async bool is_payload_supported(string media, JingleRtp.PayloadType payload_type) {
string? codec = CodecUtil.get_codec_from_payload(media, payload_type);
if (codec == null) return false;
if (unsupported_codecs.contains(codec)) return false;
@@ -131,7 +131,7 @@ public class Dino.Plugins.Rtp.Module : JingleRtp.Module {
public override async Gee.List<JingleRtp.PayloadType> get_supported_payloads(string media) {
Gee.List<JingleRtp.PayloadType> list = new ArrayList<JingleRtp.PayloadType>(JingleRtp.PayloadType.equals_func);
if (media == "audio") {
- var opus = new JingleRtp.PayloadType() { channels = 2, clockrate = 48000, name = "opus", id = 99 };
+ var opus = new JingleRtp.PayloadType() { channels = 1, clockrate = 48000, name = "opus", id = 99 };
opus.parameters["useinbandfec"] = "1";
var speex32 = new JingleRtp.PayloadType() { channels = 1, clockrate = 32000, name = "speex", id = 100 };
var speex16 = new JingleRtp.PayloadType() { channels = 1, clockrate = 16000, name = "speex", id = 101 };
diff --git a/plugins/rtp/src/plugin.vala b/plugins/rtp/src/plugin.vala
index d79fc2aa..6d6da79a 100644
--- a/plugins/rtp/src/plugin.vala
+++ b/plugins/rtp/src/plugin.vala
@@ -5,10 +5,10 @@ using Xmpp.Xep;
public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
public Dino.Application app { get; private set; }
public CodecUtil codec_util { get; private set; }
- public Gst.DeviceMonitor device_monitor { get; private set; }
- public Gst.Pipeline pipe { get; private set; }
- public Gst.Bin rtpbin { get; private set; }
- public Gst.Element echoprobe { get; private set; }
+ public Gst.DeviceMonitor? device_monitor { get; private set; }
+ public Gst.Pipeline? pipe { get; private set; }
+ public Gst.Bin? rtpbin { get; private set; }
+ public Gst.Element? echoprobe { get; private set; }
private Gee.List<Stream> streams = new ArrayList<Stream>();
private Gee.List<Device> devices = new ArrayList<Device>();
@@ -42,18 +42,22 @@ public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
if (pause_count < 0) warning("Pause count below zero!");
}
- public void startup() {
+ private void init_device_monitor() {
+ if (device_monitor != null) return;
device_monitor = new Gst.DeviceMonitor();
device_monitor.show_all = true;
device_monitor.get_bus().add_watch(Priority.DEFAULT, on_device_monitor_message);
device_monitor.start();
foreach (Gst.Device device in device_monitor.get_devices()) {
- if (device.properties.has_name("pipewire-proplist") && device.device_class.has_prefix("Audio/")) continue;
+ if (device.properties.has_name("pipewire-proplist") && device.has_classes("Audio")) continue;
if (device.properties.get_string("device.class") == "monitor") continue;
if (devices.any_match((it) => it.matches(device))) continue;
devices.add(new Device(this, device));
}
+ }
+ private void init_call_pipe() {
+ if (pipe != null) return;
pipe = new Gst.Pipeline(null);
// RTP
@@ -66,7 +70,7 @@ public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
rtpbin.pad_added.connect(on_rtp_pad_added);
rtpbin.@set("latency", 100);
rtpbin.@set("do-lost", true);
- rtpbin.@set("do-sync-event", true);
+// rtpbin.@set("do-sync-event", true);
rtpbin.@set("drop-on-latency", true);
rtpbin.connect("signal::request-pt-map", request_pt_map, this);
pipe.add(rtpbin);
@@ -86,6 +90,20 @@ public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
pipe.set_state(Gst.State.PLAYING);
}
+ private void destroy_call_pipe() {
+ if (pipe == null) return;
+ pipe.set_state(Gst.State.NULL);
+ rtpbin = null;
+#if WITH_VOICE_PROCESSOR
+ echoprobe = null;
+#endif
+ pipe = null;
+ }
+
+ public void startup() {
+ init_device_monitor();
+ }
+
private static Gst.Caps? request_pt_map(Gst.Element rtpbin, uint session, uint pt, Plugin plugin) {
debug("request-pt-map");
return null;
@@ -98,7 +116,7 @@ public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
uint8 rtpid = (uint8)int.parse(split[3]);
foreach (Stream stream in streams) {
if (stream.rtpid == rtpid) {
- stream.on_ssrc_pad_added(split[4], pad);
+ stream.on_ssrc_pad_added((uint32) split[4].to_uint64(), pad);
}
}
}
@@ -179,51 +197,39 @@ public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
}
private bool on_device_monitor_message(Gst.Bus bus, Gst.Message message) {
- Gst.Device old_device = null;
- Gst.Device device = null;
- Device old = null;
+ Gst.Device? old_gst_device = null;
+ Gst.Device? gst_device = null;
+ Device? device = null;
switch (message.type) {
case Gst.MessageType.DEVICE_ADDED:
- message.parse_device_added(out device);
- if (device.properties.has_name("pipewire-proplist") && device.device_class.has_prefix("Audio/")) return Source.CONTINUE;
- if (device.properties.get_string("device.class") == "monitor") return Source.CONTINUE;
- if (devices.any_match((it) => it.matches(device))) return Source.CONTINUE;
- devices.add(new Device(this, device));
+ message.parse_device_added(out gst_device);
+ if (gst_device.properties.has_name("pipewire-proplist") && gst_device.has_classes("Audio")) return Source.CONTINUE;
+ if (gst_device.properties.get_string("device.class") == "monitor") return Source.CONTINUE;
+ if (devices.any_match((it) => it.matches(gst_device))) return Source.CONTINUE;
+ device = new Device(this, gst_device);
+ devices.add(device);
break;
#if GST_1_16
case Gst.MessageType.DEVICE_CHANGED:
- message.parse_device_changed(out device, out old_device);
- if (device.properties.has_name("pipewire-proplist") && device.device_class.has_prefix("Audio/")) return Source.CONTINUE;
- if (device.properties.get_string("device.class") == "monitor") return Source.CONTINUE;
- old = devices.first_match((it) => it.matches(old_device));
- if (old != null) old.update(device);
+ message.parse_device_changed(out gst_device, out old_gst_device);
+ if (gst_device.properties.has_name("pipewire-proplist") && gst_device.has_classes("Audio")) return Source.CONTINUE;
+ if (gst_device.properties.get_string("device.class") == "monitor") return Source.CONTINUE;
+ device = devices.first_match((it) => it.matches(old_gst_device));
+ if (device != null) device.update(gst_device);
break;
#endif
case Gst.MessageType.DEVICE_REMOVED:
- message.parse_device_removed(out device);
- if (device.properties.has_name("pipewire-proplist") && device.device_class.has_prefix("Audio/")) return Source.CONTINUE;
- if (device.properties.get_string("device.class") == "monitor") return Source.CONTINUE;
- old = devices.first_match((it) => it.matches(device));
- if (old != null) devices.remove(old);
+ message.parse_device_removed(out gst_device);
+ if (gst_device.properties.has_name("pipewire-proplist") && gst_device.has_classes("Audio")) return Source.CONTINUE;
+ if (gst_device.properties.get_string("device.class") == "monitor") return Source.CONTINUE;
+ device = devices.first_match((it) => it.matches(gst_device));
+ if (device != null) devices.remove(device);
break;
default:
break;
}
if (device != null) {
- switch (device.device_class) {
- case "Audio/Source":
- devices_changed("audio", false);
- break;
- case "Audio/Sink":
- devices_changed("audio", true);
- break;
- case "Video/Source":
- devices_changed("video", false);
- break;
- case "Video/Sink":
- devices_changed("video", true);
- break;
- }
+ devices_changed(device.media, device.is_sink);
}
return Source.CONTINUE;
}
@@ -253,6 +259,7 @@ public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
// }
public Stream open_stream(Xmpp.Xep.Jingle.Content content) {
+ init_call_pipe();
var content_params = content.content_params as Xmpp.Xep.JingleRtp.Parameters;
if (content_params == null) return null;
Stream stream;
@@ -271,15 +278,15 @@ public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
}
public void shutdown() {
- device_monitor.stop();
- pipe.set_state(Gst.State.NULL);
- rtpbin = null;
- pipe = null;
+ if (device_monitor != null) {
+ device_monitor.stop();
+ }
+ destroy_call_pipe();
Gst.deinit();
}
public bool supports(string media) {
- if (rtpbin == null) return false;
+ if (!codec_util.is_element_supported("rtpbin")) return false;
if (media == "audio") {
if (get_devices("audio", false).is_empty) return false;
@@ -287,7 +294,7 @@ public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
}
if (media == "video") {
- if (Gst.ElementFactory.make("gtksink", null) == null) return false;
+ if (!codec_util.is_element_supported("gtksink")) return false;
if (get_devices("video", false).is_empty) return false;
}
@@ -295,6 +302,7 @@ public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
}
public VideoCallWidget? create_widget(WidgetType type) {
+ init_call_pipe();
if (type == WidgetType.GTK) {
return new VideoWidget(this);
}
@@ -422,10 +430,11 @@ public class Dino.Plugins.Rtp.Plugin : RootInterface, VideoCallPlugin, Object {
}
}
- private void dump_dot() {
+ public void dump_dot() {
+ if (pipe == null) return;
string name = @"pipe-$(pipe.clock.get_time())-$(pipe.current_state)";
Gst.Debug.bin_to_dot_file(pipe, Gst.DebugGraphDetails.ALL, name);
- debug("Stored pipe details as %s", name);
+ print(@"Stored pipe details as $name\n");
}
public void set_pause(Xmpp.Xep.JingleRtp.Stream stream, bool pause) {
diff --git a/plugins/rtp/src/stream.vala b/plugins/rtp/src/stream.vala
index bd8a279f..dc712b61 100644
--- a/plugins/rtp/src/stream.vala
+++ b/plugins/rtp/src/stream.vala
@@ -18,22 +18,19 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
private Gst.App.Sink send_rtcp;
private Gst.App.Src recv_rtp;
private Gst.App.Src recv_rtcp;
- private Gst.Element encode;
- private Gst.RTP.BasePayload encode_pay;
private Gst.Element decode;
private Gst.RTP.BaseDepayload decode_depay;
private Gst.Element input;
+ private Gst.Pad input_pad;
private Gst.Element output;
private Gst.Element session;
private Device _input_device;
public Device input_device { get { return _input_device; } set {
if (!paused) {
- if (this._input_device != null) {
- this._input_device.unlink();
- this._input_device = null;
- }
- set_input(value != null ? value.link_source() : null);
+ var input = this.input;
+ set_input(value != null ? value.link_source(payload_type, our_ssrc, next_seqnum_offset, next_timestamp_offset) : null);
+ if (this._input_device != null) this._input_device.unlink(input);
}
this._input_device = value;
}}
@@ -47,7 +44,16 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
public bool created { get; private set; default = false; }
public bool paused { get; private set; default = false; }
private bool push_recv_data = false;
- private string participant_ssrc = null;
+ private uint our_ssrc = Random.next_int();
+ private int next_seqnum_offset = -1;
+ private uint32 next_timestamp_offset_base = 0;
+ private int64 next_timestamp_offset_stamp = 0;
+ private uint32 next_timestamp_offset { get {
+ if (next_timestamp_offset_base == 0) return 0;
+ int64 monotonic_diff = get_monotonic_time() - next_timestamp_offset_stamp;
+ return next_timestamp_offset_base + (uint32)((double)monotonic_diff / 1000000.0 * payload_type.clockrate);
+ } }
+ private uint32 participant_ssrc = 0;
private Gst.Pad recv_rtcp_sink_pad;
private Gst.Pad recv_rtp_sink_pad;
@@ -92,16 +98,22 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
send_rtp.async = false;
send_rtp.caps = CodecUtil.get_caps(media, payload_type, false);
send_rtp.emit_signals = true;
- send_rtp.sync = false;
+ send_rtp.sync = true;
+ send_rtp.drop = true;
+ send_rtp.wait_on_eos = false;
send_rtp.new_sample.connect(on_new_sample);
+ send_rtp.connect("signal::eos", on_eos_static, this);
pipe.add(send_rtp);
send_rtcp = Gst.ElementFactory.make("appsink", @"rtcp_sink_$rtpid") as Gst.App.Sink;
send_rtcp.async = false;
send_rtcp.caps = new Gst.Caps.empty_simple("application/x-rtcp");
send_rtcp.emit_signals = true;
- send_rtcp.sync = false;
+ send_rtcp.sync = true;
+ send_rtcp.drop = true;
+ send_rtcp.wait_on_eos = false;
send_rtcp.new_sample.connect(on_new_sample);
+ send_rtcp.connect("signal::eos", on_eos_static, this);
pipe.add(send_rtcp);
recv_rtp = Gst.ElementFactory.make("appsrc", @"rtp_src_$rtpid") as Gst.App.Src;
@@ -125,18 +137,15 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
recv_rtcp.get_static_pad("src").link(recv_rtcp_sink_pad);
// Connect input
- encode = codec_util.get_encode_bin(media, payload_type, @"encode_$rtpid");
- encode_pay = (Gst.RTP.BasePayload)((Gst.Bin)encode).get_by_name(@"encode_$(rtpid)_rtp_pay");
- pipe.add(encode);
send_rtp_sink_pad = rtpbin.get_request_pad(@"send_rtp_sink_$rtpid");
- encode.get_static_pad("src").link(send_rtp_sink_pad);
if (input != null) {
- input.link(encode);
+ input_pad = input.get_request_pad(@"src_$rtpid");
+ input_pad.link(send_rtp_sink_pad);
}
// Connect output
decode = codec_util.get_decode_bin(media, payload_type, @"decode_$rtpid");
- decode_depay = (Gst.RTP.BaseDepayload)((Gst.Bin)encode).get_by_name(@"decode_$(rtpid)_rtp_depay");
+ decode_depay = (Gst.RTP.BaseDepayload)((Gst.Bin)decode).get_by_name(@"decode_$(rtpid)_rtp_depay");
pipe.add(decode);
if (output != null) {
decode.link(output);
@@ -151,7 +160,7 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
plugin.unpause();
GLib.Signal.emit_by_name(rtpbin, "get-session", rtpid, out session);
- if (session != null && payload_type.rtcp_fbs.any_match((it) => it.type_ == "goog-remb")) {
+ if (session != null && remb_enabled) {
Object internal_session;
session.@get("internal-session", out internal_session);
if (internal_session != null) {
@@ -159,15 +168,16 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
}
Timeout.add(1000, () => remb_adjust());
}
- if (media == "video") {
- codec_util.update_bitrate(media, payload_type, encode, 256);
+ if (input_device != null && media == "video") {
+ input_device.update_bitrate(payload_type, target_send_bitrate);
}
}
- private uint remb = 256;
private int last_packets_lost = -1;
- private uint64 last_packets_received;
- private uint64 last_octets_received;
+ private uint64 last_packets_received = 0;
+ private uint64 last_octets_received = 0;
+ private uint max_target_receive_bitrate = 0;
+ private int64 last_remb_time = 0;
private bool remb_adjust() {
unowned Gst.Structure? stats;
if (session == null) {
@@ -185,73 +195,95 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
warning("No source-stats for session %u", rtpid);
return Source.REMOVE;
}
+
+ if (input_device == null) return Source.CONTINUE;
+
foreach (Value value in source_stats.values) {
unowned Gst.Structure source_stat = (Gst.Structure) value.get_boxed();
- uint ssrc;
+ uint32 ssrc;
if (!source_stat.get_uint("ssrc", out ssrc)) continue;
- if (ssrc.to_string() == participant_ssrc) {
+ if (ssrc == participant_ssrc) {
int packets_lost;
uint64 packets_received, octets_received;
source_stat.get_int("packets-lost", out packets_lost);
source_stat.get_uint64("packets-received", out packets_received);
source_stat.get_uint64("octets-received", out octets_received);
int new_lost = packets_lost - last_packets_lost;
+ if (new_lost < 0) new_lost = 0;
uint64 new_received = packets_received - last_packets_received;
+ if (packets_received < last_packets_received) new_received = 0;
uint64 new_octets = octets_received - last_octets_received;
+ if (octets_received < last_octets_received) octets_received = 0;
if (new_received == 0) continue;
last_packets_lost = packets_lost;
last_packets_received = packets_received;
last_octets_received = octets_received;
double loss_rate = (double)new_lost / (double)(new_lost + new_received);
+ uint new_target_receive_bitrate;
if (new_lost <= 0 || loss_rate < 0.02) {
- remb = (uint)(1.08 * (double)remb);
+ new_target_receive_bitrate = (uint)(1.08 * (double)target_receive_bitrate);
} else if (loss_rate > 0.1) {
- remb = (uint)((1.0 - 0.5 * loss_rate) * (double)remb);
+ new_target_receive_bitrate = (uint)((1.0 - 0.5 * loss_rate) * (double)target_receive_bitrate);
+ } else {
+ new_target_receive_bitrate = target_receive_bitrate;
}
- remb = uint.max(remb, (uint)((new_octets * 8) / 1000));
- remb = uint.max(16, remb); // Never go below 16
- uint8[] data = new uint8[] {
- 143, 206, 0, 5,
- 0, 0, 0, 0,
- 0, 0, 0, 0,
- 'R', 'E', 'M', 'B',
- 1, 0, 0, 0,
- 0, 0, 0, 0
- };
- data[4] = (uint8)((encode_pay.ssrc >> 24) & 0xff);
- data[5] = (uint8)((encode_pay.ssrc >> 16) & 0xff);
- data[6] = (uint8)((encode_pay.ssrc >> 8) & 0xff);
- data[7] = (uint8)(encode_pay.ssrc & 0xff);
- uint8 br_exp = 0;
- uint32 br_mant = remb * 1000;
- uint8 bits = (uint8)Math.log2(br_mant);
- if (bits > 16) {
- br_exp = (uint8)bits - 16;
- br_mant = br_mant >> br_exp;
+ if (last_remb_time == 0) {
+ last_remb_time = get_monotonic_time();
+ } else {
+ int64 time_now = get_monotonic_time();
+ int64 time_diff = time_now - last_remb_time;
+ last_remb_time = time_now;
+ uint actual_bitrate = (uint)(((double)new_octets * 8.0) * (double)time_diff / 1000.0 / 1000000.0);
+ new_target_receive_bitrate = uint.max(new_target_receive_bitrate, (uint)(0.9 * (double)actual_bitrate));
+ max_target_receive_bitrate = uint.max((uint)(1.5 * (double)actual_bitrate), max_target_receive_bitrate);
+ new_target_receive_bitrate = uint.min(new_target_receive_bitrate, max_target_receive_bitrate);
+ }
+ new_target_receive_bitrate = uint.max(16, new_target_receive_bitrate); // Never go below 16
+ if (new_target_receive_bitrate != target_receive_bitrate) {
+ target_receive_bitrate = new_target_receive_bitrate;
+ uint8[] data = new uint8[] {
+ 143, 206, 0, 5,
+ 0, 0, 0, 0,
+ 0, 0, 0, 0,
+ 'R', 'E', 'M', 'B',
+ 1, 0, 0, 0,
+ 0, 0, 0, 0
+ };
+ data[4] = (uint8)((our_ssrc >> 24) & 0xff);
+ data[5] = (uint8)((our_ssrc >> 16) & 0xff);
+ data[6] = (uint8)((our_ssrc >> 8) & 0xff);
+ data[7] = (uint8)(our_ssrc & 0xff);
+ uint8 br_exp = 0;
+ uint32 br_mant = target_receive_bitrate * 1000;
+ uint8 bits = (uint8)Math.log2(br_mant);
+ if (bits > 16) {
+ br_exp = (uint8)bits - 16;
+ br_mant = br_mant >> br_exp;
+ }
+ data[17] = (uint8)((br_exp << 2) | ((br_mant >> 16) & 0x3));
+ data[18] = (uint8)((br_mant >> 8) & 0xff);
+ data[19] = (uint8)(br_mant & 0xff);
+ data[20] = (uint8)((ssrc >> 24) & 0xff);
+ data[21] = (uint8)((ssrc >> 16) & 0xff);
+ data[22] = (uint8)((ssrc >> 8) & 0xff);
+ data[23] = (uint8)(ssrc & 0xff);
+ encrypt_and_send_rtcp(data);
}
- data[17] = (uint8)((br_exp << 2) | ((br_mant >> 16) & 0x3));
- data[18] = (uint8)((br_mant >> 8) & 0xff);
- data[19] = (uint8)(br_mant & 0xff);
- data[20] = (uint8)((ssrc >> 24) & 0xff);
- data[21] = (uint8)((ssrc >> 16) & 0xff);
- data[22] = (uint8)((ssrc >> 8) & 0xff);
- data[23] = (uint8)(ssrc & 0xff);
- encrypt_and_send_rtcp(data);
}
}
return Source.CONTINUE;
}
private static void on_feedback_rtcp(Gst.Element session, uint type, uint fbtype, uint sender_ssrc, uint media_ssrc, Gst.Buffer? fci, Stream self) {
- if (type == 206 && fbtype == 15 && fci != null && sender_ssrc.to_string() == self.participant_ssrc) {
+ if (self.input_device != null && self.media == "video" && type == 206 && fbtype == 15 && fci != null && sender_ssrc == self.participant_ssrc) {
// https://tools.ietf.org/html/draft-alvestrand-rmcat-remb-03
uint8[] data;
fci.extract_dup(0, fci.get_size(), out data);
if (data[0] != 'R' || data[1] != 'E' || data[2] != 'M' || data[3] != 'B') return;
uint8 br_exp = data[5] >> 2;
uint32 br_mant = (((uint32)data[5] & 0x3) << 16) + ((uint32)data[6] << 8) + (uint32)data[7];
- uint bitrate = (br_mant << br_exp) / 1000;
- self.codec_util.update_bitrate(self.media, self.payload_type, self.encode, bitrate * 8);
+ self.target_send_bitrate = (br_mant << br_exp) / 1000;
+ self.input_device.update_bitrate(self.payload_type, self.target_send_bitrate);
}
}
@@ -267,32 +299,63 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
debug("Sink is null");
return Gst.FlowReturn.EOS;
}
+ if (sink != send_rtp && sink != send_rtcp) {
+ warning("unknown sample");
+ return Gst.FlowReturn.NOT_SUPPORTED;
+ }
Gst.Sample sample = sink.pull_sample();
Gst.Buffer buffer = sample.get_buffer();
+ if (sink == send_rtp) {
+ uint buffer_ssrc = 0, buffer_seq = 0;
+ Gst.RTP.Buffer rtp_buffer;
+ if (Gst.RTP.Buffer.map(buffer, Gst.MapFlags.READ, out rtp_buffer)) {
+ buffer_ssrc = rtp_buffer.get_ssrc();
+ buffer_seq = rtp_buffer.get_seq();
+ next_seqnum_offset = rtp_buffer.get_seq() + 1;
+ next_timestamp_offset_base = rtp_buffer.get_timestamp();
+ next_timestamp_offset_stamp = get_monotonic_time();
+ rtp_buffer.unmap();
+ }
+ if (our_ssrc != buffer_ssrc) {
+ warning("Sending RTP %s buffer seq %u with SSRC %u when our ssrc is %u", media, buffer_seq, buffer_ssrc, our_ssrc);
+ } else {
+ debug("Sending RTP %s buffer seq %u with SSRC %u", media, buffer_seq, buffer_ssrc);
+ }
+ }
+
+ prepare_local_crypto();
+
uint8[] data;
buffer.extract_dup(0, buffer.get_size(), out data);
- prepare_local_crypto();
if (sink == send_rtp) {
- if (crypto_session.has_encrypt) {
- data = crypto_session.encrypt_rtp(data);
- }
- on_send_rtp_data(new Bytes.take((owned) data));
+ encrypt_and_send_rtp((owned) data);
} else if (sink == send_rtcp) {
encrypt_and_send_rtcp((owned) data);
- } else {
- warning("unknown sample");
}
return Gst.FlowReturn.OK;
}
+ private void encrypt_and_send_rtp(owned uint8[] data) {
+ Bytes bytes;
+ if (crypto_session.has_encrypt) {
+ bytes = new Bytes.take(crypto_session.encrypt_rtp(data));
+ } else {
+ bytes = new Bytes.take(data);
+ }
+ on_send_rtp_data(bytes);
+ }
+
private void encrypt_and_send_rtcp(owned uint8[] data) {
+ Bytes bytes;
if (crypto_session.has_encrypt) {
- data = crypto_session.encrypt_rtcp(data);
+ bytes = new Bytes.take(crypto_session.encrypt_rtcp(data));
+ } else {
+ bytes = new Bytes.take(data);
}
if (rtcp_mux) {
- on_send_rtp_data(new Bytes.take((owned) data));
+ on_send_rtp_data(bytes);
} else {
- on_send_rtcp_data(new Bytes.take((owned) data));
+ on_send_rtcp_data(bytes);
}
}
@@ -300,41 +363,59 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
return Gst.PadProbeReturn.DROP;
}
+ private static void on_eos_static(Gst.App.Sink sink, Stream self) {
+ debug("EOS on %s", sink.name);
+ if (sink == self.send_rtp) {
+ Idle.add(() => { self.on_send_rtp_eos(); return Source.REMOVE; });
+ } else if (sink == self.send_rtcp) {
+ Idle.add(() => { self.on_send_rtcp_eos(); return Source.REMOVE; });
+ }
+ }
+
+ private void on_send_rtp_eos() {
+ if (send_rtp_src_pad != null) {
+ send_rtp_src_pad.unlink(send_rtp.get_static_pad("sink"));
+ send_rtp_src_pad = null;
+ }
+ send_rtp.set_locked_state(true);
+ send_rtp.set_state(Gst.State.NULL);
+ pipe.remove(send_rtp);
+ send_rtp = null;
+ debug("Stopped sending RTP for %u", rtpid);
+ }
+
+ private void on_send_rtcp_eos() {
+ send_rtcp.set_locked_state(true);
+ send_rtcp.set_state(Gst.State.NULL);
+ pipe.remove(send_rtcp);
+ send_rtcp = null;
+ debug("Stopped sending RTCP for %u", rtpid);
+ }
+
public override void destroy() {
// Stop network communication
push_recv_data = false;
- recv_rtp.end_of_stream();
- recv_rtcp.end_of_stream();
- send_rtp.new_sample.disconnect(on_new_sample);
- send_rtcp.new_sample.disconnect(on_new_sample);
+ if (recv_rtp != null) recv_rtp.end_of_stream();
+ if (recv_rtcp != null) recv_rtcp.end_of_stream();
+ if (send_rtp != null) send_rtp.new_sample.disconnect(on_new_sample);
+ if (send_rtcp != null) send_rtcp.new_sample.disconnect(on_new_sample);
// Disconnect input device
if (input != null) {
- input.unlink(encode);
- input = null;
+ input_pad.unlink(send_rtp_sink_pad);
+ input.release_request_pad(input_pad);
+ input_pad = null;
}
if (this._input_device != null) {
- if (!paused) this._input_device.unlink();
+ if (!paused) this._input_device.unlink(input);
this._input_device = null;
+ this.input = null;
}
- // Disconnect encode
- encode.set_locked_state(true);
- encode.set_state(Gst.State.NULL);
- encode.get_static_pad("src").unlink(send_rtp_sink_pad);
- pipe.remove(encode);
- encode = null;
- encode_pay = null;
-
- // Disconnect RTP sending
- if (send_rtp_src_pad != null) {
- send_rtp_src_pad.add_probe(Gst.PadProbeType.BLOCK, drop_probe);
- send_rtp_src_pad.unlink(send_rtp.get_static_pad("sink"));
+ // Inject EOS
+ if (send_rtp_sink_pad != null) {
+ send_rtp_sink_pad.send_event(new Gst.Event.eos());
}
- send_rtp.set_locked_state(true);
- send_rtp.set_state(Gst.State.NULL);
- pipe.remove(send_rtp);
- send_rtp = null;
// Disconnect decode
if (recv_rtp_src_pad != null) {
@@ -342,57 +423,63 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
recv_rtp_src_pad.unlink(decode.get_static_pad("sink"));
}
- // Disconnect RTP receiving
- recv_rtp.set_locked_state(true);
- recv_rtp.set_state(Gst.State.NULL);
- recv_rtp.get_static_pad("src").unlink(recv_rtp_sink_pad);
- pipe.remove(recv_rtp);
- recv_rtp = null;
-
// Disconnect output
if (output != null) {
+ decode.get_static_pad("src").add_probe(Gst.PadProbeType.BLOCK, drop_probe);
decode.unlink(output);
}
- decode.set_locked_state(true);
- decode.set_state(Gst.State.NULL);
- pipe.remove(decode);
- decode = null;
- decode_depay = null;
- output = null;
// Disconnect output device
if (this._output_device != null) {
- this._output_device.unlink();
+ this._output_device.unlink(output);
this._output_device = null;
}
+ output = null;
- // Disconnect RTCP receiving
- recv_rtcp.get_static_pad("src").unlink(recv_rtcp_sink_pad);
- recv_rtcp.set_locked_state(true);
- recv_rtcp.set_state(Gst.State.NULL);
- pipe.remove(recv_rtcp);
- recv_rtcp = null;
+ // Destroy decode
+ if (decode != null) {
+ decode.set_locked_state(true);
+ decode.set_state(Gst.State.NULL);
+ pipe.remove(decode);
+ decode = null;
+ decode_depay = null;
+ }
- // Disconnect RTCP sending
- send_rtcp_src_pad.unlink(send_rtcp.get_static_pad("sink"));
- send_rtcp.set_locked_state(true);
- send_rtcp.set_state(Gst.State.NULL);
- pipe.remove(send_rtcp);
- send_rtcp = null;
+ // Disconnect and remove RTP input
+ if (recv_rtp != null) {
+ recv_rtp.get_static_pad("src").unlink(recv_rtp_sink_pad);
+ recv_rtp.set_locked_state(true);
+ recv_rtp.set_state(Gst.State.NULL);
+ pipe.remove(recv_rtp);
+ recv_rtp = null;
+ }
- // Release rtp pads
- rtpbin.release_request_pad(send_rtp_sink_pad);
- send_rtp_sink_pad = null;
- rtpbin.release_request_pad(recv_rtp_sink_pad);
- recv_rtp_sink_pad = null;
- rtpbin.release_request_pad(recv_rtcp_sink_pad);
- recv_rtcp_sink_pad = null;
- rtpbin.release_request_pad(send_rtcp_src_pad);
- send_rtcp_src_pad = null;
- send_rtp_src_pad = null;
- recv_rtp_src_pad = null;
+ // Disconnect and remove RTCP input
+ if (recv_rtcp != null) {
+ recv_rtcp.get_static_pad("src").unlink(recv_rtcp_sink_pad);
+ recv_rtcp.set_locked_state(true);
+ recv_rtcp.set_state(Gst.State.NULL);
+ pipe.remove(recv_rtcp);
+ recv_rtcp = null;
+ }
- session = null;
+ // Release rtp pads
+ if (send_rtp_sink_pad != null) {
+ rtpbin.release_request_pad(send_rtp_sink_pad);
+ send_rtp_sink_pad = null;
+ }
+ if (recv_rtp_sink_pad != null) {
+ rtpbin.release_request_pad(recv_rtp_sink_pad);
+ recv_rtp_sink_pad = null;
+ }
+ if (send_rtcp_src_pad != null) {
+ rtpbin.release_request_pad(send_rtcp_src_pad);
+ send_rtcp_src_pad = null;
+ }
+ if (recv_rtcp_sink_pad != null) {
+ rtpbin.release_request_pad(recv_rtcp_sink_pad);
+ recv_rtcp_sink_pad = null;
+ }
}
private void prepare_remote_crypto() {
@@ -410,17 +497,38 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
on_recv_rtcp_data(bytes);
return;
}
- prepare_remote_crypto();
- uint8[] data = bytes.get_data();
- if (crypto_session.has_decrypt) {
- try {
- data = crypto_session.decrypt_rtp(data);
- } catch (Error e) {
- warning("%s (%d)", e.message, e.code);
+#if GST_1_16
+ {
+ Gst.Buffer buffer = new Gst.Buffer.wrapped_bytes(bytes);
+ Gst.RTP.Buffer rtp_buffer;
+ uint buffer_ssrc = 0, buffer_seq = 0;
+ if (Gst.RTP.Buffer.map(buffer, Gst.MapFlags.READ, out rtp_buffer)) {
+ buffer_ssrc = rtp_buffer.get_ssrc();
+ buffer_seq = rtp_buffer.get_seq();
+ rtp_buffer.unmap();
}
+ debug("Received RTP %s buffer seq %u with SSRC %u", media, buffer_seq, buffer_ssrc);
}
+#endif
if (push_recv_data) {
- Gst.Buffer buffer = new Gst.Buffer.wrapped((owned) data);
+ prepare_remote_crypto();
+
+ Gst.Buffer buffer;
+ if (crypto_session.has_decrypt) {
+ try {
+ buffer = new Gst.Buffer.wrapped(crypto_session.decrypt_rtp(bytes.get_data()));
+ } catch (Error e) {
+ warning("%s (%d)", e.message, e.code);
+ return;
+ }
+ } else {
+#if GST_1_16
+ buffer = new Gst.Buffer.wrapped_bytes(bytes);
+#else
+ buffer = new Gst.Buffer.wrapped(bytes.get_data());
+#endif
+ }
+
Gst.RTP.Buffer rtp_buffer;
if (Gst.RTP.Buffer.map(buffer, Gst.MapFlags.READ, out rtp_buffer)) {
if (rtp_buffer.get_extension()) {
@@ -448,11 +556,7 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
rtp_buffer.unmap();
}
- // FIXME: VAPI file in Vala < 0.49.1 has a bug that results in broken ownership of buffer in push_buffer()
- // We workaround by using the plain signal. The signal unfortunately will cause an unnecessary copy of
- // the underlying buffer, so and some point we should move over to the new version (once we require
- // Vala >= 0.50)
-#if FIXED_APPSRC_PUSH_BUFFER_IN_VAPI
+#if VALA_0_50
recv_rtp.push_buffer((owned) buffer);
#else
Gst.FlowReturn ret;
@@ -462,19 +566,26 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
}
public override void on_recv_rtcp_data(Bytes bytes) {
- prepare_remote_crypto();
- uint8[] data = bytes.get_data();
- if (crypto_session.has_decrypt) {
- try {
- data = crypto_session.decrypt_rtcp(data);
- } catch (Error e) {
- warning("%s (%d)", e.message, e.code);
- }
- }
if (push_recv_data) {
- Gst.Buffer buffer = new Gst.Buffer.wrapped((owned) data);
- // See above
-#if FIXED_APPSRC_PUSH_BUFFER_IN_VAPI
+ prepare_remote_crypto();
+
+ Gst.Buffer buffer;
+ if (crypto_session.has_decrypt) {
+ try {
+ buffer = new Gst.Buffer.wrapped(crypto_session.decrypt_rtcp(bytes.get_data()));
+ } catch (Error e) {
+ warning("%s (%d)", e.message, e.code);
+ return;
+ }
+ } else {
+#if GST_1_16
+ buffer = new Gst.Buffer.wrapped_bytes(bytes);
+#else
+ buffer = new Gst.Buffer.wrapped(bytes.get_data());
+#endif
+ }
+
+#if VALA_0_50
recv_rtcp.push_buffer((owned) buffer);
#else
Gst.FlowReturn ret;
@@ -502,10 +613,10 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
debug("RTCP is ready, resending rtcp: %s", rtp_sent.to_string());
}
- public void on_ssrc_pad_added(string ssrc, Gst.Pad pad) {
- debug("New ssrc %s with pad %s", ssrc, pad.name);
- if (participant_ssrc != null && participant_ssrc != ssrc) {
- warning("Got second ssrc on stream (old: %s, new: %s), ignoring", participant_ssrc, ssrc);
+ public void on_ssrc_pad_added(uint32 ssrc, Gst.Pad pad) {
+ debug("New ssrc %u with pad %s", ssrc, pad.name);
+ if (participant_ssrc != 0 && participant_ssrc != ssrc) {
+ warning("Got second ssrc on stream (old: %u, new: %u), ignoring", participant_ssrc, ssrc);
return;
}
participant_ssrc = ssrc;
@@ -534,7 +645,9 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
private void set_input_and_pause(Gst.Element? input, bool paused) {
if (created && this.input != null) {
- this.input.unlink(encode);
+ this.input_pad.unlink(send_rtp_sink_pad);
+ this.input.release_request_pad(this.input_pad);
+ this.input_pad = null;
this.input = null;
}
@@ -543,28 +656,42 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
if (created && sending && !paused && input != null) {
plugin.pause();
- input.link(encode);
+ input_pad = input.get_request_pad(@"src_$rtpid");
+ input_pad.link(send_rtp_sink_pad);
plugin.unpause();
}
}
public void pause() {
if (paused) return;
+ var input = this.input;
set_input_and_pause(null, true);
- if (input_device != null) input_device.unlink();
+ if (input != null && input_device != null) input_device.unlink(input);
}
public void unpause() {
if (!paused) return;
- set_input_and_pause(input_device != null ? input_device.link_source() : null, false);
+ set_input_and_pause(input_device != null ? input_device.link_source(payload_type, our_ssrc, next_seqnum_offset, next_timestamp_offset) : null, false);
+ input_device.update_bitrate(payload_type, target_send_bitrate);
+ }
+
+ public uint get_participant_ssrc(Xmpp.Jid participant) {
+ if (participant.equals(content.session.peer_full_jid)) {
+ return participant_ssrc;
+ }
+ return 0;
}
ulong block_probe_handler_id = 0;
- public virtual void add_output(Gst.Element element) {
+ public virtual void add_output(Gst.Element element, Xmpp.Jid? participant = null) {
if (output != null) {
critical("add_output() invoked more than once");
return;
}
+ if (participant != null) {
+ critical("add_output() invoked with participant when not supported");
+ return;
+ }
this.output = element;
if (created) {
plugin.pause();
@@ -586,7 +713,7 @@ public class Dino.Plugins.Rtp.Stream : Xmpp.Xep.JingleRtp.Stream {
decode.unlink(element);
}
if (this._output_device != null) {
- this._output_device.unlink();
+ this._output_device.unlink(element);
this._output_device = null;
}
this.output = null;
@@ -657,7 +784,7 @@ public class Dino.Plugins.Rtp.VideoStream : Stream {
disconnect(video_orientation_changed_handler);
}
- public override void add_output(Gst.Element element) {
+ public override void add_output(Gst.Element element, Xmpp.Jid? participant) {
if (element == output_tee || element == rotate) {
base.add_output(element);
return;
@@ -678,4 +805,4 @@ public class Dino.Plugins.Rtp.VideoStream : Stream {
output_tee.unlink(element);
}
}
-} \ No newline at end of file
+}
diff --git a/plugins/rtp/src/video_widget.vala b/plugins/rtp/src/video_widget.vala
index 351069a7..3daf5284 100644
--- a/plugins/rtp/src/video_widget.vala
+++ b/plugins/rtp/src/video_widget.vala
@@ -12,8 +12,9 @@ public class Dino.Plugins.Rtp.VideoWidget : Gtk.Bin, Dino.Plugins.VideoCallWidge
private bool attached;
private Device? connected_device;
+ private Gst.Element? connected_device_element;
private Stream? connected_stream;
- private Gst.Element convert;
+ private Gst.Element prepare;
public VideoWidget(Plugin plugin) {
this.plugin = plugin;
@@ -24,26 +25,38 @@ public class Dino.Plugins.Rtp.VideoWidget : Gtk.Bin, Dino.Plugins.VideoCallWidge
Gtk.Widget widget;
element.@get("widget", out widget);
element.@set("async", false);
- element.@set("sync", false);
+ element.@set("sync", true);
this.widget = widget;
add(widget);
widget.visible = true;
-
- // Listen for resolution changes
- element.get_static_pad("sink").notify["caps"].connect(() => {
- if (element.get_static_pad("sink").caps == null) return;
-
- int width, height;
- element.get_static_pad("sink").caps.get_structure(0).get_int("width", out width);
- element.get_static_pad("sink").caps.get_structure(0).get_int("height", out height);
- resolution_changed(width, height);
- });
} else {
warning("Could not create GTK video sink. Won't display videos.");
}
+ size_allocate.connect_after(after_size_allocate);
+ }
+
+ public void input_caps_changed(GLib.Object pad, ParamSpec spec) {
+ Gst.Caps? caps = (pad as Gst.Pad).caps;
+ if (caps == null) return;
+
+ int width, height;
+ caps.get_structure(0).get_int("width", out width);
+ caps.get_structure(0).get_int("height", out height);
+ resolution_changed(width, height);
+ }
+
+ public void after_size_allocate(Gtk.Allocation allocation) {
+ if (prepare != null) {
+ Gst.Element crop = ((Gst.Bin)prepare).get_by_name(@"video_widget_$(id)_crop");
+ if (crop != null) {
+ Value ratio = new Value(typeof(Gst.Fraction));
+ Gst.Value.set_fraction(ref ratio, allocation.width, allocation.height);
+ crop.set_property("aspect-ratio", ratio);
+ }
+ }
}
- public void display_stream(Xmpp.Xep.JingleRtp.Stream stream) {
+ public void display_stream(Xmpp.Xep.JingleRtp.Stream stream, Xmpp.Jid jid) {
if (element == null) return;
detach();
if (stream.media != "video") return;
@@ -51,11 +64,12 @@ public class Dino.Plugins.Rtp.VideoWidget : Gtk.Bin, Dino.Plugins.VideoCallWidge
if (connected_stream == null) return;
plugin.pause();
pipe.add(element);
- convert = Gst.parse_bin_from_description(@"videoconvert name=video_widget_$(id)_convert", true);
- convert.name = @"video_widget_$(id)_prepare";
- pipe.add(convert);
- convert.link(element);
- connected_stream.add_output(convert);
+ prepare = Gst.parse_bin_from_description(@"aspectratiocrop aspect-ratio=4/3 name=video_widget_$(id)_crop ! videoconvert name=video_widget_$(id)_convert", true);
+ prepare.name = @"video_widget_$(id)_prepare";
+ prepare.get_static_pad("sink").notify["caps"].connect(input_caps_changed);
+ pipe.add(prepare);
+ connected_stream.add_output(prepare);
+ prepare.link(element);
element.set_locked_state(false);
plugin.unpause();
attached = true;
@@ -68,11 +82,13 @@ public class Dino.Plugins.Rtp.VideoWidget : Gtk.Bin, Dino.Plugins.VideoCallWidge
if (connected_device == null) return;
plugin.pause();
pipe.add(element);
- convert = Gst.parse_bin_from_description(@"videoflip method=horizontal-flip name=video_widget_$(id)_flip ! videoconvert name=video_widget_$(id)_convert", true);
- convert.name = @"video_widget_$(id)_prepare";
- pipe.add(convert);
- convert.link(element);
- connected_device.link_source().link(convert);
+ prepare = Gst.parse_bin_from_description(@"aspectratiocrop aspect-ratio=4/3 name=video_widget_$(id)_crop ! videoflip method=horizontal-flip name=video_widget_$(id)_flip ! videoconvert name=video_widget_$(id)_convert", true);
+ prepare.name = @"video_widget_$(id)_prepare";
+ prepare.get_static_pad("sink").notify["caps"].connect(input_caps_changed);
+ pipe.add(prepare);
+ connected_device_element = connected_device.link_source();
+ connected_device_element.link(prepare);
+ prepare.link(element);
element.set_locked_state(false);
plugin.unpause();
attached = true;
@@ -82,19 +98,19 @@ public class Dino.Plugins.Rtp.VideoWidget : Gtk.Bin, Dino.Plugins.VideoCallWidge
if (element == null) return;
if (attached) {
if (connected_stream != null) {
- connected_stream.remove_output(convert);
+ connected_stream.remove_output(prepare);
connected_stream = null;
}
if (connected_device != null) {
- connected_device.link_source().unlink(element);
- connected_device.unlink(); // We get a new ref to recover the element, so unlink twice
+ connected_device_element.unlink(element);
+ connected_device_element = null;
connected_device.unlink();
connected_device = null;
}
- convert.set_locked_state(true);
- convert.set_state(Gst.State.NULL);
- pipe.remove(convert);
- convert = null;
+ prepare.set_locked_state(true);
+ prepare.set_state(Gst.State.NULL);
+ pipe.remove(prepare);
+ prepare = null;
element.set_locked_state(true);
element.set_state(Gst.State.NULL);
pipe.remove(element);