1*f39251c4SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*f39251c4SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*f39251c4SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*f39251c4SAndrew Rist * distributed with this work for additional information
6*f39251c4SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*f39251c4SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*f39251c4SAndrew Rist * "License"); you may not use this file except in compliance
9*f39251c4SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*f39251c4SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*f39251c4SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*f39251c4SAndrew Rist * software distributed under the License is distributed on an
15*f39251c4SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*f39251c4SAndrew Rist * KIND, either express or implied. See the License for the
17*f39251c4SAndrew Rist * specific language governing permissions and limitations
18*f39251c4SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*f39251c4SAndrew Rist *************************************************************/
21*f39251c4SAndrew Rist
22*f39251c4SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include "gstframegrabber.hxx"
25cdf0e10cSrcweir #include "gstplayer.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <vcl/graph.hxx>
28cdf0e10cSrcweir #include <vcl/bmpacc.hxx>
29cdf0e10cSrcweir
30cdf0e10cSrcweir #include <string>
31cdf0e10cSrcweir
32cdf0e10cSrcweir
33cdf0e10cSrcweir using namespace ::com::sun::star;
34cdf0e10cSrcweir
35cdf0e10cSrcweir namespace avmedia { namespace gst {
36cdf0e10cSrcweir
37cdf0e10cSrcweir const gulong GRAB_TIMEOUT = 10000000;
38cdf0e10cSrcweir
39cdf0e10cSrcweir // ----------------
40cdf0e10cSrcweir // - FrameGrabber -
41cdf0e10cSrcweir // ----------------
42cdf0e10cSrcweir
FrameGrabber(GString * pURI)43cdf0e10cSrcweir FrameGrabber::FrameGrabber( GString* pURI ) :
44e3251387SMichael Stahl FrameGrabber_BASE(pURI),
45cdf0e10cSrcweir mpFrameMutex( g_mutex_new() ),
46cdf0e10cSrcweir mpFrameCond( g_cond_new() ),
47cdf0e10cSrcweir mpLastPixbuf( NULL ),
48cdf0e10cSrcweir mbIsInGrabMode( false )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir }
51cdf0e10cSrcweir
52cdf0e10cSrcweir // ------------------------------------------------------------------------------
53cdf0e10cSrcweir
~FrameGrabber()54cdf0e10cSrcweir FrameGrabber::~FrameGrabber()
55cdf0e10cSrcweir {
56cdf0e10cSrcweir if( g_atomic_pointer_get( &mpPlayer ) )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir implQuitThread();
59cdf0e10cSrcweir }
60cdf0e10cSrcweir
61cdf0e10cSrcweir // thread has ended, so that no more synchronization is necessary
62cdf0e10cSrcweir if( mpLastPixbuf )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir g_object_unref( mpLastPixbuf );
65cdf0e10cSrcweir mpLastPixbuf = NULL;
66cdf0e10cSrcweir }
67cdf0e10cSrcweir
68cdf0e10cSrcweir g_cond_free( mpFrameCond );
69cdf0e10cSrcweir g_mutex_free( mpFrameMutex );
70cdf0e10cSrcweir }
71cdf0e10cSrcweir
72cdf0e10cSrcweir // ------------------------------------------------------------------------------
73cdf0e10cSrcweir
create(const GString * pURI)74cdf0e10cSrcweir FrameGrabber* FrameGrabber::create( const GString* pURI )
75cdf0e10cSrcweir {
76cdf0e10cSrcweir FrameGrabber* pFrameGrabber = NULL;
77cdf0e10cSrcweir
78cdf0e10cSrcweir if( pURI && pURI->len )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir // safely initialize GLib threading framework
81cdf0e10cSrcweir try
82cdf0e10cSrcweir {
83cdf0e10cSrcweir if( !g_thread_supported() )
84cdf0e10cSrcweir {
85cdf0e10cSrcweir g_thread_init( NULL );
86cdf0e10cSrcweir }
87cdf0e10cSrcweir }
88cdf0e10cSrcweir catch( ... )
89cdf0e10cSrcweir {}
90cdf0e10cSrcweir
91cdf0e10cSrcweir if( g_thread_supported() )
92cdf0e10cSrcweir {
93cdf0e10cSrcweir pFrameGrabber = new FrameGrabber( g_string_new( pURI->str ) );
94cdf0e10cSrcweir
95cdf0e10cSrcweir // wait until thread signals that it has finished initialization
96cdf0e10cSrcweir if( pFrameGrabber->mpThread )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir g_mutex_lock( pFrameGrabber->mpMutex );
99cdf0e10cSrcweir
100cdf0e10cSrcweir while( !pFrameGrabber->implIsInitialized() )
101cdf0e10cSrcweir {
102cdf0e10cSrcweir g_cond_wait( pFrameGrabber->mpCond, pFrameGrabber->mpMutex );
103cdf0e10cSrcweir }
104cdf0e10cSrcweir
105cdf0e10cSrcweir g_mutex_unlock( pFrameGrabber->mpMutex );
106cdf0e10cSrcweir }
107cdf0e10cSrcweir
108cdf0e10cSrcweir GstElement* pPixbufSink = gst_element_factory_make( "gdkpixbufsink", NULL );
109cdf0e10cSrcweir
110cdf0e10cSrcweir // check if player pipeline and GdkPixbufSink could be initialized
111cdf0e10cSrcweir if( !pFrameGrabber->mpPlayer || !pPixbufSink )
112cdf0e10cSrcweir {
113cdf0e10cSrcweir delete pFrameGrabber;
114cdf0e10cSrcweir pFrameGrabber = NULL;
115cdf0e10cSrcweir }
116cdf0e10cSrcweir else
117cdf0e10cSrcweir {
118cdf0e10cSrcweir g_object_set( pFrameGrabber->mpPlayer, "audio-sink", gst_element_factory_make( "fakesink", NULL ), NULL );
119cdf0e10cSrcweir g_object_set( pFrameGrabber->mpPlayer, "video-sink", pPixbufSink, NULL );
120cdf0e10cSrcweir }
121cdf0e10cSrcweir }
122cdf0e10cSrcweir }
123cdf0e10cSrcweir
124cdf0e10cSrcweir return( pFrameGrabber );
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
127cdf0e10cSrcweir // ------------------------------------------------------------------------------
128cdf0e10cSrcweir
busCallback(GstBus * pBus,GstMessage * pMsg)129cdf0e10cSrcweir gboolean FrameGrabber::busCallback( GstBus* pBus, GstMessage* pMsg )
130cdf0e10cSrcweir {
131cdf0e10cSrcweir bool bDone = false;
132cdf0e10cSrcweir
133cdf0e10cSrcweir if( pMsg && pMsg->structure )
134cdf0e10cSrcweir {
135cdf0e10cSrcweir GstStructure* pStruct = pMsg->structure;
136cdf0e10cSrcweir const gchar* pStructName = gst_structure_get_name( pStruct );
137cdf0e10cSrcweir
138cdf0e10cSrcweir if( ( ::std::string( pStructName ).find( "pixbuf" ) != ::std::string::npos ) &&
139cdf0e10cSrcweir gst_structure_has_field ( pStruct, "pixbuf") )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir bool bFrameGrabbed = false;
142cdf0e10cSrcweir
143cdf0e10cSrcweir g_mutex_lock( mpFrameMutex );
144cdf0e10cSrcweir
145cdf0e10cSrcweir if( mbIsInGrabMode && ( getMediaTime() >= mfGrabTime ) )
146cdf0e10cSrcweir {
147cdf0e10cSrcweir OSL_TRACE( "Grabbing frame at %fs", getMediaTime() );
148cdf0e10cSrcweir
149cdf0e10cSrcweir if( mpLastPixbuf )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir g_object_unref( mpLastPixbuf );
152cdf0e10cSrcweir mpLastPixbuf = NULL;
153cdf0e10cSrcweir }
154cdf0e10cSrcweir
155cdf0e10cSrcweir mpLastPixbuf = GDK_PIXBUF( g_value_dup_object( gst_structure_get_value( pStruct, "pixbuf" ) ) );
156cdf0e10cSrcweir bFrameGrabbed = true;
157cdf0e10cSrcweir }
158cdf0e10cSrcweir
159cdf0e10cSrcweir g_mutex_unlock( mpFrameMutex );
160cdf0e10cSrcweir
161cdf0e10cSrcweir if( bFrameGrabbed )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir g_cond_signal( mpFrameCond );
164cdf0e10cSrcweir }
165cdf0e10cSrcweir
166cdf0e10cSrcweir bDone = true;
167cdf0e10cSrcweir }
168cdf0e10cSrcweir }
169cdf0e10cSrcweir
170cdf0e10cSrcweir return( bDone || Player::busCallback( pBus, pMsg ) );
171cdf0e10cSrcweir }
172cdf0e10cSrcweir
173cdf0e10cSrcweir // ------------------------------------------------------------------------------
174cdf0e10cSrcweir
grabFrame(double fMediaTime)175cdf0e10cSrcweir uno::Reference< graphic::XGraphic > SAL_CALL FrameGrabber::grabFrame( double fMediaTime )
176cdf0e10cSrcweir throw (uno::RuntimeException)
177cdf0e10cSrcweir {
178cdf0e10cSrcweir uno::Reference< graphic::XGraphic > xRet;
179cdf0e10cSrcweir
180cdf0e10cSrcweir if( implInitPlayer() )
181cdf0e10cSrcweir {
182cdf0e10cSrcweir OSL_TRACE( "Trying to grab frame at %fs", fMediaTime );
183cdf0e10cSrcweir
184cdf0e10cSrcweir GTimeVal aTimeoutTime;
185cdf0e10cSrcweir
186cdf0e10cSrcweir g_get_current_time( &aTimeoutTime );
187cdf0e10cSrcweir g_time_val_add( &aTimeoutTime, GRAB_TIMEOUT );
188cdf0e10cSrcweir setMediaTime( fMediaTime );
189cdf0e10cSrcweir start();
190cdf0e10cSrcweir
191cdf0e10cSrcweir if( isPlaying() )
192cdf0e10cSrcweir {
193cdf0e10cSrcweir g_mutex_lock( mpFrameMutex );
194cdf0e10cSrcweir
195cdf0e10cSrcweir mbIsInGrabMode = true;
196cdf0e10cSrcweir mfGrabTime = fMediaTime;
197cdf0e10cSrcweir g_cond_timed_wait( mpFrameCond, mpFrameMutex, &aTimeoutTime );
198cdf0e10cSrcweir mbIsInGrabMode = false;
199cdf0e10cSrcweir
200cdf0e10cSrcweir g_mutex_unlock( mpFrameMutex );
201cdf0e10cSrcweir
202cdf0e10cSrcweir stop();
203cdf0e10cSrcweir }
204cdf0e10cSrcweir
205cdf0e10cSrcweir OSL_ENSURE( g_atomic_pointer_get( &mpLastPixbuf ), "FrameGrabber timed out without receiving a Pixbuf" );
206cdf0e10cSrcweir
207cdf0e10cSrcweir if( g_atomic_pointer_get( &mpLastPixbuf ) )
208cdf0e10cSrcweir {
209cdf0e10cSrcweir OSL_TRACE( "FrameGrabber received a GdkPixbuf");
210cdf0e10cSrcweir
211cdf0e10cSrcweir g_mutex_lock( mpFrameMutex );
212cdf0e10cSrcweir
213cdf0e10cSrcweir const int nWidth = gdk_pixbuf_get_width( mpLastPixbuf );
214cdf0e10cSrcweir const int nHeight = gdk_pixbuf_get_height( mpLastPixbuf );
215cdf0e10cSrcweir const int nChannels = gdk_pixbuf_get_n_channels( mpLastPixbuf );
216cdf0e10cSrcweir const guchar* pBuffer = gdk_pixbuf_get_pixels( mpLastPixbuf );
217cdf0e10cSrcweir
218cdf0e10cSrcweir if( pBuffer && ( nWidth > 0 ) && ( nHeight > 0 ) )
219cdf0e10cSrcweir {
220cdf0e10cSrcweir Bitmap aFrame( Size( nWidth, nHeight), 24 );
221cdf0e10cSrcweir bool bInit = false;
222cdf0e10cSrcweir
223cdf0e10cSrcweir if( ( gdk_pixbuf_get_colorspace( mpLastPixbuf ) == GDK_COLORSPACE_RGB ) &&
224cdf0e10cSrcweir ( nChannels >= 3 ) && ( nChannels <= 4 ) &&
225cdf0e10cSrcweir ( gdk_pixbuf_get_bits_per_sample( mpLastPixbuf ) == 8 ) )
226cdf0e10cSrcweir {
227cdf0e10cSrcweir BitmapWriteAccess* pAcc = aFrame.AcquireWriteAccess();
228cdf0e10cSrcweir
229cdf0e10cSrcweir if( pAcc )
230cdf0e10cSrcweir {
231cdf0e10cSrcweir BitmapColor aPixel( 0, 0, 0 );
232cdf0e10cSrcweir const int nRowStride = gdk_pixbuf_get_rowstride( mpLastPixbuf );
233cdf0e10cSrcweir const bool bAlpha = ( nChannels == 4 );
234cdf0e10cSrcweir
235cdf0e10cSrcweir for( int nRow = 0; nRow < nHeight; ++nRow )
236cdf0e10cSrcweir {
237cdf0e10cSrcweir guchar* pCur = const_cast< guchar* >( pBuffer + nRow * nRowStride );
238cdf0e10cSrcweir
239cdf0e10cSrcweir for( int nCol = 0; nCol < nWidth; ++nCol )
240cdf0e10cSrcweir {
241cdf0e10cSrcweir aPixel.SetRed( *pCur++ );
242cdf0e10cSrcweir aPixel.SetGreen( *pCur++ );
243cdf0e10cSrcweir aPixel.SetBlue( *pCur++ );
244cdf0e10cSrcweir
245cdf0e10cSrcweir // ignore alpha channel
246cdf0e10cSrcweir if( bAlpha )
247cdf0e10cSrcweir {
248cdf0e10cSrcweir ++pCur;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir
251cdf0e10cSrcweir pAcc->SetPixel( nRow, nCol, aPixel );
252cdf0e10cSrcweir }
253cdf0e10cSrcweir }
254cdf0e10cSrcweir
255cdf0e10cSrcweir aFrame.ReleaseAccess( pAcc );
256cdf0e10cSrcweir bInit = true;
257cdf0e10cSrcweir }
258cdf0e10cSrcweir }
259cdf0e10cSrcweir
260cdf0e10cSrcweir if( !bInit )
261cdf0e10cSrcweir {
262cdf0e10cSrcweir aFrame.Erase( Color( COL_BLACK ) );
263cdf0e10cSrcweir }
264cdf0e10cSrcweir
265cdf0e10cSrcweir xRet = Graphic( aFrame ).GetXGraphic();
266cdf0e10cSrcweir }
267cdf0e10cSrcweir
268cdf0e10cSrcweir g_object_unref( mpLastPixbuf );
269cdf0e10cSrcweir mpLastPixbuf = NULL;
270cdf0e10cSrcweir
271cdf0e10cSrcweir g_mutex_unlock( mpFrameMutex );
272cdf0e10cSrcweir }
273cdf0e10cSrcweir }
274cdf0e10cSrcweir
275cdf0e10cSrcweir return xRet;
276cdf0e10cSrcweir }
277cdf0e10cSrcweir
278cdf0e10cSrcweir // ------------------------------------------------------------------------------
279cdf0e10cSrcweir
getImplementationName()280cdf0e10cSrcweir ::rtl::OUString SAL_CALL FrameGrabber::getImplementationName( )
281cdf0e10cSrcweir throw (uno::RuntimeException)
282cdf0e10cSrcweir {
283cdf0e10cSrcweir return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_GSTREAMER_FRAMEGRABBER_IMPLEMENTATIONNAME ) );
284cdf0e10cSrcweir }
285cdf0e10cSrcweir
286cdf0e10cSrcweir // ------------------------------------------------------------------------------
287cdf0e10cSrcweir
supportsService(const::rtl::OUString & ServiceName)288cdf0e10cSrcweir sal_Bool SAL_CALL FrameGrabber::supportsService( const ::rtl::OUString& ServiceName )
289cdf0e10cSrcweir throw (uno::RuntimeException)
290cdf0e10cSrcweir {
291cdf0e10cSrcweir return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( AVMEDIA_GSTREAMER_FRAMEGRABBER_SERVICENAME ) );
292cdf0e10cSrcweir }
293cdf0e10cSrcweir
294cdf0e10cSrcweir // ------------------------------------------------------------------------------
295cdf0e10cSrcweir
getSupportedServiceNames()296cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL FrameGrabber::getSupportedServiceNames( )
297cdf0e10cSrcweir throw (uno::RuntimeException)
298cdf0e10cSrcweir {
299cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > aRet(1);
300cdf0e10cSrcweir aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( AVMEDIA_GSTREAMER_FRAMEGRABBER_SERVICENAME ) );
301cdf0e10cSrcweir
302cdf0e10cSrcweir return aRet;
303cdf0e10cSrcweir }
304cdf0e10cSrcweir
305cdf0e10cSrcweir } // namespace win
306cdf0e10cSrcweir } // namespace avmedia
307