1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
),
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()
57 void AnimationAudioNode::activate_st()
61 AnimationEventHandlerSharedPtr
aHandler(
62 std::dynamic_pointer_cast
<AnimationEventHandler
>( getSelf() ) );
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();
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.
80 scheduleDeactivationEvent(
81 makeDelay( [this] () { this->checkPlayingStatus(); },
82 mpPlayer
->getDuration(),
83 "AnimationAudioNode::check if still playing with delay") );
90 scheduleDeactivationEvent(
91 makeEvent( [self
] () { self
->deactivate(); },
92 "AnimationAudioNode::deactivate without delay") );
96 // TODO(F2): generate deactivation event, when sound
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
) { }
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
);
131 mpPlayer
->stopPlayback();
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
149 void AnimationAudioNode::createPlayer() const
156 mpPlayer
= SoundPlayer::create( getContext().mrEventMultiplexer
,
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
174 mpPlayer
->stopPlayback();
180 bool AnimationAudioNode::handleAnimationEvent(
181 const AnimationNodeSharedPtr
& /*rNode*/ )
183 // TODO(F2): for now we support only STOPAUDIO events.
188 void AnimationAudioNode::checkPlayingStatus()
190 auto self(getSelf());
191 double nDuration
= mpPlayer
->getDuration();
192 if (!mpPlayer
->isPlaying() || nDuration
< 0.0)
195 scheduleDeactivationEvent(
196 makeDelay( [self
] () { self
->deactivate(); },
198 "AnimationAudioNode::deactivate with delay") );
201 } // namespace slideshow::internal
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */