1*70f497fbSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*70f497fbSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*70f497fbSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*70f497fbSAndrew Rist  * distributed with this work for additional information
6*70f497fbSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*70f497fbSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*70f497fbSAndrew Rist  * "License"); you may not use this file except in compliance
9*70f497fbSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*70f497fbSAndrew Rist  *
11*70f497fbSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*70f497fbSAndrew Rist  *
13*70f497fbSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*70f497fbSAndrew Rist  * software distributed under the License is distributed on an
15*70f497fbSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*70f497fbSAndrew Rist  * KIND, either express or implied.  See the License for the
17*70f497fbSAndrew Rist  * specific language governing permissions and limitations
18*70f497fbSAndrew Rist  * under the License.
19*70f497fbSAndrew Rist  *
20*70f497fbSAndrew Rist  *************************************************************/
21*70f497fbSAndrew Rist 
22*70f497fbSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_slideshow.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <canvas/debug.hxx>
28cdf0e10cSrcweir #include <tools/diagnose_ex.h>
29cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <comphelper/anytostring.hxx>
32cdf0e10cSrcweir #include <cppuhelper/exc_hlp.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/NoSupportException.hpp>
36cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hdl>
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include <tools/urlobj.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <avmedia/mediawindow.hxx>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include "soundplayer.hxx"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir #include <algorithm>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir using namespace ::com::sun::star;
47cdf0e10cSrcweir 
48cdf0e10cSrcweir 
49cdf0e10cSrcweir namespace slideshow
50cdf0e10cSrcweir {
51cdf0e10cSrcweir     namespace internal
52cdf0e10cSrcweir     {
53cdf0e10cSrcweir         // TODO(Q3): Move the whole SoundPlayer class to avmedia.
54cdf0e10cSrcweir 
create(EventMultiplexer & rEventMultiplexer,const::rtl::OUString & rSoundURL,const uno::Reference<uno::XComponentContext> & rComponentContext)55cdf0e10cSrcweir         boost::shared_ptr<SoundPlayer> SoundPlayer::create(
56cdf0e10cSrcweir             EventMultiplexer & rEventMultiplexer,
57cdf0e10cSrcweir             const ::rtl::OUString& rSoundURL,
58cdf0e10cSrcweir             const uno::Reference< uno::XComponentContext>&	rComponentContext )
59cdf0e10cSrcweir         {
60cdf0e10cSrcweir             boost::shared_ptr<SoundPlayer> pPlayer(
61cdf0e10cSrcweir                 new SoundPlayer( rEventMultiplexer,
62cdf0e10cSrcweir                                  rSoundURL,
63cdf0e10cSrcweir                                  rComponentContext ) );
64cdf0e10cSrcweir             rEventMultiplexer.addPauseHandler( pPlayer );
65cdf0e10cSrcweir             pPlayer->mThis = pPlayer;
66cdf0e10cSrcweir             return pPlayer;
67cdf0e10cSrcweir         }
68cdf0e10cSrcweir 
handlePause(bool bPauseShow)69cdf0e10cSrcweir         bool SoundPlayer::handlePause( bool bPauseShow )
70cdf0e10cSrcweir         {
71cdf0e10cSrcweir             return bPauseShow ? stopPlayback() : startPlayback();
72cdf0e10cSrcweir         }
73cdf0e10cSrcweir 
dispose()74cdf0e10cSrcweir         void SoundPlayer::dispose()
75cdf0e10cSrcweir         {
76cdf0e10cSrcweir             if( mThis )
77cdf0e10cSrcweir             {
78cdf0e10cSrcweir                 mrEventMultiplexer.removePauseHandler( mThis );
79cdf0e10cSrcweir                 mThis.reset();
80cdf0e10cSrcweir             }
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 			if( mxPlayer.is() )
83cdf0e10cSrcweir             {
84cdf0e10cSrcweir 				mxPlayer->stop();
85cdf0e10cSrcweir 				uno::Reference<lang::XComponent> xComponent(
86cdf0e10cSrcweir                     mxPlayer, uno::UNO_QUERY );
87cdf0e10cSrcweir 				if( xComponent.is() )
88cdf0e10cSrcweir 					xComponent->dispose();
89cdf0e10cSrcweir                 mxPlayer.clear();
90cdf0e10cSrcweir 			}
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir 
SoundPlayer(EventMultiplexer & rEventMultiplexer,const::rtl::OUString & rSoundURL,const uno::Reference<uno::XComponentContext> & rComponentContext)93cdf0e10cSrcweir         SoundPlayer::SoundPlayer(
94cdf0e10cSrcweir             EventMultiplexer & rEventMultiplexer,
95cdf0e10cSrcweir             const ::rtl::OUString& rSoundURL,
96cdf0e10cSrcweir             const uno::Reference< uno::XComponentContext>&	rComponentContext )
97cdf0e10cSrcweir             : mrEventMultiplexer(rEventMultiplexer),
98cdf0e10cSrcweir               mThis(),
99cdf0e10cSrcweir               mxPlayer()
100cdf0e10cSrcweir         {
101cdf0e10cSrcweir             ENSURE_OR_THROW( rComponentContext.is(),
102cdf0e10cSrcweir                               "SoundPlayer::SoundPlayer(): Invalid component context" );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir             try
105cdf0e10cSrcweir             {
106cdf0e10cSrcweir         		const INetURLObject aURL( rSoundURL );
107cdf0e10cSrcweir                 mxPlayer.set( avmedia::MediaWindow::createPlayer(
108cdf0e10cSrcweir                                 aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ) ),
109cdf0e10cSrcweir                                 uno::UNO_QUERY);
110cdf0e10cSrcweir             }
111cdf0e10cSrcweir             catch( uno::RuntimeException& )
112cdf0e10cSrcweir             {
113cdf0e10cSrcweir                 throw;
114cdf0e10cSrcweir             }
115cdf0e10cSrcweir             catch( uno::Exception& )
116cdf0e10cSrcweir             {
117cdf0e10cSrcweir             }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             if( !mxPlayer.is() )
120cdf0e10cSrcweir                 throw lang::NoSupportException(
121cdf0e10cSrcweir                     rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
122cdf0e10cSrcweir                                        "No sound support for ") ) + rSoundURL,
123cdf0e10cSrcweir                     uno::Reference<uno::XInterface>() );
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir 
~SoundPlayer()126cdf0e10cSrcweir         SoundPlayer::~SoundPlayer()
127cdf0e10cSrcweir         {
128cdf0e10cSrcweir             try
129cdf0e10cSrcweir             {
130cdf0e10cSrcweir                 dispose();
131cdf0e10cSrcweir             }
132cdf0e10cSrcweir             catch (uno::Exception &) {
133cdf0e10cSrcweir                 OSL_ENSURE( false, rtl::OUStringToOString(
134cdf0e10cSrcweir                                 comphelper::anyToString(
135cdf0e10cSrcweir                                     cppu::getCaughtException() ),
136cdf0e10cSrcweir                                 RTL_TEXTENCODING_UTF8 ).getStr() );
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir 
getDuration() const140cdf0e10cSrcweir         double SoundPlayer::getDuration() const
141cdf0e10cSrcweir         {
142cdf0e10cSrcweir             if( !mxPlayer.is() )
143cdf0e10cSrcweir                 return 0.0;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir             const double nDuration( mxPlayer->getDuration() );
146cdf0e10cSrcweir             if( mxPlayer->isPlaying() )
147cdf0e10cSrcweir                 return ::std::max( 0.0,
148cdf0e10cSrcweir                                    nDuration - mxPlayer->getMediaTime() );
149cdf0e10cSrcweir             else
150cdf0e10cSrcweir                 return nDuration;
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir 
startPlayback()153cdf0e10cSrcweir         bool SoundPlayer::startPlayback()
154cdf0e10cSrcweir         {
155cdf0e10cSrcweir             if( !mxPlayer.is() )
156cdf0e10cSrcweir                 return false;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir             if( mxPlayer->isPlaying() )
159cdf0e10cSrcweir                 mxPlayer->stop();
160cdf0e10cSrcweir 
161cdf0e10cSrcweir             mxPlayer->start();
162cdf0e10cSrcweir             return true;
163cdf0e10cSrcweir         }
164cdf0e10cSrcweir 
stopPlayback()165cdf0e10cSrcweir         bool SoundPlayer::stopPlayback()
166cdf0e10cSrcweir         {
167cdf0e10cSrcweir             if( mxPlayer.is() )
168cdf0e10cSrcweir                 mxPlayer->stop();
169cdf0e10cSrcweir 
170cdf0e10cSrcweir             return true;
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir 
setPlaybackLoop(bool bLoop)173cdf0e10cSrcweir 		void SoundPlayer::setPlaybackLoop( bool bLoop )
174cdf0e10cSrcweir 		{
175cdf0e10cSrcweir 			if( mxPlayer.is() )
176cdf0e10cSrcweir 				mxPlayer->setPlaybackLoop( bLoop );
177cdf0e10cSrcweir 		}
178cdf0e10cSrcweir     }
179cdf0e10cSrcweir }
180