Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / avmedia / source / quicktime / player.mm
blobd1c44306834ffdbbfca7d2625a9569439bf4bc37
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  */
20 #include <math.h>
22 #include "player.hxx"
23 #include "framegrabber.hxx"
24 #include "window.hxx"
26 using namespace ::com::sun::star;
28 SAL_WNODEPRECATED_DECLARATIONS_PUSH //TODO: 10.9
30 namespace avmedia { namespace quicktime {
32 Player::Player( const uno::Reference< lang::XMultiServiceFactory >& rxMgr ) :
33     mxMgr( rxMgr ),
34     mpMovie( nil ),
35     mnUnmutedVolume( 0 ),
36     mnStopTime( DBL_MAX ),  //max double
37     mbMuted( false ),
38     mbInitialized( false )
40     NSApplicationLoad();
41     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
42     mbInitialized = true;
43     [pool release];
47 Player::~Player()
49     if( mpMovie )
50     {
51         [mpMovie release];
52         mpMovie = nil;
53     }
57 QTMovie* Player::getMovie()
59     OSL_ASSERT( mpMovie );
60     return mpMovie;
64 bool Player::create( const ::rtl::OUString& rURL )
66     bool    bRet = false;
67     // create the Movie
68     if( mbInitialized )
69     {
70         NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
72         if( mpMovie )
73         {
74             [mpMovie release];
75             mpMovie = nil;
76         }
78         NSString* aNSStr = [[[NSString alloc] initWithCharacters: reinterpret_cast<unichar const *>(rURL.getStr()) length: rURL.getLength()]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
79         NSURL* aURL = [NSURL URLWithString:aNSStr ];
81         NSError* pErr = nil;
82         mpMovie = [QTMovie movieWithURL:aURL error:&pErr];
83         if(mpMovie)
84         {
85             [mpMovie retain];
86             maURL = rURL;
87             bRet = true;
88         }
89         if( pErr )
90         {
91             SAL_INFO ( "avmedia.quicktime",
92                        "NSMovie create failed with error " << (long)[pErr code] <<
93                        " (" << [[pErr localizedDescription] UTF8String] << ")"
94                        );
95         }
96         [pool release];
97     }
99     return bRet;
103 void SAL_CALL Player::start(  )
105   SAL_INFO ( "avmedia.quicktime", "Player::start" );
107   if( mpMovie )
108   {
109       [mpMovie play];
110   }
114 void SAL_CALL Player::stop(  )
116     SAL_INFO ( "avmedia.quicktime", "Player::stop" );
117     if( mpMovie )
118     {
119         [mpMovie stop];
120     }
124 sal_Bool SAL_CALL Player::isPlaying()
126     bool bRet = false;
128     if ( mpMovie )
129     {
130         if ([mpMovie rate] != 0)
131         {
132             bRet = true;
133         }
134     }
136     return bRet;
140 double SAL_CALL Player::getDuration(  )
142     // slideshow checks for non-zero duration, so cheat here
143     double duration = 0.01;
145     if ( mpMovie ) // && mnDuration > 0 ) {
146     {
147         QTTime structDuration =  [mpMovie duration] ;
148         duration = (double)structDuration.timeValue / (double)structDuration.timeScale;
149     }
151     return duration;
155 void SAL_CALL Player::setMediaTime( double fTime )
157     SAL_INFO ( "avmedia.quicktime", "Player::setMediaTime" );
159     if ( mpMovie )
160     {
161         [mpMovie setCurrentTime: QTMakeTimeWithTimeInterval(fTime)];
162     }
166 double SAL_CALL Player::getMediaTime(  )
168   double position = 0.0;
170   if ( mpMovie )
171   {
172       QTTime structDuration =  [mpMovie currentTime] ;
173       position = (double)structDuration.timeValue / (double)structDuration.timeScale;
174   }
176   if(isPlaying() && position>mnStopTime)
177   {
178       stop();
179   }
181   return position;
184 void SAL_CALL Player::setPlaybackLoop( sal_Bool bSet )
186     SAL_INFO ( "avmedia.quicktime",
187                "Player::setPlaybackLoop ? " << ( bSet?"True":"False" ) );
189     if(bSet)
190     {
191         [mpMovie setAttribute:[NSNumber numberWithBool:YES] forKey: QTMovieLoopsAttribute]  ;
192     }
193     else
194     {
195          [mpMovie setAttribute:[NSNumber numberWithBool:NO] forKey: QTMovieLoopsAttribute]  ;
196     }
200 sal_Bool SAL_CALL Player::isPlaybackLoop(  )
202     bool bRet = [[mpMovie attributeForKey:QTMovieLoopsAttribute] boolValue];
204     SAL_INFO ( "avmedia.quicktime",
205                "Player::isPlaybackLoop ? " << ( bRet?"True":"False" ) );
207     return bRet;
211 void SAL_CALL Player::setMute( sal_Bool bSet )
213     SAL_INFO ( "avmedia.quicktime",
214                "set mute: " << bSet <<
215                " muted: " << mbMuted <<
216                " unmuted volume: " << mnUnmutedVolume );
218     // change the volume to 0 or the unmuted volume
219     if(  mpMovie && mbMuted != bool(bSet) )
220     {
221         [mpMovie setMuted: bSet ];
222         mbMuted = bSet;
223     }
228 sal_Bool SAL_CALL Player::isMute(  )
230     SAL_INFO ( "avmedia.quicktime", "Player::isMuted" );
232     return mbMuted;
236 void SAL_CALL Player::setVolumeDB( sal_Int16 nVolumeDB )
238     // OOo db volume -40 = QTVolume 0
239     // OOo db volume 0   = QTvolume 1
240     if(nVolumeDB==-40)
241     {
242         mnUnmutedVolume = 0;
243     }
244     else
245     {
246         mnUnmutedVolume = pow( 10.0, nVolumeDB / 20.0 );
247     }
249     SAL_INFO ( "avmedia.quicktime",
250                "set volume: " << nVolumeDB <<
251                " gst volume: " << mnUnmutedVolume );
253     // change volume
254     if( !mbMuted && mpMovie )
255     {
256         [mpMovie setVolume: mnUnmutedVolume ];
257     }
261 sal_Int16 SAL_CALL Player::getVolumeDB(  )
263     sal_Int16 nVolumeDB = 0.0;
265     if( mpMovie )
266       {
267           float volume = 0.0;
269           volume = [mpMovie volume];
270           if(volume>0)            //protect from log10(0)
271           {
272               nVolumeDB = (sal_Int16) ( 20.0*log10 ( volume ) );
273           }
274           else
275           {
276               nVolumeDB = -40 ;  // QT zero volume is no volume, -40db
277           }
278       }
280     return nVolumeDB;
284 awt::Size SAL_CALL Player::getPreferredPlayerWindowSize(  )
286     NSSize  nsSize = [[mpMovie attributeForKey:QTMovieNaturalSizeAttribute] sizeValue];
287     awt::Size aSize( nsSize.width, nsSize.height );
288     return aSize;
292 uno::Reference< ::media::XPlayerWindow > SAL_CALL Player::createPlayerWindow( const uno::Sequence< uno::Any >& aArguments )
294     uno::Reference< ::media::XPlayerWindow >    xRet;
295     awt::Size                                   aSize( getPreferredPlayerWindowSize() );
297     SAL_INFO ( "avmedia.quicktime",
298                "Player::createPlayerWindow " << aSize.Width << " x " << aSize.Height <<
299                " length: " << aArguments.getLength() );
301     if( aSize.Width > 0 && aSize.Height > 0 )
302     {
303         sal_IntPtr nPtr = 0;
304         aArguments[0] >>= nPtr;
305         NSView* pParentView = reinterpret_cast< NSView * >(nPtr);
307         ::avmedia::quicktime::Window* pWindow = new ::avmedia::quicktime::Window( mxMgr, *this, pParentView );
308         xRet = pWindow;
309     }
311     return xRet;
315 uno::Reference< media::XFrameGrabber > SAL_CALL Player::createFrameGrabber(  )
317   uno::Reference< media::XFrameGrabber > xRet;
318   SAL_INFO ( "avmedia.quicktime", "Player::createFrameGrabber" );
320   if( !maURL.isEmpty() )
321   {
322       FrameGrabber* pGrabber = new FrameGrabber( mxMgr );
324       xRet = pGrabber;
326       if( !pGrabber->create( maURL ) )
327       {
328           xRet.clear();
329       }
330   }
332   return xRet;
336 ::rtl::OUString SAL_CALL Player::getImplementationName(  )
338     return ::rtl::OUString( AVMEDIA_QUICKTIME_PLAYER_IMPLEMENTATIONNAME );
342 sal_Bool SAL_CALL Player::supportsService( const ::rtl::OUString& ServiceName )
344     return ( ServiceName == AVMEDIA_QUICKTIME_PLAYER_SERVICENAME );
348 uno::Sequence< ::rtl::OUString > SAL_CALL Player::getSupportedServiceNames(  )
350     return { AVMEDIA_QUICKTIME_PLAYER_SERVICENAME };
353 } // namespace quicktime
354 } // namespace avmedia
356 SAL_WNODEPRECATED_DECLARATIONS_POP
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */