From eeb4755a3bfbb4998e8ec5060b70ab60fd4ba80f Mon Sep 17 00:00:00 2001 From: Xavi Artigas Date: Tue, 14 May 2013 18:11:04 +0200 Subject: [PATCH] Inform the application about the media size, so it can adapt the UIView to the correct aspect ratio. --- .../xcode iOS/Tutorial 4/GStreamerBackend.m | 37 +++++++++++++++++++ .../Tutorial 4/GStreamerBackendDelegate.h | 3 ++ .../Tutorial 4/VideoViewController.m | 11 ++++++ 3 files changed, 51 insertions(+) diff --git a/gst-sdk/tutorials/xcode iOS/Tutorial 4/GStreamerBackend.m b/gst-sdk/tutorials/xcode iOS/Tutorial 4/GStreamerBackend.m index b15ef43b0b..60758cc336 100644 --- a/gst-sdk/tutorials/xcode iOS/Tutorial 4/GStreamerBackend.m +++ b/gst-sdk/tutorials/xcode iOS/Tutorial 4/GStreamerBackend.m @@ -2,6 +2,7 @@ #include #include +#include GST_DEBUG_CATEGORY_STATIC (debug_category); #define GST_CAT_DEFAULT debug_category @@ -83,6 +84,37 @@ GST_DEBUG_CATEGORY_STATIC (debug_category); } } +static void check_media_size (GStreamerBackend *self) { + GstElement *video_sink; + GstPad *video_sink_pad; + GstCaps *caps; + GstVideoFormat fmt; + int width; + int height; + + /* Retrieve the Caps at the entrance of the video sink */ + g_object_get (self->pipeline, "video-sink", &video_sink, NULL); + video_sink_pad = gst_element_get_static_pad (video_sink, "sink"); + caps = gst_pad_get_negotiated_caps (video_sink_pad); + + if (gst_video_format_parse_caps(caps, &fmt, &width, &height)) { + int par_n, par_d; + if (gst_video_parse_caps_pixel_aspect_ratio (caps, &par_n, &par_d)) { + width = width * par_n / par_d; + } + GST_DEBUG ("Media size is %dx%d, notifying application", width, height); + + if (self->ui_delegate && [self->ui_delegate respondsToSelector:@selector(mediaSizeChanged:height:)]) + { + [self->ui_delegate mediaSizeChanged:width height:height]; + } + } + + gst_caps_unref(caps); + gst_object_unref (video_sink_pad); + gst_object_unref(video_sink); +} + /* Retrieve errors from the bus and show them on the UI */ static void error_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *self) { @@ -109,6 +141,11 @@ static void state_changed_cb (GstBus *bus, GstMessage *msg, GStreamerBackend *se gchar *message = g_strdup_printf("State changed to %s", gst_element_state_get_name(new_state)); [self setUIMessage:message]; g_free (message); + + if (old_state == GST_STATE_READY && new_state == GST_STATE_PAUSED) + { + check_media_size(self); + } } } diff --git a/gst-sdk/tutorials/xcode iOS/Tutorial 4/GStreamerBackendDelegate.h b/gst-sdk/tutorials/xcode iOS/Tutorial 4/GStreamerBackendDelegate.h index 5586373157..739461cfaa 100644 --- a/gst-sdk/tutorials/xcode iOS/Tutorial 4/GStreamerBackendDelegate.h +++ b/gst-sdk/tutorials/xcode iOS/Tutorial 4/GStreamerBackendDelegate.h @@ -11,4 +11,7 @@ * to the screen. */ -(void) gstreamerSetUIMessage:(NSString *)message; +/* Called when the media size is first discovered or it changes */ +-(void) mediaSizeChanged:(NSInteger)width height:(NSInteger)height; + @end diff --git a/gst-sdk/tutorials/xcode iOS/Tutorial 4/VideoViewController.m b/gst-sdk/tutorials/xcode iOS/Tutorial 4/VideoViewController.m index d4bc16d0d6..6b4ae04372 100644 --- a/gst-sdk/tutorials/xcode iOS/Tutorial 4/VideoViewController.m +++ b/gst-sdk/tutorials/xcode iOS/Tutorial 4/VideoViewController.m @@ -96,4 +96,15 @@ }); } +-(void) mediaSizeChanged:(NSInteger)width height:(NSInteger)height +{ + media_width = width; + media_height = height; + dispatch_async(dispatch_get_main_queue(), ^{ + [self viewDidLayoutSubviews]; + [video_view setNeedsLayout]; + [video_view layoutIfNeeded]; + }); +} + @end