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