cid#1606940 Check of thread-shared field evades lock acquisition
[LibreOffice.git] / slideshow / source / engine / animationnodes / animationaudionode.cxx
blob6c50d3d03d3c17ea9cdf72656ac8513c24b21fc5
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>
27 #include <utility>
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 ),
39 maSoundURL(),
40 mpPlayer()
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()
53 resetPlayer();
54 mxAudioNode.clear();
55 BaseNode::dispose();
58 void AnimationAudioNode::activate_st()
60 createPlayer();
62 AnimationEventHandlerSharedPtr aHandler(
63 std::dynamic_pointer_cast<AnimationEventHandler>( getSelf() ) );
64 OSL_ENSURE( aHandler,
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();
75 else
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.
80 auto self(getSelf());
81 scheduleDeactivationEvent(
82 makeDelay( [this] () { this->checkPlayingStatus(); },
83 mpPlayer->getDuration(),
84 u"AnimationAudioNode::check if still playing with delay"_ustr) );
87 else
89 // deactivate ASAP:
90 auto self(getSelf());
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
98 // is over
100 namespace {
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)) { }
113 void operator()()
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 );
129 // force-end sound
130 if (mpPlayer)
132 mpPlayer->stopPlayback();
133 resetPlayer();
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
146 // not be played).
147 return true;
150 void AnimationAudioNode::createPlayer() const
152 if (mpPlayer)
153 return;
157 mpPlayer = SoundPlayer::create( getContext().mrEventMultiplexer,
158 maSoundURL,
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
173 if (mpPlayer)
175 mpPlayer->stopPlayback();
176 mpPlayer->dispose();
177 mpPlayer.reset();
181 bool AnimationAudioNode::handleAnimationEvent(
182 const AnimationNodeSharedPtr& /*rNode*/ )
184 // TODO(F2): for now we support only STOPAUDIO events.
185 deactivate();
186 return true;
189 void AnimationAudioNode::checkPlayingStatus()
191 auto xSelf(getSelf());
192 double nDuration = mpPlayer->getDuration();
193 if (!mpPlayer->isPlaying() || nDuration < 0.0)
194 nDuration = 0.0;
196 scheduleDeactivationEvent(
197 makeDelay( [xSelf=std::move(xSelf)] () { xSelf->deactivate(); },
198 nDuration,
199 u"AnimationAudioNode::deactivate with delay"_ustr) );
202 } // namespace slideshow::internal
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */