1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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
) );
51 FrameGrabber::FrameGrabber( const OUString
&rURL
) :
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
);
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" );
81 gst_element_get_state( mpPipeline
, nullptr, nullptr, 5 * GST_SECOND
) == GST_STATE_CHANGE_FAILURE
)
85 FrameGrabber::~FrameGrabber()
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
;
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
),
108 GstElement
*pSink
= gst_bin_get_by_name( GST_BIN( mpPipeline
), "sink" );
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 );
121 pBuf
= gst_sample_get_buffer( pSample
);
122 pCaps
= gst_sample_get_caps( pSample
);
126 int nWidth
= 0, nHeight
= 0;
128 g_warning( "could not get snapshot format\n" );
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;
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();
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
};
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */