2021-12-23 05:39:18 +00:00
|
|
|
#if !VALA_0_52
|
|
|
|
[CCode (cheader_filename = "gst/gst.h")]
|
|
|
|
private static extern void gst_value_set_fraction(ref GLib.Value value, int numerator, int denominator);
|
|
|
|
#endif
|
|
|
|
|
2021-03-21 11:41:38 +00:00
|
|
|
public class Dino.Plugins.Rtp.VideoWidget : Gtk.Bin, Dino.Plugins.VideoCallWidget {
|
2022-01-28 10:01:07 +00:00
|
|
|
private const int RECAPS_AFTER_CHANGE = 5;
|
2021-03-21 11:41:38 +00:00
|
|
|
private static uint last_id = 0;
|
|
|
|
|
|
|
|
public uint id { get; private set; }
|
2022-01-27 21:08:35 +00:00
|
|
|
public Gst.Base.Sink sink { get; private set; }
|
2021-03-21 11:41:38 +00:00
|
|
|
public Gtk.Widget widget { get; private set; }
|
|
|
|
|
|
|
|
public Plugin plugin { get; private set; }
|
|
|
|
public Gst.Pipeline pipe { get {
|
|
|
|
return plugin.pipe;
|
|
|
|
}}
|
|
|
|
|
|
|
|
private bool attached;
|
|
|
|
private Device? connected_device;
|
2021-11-09 21:06:45 +00:00
|
|
|
private Gst.Element? connected_device_element;
|
2021-03-21 11:41:38 +00:00
|
|
|
private Stream? connected_stream;
|
2021-11-09 21:06:45 +00:00
|
|
|
private Gst.Element prepare;
|
2022-01-27 21:08:35 +00:00
|
|
|
private Gst.Caps last_input_caps;
|
|
|
|
private Gst.Caps last_caps;
|
2022-01-28 10:01:07 +00:00
|
|
|
private int recaps_since_change;
|
2021-03-21 11:41:38 +00:00
|
|
|
|
|
|
|
public VideoWidget(Plugin plugin) {
|
|
|
|
this.plugin = plugin;
|
|
|
|
|
|
|
|
id = last_id++;
|
2022-01-27 21:08:35 +00:00
|
|
|
sink = Gst.ElementFactory.make("gtksink", @"video_widget_$id") as Gst.Base.Sink;
|
|
|
|
if (sink != null) {
|
2021-03-21 11:41:38 +00:00
|
|
|
Gtk.Widget widget;
|
2022-01-27 21:08:35 +00:00
|
|
|
sink.@get("widget", out widget);
|
|
|
|
sink.@set("async", false);
|
|
|
|
sink.@set("sync", true);
|
|
|
|
sink.@set("ignore-alpha", false);
|
2021-03-21 11:41:38 +00:00
|
|
|
this.widget = widget;
|
2022-01-28 10:01:07 +00:00
|
|
|
this.widget.draw.connect_after(fix_caps_issues);
|
2021-03-21 11:41:38 +00:00
|
|
|
add(widget);
|
|
|
|
widget.visible = true;
|
|
|
|
} else {
|
|
|
|
warning("Could not create GTK video sink. Won't display videos.");
|
|
|
|
}
|
2021-11-09 21:06:45 +00:00
|
|
|
size_allocate.connect_after(after_size_allocate);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void input_caps_changed(GLib.Object pad, ParamSpec spec) {
|
2022-01-23 18:00:05 +00:00
|
|
|
Gst.Caps? caps = ((Gst.Pad)pad).caps;
|
2022-01-27 21:08:35 +00:00
|
|
|
if (caps == null) {
|
2022-01-28 10:01:07 +00:00
|
|
|
debug("Input: No caps");
|
2022-01-27 21:08:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-11-09 21:06:45 +00:00
|
|
|
|
|
|
|
int width, height;
|
|
|
|
caps.get_structure(0).get_int("width", out width);
|
|
|
|
caps.get_structure(0).get_int("height", out height);
|
2022-01-27 21:08:35 +00:00
|
|
|
debug("Input resolution changed: %ix%i", width, height);
|
2021-11-09 21:06:45 +00:00
|
|
|
resolution_changed(width, height);
|
2022-01-27 21:08:35 +00:00
|
|
|
last_input_caps = caps;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void processed_input_caps_changed(GLib.Object pad, ParamSpec spec) {
|
2022-01-28 10:01:07 +00:00
|
|
|
Gst.Caps? caps = ((Gst.Pad)pad).caps;
|
2022-01-27 21:08:35 +00:00
|
|
|
if (caps == null) {
|
2022-01-28 10:01:07 +00:00
|
|
|
debug("Processed input: No caps");
|
2022-01-27 21:08:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int width, height;
|
|
|
|
caps.get_structure(0).get_int("width", out width);
|
|
|
|
caps.get_structure(0).get_int("height", out height);
|
|
|
|
debug("Processed resolution changed: %ix%i", width, height);
|
|
|
|
sink.set_caps(caps);
|
|
|
|
last_caps = caps;
|
2022-01-28 10:01:07 +00:00
|
|
|
recaps_since_change = 0;
|
2021-11-09 21:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-01-27 21:08:35 +00:00
|
|
|
int output_width = allocation.width;
|
|
|
|
int output_height = allocation.height;
|
|
|
|
int target_num, target_den;
|
|
|
|
if (last_input_caps != null) {
|
|
|
|
int input_width, input_height;
|
|
|
|
last_input_caps.get_structure(0).get_int("width", out input_width);
|
|
|
|
last_input_caps.get_structure(0).get_int("height", out input_height);
|
|
|
|
double target_ratio = 3.0/2.0;
|
|
|
|
double ratio = (double)(output_width*input_height)/(double)(input_width*output_height);
|
|
|
|
if (ratio > target_ratio) {
|
|
|
|
target_num = (int)((double)input_width * target_ratio);
|
|
|
|
target_den = input_height;
|
|
|
|
sink.@set("force-aspect-ratio", true);
|
|
|
|
} else if (ratio < 1.0/target_ratio) {
|
|
|
|
target_num = input_width;
|
|
|
|
target_den = (int)((double)input_height * target_ratio);;
|
|
|
|
sink.@set("force-aspect-ratio", true);
|
|
|
|
} else {
|
|
|
|
target_num = output_width;
|
|
|
|
target_den = output_height;
|
|
|
|
sink.@set("force-aspect-ratio", false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
target_num = output_width;
|
|
|
|
target_den = output_height;
|
|
|
|
sink.@set("force-aspect-ratio", false);
|
|
|
|
}
|
2022-01-23 18:00:05 +00:00
|
|
|
Value ratio = Value(typeof(Gst.Fraction));
|
2021-12-23 05:39:18 +00:00
|
|
|
#if VALA_0_52
|
2022-01-27 21:08:35 +00:00
|
|
|
Gst.Value.set_fraction(ref ratio, target_num, target_den);
|
2021-12-23 05:39:18 +00:00
|
|
|
#else
|
2022-01-27 21:08:35 +00:00
|
|
|
gst_value_set_fraction(ref ratio, target_num, target_den);
|
2021-12-23 05:39:18 +00:00
|
|
|
#endif
|
2021-11-09 21:06:45 +00:00
|
|
|
crop.set_property("aspect-ratio", ratio);
|
|
|
|
}
|
|
|
|
}
|
2021-03-21 11:41:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 21:08:35 +00:00
|
|
|
public bool fix_caps_issues() {
|
|
|
|
// FIXME: Detect if draw would fail and do something better
|
2022-01-28 10:01:07 +00:00
|
|
|
if (last_caps != null && recaps_since_change++ < RECAPS_AFTER_CHANGE) {
|
2022-01-27 21:08:35 +00:00
|
|
|
Gst.Caps? temp = last_caps.copy();
|
|
|
|
temp.set_simple("width", typeof(int), 1, "height", typeof(int), 1, null);
|
|
|
|
sink.set_caps(temp);
|
|
|
|
sink.set_caps(last_caps);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-09 21:06:45 +00:00
|
|
|
public void display_stream(Xmpp.Xep.JingleRtp.Stream stream, Xmpp.Jid jid) {
|
2022-01-27 21:08:35 +00:00
|
|
|
if (sink == null) return;
|
2021-03-21 11:41:38 +00:00
|
|
|
detach();
|
|
|
|
if (stream.media != "video") return;
|
|
|
|
connected_stream = stream as Stream;
|
|
|
|
if (connected_stream == null) return;
|
|
|
|
plugin.pause();
|
2022-01-27 21:08:35 +00:00
|
|
|
pipe.add(sink);
|
2021-11-09 21:06:45 +00:00
|
|
|
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);
|
2022-01-27 21:08:35 +00:00
|
|
|
prepare.get_static_pad("src").notify["caps"].connect(processed_input_caps_changed);
|
2021-11-09 21:06:45 +00:00
|
|
|
pipe.add(prepare);
|
|
|
|
connected_stream.add_output(prepare);
|
2022-01-27 21:08:35 +00:00
|
|
|
prepare.link(sink);
|
|
|
|
sink.set_locked_state(false);
|
2021-03-21 11:41:38 +00:00
|
|
|
plugin.unpause();
|
|
|
|
attached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void display_device(MediaDevice media_device) {
|
2022-01-27 21:08:35 +00:00
|
|
|
if (sink == null) return;
|
2021-03-21 11:41:38 +00:00
|
|
|
detach();
|
|
|
|
connected_device = media_device as Device;
|
|
|
|
if (connected_device == null) return;
|
|
|
|
plugin.pause();
|
2022-01-27 21:08:35 +00:00
|
|
|
pipe.add(sink);
|
2021-11-09 21:06:45 +00:00
|
|
|
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);
|
2022-01-27 21:08:35 +00:00
|
|
|
prepare.link(sink);
|
|
|
|
sink.set_locked_state(false);
|
2021-03-21 11:41:38 +00:00
|
|
|
plugin.unpause();
|
|
|
|
attached = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void detach() {
|
2022-01-27 21:08:35 +00:00
|
|
|
if (sink == null) return;
|
2021-03-21 11:41:38 +00:00
|
|
|
if (attached) {
|
|
|
|
if (connected_stream != null) {
|
2021-11-09 21:06:45 +00:00
|
|
|
connected_stream.remove_output(prepare);
|
2021-03-21 11:41:38 +00:00
|
|
|
connected_stream = null;
|
|
|
|
}
|
|
|
|
if (connected_device != null) {
|
2022-01-27 21:08:35 +00:00
|
|
|
connected_device_element.unlink(sink);
|
2021-11-09 21:06:45 +00:00
|
|
|
connected_device_element = null;
|
2021-03-21 11:41:38 +00:00
|
|
|
connected_device.unlink();
|
|
|
|
connected_device = null;
|
|
|
|
}
|
2021-11-09 21:06:45 +00:00
|
|
|
prepare.set_locked_state(true);
|
|
|
|
prepare.set_state(Gst.State.NULL);
|
|
|
|
pipe.remove(prepare);
|
|
|
|
prepare = null;
|
2022-01-27 21:08:35 +00:00
|
|
|
sink.set_locked_state(true);
|
|
|
|
sink.set_state(Gst.State.NULL);
|
|
|
|
pipe.remove(sink);
|
2021-03-21 11:41:38 +00:00
|
|
|
attached = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void dispose() {
|
|
|
|
detach();
|
|
|
|
widget = null;
|
2022-01-27 21:08:35 +00:00
|
|
|
sink = null;
|
2021-03-21 11:41:38 +00:00
|
|
|
}
|
2022-01-27 21:08:35 +00:00
|
|
|
}
|