LibreOffice Module slideshow (master) 1
animationaudionode.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <com/sun/star/lang/NoSupportException.hpp>
23
24#include <eventqueue.hxx>
26#include <delayevent.hxx>
27#include <utility>
28
29using namespace com::sun::star;
30
31namespace slideshow::internal {
32
35 const BaseContainerNodeSharedPtr& rParent,
36 const NodeContext& rContext )
37 : BaseNode( xNode, rParent, rContext ),
38 mxAudioNode( xNode, uno::UNO_QUERY_THROW ),
39 maSoundURL(),
40 mpPlayer()
41{
42 mxAudioNode->getSource() >>= maSoundURL;
43
44 OSL_ENSURE( !maSoundURL.isEmpty(),
45 "could not extract sound source URL/empty URL string" );
46
48 "Invalid component context" );
49}
50
52{
54 mxAudioNode.clear();
56}
57
59{
61
63 std::dynamic_pointer_cast<AnimationEventHandler>( getSelf() ) );
64 OSL_ENSURE( aHandler,
65 "could not cast self to AnimationEventHandler?" );
67
68 if (mpPlayer && mpPlayer->startPlayback())
69 {
70 // TODO(F2): Handle end time attribute, too
71 if( getXAnimationNode()->getDuration().hasValue() )
72 {
74 }
75 else
76 {
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());
82 makeDelay( [this] () { this->checkPlayingStatus(); },
83 mpPlayer->getDuration(),
84 "AnimationAudioNode::check if still playing with delay") );
85 }
86 }
87 else
88 {
89 // deactivate ASAP:
90 auto self(getSelf());
92 makeEvent( [self] () { self->deactivate(); },
93 "AnimationAudioNode::deactivate without delay") );
94 }
95}
96
97// TODO(F2): generate deactivation event, when sound
98// is over
99
100namespace {
101
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
105struct NotifyAudioStopped
106{
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)) { }
112
113 void operator()()
114 {
115 m_rEventMultiplexer.notifyAudioStopped(m_pSelf);
116 }
117};
118
119}
120
122{
124 std::dynamic_pointer_cast<AnimationEventHandler>( getSelf() ) );
125 OSL_ENSURE( aHandler,
126 "could not cast self to AnimationEventHandler?" );
128
129 // force-end sound
130 if (mpPlayer)
131 {
132 mpPlayer->stopPlayback();
133 resetPlayer();
134 }
135
136 // notify _after_ state change:
138 makeEvent( NotifyAudioStopped(getContext().mrEventMultiplexer, getSelf()),
139 "AnimationAudioNode::notifyAudioStopped") );
140}
141
143{
144 // force slide to use the animation framework
145 // (otherwise, a single sound on the slide would
146 // not be played).
147 return true;
148}
149
151{
152 if (mpPlayer)
153 return;
154
155 try
156 {
160 getContext().mrMediaFileManager);
161 }
162 catch( lang::NoSupportException& )
163 {
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).
168 }
169}
170
172{
173 if (mpPlayer)
174 {
175 mpPlayer->stopPlayback();
176 mpPlayer->dispose();
177 mpPlayer.reset();
178 }
179}
180
182 const AnimationNodeSharedPtr& /*rNode*/ )
183{
184 // TODO(F2): for now we support only STOPAUDIO events.
185 deactivate();
186 return true;
187}
188
190{
191 auto self(getSelf());
192 double nDuration = mpPlayer->getDuration();
193 if (!mpPlayer->isPlaying() || nDuration < 0.0)
194 nDuration = 0.0;
195
197 makeDelay( [self] () { self->deactivate(); },
198 nDuration,
199 "AnimationAudioNode::deactivate with delay") );
200}
201
202} // namespace slideshow::internal
203
204/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > mxComponentContext
EventMultiplexer & m_rEventMultiplexer
::std::shared_ptr< BaseNode > m_pSelf
AnimationAudioNode(css::uno::Reference< css::animations::XAnimationNode > const &xNode, BaseContainerNodeSharedPtr const &pParent, NodeContext const &rContext)
virtual bool hasPendingAnimation() const override
Query node whether it has an animation pending.
virtual bool handleAnimationEvent(const AnimationNodeSharedPtr &rNode) override
overridden, because we need to deal with STOPAUDIO commands
virtual void deactivate_st(NodeState eDestState) override
css::uno::Reference< css::animations::XAudio > mxAudioNode
virtual void dispose() override
Dispose all object references.
NodeState
The current state of this AnimationNode.
This interface extends AnimationNode with some file-private accessor methods.
Definition: basenode.hxx:83
virtual css::uno::Reference< css::animations::XAnimationNode > getXAnimationNode() const override
Query the corresponding XAnimationNode.
Definition: basenode.cxx:408
SlideShowContext const & getContext() const
Definition: basenode.hxx:135
::std::shared_ptr< BaseNode > const & getSelf() const
Definition: basenode.hxx:136
virtual void dispose() override
Dispose all object references.
Definition: basenode.cxx:339
virtual void deactivate() override
Immediately stop this node.
Definition: basenode.cxx:564
void scheduleDeactivationEvent(EventSharedPtr const &pEvent=EventSharedPtr())
Definition: basenode.cxx:519
void addCommandStopAudioHandler(const AnimationEventHandlerSharedPtr &rHandler)
Register an event handler that will be called when an XCommand node's with the command STOPAUDIO is a...
void removeCommandStopAudioHandler(const AnimationEventHandlerSharedPtr &rHandler)
bool addEvent(const EventSharedPtr &event)
Add the given event to the queue.
Definition: eventqueue.cxx:79
static ::std::shared_ptr< SoundPlayer > create(EventMultiplexer &rEventMultiplexer, const OUString &rSoundURL, const css::uno::Reference< css::uno::XComponentContext > &rComponentContext, MediaFileManager &rMediaFileManager)
Create a sound player object.
Definition: soundplayer.cxx:42
#define makeEvent(f, d)
Definition: delayevent.hxx:131
#define makeDelay(f, t, d)
Definition: delayevent.hxx:128
#define ENSURE_OR_THROW(c, m)
::std::shared_ptr< BaseContainerNode > BaseContainerNodeSharedPtr
::std::shared_ptr< AnimationNode > AnimationNodeSharedPtr
::std::shared_ptr< AnimationEventHandler > AnimationEventHandlerSharedPtr
EventMultiplexer & mrEventMultiplexer
Definition: slideview.cxx:728
Context for every node.
Definition: basenode.hxx:45