mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-11-29 21:21:12 +00:00
compositor: send translated navigation events to the relevant sink pads
Fixes #888 Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1495>
This commit is contained in:
parent
03ff52ee69
commit
6fd7e7699d
3 changed files with 224 additions and 1 deletions
|
@ -422,6 +422,15 @@ _mixer_pad_get_output_size (GstCompositor * comp, GstCompositorPad * comp_pad,
|
|||
*height = pad_height;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
is_point_contained (const GstVideoRectangle rect, const gint px, const gint py)
|
||||
{
|
||||
if ((px >= rect.x) && (px <= rect.x + rect.w) &&
|
||||
(py >= rect.y) && (py <= rect.y + rect.h))
|
||||
return TRUE;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Test whether rectangle2 contains rectangle 1 (geometrically) */
|
||||
static gboolean
|
||||
is_rectangle_contained (const GstVideoRectangle rect1,
|
||||
|
@ -1512,6 +1521,79 @@ gst_compositor_release_pad (GstElement * element, GstPad * pad)
|
|||
GST_ELEMENT_CLASS (parent_class)->release_pad (element, pad);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
src_pad_mouse_event (GstElement * element, GstPad * pad, gpointer user_data)
|
||||
{
|
||||
GstVideoAggregator *vagg = GST_VIDEO_AGGREGATOR_CAST (element);
|
||||
GstCompositor *comp = GST_COMPOSITOR (element);
|
||||
GstCompositorPad *cpad = GST_COMPOSITOR_PAD (pad);
|
||||
GstStructure *st =
|
||||
gst_structure_copy (gst_event_get_structure (GST_EVENT_CAST (user_data)));
|
||||
gdouble event_x, event_y;
|
||||
gint offset_x, offset_y;
|
||||
GstVideoRectangle rect;
|
||||
|
||||
gst_structure_get (st, "pointer_x", G_TYPE_DOUBLE, &event_x,
|
||||
"pointer_y", G_TYPE_DOUBLE, &event_y, NULL);
|
||||
|
||||
/* Find output rectangle of this pad */
|
||||
_mixer_pad_get_output_size (comp, cpad,
|
||||
GST_VIDEO_INFO_PAR_N (&vagg->info),
|
||||
GST_VIDEO_INFO_PAR_D (&vagg->info),
|
||||
&(rect.w), &(rect.h), &offset_x, &offset_y);
|
||||
rect.x = cpad->xpos + offset_x;
|
||||
rect.y = cpad->ypos + offset_y;
|
||||
|
||||
/* Translate coordinates and send event if it lies in this rectangle */
|
||||
if (is_point_contained (rect, event_x, event_y)) {
|
||||
GstVideoAggregatorPad *vpad = GST_VIDEO_AGGREGATOR_PAD_CAST (cpad);
|
||||
gdouble w, h, x, y;
|
||||
|
||||
w = (gdouble) GST_VIDEO_INFO_WIDTH (&vpad->info);
|
||||
h = (gdouble) GST_VIDEO_INFO_HEIGHT (&vpad->info);
|
||||
x = (event_x - (gdouble) rect.x) * (w / (gdouble) rect.w);
|
||||
y = (event_y - (gdouble) rect.y) * (h / (gdouble) rect.h);
|
||||
|
||||
gst_structure_set (st, "pointer_x", G_TYPE_DOUBLE, x,
|
||||
"pointer_y", G_TYPE_DOUBLE, y, NULL);
|
||||
gst_pad_push_event (pad, gst_event_new_navigation (st));
|
||||
} else {
|
||||
gst_structure_free (st);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_src_event (GstAggregator * agg, GstEvent * event)
|
||||
{
|
||||
GstNavigationEventType event_type;
|
||||
|
||||
switch (GST_EVENT_TYPE (event)) {
|
||||
case GST_EVENT_NAVIGATION:
|
||||
{
|
||||
event_type = gst_navigation_event_get_type (event);
|
||||
switch (event_type) {
|
||||
case GST_NAVIGATION_EVENT_MOUSE_BUTTON_PRESS:
|
||||
case GST_NAVIGATION_EVENT_MOUSE_BUTTON_RELEASE:
|
||||
case GST_NAVIGATION_EVENT_MOUSE_MOVE:
|
||||
case GST_NAVIGATION_EVENT_MOUSE_SCROLL:
|
||||
gst_element_foreach_sink_pad (GST_ELEMENT_CAST (agg),
|
||||
src_pad_mouse_event, event);
|
||||
gst_event_unref (event);
|
||||
return TRUE;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return GST_AGGREGATOR_CLASS (parent_class)->src_event (agg, event);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_sink_query (GstAggregator * agg, GstAggregatorPad * bpad, GstQuery * query)
|
||||
{
|
||||
|
@ -1585,6 +1667,7 @@ gst_compositor_class_init (GstCompositorClass * klass)
|
|||
gstelement_class->release_pad =
|
||||
GST_DEBUG_FUNCPTR (gst_compositor_release_pad);
|
||||
agg_class->sink_query = _sink_query;
|
||||
agg_class->src_event = _src_event;
|
||||
agg_class->fixate_src_caps = _fixate_caps;
|
||||
agg_class->negotiated_src_caps = _negotiated_caps;
|
||||
videoaggregator_class->aggregate_frames = gst_compositor_aggregate_frames;
|
||||
|
|
|
@ -280,6 +280,145 @@ GST_START_TEST (test_event)
|
|||
|
||||
GST_END_TEST;
|
||||
|
||||
typedef struct _ProbeEvent
|
||||
{
|
||||
gboolean received;
|
||||
gdouble x_pos, y_pos;
|
||||
} ProbeEvent;
|
||||
|
||||
static GstPadProbeReturn
|
||||
probe_nav_event (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
|
||||
{
|
||||
GstEvent *event = gst_pad_probe_info_get_event (info);
|
||||
ProbeEvent *probe_ev = (ProbeEvent *) user_data;
|
||||
|
||||
if (GST_EVENT_TYPE (event) == GST_EVENT_NAVIGATION) {
|
||||
probe_ev->received = TRUE;
|
||||
gst_navigation_event_parse_mouse_move_event (event,
|
||||
&(probe_ev->x_pos), &(probe_ev->y_pos));
|
||||
}
|
||||
|
||||
return GST_PAD_PROBE_OK;
|
||||
}
|
||||
|
||||
GST_START_TEST (test_navigation_events)
|
||||
{
|
||||
GstElement *bin, *src1, *src2, *src3, *filter1, *filter2, *filter3;
|
||||
GstElement *compositor, *sink;
|
||||
GstPad *srcpad, *sinkpad;
|
||||
GstCaps *caps;
|
||||
gboolean res;
|
||||
ProbeEvent probe_events[3];
|
||||
GstStateChangeReturn state_res;
|
||||
GstEvent *event;
|
||||
|
||||
GST_INFO ("preparing test");
|
||||
|
||||
/* build pipeline */
|
||||
bin = gst_pipeline_new ("pipeline");
|
||||
src1 = gst_element_factory_make ("videotestsrc", "src1");
|
||||
src2 = gst_element_factory_make ("videotestsrc", "src2");
|
||||
src3 = gst_element_factory_make ("videotestsrc", "src3");
|
||||
filter1 = gst_element_factory_make ("capsfilter", "filter1");
|
||||
filter2 = gst_element_factory_make ("capsfilter", "filter2");
|
||||
filter3 = gst_element_factory_make ("capsfilter", "filter3");
|
||||
compositor = gst_element_factory_make ("compositor", "compositor");
|
||||
sink = gst_element_factory_make ("fakesink", "sink");
|
||||
gst_bin_add_many (GST_BIN (bin), src1, src2, src3, filter1, filter2, filter3,
|
||||
compositor, sink, NULL);
|
||||
|
||||
/* configure capsfilters */
|
||||
caps = gst_caps_from_string ("video/x-raw,width=800,height=400");
|
||||
g_object_set (filter1, "caps", caps, NULL);
|
||||
gst_caps_unref (caps);
|
||||
caps = gst_caps_from_string ("video/x-raw,width=400,height=200");
|
||||
g_object_set (filter2, "caps", caps, NULL);
|
||||
gst_caps_unref (caps);
|
||||
caps = gst_caps_from_string ("video/x-raw,width=200,height=50");
|
||||
g_object_set (filter3, "caps", caps, NULL);
|
||||
gst_caps_unref (caps);
|
||||
|
||||
res = gst_element_link_many (src1, filter1, compositor, NULL);
|
||||
fail_unless (res == TRUE, NULL);
|
||||
res = gst_element_link_many (src2, filter2, compositor, NULL);
|
||||
fail_unless (res == TRUE, NULL);
|
||||
res = gst_element_link_many (src3, filter3, compositor, NULL);
|
||||
fail_unless (res == TRUE, NULL);
|
||||
res = gst_element_link (compositor, sink);
|
||||
fail_unless (res == TRUE, NULL);
|
||||
|
||||
srcpad = gst_element_get_static_pad (compositor, "src");
|
||||
gst_object_unref (srcpad);
|
||||
|
||||
/* configure pads and add probes */
|
||||
srcpad = gst_element_get_static_pad (filter1, "src");
|
||||
sinkpad = gst_pad_get_peer (srcpad);
|
||||
g_object_set (sinkpad,
|
||||
"width", 400, "height", 300, "xpos", 200, "ypos", 100, NULL);
|
||||
probe_events[0].received = FALSE;
|
||||
gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
|
||||
probe_nav_event, (gpointer) probe_events, NULL);
|
||||
gst_object_unref (srcpad);
|
||||
gst_object_unref (sinkpad);
|
||||
|
||||
srcpad = gst_element_get_static_pad (filter2, "src");
|
||||
sinkpad = gst_pad_get_peer (srcpad);
|
||||
g_object_set (sinkpad,
|
||||
"width", 400, "height", 200, "xpos", 20, "ypos", 0, NULL);
|
||||
probe_events[1].received = FALSE;
|
||||
gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
|
||||
probe_nav_event, (gpointer) (probe_events + 1), NULL);
|
||||
gst_object_unref (srcpad);
|
||||
gst_object_unref (sinkpad);
|
||||
|
||||
srcpad = gst_element_get_static_pad (filter3, "src");
|
||||
sinkpad = gst_pad_get_peer (srcpad);
|
||||
g_object_set (sinkpad,
|
||||
"width", 200, "height", 50, "xpos", 0, "ypos", 0, NULL);
|
||||
probe_events[2].received = FALSE;
|
||||
gst_pad_add_probe (srcpad, GST_PAD_PROBE_TYPE_EVENT_UPSTREAM,
|
||||
probe_nav_event, (gpointer) (probe_events + 2), NULL);
|
||||
gst_object_unref (srcpad);
|
||||
gst_object_unref (sinkpad);
|
||||
|
||||
event =
|
||||
gst_event_new_navigation (gst_structure_new
|
||||
("application/x-gst-navigation", "event", G_TYPE_STRING, "mouse-move",
|
||||
"button", G_TYPE_INT, 0, "pointer_x", G_TYPE_DOUBLE, 350.0,
|
||||
"pointer_y", G_TYPE_DOUBLE, 100.0, NULL));
|
||||
|
||||
GST_INFO ("starting test");
|
||||
|
||||
/* prepare playing */
|
||||
state_res = gst_element_set_state (bin, GST_STATE_PAUSED);
|
||||
ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
|
||||
|
||||
/* wait for completion */
|
||||
state_res = gst_element_get_state (bin, NULL, NULL, GST_CLOCK_TIME_NONE);
|
||||
ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
|
||||
|
||||
/* send event and validate */
|
||||
res = gst_element_send_event (sink, event);
|
||||
fail_unless (res == TRUE, NULL);
|
||||
|
||||
/* check received events */
|
||||
ck_assert_msg (probe_events[0].received);
|
||||
ck_assert_msg (probe_events[1].received);
|
||||
ck_assert_msg (!probe_events[2].received);
|
||||
|
||||
ck_assert_int_eq ((gint) probe_events[0].x_pos, 300);
|
||||
ck_assert_int_eq ((gint) probe_events[0].y_pos, 0);
|
||||
ck_assert_int_eq ((gint) probe_events[1].x_pos, 330);
|
||||
ck_assert_int_eq ((gint) probe_events[1].y_pos, 100);
|
||||
|
||||
state_res = gst_element_set_state (bin, GST_STATE_NULL);
|
||||
ck_assert_int_ne (state_res, GST_STATE_CHANGE_FAILURE);
|
||||
|
||||
gst_object_unref (bin);
|
||||
}
|
||||
|
||||
GST_END_TEST;
|
||||
|
||||
static GstBuffer *
|
||||
create_video_buffer (GstCaps * caps, gint ts_in_seconds)
|
||||
{
|
||||
|
@ -2386,6 +2525,7 @@ compositor_suite (void)
|
|||
suite_add_tcase (s, tc_chain);
|
||||
tcase_add_test (tc_chain, test_caps);
|
||||
tcase_add_test (tc_chain, test_event);
|
||||
tcase_add_test (tc_chain, test_navigation_events);
|
||||
tcase_add_test (tc_chain, test_caps_query);
|
||||
tcase_add_test (tc_chain, test_caps_query_interlaced);
|
||||
tcase_add_test (tc_chain, test_late_caps_query);
|
||||
|
|
|
@ -2412,7 +2412,7 @@ gst_aggregator_default_src_event (GstAggregator * self, GstEvent * event)
|
|||
/* _do_seek() unrefs the event. */
|
||||
return gst_aggregator_do_seek (self, event);
|
||||
case GST_EVENT_NAVIGATION:
|
||||
/* navigation is rather pointless. */
|
||||
/* specific handling has to be implemented in subclasses */
|
||||
gst_event_unref (event);
|
||||
return FALSE;
|
||||
case GST_EVENT_RECONFIGURE:
|
||||
|
|
Loading…
Reference in a new issue