From e940b8036eaf371aa15776c5a796b5b4049f0d93 Mon Sep 17 00:00:00 2001 From: Cole Brinsfield Date: Fri, 21 May 2021 19:59:57 -0700 Subject: [PATCH] custom aspect ratios (#21) * Working on proper resizing of AA * allow for custom aspect ratios --- .../openauto/Projection/GSTVideoOutput.hpp | 1 + openauto/Projection/GSTVideoOutput.cpp | 50 ++++++++++++++++++- openauto/Service/ServiceFactory.cpp | 5 ++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/include/openauto/Projection/GSTVideoOutput.hpp b/include/openauto/Projection/GSTVideoOutput.hpp index 440eb0c..08ed16b 100644 --- a/include/openauto/Projection/GSTVideoOutput.hpp +++ b/include/openauto/Projection/GSTVideoOutput.hpp @@ -79,6 +79,7 @@ private: QGst::ElementPtr videoSink_; QQuickWidget* videoWidget_; GstElement* vidPipeline_; + GstVideoFilter* vidCrop_; GstAppSrc* vidSrc_; QWidget* videoContainer_; QGst::Quick::VideoSurface* surface_; diff --git a/openauto/Projection/GSTVideoOutput.cpp b/openauto/Projection/GSTVideoOutput.cpp index b0d7c7f..5d55b02 100644 --- a/openauto/Projection/GSTVideoOutput.cpp +++ b/openauto/Projection/GSTVideoOutput.cpp @@ -15,7 +15,6 @@ * You should have received a copy of the GNU General Public License * along with openauto. If not, see . */ - #ifdef USE_GST #include "aasdk/Common/Data.hpp" @@ -27,6 +26,7 @@ namespace openauto namespace projection { + GSTVideoOutput::GSTVideoOutput(configuration::IConfiguration::Pointer configuration, QWidget* videoContainer, std::function activeCallback) : VideoOutput(std::move(configuration)) , videoContainer_(videoContainer) @@ -53,7 +53,7 @@ GSTVideoOutput::GSTVideoOutput(configuration::IConfiguration::Pointer configurat #else "avdec_h264 ! " #endif - "capsfilter caps=video/x-raw name=mycapsfilter"; + "videocrop top=0 bottom=0 name=videocropper ! capsfilter caps=video/x-raw name=mycapsfilter"; #ifdef RPI OPENAUTO_LOG(info) << "[GSTVideoOutput] RPI Build, running with " << #ifdef PI4 @@ -80,6 +80,8 @@ GSTVideoOutput::GSTVideoOutput(configuration::IConfiguration::Pointer configurat vidSrc_ = GST_APP_SRC(gst_bin_get_by_name(GST_BIN(vidPipeline_), "mysrc")); gst_app_src_set_stream_type(vidSrc_, GST_APP_STREAM_TYPE_STREAM); + vidCrop_ = GST_VIDEO_FILTER(gst_bin_get_by_name(GST_BIN(vidPipeline_), "videocropper")); + connect(this, &GSTVideoOutput::startPlayback, this, &GSTVideoOutput::onStartPlayback, Qt::QueuedConnection); connect(this, &GSTVideoOutput::stopPlayback, this, &GSTVideoOutput::onStopPlayback, Qt::QueuedConnection); } @@ -222,6 +224,50 @@ void GSTVideoOutput::resize() { videoWidget_->resize(videoContainer_->size()); } + + int width = 0; + int height = 0; + int containerWidth = videoContainer_->width(); + int containerHeight = videoContainer_->height(); + + switch(this->getVideoResolution()){ + case aasdk::proto::enums::VideoResolution_Enum__1080p: + width = 1920; + height = 1080; + break; + case aasdk::proto::enums::VideoResolution_Enum__720p: + width = 1280; + height = 720; + break; + case aasdk::proto::enums::VideoResolution_Enum__480p: + width = 800; + height = 480; + break; + } + + double marginWidth = 0; + double marginHeight = 0; + + double widthRatio = (double)containerWidth / width; + double heightRatio = (double)containerHeight / height; + + if(widthRatio > heightRatio){ + //cropping height + marginHeight = (widthRatio * height - containerHeight)/widthRatio; + marginHeight /= 2; + }else{ + //cropping width + marginWidth = (heightRatio * width - containerWidth)/heightRatio; + marginWidth /= 2; + } + + + OPENAUTO_LOG(info) << "[GSTVideoOutput] Android Auto is "<< width << "x" << height << ", calculated margins of: " << marginWidth << "x" << marginHeight; + g_object_set(vidCrop_, "top", (int)marginHeight, nullptr); + g_object_set(vidCrop_, "bottom", (int)marginHeight, nullptr); + g_object_set(vidCrop_, "left", (int)marginWidth, nullptr); + g_object_set(vidCrop_, "right", (int)marginWidth, nullptr); + this->configuration_->setVideoMargins(QRect(0,0,(int)(marginWidth*2), (int)(marginHeight*2))); } } diff --git a/openauto/Service/ServiceFactory.cpp b/openauto/Service/ServiceFactory.cpp index 6cb310a..240a7be 100644 --- a/openauto/Service/ServiceFactory.cpp +++ b/openauto/Service/ServiceFactory.cpp @@ -145,6 +145,11 @@ IService::Pointer ServiceFactory::createInputService(aasdk::messenger::IMessenge break; } + //account for margins being applied to android auto + videoGeometry.setWidth(videoGeometry.width()-configuration_->getVideoMargins().width()); + videoGeometry.setHeight(videoGeometry.height()-configuration_->getVideoMargins().height()); + + QObject* inputObject = activeArea_ == nullptr ? qobject_cast(QApplication::instance()) : qobject_cast(activeArea_); inputDevice_ = std::make_shared(*inputObject, configuration_, std::move(screenGeometry_), std::move(videoGeometry));