LibreOffice Module avmedia (master) 1
gstframegrabber.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "gstframegrabber.hxx"
21#include "gstplayer.hxx"
22
24
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>
31
32#include <string>
33
34constexpr OUStringLiteral AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME = u"com.sun.star.comp.avmedia.FrameGrabber_GStreamer";
35constexpr OUStringLiteral AVMEDIA_GST_FRAMEGRABBER_SERVICENAME = u"com.sun.star.media.FrameGrabber_GStreamer";
36
37using namespace ::com::sun::star;
38
40
42{
43 if( mpPipeline != nullptr )
44 {
45 gst_element_set_state( mpPipeline, GST_STATE_NULL );
46 g_object_unref( G_OBJECT( mpPipeline ) );
47 mpPipeline = nullptr;
48 }
49}
50
51FrameGrabber::FrameGrabber( std::u16string_view rURL )
52{
53 gchar *pPipelineStr;
54 pPipelineStr = g_strdup_printf(
55 "uridecodebin uri=%s ! videoconvert ! videoscale ! appsink "
56 "name=sink caps=\"video/x-raw,format=RGB,pixel-aspect-ratio=1/1\"",
57 OUStringToOString( rURL, RTL_TEXTENCODING_UTF8 ).getStr() );
58
59 GError *pError = nullptr;
60 mpPipeline = gst_parse_launch( pPipelineStr, &pError );
61 if( pError != nullptr) {
62 g_warning( "Failed to construct frame-grabber pipeline '%s'\n", pError->message );
63 g_error_free( pError );
65 }
66
67 if( mpPipeline ) {
68 // pre-roll
69 switch( gst_element_set_state( mpPipeline, GST_STATE_PAUSED ) ) {
70 case GST_STATE_CHANGE_FAILURE:
71 case GST_STATE_CHANGE_NO_PREROLL:
72 g_warning( "failure pre-rolling media" );
74 break;
75 default:
76 break;
77 }
78 }
79 if( mpPipeline &&
80 gst_element_get_state( mpPipeline, nullptr, nullptr, 5 * GST_SECOND ) == GST_STATE_CHANGE_FAILURE )
82}
83
85{
87}
88
90{
91 return new FrameGrabber( rURL );
92}
93
94uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
95{
96 uno::Reference< graphic::XGraphic > xRet;
97
98 if( !mpPipeline )
99 return xRet;
100
101 gint64 gst_position = llround( fMediaTime * GST_SECOND );
102 gst_element_seek_simple(
103 mpPipeline, GST_FORMAT_TIME,
104 GstSeekFlags(GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_FLUSH),
105 gst_position );
106
107 GstElement *pSink = gst_bin_get_by_name( GST_BIN( mpPipeline ), "sink" );
108 if( !pSink )
109 return xRet;
110
111 GstBuffer *pBuf = nullptr;
112 GstCaps *pCaps = nullptr;
113
114 // synchronously fetch the frame
115 GstSample *pSample = nullptr;
116 g_signal_emit_by_name( pSink, "pull-preroll", &pSample, nullptr );
117
118 if( pSample )
119 {
120 pBuf = gst_sample_get_buffer( pSample );
121 pCaps = gst_sample_get_caps( pSample );
122 }
123
124 // get geometry
125 int nWidth = 0, nHeight = 0;
126 if( !pCaps )
127 g_warning( "could not get snapshot format\n" );
128 else
129 {
130 GstStructure *pStruct = gst_caps_get_structure( pCaps, 0 );
131
132 /* we need to get the final caps on the buffer to get the size */
133 if( !gst_structure_get_int( pStruct, "width", &nWidth ) ||
134 !gst_structure_get_int( pStruct, "height", &nHeight ) )
135 nWidth = nHeight = 0;
136 }
137
138 if( pBuf && nWidth > 0 && nHeight > 0 &&
139 // sanity check the size
140 gst_buffer_get_size( pBuf ) >= o3tl::make_unsigned( nWidth * nHeight * 3 )
141 )
142 {
143 sal_uInt8 *pData = nullptr;
144 GstMapInfo aMapInfo;
145 gst_buffer_map( pBuf, &aMapInfo, GST_MAP_READ );
146 pData = aMapInfo.data;
147
148 int nStride = GST_ROUND_UP_4( nWidth * 3 );
149 BitmapEx aBmp = vcl::bitmap::CreateFromData(pData, nWidth, nHeight, nStride, /*nBitsPerPixel*/24);
150
151 gst_buffer_unmap( pBuf, &aMapInfo );
152 xRet = Graphic( aBmp ).GetXGraphic();
153 }
154
155 return xRet;
156}
157
159{
161}
162
163sal_Bool SAL_CALL FrameGrabber::supportsService( const OUString& ServiceName )
164{
166}
167
168uno::Sequence< OUString > SAL_CALL FrameGrabber::getSupportedServiceNames()
169{
171}
172
173} // namespace
174
175/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::graphic::XGraphic > GetXGraphic() const
virtual css::uno::Reference< css::graphic::XGraphic > SAL_CALL grabFrame(double fMediaTime) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
FrameGrabber(const FrameGrabber &)=delete
virtual OUString SAL_CALL getImplementationName() override
static rtl::Reference< FrameGrabber > create(std::u16string_view rURL)
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
float u
constexpr OUStringLiteral AVMEDIA_GST_FRAMEGRABBER_SERVICENAME
constexpr OUStringLiteral AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME
std::unique_ptr< sal_Int32[]> pData
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
BitmapEx CreateFromData(sal_uInt8 const *pData, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride, sal_Int8 nBitCount, bool bReversColors, bool bReverseAlpha)
unsigned char sal_uInt8
unsigned char sal_Bool