merge the formfield patch from ooo-build
[ooovba.git] / avmedia / source / quicktime / player.cxx
bloba66c77e3f4b2f3884094583db1240ff5366e7d8c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: player.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <math.h>
33 #include "player.hxx"
34 #include "framegrabber.hxx"
35 #include "window.hxx"
37 // dbg_dump for development
38 #if OSL_DEBUG_LEVEL > 1
39 #include <rtl/strbuf.hxx>
40 #include <rtl/ustring.hxx>
42 const sal_Char *dbg_dump(const rtl::OString &rStr)
44 static rtl::OStringBuffer aStr;
46 aStr = rtl::OStringBuffer(rStr);
47 aStr.append(static_cast<char>(0));
48 return aStr.getStr();
51 const sal_Char *dbg_dump(const rtl::OUString &rStr)
53 return dbg_dump(rtl::OUStringToOString(rStr, RTL_TEXTENCODING_UTF8));
56 const sal_Char *dbg_dump(rtl_String *pStr)
58 return dbg_dump(rtl::OString(pStr));
61 const sal_Char *dbg_dump(rtl_uString *pStr)
63 return dbg_dump(rtl::OUString(pStr));
66 #endif
68 using namespace ::com::sun::star;
70 namespace avmedia { namespace quicktime {
72 // ----------------
73 // - Player -
74 // ----------------
76 Player::Player( const uno::Reference< lang::XMultiServiceFactory >& rxMgr ) :
77 mxMgr( rxMgr ),
78 mpMovie( nil ),
79 /* GST
80 mbFakeVideo (sal_False ),
82 mnUnmutedVolume( 0 ),
83 mnStopTime( DBL_MAX ), //max double
84 mbMuted( false ),
85 mbLooping( false ),
86 mbInitialized( false ),
87 mnWindowID( 0 ),
88 mnDuration( 0 ),
89 mnWidth( 0 ),
90 mnHeight( 0 ),
91 mnVersion( 0 ),
92 maSizeCondition( osl_createCondition() )
94 OSErr result;
96 NSApplicationLoad();
97 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
98 // check the version of QuickTime installed
99 result = Gestalt(gestaltQuickTime,&mnVersion);
100 if ((result == noErr) && (mnVersion >= QT701))
102 // we have version 7.01 or later, initialize
103 mpMovie = [QTMovie movie];
104 [mpMovie retain];
105 mbInitialized = true;
107 [pool release];
110 // ------------------------------------------------------------------------------
112 Player::~Player()
114 if( mbInitialized )
116 if( mpMovie )
118 [mpMovie release];
119 mpMovie = nil;
126 // ------------------------------------------------------------------------------
128 bool Player::create( const ::rtl::OUString& rURL )
130 bool bRet = false;
131 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
132 NSString* aNSStr = [[[NSString alloc] initWithCharacters: rURL.getStr() length: rURL.getLength()]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
133 // NSString * aNSStringEscaped = [aNSStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
134 NSURL* aURL = [NSURL URLWithString:aNSStr ];
136 // create the Movie
138 if( mbInitialized )
141 mpMovie = [mpMovie initWithURL:aURL error:nil];
142 if(mpMovie)
144 [mpMovie retain];
145 maURL = rURL;
146 bRet = true;
150 [pool release];
152 return bRet;
155 // ------------------------------------------------------------------------------
157 void SAL_CALL Player::start( )
158 throw (uno::RuntimeException)
160 OSL_TRACE ("Player::start");
162 if ( mbInitialized && mpMovie )
164 [mpMovie play];
168 // ------------------------------------------------------------------------------
170 void SAL_CALL Player::stop( )
171 throw (uno::RuntimeException)
173 OSL_TRACE ("Player::stop");
174 if ( mpMovie )
176 [mpMovie stop];
181 // ------------------------------------------------------------------------------
183 sal_Bool SAL_CALL Player::isPlaying()
184 throw (uno::RuntimeException)
186 bool bRet = false;
188 if ( mbInitialized )
190 if ([mpMovie rate] != 0)
192 bRet = true;
196 return bRet;
199 // ------------------------------------------------------------------------------
201 double SAL_CALL Player::getDuration( )
202 throw (uno::RuntimeException)
204 // slideshow checks for non-zero duration, so cheat here
205 double duration = 0.01;
207 if ( mpMovie ) // && mnDuration > 0 ) {
209 QTTime structDuration = [mpMovie duration] ;
210 duration = (double)structDuration.timeValue / (double)structDuration.timeScale;
213 return duration;
216 // ------------------------------------------------------------------------------
218 void SAL_CALL Player::setMediaTime( double fTime )
219 throw (uno::RuntimeException)
221 OSL_TRACE ("Player::setMediaTime");
223 if ( mpMovie )
225 [mpMovie setCurrentTime: QTMakeTimeWithTimeInterval(fTime)];
229 // ------------------------------------------------------------------------------
231 double SAL_CALL Player::getMediaTime( )
232 throw (uno::RuntimeException)
234 double position = 0.0;
236 if ( mpMovie )
238 QTTime structDuration = [mpMovie currentTime] ;
239 position = (double)structDuration.timeValue / (double)structDuration.timeScale;
242 if(isPlaying() && position>mnStopTime)
244 stop();
248 return position;
251 // ------------------------------------------------------------------------------
253 void SAL_CALL Player::setStopTime( double fTime )
254 throw (uno::RuntimeException)
256 OSL_TRACE ("Player::setStopTime %f", fTime);
258 mnStopTime = fTime;
262 // ------------------------------------------------------------------------------
264 double SAL_CALL Player::getStopTime( )
265 throw (uno::RuntimeException)
267 double fRet = 0.0;
269 fRet = mnStopTime;
271 return fRet;
274 // ------------------------------------------------------------------------------
276 void SAL_CALL Player::setRate( double fRate )
277 throw (uno::RuntimeException)
279 OSL_TRACE ("Player::setRate");
281 // Quicktime: 0 = stop, 1 = normal speed, 2 = double speed, -1 = normal speed backwards
282 if ( mpMovie )
284 [mpMovie setRate: fRate];
288 // ------------------------------------------------------------------------------
290 double SAL_CALL Player::getRate( )
291 throw (uno::RuntimeException)
293 // Quicktime: 0 = stop, 1 = normal speed, 2 = double speed, -1 = normal speed backwards
294 double rate = 1.0;
296 OSL_TRACE ("Player::getRate");
298 if ( mpMovie )
300 rate = (double) [mpMovie rate];
303 return rate;
306 // ------------------------------------------------------------------------------
308 void SAL_CALL Player::setPlaybackLoop( sal_Bool bSet )
309 throw (uno::RuntimeException)
311 OSL_TRACE ("Player::setPlaybackLoop? %s", bSet?"True":"False" );
313 if(bSet)
315 [mpMovie setAttribute:[NSNumber numberWithBool:YES] forKey: QTMovieLoopsAttribute] ;
317 else
319 [mpMovie setAttribute:[NSNumber numberWithBool:NO] forKey: QTMovieLoopsAttribute] ;
323 // ------------------------------------------------------------------------------
325 sal_Bool SAL_CALL Player::isPlaybackLoop( )
326 throw (uno::RuntimeException)
328 bool bRet = [[mpMovie attributeForKey:QTMovieLoopsAttribute] boolValue];
330 OSL_TRACE ("Player::isPlaybackLoop ? %s", bRet?"True":"False" );
332 return bRet;
335 // ------------------------------------------------------------------------------
337 void SAL_CALL Player::setMute( sal_Bool bSet )
338 throw (uno::RuntimeException)
340 OSL_TRACE( "set mute: %d muted: %d unmuted volume: %lf", bSet, mbMuted, mnUnmutedVolume );
342 // change the volume to 0 or the unmuted volume
343 if( mpMovie && mbMuted != bSet )
345 [mpMovie setMuted: bSet ];
346 mbMuted = bSet;
351 // ------------------------------------------------------------------------------
353 sal_Bool SAL_CALL Player::isMute( )
354 throw (uno::RuntimeException)
356 OSL_TRACE ("Player::isMuted");
358 return mbMuted;
361 // ------------------------------------------------------------------------------
363 void SAL_CALL Player::setVolumeDB( sal_Int16 nVolumeDB )
364 throw (uno::RuntimeException)
366 // OOo db volume -40 = QTVolume 0
367 // OOo db volume 0 = QTvolume 1
368 if(nVolumeDB==-40)
370 mnUnmutedVolume = 0;
372 else
374 mnUnmutedVolume = pow( 10.0, nVolumeDB / 20.0 );
377 OSL_TRACE( "set volume: %d gst volume: %f", nVolumeDB, mnUnmutedVolume );
379 // change volume
380 if( !mbMuted && mpMovie )
382 [mpMovie setVolume: mnUnmutedVolume ];
386 // ------------------------------------------------------------------------------
388 sal_Int16 SAL_CALL Player::getVolumeDB( )
389 throw (uno::RuntimeException)
391 sal_Int16 nVolumeDB = 0.0;
393 if( mpMovie )
395 float volume = 0.0;
397 volume = [mpMovie volume];
398 if(volume>0) //protect from log10(0)
400 nVolumeDB = (sal_Int16) ( 20.0*log10 ( volume ) );
402 else
404 nVolumeDB = -40 ; // QT zero volume is no volume, -40db
408 return nVolumeDB;
411 // ------------------------------------------------------------------------------
413 awt::Size SAL_CALL Player::getPreferredPlayerWindowSize( )
414 throw (uno::RuntimeException)
416 NSSize nsSize = [[mpMovie attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
417 awt::Size aSize( nsSize.width, nsSize.height );
418 return aSize;
422 // ------------------------------------------------------------------------------
424 uno::Reference< ::media::XPlayerWindow > SAL_CALL Player::createPlayerWindow( const uno::Sequence< uno::Any >& aArguments )
425 throw (uno::RuntimeException)
427 uno::Reference< ::media::XPlayerWindow > xRet;
428 awt::Size aSize( getPreferredPlayerWindowSize() );
429 NSSize nsSize( NSMakeSize(aSize.Width, aSize.Height) );
431 OSL_TRACE( "Player::createPlayerWindow %d %d length: %d", aSize.Width, aSize.Height, aArguments.getLength() );
433 if( aSize.Width > 0 && aSize.Height > 0 )
435 sal_IntPtr nPtr = NULL;
436 aArguments[0] >>= nPtr;
437 NSView* pParentView = reinterpret_cast< NSView * >(nPtr);
439 ::avmedia::quicktime::Window* pWindow = new ::avmedia::quicktime::Window( mxMgr, *this, pParentView );
440 xRet = pWindow;
443 return xRet;
446 // ------------------------------------------------------------------------------
448 uno::Reference< media::XFrameGrabber > SAL_CALL Player::createFrameGrabber( )
449 throw (::com::sun::star::uno::RuntimeException)
451 uno::Reference< media::XFrameGrabber > xRet;
452 OSL_TRACE ("Player::createFrameGrabber");
454 if( maURL.getLength() > 0 )
456 FrameGrabber* pGrabber = new FrameGrabber( mxMgr );
458 xRet = pGrabber;
460 if( !pGrabber->create( maURL ) )
462 xRet.clear();
466 return xRet;
469 // ------------------------------------------------------------------------------
471 ::rtl::OUString SAL_CALL Player::getImplementationName( )
472 throw (uno::RuntimeException)
474 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( AVMEDIA_QUICKTIME_PLAYER_IMPLEMENTATIONNAME ) );
477 // ------------------------------------------------------------------------------
479 sal_Bool SAL_CALL Player::supportsService( const ::rtl::OUString& ServiceName )
480 throw (uno::RuntimeException)
482 return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( AVMEDIA_QUICKTIME_PLAYER_SERVICENAME ) );
485 // ------------------------------------------------------------------------------
487 uno::Sequence< ::rtl::OUString > SAL_CALL Player::getSupportedServiceNames( )
488 throw (uno::RuntimeException)
490 uno::Sequence< ::rtl::OUString > aRet(1);
491 aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( AVMEDIA_QUICKTIME_PLAYER_SERVICENAME ) );
493 return aRet;
496 } // namespace quicktime
497 } // namespace avmedia