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>
29 using namespace com::sun::star
;
31 namespace slideshow::internal
{
33 AnimationAudioNode::AnimationAudioNode(
34 const uno::Reference
< animations::XAnimationNode
>& xNode
,
35 const BaseContainerNodeSharedPtr
& rParent
,
36 const NodeContext
& rContext
)
37 : BaseNode( xNode
, rParent
, rContext
),
38 mxAudioNode( xNode
, uno::UNO_QUERY_THROW
),
42 mxAudioNode
->getSource() >>= maSoundURL
;
44 OSL_ENSURE( !maSoundURL
.isEmpty(),
45 "could not extract sound source URL/empty URL string" );
47 ENSURE_OR_THROW( getContext().mxComponentContext
.is(),
48 "Invalid component context" );
51 void AnimationAudioNode::dispose()
58 void AnimationAudioNode::activate_st()
62 AnimationEventHandlerSharedPtr
aHandler(
63 std::dynamic_pointer_cast
<AnimationEventHandler
>( getSelf() ) );
65 "could not cast self to AnimationEventHandler?" );
66 getContext().mrEventMultiplexer
.addCommandStopAudioHandler( aHandler
);
68 if (mpPlayer
&& mpPlayer
->startPlayback())
70 // TODO(F2): Handle end time attribute, too
71 if( getXAnimationNode()->getDuration().hasValue() )
73 scheduleDeactivationEvent();
77 // no node duration. Take inherent media time. We have to recheck
78 // if the player is playing in case the duration isn't accurate
79 // or the progress fall behind.
81 scheduleDeactivationEvent(
82 makeDelay( [this] () { this->checkPlayingStatus(); },
83 mpPlayer
->getDuration(),
84 u
"AnimationAudioNode::check if still playing with delay"_ustr
) );
91 scheduleDeactivationEvent(
92 makeEvent( [self
=std::move(self
)] () { self
->deactivate(); },
93 u
"AnimationAudioNode::deactivate without delay"_ustr
) );
97 // TODO(F2): generate deactivation event, when sound
102 // libc++ and MSVC std::bind doesn't cut it here, and it's not possible to use
103 // a lambda because the preprocessor thinks that comma in capture list
104 // separates macro parameters
105 struct NotifyAudioStopped
107 EventMultiplexer
& m_rEventMultiplexer
;
108 ::std::shared_ptr
<BaseNode
> m_pSelf
;
109 NotifyAudioStopped(EventMultiplexer
& rEventMultiplexer
,
110 ::std::shared_ptr
<BaseNode
> pSelf
)
111 : m_rEventMultiplexer(rEventMultiplexer
), m_pSelf(std::move(pSelf
)) { }
115 m_rEventMultiplexer
.notifyAudioStopped(m_pSelf
);
121 void AnimationAudioNode::deactivate_st( NodeState
/*eDestState*/ )
123 AnimationEventHandlerSharedPtr
aHandler(
124 std::dynamic_pointer_cast
<AnimationEventHandler
>( getSelf() ) );
125 OSL_ENSURE( aHandler
,
126 "could not cast self to AnimationEventHandler?" );
127 getContext().mrEventMultiplexer
.removeCommandStopAudioHandler( aHandler
);
132 mpPlayer
->stopPlayback();
136 // notify _after_ state change:
137 getContext().mrEventQueue
.addEvent(
138 makeEvent( NotifyAudioStopped(getContext().mrEventMultiplexer
, getSelf()),
139 u
"AnimationAudioNode::notifyAudioStopped"_ustr
) );
142 bool AnimationAudioNode::hasPendingAnimation() const
144 // force slide to use the animation framework
145 // (otherwise, a single sound on the slide would
150 void AnimationAudioNode::createPlayer() const
157 mpPlayer
= SoundPlayer::create( getContext().mrEventMultiplexer
,
159 getContext().mxComponentContext
,
160 getContext().mrMediaFileManager
);
162 catch( lang::NoSupportException
& )
164 // catch possible exceptions from SoundPlayer,
165 // since being not able to playback the sound
166 // is not a hard error here (remainder of the
167 // animations should still work).
171 void AnimationAudioNode::resetPlayer() const
175 mpPlayer
->stopPlayback();
181 bool AnimationAudioNode::handleAnimationEvent(
182 const AnimationNodeSharedPtr
& /*rNode*/ )
184 // TODO(F2): for now we support only STOPAUDIO events.
189 void AnimationAudioNode::checkPlayingStatus()
191 auto xSelf(getSelf());
192 double nDuration
= mpPlayer
->getDuration();
193 if (!mpPlayer
->isPlaying() || nDuration
< 0.0)
196 scheduleDeactivationEvent(
197 makeDelay( [xSelf
=std::move(xSelf
)] () { xSelf
->deactivate(); },
199 u
"AnimationAudioNode::deactivate with delay"_ustr
) );
202 } // namespace slideshow::internal
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */