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