1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: player.cxx,v $
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 ************************************************************************/
34 #include "framegrabber.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));
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
));
68 using namespace ::com::sun::star
;
70 namespace avmedia
{ namespace quicktime
{
76 Player::Player( const uno::Reference
< lang::XMultiServiceFactory
>& rxMgr
) :
80 mbFakeVideo (sal_False ),
83 mnStopTime( DBL_MAX
), //max double
86 mbInitialized( false ),
92 maSizeCondition( osl_createCondition() )
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
];
105 mbInitialized
= true;
110 // ------------------------------------------------------------------------------
126 // ------------------------------------------------------------------------------
128 bool Player::create( const ::rtl::OUString
& rURL
)
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
];
141 mpMovie
= [mpMovie initWithURL
:aURL error
:nil
];
155 // ------------------------------------------------------------------------------
157 void SAL_CALL
Player::start( )
158 throw (uno::RuntimeException
)
160 OSL_TRACE ("Player::start");
162 if ( mbInitialized
&& mpMovie
)
168 // ------------------------------------------------------------------------------
170 void SAL_CALL
Player::stop( )
171 throw (uno::RuntimeException
)
173 OSL_TRACE ("Player::stop");
181 // ------------------------------------------------------------------------------
183 sal_Bool SAL_CALL
Player::isPlaying()
184 throw (uno::RuntimeException
)
190 if ([mpMovie rate
] != 0)
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
;
216 // ------------------------------------------------------------------------------
218 void SAL_CALL
Player::setMediaTime( double fTime
)
219 throw (uno::RuntimeException
)
221 OSL_TRACE ("Player::setMediaTime");
225 [mpMovie setCurrentTime
: QTMakeTimeWithTimeInterval(fTime
)];
229 // ------------------------------------------------------------------------------
231 double SAL_CALL
Player::getMediaTime( )
232 throw (uno::RuntimeException
)
234 double position
= 0.0;
238 QTTime structDuration
= [mpMovie currentTime
] ;
239 position
= (double)structDuration
.timeValue
/ (double)structDuration
.timeScale
;
242 if(isPlaying() && position
>mnStopTime
)
251 // ------------------------------------------------------------------------------
253 void SAL_CALL
Player::setStopTime( double fTime
)
254 throw (uno::RuntimeException
)
256 OSL_TRACE ("Player::setStopTime %f", fTime
);
262 // ------------------------------------------------------------------------------
264 double SAL_CALL
Player::getStopTime( )
265 throw (uno::RuntimeException
)
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
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
296 OSL_TRACE ("Player::getRate");
300 rate
= (double) [mpMovie rate
];
306 // ------------------------------------------------------------------------------
308 void SAL_CALL
Player::setPlaybackLoop( sal_Bool bSet
)
309 throw (uno::RuntimeException
)
311 OSL_TRACE ("Player::setPlaybackLoop? %s", bSet
?"True":"False" );
315 [mpMovie setAttribute
:[NSNumber numberWithBool
:YES
] forKey
: QTMovieLoopsAttribute
] ;
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" );
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
];
351 // ------------------------------------------------------------------------------
353 sal_Bool SAL_CALL
Player::isMute( )
354 throw (uno::RuntimeException
)
356 OSL_TRACE ("Player::isMuted");
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
374 mnUnmutedVolume
= pow( 10.0, nVolumeDB
/ 20.0 );
377 OSL_TRACE( "set volume: %d gst volume: %f", nVolumeDB
, mnUnmutedVolume
);
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;
397 volume
= [mpMovie volume
];
398 if(volume
>0) //protect from log10(0)
400 nVolumeDB
= (sal_Int16
) ( 20.0*log10 ( volume
) );
404 nVolumeDB
= -40 ; // QT zero volume is no volume, -40db
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
);
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
);
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
);
460 if( !pGrabber
->create( maURL
) )
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
) );
496 } // namespace quicktime
497 } // namespace avmedia