nss: upgrade to release 3.73
[LibreOffice.git] / avmedia / source / gstreamer / gstframegrabber.cxx
blob7da1917a019b3570974dc133511b98d933f64ba8
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "gstframegrabber.hxx"
21 #include "gstplayer.hxx"
23 #include <cppuhelper/supportsservice.hxx>
25 #include <gst/gstbuffer.h>
26 #include <gst/video/video.h>
27 #include <gst/video/gstvideosink.h>
28 #include <o3tl/safeint.hxx>
29 #include <vcl/graph.hxx>
30 #include <vcl/BitmapTools.hxx>
32 #include <string>
34 #define AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_GStreamer"
35 #define AVMEDIA_GST_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_GStreamer"
37 using namespace ::com::sun::star;
39 namespace avmedia::gstreamer {
41 void FrameGrabber::disposePipeline()
43 if( mpPipeline != nullptr )
45 gst_element_set_state( mpPipeline, GST_STATE_NULL );
46 g_object_unref( G_OBJECT( mpPipeline ) );
47 mpPipeline = nullptr;
51 FrameGrabber::FrameGrabber( const OUString &rURL ) :
52 FrameGrabber_BASE()
54 gchar *pPipelineStr;
55 pPipelineStr = g_strdup_printf(
56 "uridecodebin uri=%s ! videoconvert ! videoscale ! appsink "
57 "name=sink caps=\"video/x-raw,format=RGB,pixel-aspect-ratio=1/1\"",
58 OUStringToOString( rURL, RTL_TEXTENCODING_UTF8 ).getStr() );
60 GError *pError = nullptr;
61 mpPipeline = gst_parse_launch( pPipelineStr, &pError );
62 if( pError != nullptr) {
63 g_warning( "Failed to construct frame-grabber pipeline '%s'\n", pError->message );
64 g_error_free( pError );
65 disposePipeline();
68 if( mpPipeline ) {
69 // pre-roll
70 switch( gst_element_set_state( mpPipeline, GST_STATE_PAUSED ) ) {
71 case GST_STATE_CHANGE_FAILURE:
72 case GST_STATE_CHANGE_NO_PREROLL:
73 g_warning( "failure pre-rolling media" );
74 disposePipeline();
75 break;
76 default:
77 break;
80 if( mpPipeline &&
81 gst_element_get_state( mpPipeline, nullptr, nullptr, 5 * GST_SECOND ) == GST_STATE_CHANGE_FAILURE )
82 disposePipeline();
85 FrameGrabber::~FrameGrabber()
87 disposePipeline();
90 FrameGrabber* FrameGrabber::create( const OUString &rURL )
92 return new FrameGrabber( rURL );
95 uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
97 uno::Reference< graphic::XGraphic > xRet;
99 if( !mpPipeline )
100 return xRet;
102 gint64 gst_position = llround( fMediaTime * GST_SECOND );
103 gst_element_seek_simple(
104 mpPipeline, GST_FORMAT_TIME,
105 GstSeekFlags(GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_FLUSH),
106 gst_position );
108 GstElement *pSink = gst_bin_get_by_name( GST_BIN( mpPipeline ), "sink" );
109 if( !pSink )
110 return xRet;
112 GstBuffer *pBuf = nullptr;
113 GstCaps *pCaps = nullptr;
115 // synchronously fetch the frame
116 GstSample *pSample = nullptr;
117 g_signal_emit_by_name( pSink, "pull-preroll", &pSample, nullptr );
119 if( pSample )
121 pBuf = gst_sample_get_buffer( pSample );
122 pCaps = gst_sample_get_caps( pSample );
125 // get geometry
126 int nWidth = 0, nHeight = 0;
127 if( !pCaps )
128 g_warning( "could not get snapshot format\n" );
129 else
131 GstStructure *pStruct = gst_caps_get_structure( pCaps, 0 );
133 /* we need to get the final caps on the buffer to get the size */
134 if( !gst_structure_get_int( pStruct, "width", &nWidth ) ||
135 !gst_structure_get_int( pStruct, "height", &nHeight ) )
136 nWidth = nHeight = 0;
139 if( pBuf && nWidth > 0 && nHeight > 0 &&
140 // sanity check the size
141 gst_buffer_get_size( pBuf ) >= o3tl::make_unsigned( nWidth * nHeight * 3 )
144 sal_uInt8 *pData = nullptr;
145 GstMapInfo aMapInfo;
146 gst_buffer_map( pBuf, &aMapInfo, GST_MAP_READ );
147 pData = aMapInfo.data;
149 int nStride = GST_ROUND_UP_4( nWidth * 3 );
150 BitmapEx aBmp = vcl::bitmap::CreateFromData(pData, nWidth, nHeight, nStride, 24 );
152 gst_buffer_unmap( pBuf, &aMapInfo );
153 xRet = Graphic( aBmp ).GetXGraphic();
156 return xRet;
159 OUString SAL_CALL FrameGrabber::getImplementationName( )
161 return AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME;
164 sal_Bool SAL_CALL FrameGrabber::supportsService( const OUString& ServiceName )
166 return cppu::supportsService(this, ServiceName);
169 uno::Sequence< OUString > SAL_CALL FrameGrabber::getSupportedServiceNames()
171 return { AVMEDIA_GST_FRAMEGRABBER_SERVICENAME };
174 } // namespace
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */