fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / avmedia / source / gstreamer / gstframegrabber.cxx
blobf240530edafc8232f3bc48808c32d33d82a138e4
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>
29 #include <vcl/graph.hxx>
30 #include <vcl/bmpacc.hxx>
32 #include <string>
34 #ifdef AVMEDIA_GST_0_10
35 # define AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_GStreamer_0_10"
36 # define AVMEDIA_GST_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_GStreamer_0_10"
37 #else
38 # define AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME "com.sun.star.comp.avmedia.FrameGrabber_GStreamer"
39 # define AVMEDIA_GST_FRAMEGRABBER_SERVICENAME "com.sun.star.media.FrameGrabber_GStreamer"
40 #endif
42 using namespace ::com::sun::star;
44 namespace avmedia { namespace gstreamer {
46 void FrameGrabber::disposePipeline()
48 if( mpPipeline != NULL )
50 gst_element_set_state( mpPipeline, GST_STATE_NULL );
51 g_object_unref( G_OBJECT( mpPipeline ) );
52 mpPipeline = NULL;
56 FrameGrabber::FrameGrabber( const OUString &rURL ) :
57 FrameGrabber_BASE()
59 gchar *pPipelineStr;
60 pPipelineStr = g_strdup_printf(
61 #ifdef AVMEDIA_GST_0_10
62 "uridecodebin uri=%s ! ffmpegcolorspace ! videoscale ! appsink "
63 "name=sink caps=\"video/x-raw-rgb,format=RGB,pixel-aspect-ratio=1/1,"
64 "bpp=(int)24,depth=(int)24,endianness=(int)4321,"
65 "red_mask=(int)0xff0000, green_mask=(int)0x00ff00, blue_mask=(int)0x0000ff\"",
66 #else
67 "uridecodebin uri=%s ! videoconvert ! videoscale ! appsink "
68 "name=sink caps=\"video/x-raw,format=RGB,pixel-aspect-ratio=1/1\"",
69 #endif
70 OUStringToOString( rURL, RTL_TEXTENCODING_UTF8 ).getStr() );
72 GError *pError = NULL;
73 mpPipeline = gst_parse_launch( pPipelineStr, &pError );
74 if( pError != NULL) {
75 g_warning( "Failed to construct frame-grabber pipeline '%s'\n", pError->message );
76 g_error_free( pError );
77 disposePipeline();
80 if( mpPipeline ) {
81 // pre-roll
82 switch( gst_element_set_state( mpPipeline, GST_STATE_PAUSED ) ) {
83 case GST_STATE_CHANGE_FAILURE:
84 case GST_STATE_CHANGE_NO_PREROLL:
85 g_warning( "failure pre-rolling media" );
86 disposePipeline();
87 break;
88 default:
89 break;
92 if( mpPipeline &&
93 gst_element_get_state( mpPipeline, NULL, NULL, 5 * GST_SECOND ) == GST_STATE_CHANGE_FAILURE )
94 disposePipeline();
97 FrameGrabber::~FrameGrabber()
99 disposePipeline();
102 FrameGrabber* FrameGrabber::create( const OUString &rURL )
104 return new FrameGrabber( rURL );
107 uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
108 throw (uno::RuntimeException, std::exception)
110 uno::Reference< graphic::XGraphic > xRet;
112 if( !mpPipeline )
113 return xRet;
115 gint64 gst_position = llround( fMediaTime * GST_SECOND );
116 gst_element_seek_simple(
117 mpPipeline, GST_FORMAT_TIME,
118 (GstSeekFlags)(GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_FLUSH),
119 gst_position );
121 GstElement *pSink = gst_bin_get_by_name( GST_BIN( mpPipeline ), "sink" );
122 if( !pSink )
123 return xRet;
125 GstBuffer *pBuf = NULL;
126 GstCaps *pCaps = NULL;
128 // synchronously fetch the frame
129 #ifdef AVMEDIA_GST_0_10
130 g_signal_emit_by_name( pSink, "pull-preroll", &pBuf, NULL );
131 if( pBuf )
132 pCaps = GST_BUFFER_CAPS( pBuf );
133 #else
134 GstSample *pSample = NULL;
135 g_signal_emit_by_name( pSink, "pull-preroll", &pSample, NULL );
137 if( pSample )
139 pBuf = gst_sample_get_buffer( pSample );
140 pCaps = gst_sample_get_caps( pSample );
142 #endif
144 // get geometry
145 int nWidth = 0, nHeight = 0;
146 if( !pCaps )
147 g_warning( "could not get snapshot format\n" );
148 else
150 GstStructure *pStruct = gst_caps_get_structure( pCaps, 0 );
152 /* we need to get the final caps on the buffer to get the size */
153 if( !gst_structure_get_int( pStruct, "width", &nWidth ) ||
154 !gst_structure_get_int( pStruct, "height", &nHeight ) )
155 nWidth = nHeight = 0;
158 if( pBuf && nWidth > 0 && nHeight > 0 &&
159 // sanity check the size
160 #ifdef AVMEDIA_GST_0_10
161 GST_BUFFER_SIZE( pBuf ) >= static_cast<unsigned>( nWidth * nHeight * 3 )
162 #else
163 gst_buffer_get_size( pBuf ) >= static_cast<unsigned>( nWidth * nHeight * 3 )
164 #endif
167 sal_uInt8 *pData = NULL;
168 #ifdef AVMEDIA_GST_0_10
169 pData = GST_BUFFER_DATA( pBuf );
170 #else
171 GstMapInfo aMapInfo;
172 gst_buffer_map( pBuf, &aMapInfo, GST_MAP_READ );
173 pData = aMapInfo.data;
174 #endif
176 int nStride = GST_ROUND_UP_4( nWidth * 3 );
177 Bitmap aBmp( Size( nWidth, nHeight ), 24 );
179 BitmapWriteAccess *pWrite = aBmp.AcquireWriteAccess();
180 if( pWrite )
182 // yet another cheesy pixel copying loop
183 for( int y = 0; y < nHeight; ++y )
185 sal_uInt8 *p = pData + y * nStride;
186 for( int x = 0; x < nWidth; ++x )
188 BitmapColor col( p[0], p[1], p[2] );
189 pWrite->SetPixel( y, x, col );
190 p += 3;
194 Bitmap::ReleaseAccess( pWrite );
196 #ifndef AVMEDIA_GST_0_10
197 gst_buffer_unmap( pBuf, &aMapInfo );
198 #endif
200 xRet = Graphic( aBmp ).GetXGraphic();
203 return xRet;
206 OUString SAL_CALL FrameGrabber::getImplementationName( )
207 throw (uno::RuntimeException, std::exception)
209 return OUString( AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME );
212 sal_Bool SAL_CALL FrameGrabber::supportsService( const OUString& ServiceName )
213 throw (uno::RuntimeException, std::exception)
215 return cppu::supportsService(this, ServiceName);
218 uno::Sequence< OUString > SAL_CALL FrameGrabber::getSupportedServiceNames()
219 throw (uno::RuntimeException, std::exception)
221 uno::Sequence< OUString > aRet(1);
222 aRet[0] = AVMEDIA_GST_FRAMEGRABBER_SERVICENAME;
224 return aRet;
227 } // namespace gstreamer
228 } // namespace avmedia
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */