1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #include "framegrabber.hxx"
26 using namespace ::com::sun::star;
28 namespace avmedia { namespace quicktime {
34 Player::Player( const uno::Reference< lang::XMultiServiceFactory >& rxMgr ) :
38 mbFakeVideo (sal_False ),
41 mnStopTime( DBL_MAX ), //max double
43 mbInitialized( false ),
44 maSizeCondition( osl_createCondition() )
49 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
50 // check the version of QuickTime installed
52 result = Gestalt(gestaltQuickTime,&nVersion);
53 if ((result == noErr) && (nVersion >= QT701))
55 // we have version 7.01 or later, initialize
61 // ------------------------------------------------------------------------------
71 // ------------------------------------------------------------------------------
73 QTMovie* Player::getMovie()
75 OSL_ASSERT( mpMovie );
79 // ------------------------------------------------------------------------------
81 bool Player::create( const ::rtl::OUString& rURL )
87 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
95 NSString* aNSStr = [[[NSString alloc] initWithCharacters: rURL.getStr() length: rURL.getLength()]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
96 NSURL* aURL = [NSURL URLWithString:aNSStr ];
99 mpMovie = [QTMovie movieWithURL:aURL error:&pErr];
108 OSL_TRACE( "NSMovie create failed with error %ld (%s)",
110 [[pErr localizedDescription] UTF8String]
119 // ------------------------------------------------------------------------------
121 void SAL_CALL Player::start( )
122 throw (uno::RuntimeException)
124 OSL_TRACE ("Player::start");
132 // ------------------------------------------------------------------------------
134 void SAL_CALL Player::stop( )
135 throw (uno::RuntimeException)
137 OSL_TRACE ("Player::stop");
144 // ------------------------------------------------------------------------------
146 sal_Bool SAL_CALL Player::isPlaying()
147 throw (uno::RuntimeException)
153 if ([mpMovie rate] != 0)
162 // ------------------------------------------------------------------------------
164 double SAL_CALL Player::getDuration( )
165 throw (uno::RuntimeException)
167 // slideshow checks for non-zero duration, so cheat here
168 double duration = 0.01;
170 if ( mpMovie ) // && mnDuration > 0 ) {
172 QTTime structDuration = [mpMovie duration] ;
173 duration = (double)structDuration.timeValue / (double)structDuration.timeScale;
179 // ------------------------------------------------------------------------------
181 void SAL_CALL Player::setMediaTime( double fTime )
182 throw (uno::RuntimeException)
184 OSL_TRACE ("Player::setMediaTime");
188 [mpMovie setCurrentTime: QTMakeTimeWithTimeInterval(fTime)];
192 // ------------------------------------------------------------------------------
194 double SAL_CALL Player::getMediaTime( )
195 throw (uno::RuntimeException)
197 double position = 0.0;
201 QTTime structDuration = [mpMovie currentTime] ;
202 position = (double)structDuration.timeValue / (double)structDuration.timeScale;
205 if(isPlaying() && position>mnStopTime)
213 // ------------------------------------------------------------------------------
215 double SAL_CALL Player::getRate( )
216 throw (uno::RuntimeException)
218 // Quicktime: 0 = stop, 1 = normal speed, 2 = double speed, -1 = normal speed backwards
221 OSL_TRACE ("Player::getRate");
225 rate = (double) [mpMovie rate];
231 // ------------------------------------------------------------------------------
233 void SAL_CALL Player::setPlaybackLoop( sal_Bool bSet )
234 throw (uno::RuntimeException)
236 OSL_TRACE ("Player::setPlaybackLoop? %s", bSet?"True":"False" );
240 [mpMovie setAttribute:[NSNumber numberWithBool:YES] forKey: QTMovieLoopsAttribute] ;
244 [mpMovie setAttribute:[NSNumber numberWithBool:NO] forKey: QTMovieLoopsAttribute] ;
248 // ------------------------------------------------------------------------------
250 sal_Bool SAL_CALL Player::isPlaybackLoop( )
251 throw (uno::RuntimeException)
253 bool bRet = [[mpMovie attributeForKey:QTMovieLoopsAttribute] boolValue];
255 OSL_TRACE ("Player::isPlaybackLoop ? %s", bRet?"True":"False" );
260 // ------------------------------------------------------------------------------
262 void SAL_CALL Player::setMute( sal_Bool bSet )
263 throw (uno::RuntimeException)
265 OSL_TRACE( "set mute: %d muted: %d unmuted volume: %lf", bSet, mbMuted, mnUnmutedVolume );
267 // change the volume to 0 or the unmuted volume
268 if( mpMovie && mbMuted != bSet )
270 [mpMovie setMuted: bSet ];
276 // ------------------------------------------------------------------------------
278 sal_Bool SAL_CALL Player::isMute( )
279 throw (uno::RuntimeException)
281 OSL_TRACE ("Player::isMuted");
286 // ------------------------------------------------------------------------------
288 void SAL_CALL Player::setVolumeDB( sal_Int16 nVolumeDB )
289 throw (uno::RuntimeException)
291 // OOo db volume -40 = QTVolume 0
292 // OOo db volume 0 = QTvolume 1
299 mnUnmutedVolume = pow( 10.0, nVolumeDB / 20.0 );
302 OSL_TRACE( "set volume: %d gst volume: %f", nVolumeDB, mnUnmutedVolume );
305 if( !mbMuted && mpMovie )
307 [mpMovie setVolume: mnUnmutedVolume ];
311 // ------------------------------------------------------------------------------
313 sal_Int16 SAL_CALL Player::getVolumeDB( )
314 throw (uno::RuntimeException)
316 sal_Int16 nVolumeDB = 0.0;
322 volume = [mpMovie volume];
323 if(volume>0) //protect from log10(0)
325 nVolumeDB = (sal_Int16) ( 20.0*log10 ( volume ) );
329 nVolumeDB = -40 ; // QT zero volume is no volume, -40db
336 // ------------------------------------------------------------------------------
338 awt::Size SAL_CALL Player::getPreferredPlayerWindowSize( )
339 throw (uno::RuntimeException)
341 NSSize nsSize = [[mpMovie attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
342 awt::Size aSize( nsSize.width, nsSize.height );
346 // ------------------------------------------------------------------------------
348 uno::Reference< ::media::XPlayerWindow > SAL_CALL Player::createPlayerWindow( const uno::Sequence< uno::Any >& aArguments )
349 throw (uno::RuntimeException)
351 uno::Reference< ::media::XPlayerWindow > xRet;
352 awt::Size aSize( getPreferredPlayerWindowSize() );
354 OSL_TRACE( "Player::createPlayerWindow %d %d length: %d", aSize.Width, aSize.Height, aArguments.getLength() );
356 if( aSize.Width > 0 && aSize.Height > 0 )
359 aArguments[0] >>= nPtr;
360 NSView* pParentView = reinterpret_cast< NSView * >(nPtr);
362 ::avmedia::quicktime::Window* pWindow = new ::avmedia::quicktime::Window( mxMgr, *this, pParentView );
369 // ------------------------------------------------------------------------------
371 uno::Reference< media::XFrameGrabber > SAL_CALL Player::createFrameGrabber( )
372 throw (::com::sun::star::uno::RuntimeException)
374 uno::Reference< media::XFrameGrabber > xRet;
375 OSL_TRACE ("Player::createFrameGrabber");
377 if( maURL.getLength() > 0 )
379 FrameGrabber* pGrabber = new FrameGrabber( mxMgr );
383 if( !pGrabber->create( maURL ) )
392 // ------------------------------------------------------------------------------
394 ::rtl::OUString SAL_CALL Player::getImplementationName( )
395 throw (uno::RuntimeException)
397 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_QUICKTIME_PLAYER_IMPLEMENTATIONNAME ) );
400 // ------------------------------------------------------------------------------
402 sal_Bool SAL_CALL Player::supportsService( const ::rtl::OUString& ServiceName )
403 throw (uno::RuntimeException)
405 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( AVMEDIA_QUICKTIME_PLAYER_SERVICENAME ) );
408 // ------------------------------------------------------------------------------
410 uno::Sequence< ::rtl::OUString > SAL_CALL Player::getSupportedServiceNames( )
411 throw (uno::RuntimeException)
413 uno::Sequence< ::rtl::OUString > aRet(1);
414 aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_QUICKTIME_PLAYER_SERVICENAME ) );
419 } // namespace quicktime
420 } // namespace avmedia
422 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */