1*70f497fbSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*70f497fbSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*70f497fbSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*70f497fbSAndrew Rist  * distributed with this work for additional information
6*70f497fbSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*70f497fbSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*70f497fbSAndrew Rist  * "License"); you may not use this file except in compliance
9*70f497fbSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*70f497fbSAndrew Rist  *
11*70f497fbSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*70f497fbSAndrew Rist  *
13*70f497fbSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*70f497fbSAndrew Rist  * software distributed under the License is distributed on an
15*70f497fbSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*70f497fbSAndrew Rist  * KIND, either express or implied.  See the License for the
17*70f497fbSAndrew Rist  * specific language governing permissions and limitations
18*70f497fbSAndrew Rist  * under the License.
19*70f497fbSAndrew Rist  *
20*70f497fbSAndrew Rist  *************************************************************/
21*70f497fbSAndrew Rist 
22*70f497fbSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_slideshow.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir // must be first
28cdf0e10cSrcweir #include <canvas/debug.hxx>
29cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "eventqueue.hxx"
32cdf0e10cSrcweir #include "animationaudionode.hxx"
33cdf0e10cSrcweir #include "delayevent.hxx"
34cdf0e10cSrcweir #include "tools.hxx"
35cdf0e10cSrcweir #include "nodetools.hxx"
36cdf0e10cSrcweir #include "boost/bind.hpp"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace com::sun::star;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir namespace slideshow {
41cdf0e10cSrcweir namespace internal {
42cdf0e10cSrcweir 
AnimationAudioNode(const uno::Reference<animations::XAnimationNode> & xNode,const BaseContainerNodeSharedPtr & rParent,const NodeContext & rContext)43cdf0e10cSrcweir AnimationAudioNode::AnimationAudioNode(
44cdf0e10cSrcweir     const uno::Reference< animations::XAnimationNode >& xNode,
45cdf0e10cSrcweir     const BaseContainerNodeSharedPtr&                   rParent,
46cdf0e10cSrcweir     const NodeContext&                                  rContext )
47cdf0e10cSrcweir     : BaseNode( xNode, rParent, rContext ),
48cdf0e10cSrcweir       mxAudioNode( xNode, uno::UNO_QUERY_THROW ),
49cdf0e10cSrcweir       maSoundURL(),
50cdf0e10cSrcweir       mpPlayer()
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     mxAudioNode->getSource() >>= maSoundURL;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir     OSL_ENSURE( maSoundURL.getLength(),
55cdf0e10cSrcweir                 "could not extract sound source URL/empty URL string" );
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     ENSURE_OR_THROW( getContext().mxComponentContext.is(),
58cdf0e10cSrcweir                       "Invalid component context" );
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
dispose()61cdf0e10cSrcweir void AnimationAudioNode::dispose()
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     resetPlayer();
64cdf0e10cSrcweir     mxAudioNode.clear();
65cdf0e10cSrcweir     BaseNode::dispose();
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
activate_st()68cdf0e10cSrcweir void AnimationAudioNode::activate_st()
69cdf0e10cSrcweir {
70cdf0e10cSrcweir     createPlayer();
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     AnimationEventHandlerSharedPtr aHandler(
73cdf0e10cSrcweir         boost::dynamic_pointer_cast<AnimationEventHandler>( getSelf() ) );
74cdf0e10cSrcweir     OSL_ENSURE( aHandler,
75cdf0e10cSrcweir                 "could not cast self to AnimationEventHandler?" );
76cdf0e10cSrcweir     getContext().mrEventMultiplexer.addCommandStopAudioHandler( aHandler );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     if (mpPlayer && mpPlayer->startPlayback())
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         // TODO(F2): Handle end time attribute, too
81cdf0e10cSrcweir         if( getXAnimationNode()->getDuration().hasValue() )
82cdf0e10cSrcweir         {
83cdf0e10cSrcweir             scheduleDeactivationEvent();
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir         else
86cdf0e10cSrcweir         {
87cdf0e10cSrcweir             // no node duration. Take inherent media time, then
88cdf0e10cSrcweir             scheduleDeactivationEvent(
89cdf0e10cSrcweir                 makeDelay( boost::bind( &AnimationNode::deactivate, getSelf() ),
90cdf0e10cSrcweir                                         mpPlayer->getDuration(),
91cdf0e10cSrcweir                            "AnimationAudioNode::deactivate with delay") );
92cdf0e10cSrcweir         }
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir     else
95cdf0e10cSrcweir     {
96cdf0e10cSrcweir         // deactivate ASAP:
97cdf0e10cSrcweir         scheduleDeactivationEvent(
98cdf0e10cSrcweir             makeEvent( boost::bind( &AnimationNode::deactivate, getSelf() ),
99cdf0e10cSrcweir                                     "AnimationAudioNode::deactivate without delay") );
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir // TODO(F2): generate deactivation event, when sound
104cdf0e10cSrcweir // is over
105cdf0e10cSrcweir 
deactivate_st(NodeState)106cdf0e10cSrcweir void AnimationAudioNode::deactivate_st( NodeState /*eDestState*/ )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir     AnimationEventHandlerSharedPtr aHandler(
109cdf0e10cSrcweir         boost::dynamic_pointer_cast<AnimationEventHandler>( getSelf() ) );
110cdf0e10cSrcweir     OSL_ENSURE( aHandler,
111cdf0e10cSrcweir                 "could not cas self to AnimationEventHandler?" );
112cdf0e10cSrcweir     getContext().mrEventMultiplexer.removeCommandStopAudioHandler( aHandler );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir     // force-end sound
115cdf0e10cSrcweir     if (mpPlayer)
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         mpPlayer->stopPlayback();
118cdf0e10cSrcweir         resetPlayer();
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     // notify _after_ state change:
122cdf0e10cSrcweir     getContext().mrEventQueue.addEvent(
123cdf0e10cSrcweir         makeEvent( boost::bind( &EventMultiplexer::notifyAudioStopped,
124cdf0e10cSrcweir                                 boost::ref(getContext().mrEventMultiplexer),
125cdf0e10cSrcweir                                 getSelf() ),
126cdf0e10cSrcweir                    "AnimationAudioNode::notifyAudioStopped") );
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
hasPendingAnimation() const129cdf0e10cSrcweir bool AnimationAudioNode::hasPendingAnimation() const
130cdf0e10cSrcweir {
131cdf0e10cSrcweir     // force slide to use the animation framework
132cdf0e10cSrcweir     // (otherwise, a single sound on the slide would
133cdf0e10cSrcweir     // not be played).
134cdf0e10cSrcweir     return true;
135cdf0e10cSrcweir }
136cdf0e10cSrcweir 
createPlayer() const137cdf0e10cSrcweir void AnimationAudioNode::createPlayer() const
138cdf0e10cSrcweir {
139cdf0e10cSrcweir     if (mpPlayer)
140cdf0e10cSrcweir         return;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir     try
143cdf0e10cSrcweir     {
144cdf0e10cSrcweir         mpPlayer = SoundPlayer::create( getContext().mrEventMultiplexer,
145cdf0e10cSrcweir                                         maSoundURL,
146cdf0e10cSrcweir                                         getContext().mxComponentContext );
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir     catch( lang::NoSupportException& )
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         // catch possible exceptions from SoundPlayer,
151cdf0e10cSrcweir         // since being not able to playback the sound
152cdf0e10cSrcweir         // is not a hard error here (remainder of the
153cdf0e10cSrcweir         // animations should still work).
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir }
156cdf0e10cSrcweir 
resetPlayer() const157cdf0e10cSrcweir void AnimationAudioNode::resetPlayer() const
158cdf0e10cSrcweir {
159cdf0e10cSrcweir     if (mpPlayer)
160cdf0e10cSrcweir     {
161cdf0e10cSrcweir         mpPlayer->stopPlayback();
162cdf0e10cSrcweir         mpPlayer->dispose();
163cdf0e10cSrcweir         mpPlayer.reset();
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
handleAnimationEvent(const AnimationNodeSharedPtr &)167cdf0e10cSrcweir bool AnimationAudioNode::handleAnimationEvent(
168cdf0e10cSrcweir     const AnimationNodeSharedPtr& /*rNode*/ )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir     // TODO(F2): for now we support only STOPAUDIO events.
171cdf0e10cSrcweir     deactivate();
172cdf0e10cSrcweir     return true;
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir } // namespace internal
176cdf0e10cSrcweir } // namespace presentation
177cdf0e10cSrcweir 
178