fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / slideshow / source / engine / soundplayer.cxx
blob89024107cf14b86f0991fd5389e23beeaa6fda29
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <canvas/debug.hxx>
22 #include <tools/diagnose_ex.h>
23 #include <canvas/verbosetrace.hxx>
25 #include <comphelper/anytostring.hxx>
26 #include <cppuhelper/exc_hlp.hxx>
28 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
29 #include <com/sun/star/lang/NoSupportException.hpp>
30 #include <com/sun/star/lang/XComponent.hpp>
32 #include <tools/urlobj.hxx>
34 #include <avmedia/mediawindow.hxx>
36 #include "soundplayer.hxx"
38 #include <algorithm>
40 using namespace ::com::sun::star;
43 namespace slideshow
45 namespace internal
47 // TODO(Q3): Move the whole SoundPlayer class to avmedia.
49 boost::shared_ptr<SoundPlayer> SoundPlayer::create(
50 EventMultiplexer & rEventMultiplexer,
51 const OUString& rSoundURL,
52 const uno::Reference< uno::XComponentContext>& rComponentContext )
54 boost::shared_ptr<SoundPlayer> pPlayer(
55 new SoundPlayer( rEventMultiplexer,
56 rSoundURL,
57 rComponentContext ) );
58 rEventMultiplexer.addPauseHandler( pPlayer );
59 pPlayer->mThis = pPlayer;
60 return pPlayer;
63 bool SoundPlayer::handlePause( bool bPauseShow )
65 return bPauseShow ? stopPlayback() : startPlayback();
68 void SoundPlayer::dispose()
70 if( mThis )
72 mrEventMultiplexer.removePauseHandler( mThis );
73 mThis.reset();
76 if( mxPlayer.is() )
78 mxPlayer->stop();
79 uno::Reference<lang::XComponent> xComponent(
80 mxPlayer, uno::UNO_QUERY );
81 if( xComponent.is() )
82 xComponent->dispose();
83 mxPlayer.clear();
87 SoundPlayer::SoundPlayer(
88 EventMultiplexer & rEventMultiplexer,
89 const OUString& rSoundURL,
90 const uno::Reference< uno::XComponentContext>& rComponentContext )
91 : mrEventMultiplexer(rEventMultiplexer),
92 mThis(),
93 mxPlayer()
95 ENSURE_OR_THROW( rComponentContext.is(),
96 "SoundPlayer::SoundPlayer(): Invalid component context" );
98 try
100 const INetURLObject aURL( rSoundURL );
101 mxPlayer.set( avmedia::MediaWindow::createPlayer(
102 aURL.GetMainURL( INetURLObject::DECODE_UNAMBIGUOUS ), ""/*TODO!*/ ),
103 uno::UNO_QUERY);
105 catch( uno::RuntimeException& )
107 throw;
109 catch( uno::Exception& )
113 if( !mxPlayer.is() )
114 throw lang::NoSupportException(
115 "No sound support for " + rSoundURL );
118 SoundPlayer::~SoundPlayer()
122 dispose();
124 catch (uno::Exception &) {
125 OSL_FAIL( OUStringToOString(
126 comphelper::anyToString(
127 cppu::getCaughtException() ),
128 RTL_TEXTENCODING_UTF8 ).getStr() );
132 double SoundPlayer::getDuration() const
134 if( !mxPlayer.is() )
135 return 0.0;
137 const double nDuration( mxPlayer->getDuration() );
138 if( mxPlayer->isPlaying() )
139 return ::std::max( 0.0,
140 nDuration - mxPlayer->getMediaTime() );
141 else
142 return nDuration;
145 bool SoundPlayer::startPlayback()
147 if( !mxPlayer.is() )
148 return false;
150 if( mxPlayer->isPlaying() )
151 mxPlayer->stop();
153 mxPlayer->start();
154 return true;
157 bool SoundPlayer::stopPlayback()
159 if( mxPlayer.is() )
160 mxPlayer->stop();
162 return true;
165 void SoundPlayer::setPlaybackLoop( bool bLoop )
167 if( mxPlayer.is() )
168 mxPlayer->setPlaybackLoop( bLoop );
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */