Use o3tl::convert in Math
[LibreOffice.git] / slideshow / source / engine / animationnodes / animationaudionode.cxx
blobc6eb61d17e3ca678a65d29fc8220a98854b2756f
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 .
20 #include <sal/config.h>
22 #include <com/sun/star/lang/NoSupportException.hpp>
24 #include <eventqueue.hxx>
25 #include "animationaudionode.hxx"
26 #include <delayevent.hxx>
28 using namespace com::sun::star;
30 namespace slideshow::internal {
32 AnimationAudioNode::AnimationAudioNode(
33 const uno::Reference< animations::XAnimationNode >& xNode,
34 const BaseContainerNodeSharedPtr& rParent,
35 const NodeContext& rContext )
36 : BaseNode( xNode, rParent, rContext ),
37 mxAudioNode( xNode, uno::UNO_QUERY_THROW ),
38 maSoundURL(),
39 mpPlayer()
41 mxAudioNode->getSource() >>= maSoundURL;
43 OSL_ENSURE( !maSoundURL.isEmpty(),
44 "could not extract sound source URL/empty URL string" );
46 ENSURE_OR_THROW( getContext().mxComponentContext.is(),
47 "Invalid component context" );
50 void AnimationAudioNode::dispose()
52 resetPlayer();
53 mxAudioNode.clear();
54 BaseNode::dispose();
57 void AnimationAudioNode::activate_st()
59 createPlayer();
61 AnimationEventHandlerSharedPtr aHandler(
62 std::dynamic_pointer_cast<AnimationEventHandler>( getSelf() ) );
63 OSL_ENSURE( aHandler,
64 "could not cast self to AnimationEventHandler?" );
65 getContext().mrEventMultiplexer.addCommandStopAudioHandler( aHandler );
67 if (mpPlayer && mpPlayer->startPlayback())
69 // TODO(F2): Handle end time attribute, too
70 if( getXAnimationNode()->getDuration().hasValue() )
72 scheduleDeactivationEvent();
74 else
76 // no node duration. Take inherent media time. We have to recheck
77 // if the player is playing in case the duration isn't accurate
78 // or the progress fall behind.
79 auto self(getSelf());
80 scheduleDeactivationEvent(
81 makeDelay( [this] () { this->checkPlayingStatus(); },
82 mpPlayer->getDuration(),
83 "AnimationAudioNode::check if still playing with delay") );
86 else
88 // deactivate ASAP:
89 auto self(getSelf());
90 scheduleDeactivationEvent(
91 makeEvent( [self] () { self->deactivate(); },
92 "AnimationAudioNode::deactivate without delay") );
96 // TODO(F2): generate deactivation event, when sound
97 // is over
99 namespace {
101 // libc++ and MSVC std::bind doesn't cut it here, and it's not possible to use
102 // a lambda because the preprocessor thinks that comma in capture list
103 // separates macro parameters
104 struct NotifyAudioStopped
106 EventMultiplexer & m_rEventMultiplexer;
107 ::std::shared_ptr<BaseNode> m_pSelf;
108 NotifyAudioStopped(EventMultiplexer & rEventMultiplexer,
109 ::std::shared_ptr<BaseNode> const& pSelf)
110 : m_rEventMultiplexer(rEventMultiplexer), m_pSelf(pSelf) { }
112 void operator()()
114 m_rEventMultiplexer.notifyAudioStopped(m_pSelf);
120 void AnimationAudioNode::deactivate_st( NodeState /*eDestState*/ )
122 AnimationEventHandlerSharedPtr aHandler(
123 std::dynamic_pointer_cast<AnimationEventHandler>( getSelf() ) );
124 OSL_ENSURE( aHandler,
125 "could not cast self to AnimationEventHandler?" );
126 getContext().mrEventMultiplexer.removeCommandStopAudioHandler( aHandler );
128 // force-end sound
129 if (mpPlayer)
131 mpPlayer->stopPlayback();
132 resetPlayer();
135 // notify _after_ state change:
136 getContext().mrEventQueue.addEvent(
137 makeEvent( NotifyAudioStopped(getContext().mrEventMultiplexer, getSelf()),
138 "AnimationAudioNode::notifyAudioStopped") );
141 bool AnimationAudioNode::hasPendingAnimation() const
143 // force slide to use the animation framework
144 // (otherwise, a single sound on the slide would
145 // not be played).
146 return true;
149 void AnimationAudioNode::createPlayer() const
151 if (mpPlayer)
152 return;
156 mpPlayer = SoundPlayer::create( getContext().mrEventMultiplexer,
157 maSoundURL,
158 getContext().mxComponentContext,
159 getContext().mrMediaFileManager);
161 catch( lang::NoSupportException& )
163 // catch possible exceptions from SoundPlayer,
164 // since being not able to playback the sound
165 // is not a hard error here (remainder of the
166 // animations should still work).
170 void AnimationAudioNode::resetPlayer() const
172 if (mpPlayer)
174 mpPlayer->stopPlayback();
175 mpPlayer->dispose();
176 mpPlayer.reset();
180 bool AnimationAudioNode::handleAnimationEvent(
181 const AnimationNodeSharedPtr& /*rNode*/ )
183 // TODO(F2): for now we support only STOPAUDIO events.
184 deactivate();
185 return true;
188 void AnimationAudioNode::checkPlayingStatus()
190 auto self(getSelf());
191 double nDuration = mpPlayer->getDuration();
192 if (!mpPlayer->isPlaying() || nDuration < 0.0)
193 nDuration = 0.0;
195 scheduleDeactivationEvent(
196 makeDelay( [self] () { self->deactivate(); },
197 nDuration,
198 "AnimationAudioNode::deactivate with delay") );
201 } // namespace slideshow::internal
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */