build fix
[LibreOffice.git] / slideshow / source / engine / animationnodes / animationaudionode.cxx
blob13959bc2c4c746f0ceaa1830a3c401bcb935b86a
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 "eventqueue.hxx"
22 #include "animationaudionode.hxx"
23 #include "delayevent.hxx"
24 #include "tools.hxx"
25 #include "nodetools.hxx"
27 using namespace com::sun::star;
29 namespace slideshow {
30 namespace 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, then
77 auto self(getSelf());
78 scheduleDeactivationEvent(
79 makeDelay( [self] () { self->deactivate(); },
80 mpPlayer->getDuration(),
81 "AnimationAudioNode::deactivate with delay") );
84 else
86 // deactivate ASAP:
87 auto self(getSelf());
88 scheduleDeactivationEvent(
89 makeEvent( [self] () { self->deactivate(); },
90 "AnimationAudioNode::deactivate without delay") );
94 // TODO(F2): generate deactivation event, when sound
95 // is over
97 // libc++ and MSVC std::bind doesn't cut it here, and it's not possible to use
98 // a lambda because the preprocessor thinks that comma in capture list
99 // separates macro parameters
100 struct NotifyAudioStopped
102 EventMultiplexer & m_rEventMultiplexer;
103 ::std::shared_ptr<BaseNode> m_pSelf;
104 NotifyAudioStopped(EventMultiplexer & rEventMultiplexer,
105 ::std::shared_ptr<BaseNode> const& pSelf)
106 : m_rEventMultiplexer(rEventMultiplexer), m_pSelf(pSelf) { }
108 void operator()()
110 m_rEventMultiplexer.notifyAudioStopped(m_pSelf);
114 void AnimationAudioNode::deactivate_st( NodeState /*eDestState*/ )
116 AnimationEventHandlerSharedPtr aHandler(
117 std::dynamic_pointer_cast<AnimationEventHandler>( getSelf() ) );
118 OSL_ENSURE( aHandler,
119 "could not cas self to AnimationEventHandler?" );
120 getContext().mrEventMultiplexer.removeCommandStopAudioHandler( aHandler );
122 // force-end sound
123 if (mpPlayer)
125 mpPlayer->stopPlayback();
126 resetPlayer();
129 // notify _after_ state change:
130 getContext().mrEventQueue.addEvent(
131 makeEvent( NotifyAudioStopped(getContext().mrEventMultiplexer, getSelf()),
132 "AnimationAudioNode::notifyAudioStopped") );
135 bool AnimationAudioNode::hasPendingAnimation() const
137 // force slide to use the animation framework
138 // (otherwise, a single sound on the slide would
139 // not be played).
140 return true;
143 void AnimationAudioNode::createPlayer() const
145 if (mpPlayer)
146 return;
150 mpPlayer = SoundPlayer::create( getContext().mrEventMultiplexer,
151 maSoundURL,
152 getContext().mxComponentContext );
154 catch( lang::NoSupportException& )
156 // catch possible exceptions from SoundPlayer,
157 // since being not able to playback the sound
158 // is not a hard error here (remainder of the
159 // animations should still work).
163 void AnimationAudioNode::resetPlayer() const
165 if (mpPlayer)
167 mpPlayer->stopPlayback();
168 mpPlayer->dispose();
169 mpPlayer.reset();
173 bool AnimationAudioNode::handleAnimationEvent(
174 const AnimationNodeSharedPtr& /*rNode*/ )
176 // TODO(F2): for now we support only STOPAUDIO events.
177 deactivate();
178 return true;
181 } // namespace internal
182 } // namespace presentation
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */