Line data Source code
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 :
23 : #include <cppuhelper/supportsservice.hxx>
24 :
25 : #include <gst/gstbuffer.h>
26 : #include <gst/video/video.h>
27 : #include <gst/video/gstvideosink.h>
28 :
29 : #include <vcl/graph.hxx>
30 : #include <vcl/bmpacc.hxx>
31 :
32 : #include <string>
33 :
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
41 :
42 : using namespace ::com::sun::star;
43 :
44 : namespace avmedia { namespace gstreamer {
45 :
46 0 : void FrameGrabber::disposePipeline()
47 : {
48 0 : if( mpPipeline != NULL )
49 : {
50 0 : gst_element_set_state( mpPipeline, GST_STATE_NULL );
51 0 : g_object_unref( G_OBJECT( mpPipeline ) );
52 0 : mpPipeline = NULL;
53 : }
54 0 : }
55 :
56 0 : FrameGrabber::FrameGrabber( const OUString &rURL ) :
57 0 : FrameGrabber_BASE()
58 : {
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 0 : OUStringToOString( rURL, RTL_TEXTENCODING_UTF8 ).getStr() );
71 :
72 0 : GError *pError = NULL;
73 0 : mpPipeline = gst_parse_launch( pPipelineStr, &pError );
74 0 : if( pError != NULL) {
75 0 : g_warning( "Failed to construct frame-grabber pipeline '%s'\n", pError->message );
76 0 : g_error_free( pError );
77 0 : disposePipeline();
78 : }
79 :
80 0 : if( mpPipeline ) {
81 : // pre-roll
82 0 : switch( gst_element_set_state( mpPipeline, GST_STATE_PAUSED ) ) {
83 : case GST_STATE_CHANGE_FAILURE:
84 : case GST_STATE_CHANGE_NO_PREROLL:
85 0 : g_warning( "failure pre-rolling media" );
86 0 : disposePipeline();
87 0 : break;
88 : default:
89 0 : break;
90 : }
91 : }
92 0 : if( mpPipeline &&
93 0 : gst_element_get_state( mpPipeline, NULL, NULL, 5 * GST_SECOND ) == GST_STATE_CHANGE_FAILURE )
94 0 : disposePipeline();
95 0 : }
96 :
97 0 : FrameGrabber::~FrameGrabber()
98 : {
99 0 : disposePipeline();
100 0 : }
101 :
102 0 : FrameGrabber* FrameGrabber::create( const OUString &rURL )
103 : {
104 0 : return new FrameGrabber( rURL );
105 : }
106 :
107 0 : uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
108 : throw (uno::RuntimeException, std::exception)
109 : {
110 0 : uno::Reference< graphic::XGraphic > xRet;
111 :
112 0 : if( !mpPipeline )
113 0 : return xRet;
114 :
115 0 : gint64 gst_position = llround( fMediaTime * 1E9 );
116 : gst_element_seek_simple(
117 : mpPipeline, GST_FORMAT_TIME,
118 : (GstSeekFlags)(GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_FLUSH),
119 0 : gst_position );
120 :
121 0 : GstElement *pSink = gst_bin_get_by_name( GST_BIN( mpPipeline ), "sink" );
122 0 : if( !pSink )
123 0 : return xRet;
124 :
125 0 : GstBuffer *pBuf = NULL;
126 0 : GstCaps *pCaps = NULL;
127 :
128 : // synchronously fetch the frame
129 : #ifdef AVMEDIA_GST_0_10
130 0 : g_signal_emit_by_name( pSink, "pull-preroll", &pBuf, NULL );
131 0 : if( pBuf )
132 0 : pCaps = GST_BUFFER_CAPS( pBuf );
133 : #else
134 : GstSample *pSample = NULL;
135 : g_signal_emit_by_name( pSink, "pull-preroll", &pSample, NULL );
136 :
137 : if( pSample )
138 : {
139 : pBuf = gst_sample_get_buffer( pSample );
140 : pCaps = gst_sample_get_caps( pSample );
141 : }
142 : #endif
143 :
144 : // get geometry
145 0 : int nWidth = 0, nHeight = 0;
146 0 : if( !pCaps )
147 0 : g_warning( "could not get snapshot format\n" );
148 : else
149 : {
150 0 : GstStructure *pStruct = gst_caps_get_structure( pCaps, 0 );
151 :
152 : /* we need to get the final caps on the buffer to get the size */
153 0 : if( !gst_structure_get_int( pStruct, "width", &nWidth ) ||
154 0 : !gst_structure_get_int( pStruct, "height", &nHeight ) )
155 0 : nWidth = nHeight = 0;
156 : }
157 :
158 0 : if( pBuf && nWidth > 0 && nHeight > 0 &&
159 : // sanity check the size
160 : #ifdef AVMEDIA_GST_0_10
161 0 : 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
165 : )
166 : {
167 0 : sal_uInt8 *pData = NULL;
168 : #ifdef AVMEDIA_GST_0_10
169 0 : 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
175 :
176 0 : int nStride = GST_ROUND_UP_4( nWidth * 3 );
177 0 : Bitmap aBmp( Size( nWidth, nHeight ), 24 );
178 :
179 0 : BitmapWriteAccess *pWrite = aBmp.AcquireWriteAccess();
180 0 : if( pWrite )
181 : {
182 : // yet another cheesy pixel copying loop
183 0 : for( int y = 0; y < nHeight; ++y )
184 : {
185 0 : sal_uInt8 *p = pData + y * nStride;
186 0 : for( int x = 0; x < nWidth; ++x )
187 : {
188 0 : BitmapColor col( p[0], p[1], p[2] );
189 0 : pWrite->SetPixel( y, x, col );
190 0 : p += 3;
191 0 : }
192 : }
193 : }
194 0 : aBmp.ReleaseAccess( pWrite );
195 :
196 : #ifndef AVMEDIA_GST_0_10
197 : gst_buffer_unmap( pBuf, &aMapInfo );
198 : #endif
199 :
200 0 : xRet = Graphic( aBmp ).GetXGraphic();
201 : }
202 :
203 0 : return xRet;
204 : }
205 :
206 0 : OUString SAL_CALL FrameGrabber::getImplementationName( )
207 : throw (uno::RuntimeException, std::exception)
208 : {
209 0 : return OUString( AVMEDIA_GST_FRAMEGRABBER_IMPLEMENTATIONNAME );
210 : }
211 :
212 0 : sal_Bool SAL_CALL FrameGrabber::supportsService( const OUString& ServiceName )
213 : throw (uno::RuntimeException, std::exception)
214 : {
215 0 : return cppu::supportsService(this, ServiceName);
216 : }
217 :
218 0 : uno::Sequence< OUString > SAL_CALL FrameGrabber::getSupportedServiceNames()
219 : throw (uno::RuntimeException, std::exception)
220 : {
221 0 : uno::Sequence< OUString > aRet(1);
222 0 : aRet[0] = AVMEDIA_GST_FRAMEGRABBER_SERVICENAME;
223 :
224 0 : return aRet;
225 : }
226 :
227 : } // namespace gstreamer
228 : } // namespace avmedia
229 :
230 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|