2fcd6bd7372d437f6a1581987e3bc98c44e4c6c1
[platform/framework/web/crosswalk-tizen.git] /
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef AudioScheduledSourceNode_h
30 #define AudioScheduledSourceNode_h
31
32 #include "bindings/core/v8/ActiveScriptWrappable.h"
33 #include "modules/webaudio/AudioSourceNode.h"
34
35 namespace blink {
36
37 class BaseAudioContext;
38 class AudioBus;
39
40 class AudioScheduledSourceHandler : public AudioHandler {
41  public:
42   // These are the possible states an AudioScheduledSourceNode can be in:
43   //
44   // UNSCHEDULED_STATE - Initial playback state. Created, but not yet scheduled.
45   // SCHEDULED_STATE - Scheduled to play (via start()), but not yet playing.
46   // PLAYING_STATE - Generating sound.
47   // FINISHED_STATE - Finished generating sound.
48   //
49   // The state can only transition to the next state, except for the
50   // FINISHED_STATE which can never be changed.
51   enum PlaybackState {
52     // These must be defined with the same names and values as in the .idl file.
53     UNSCHEDULED_STATE = 0,
54     SCHEDULED_STATE = 1,
55     PLAYING_STATE = 2,
56     FINISHED_STATE = 3
57   };
58
59   AudioScheduledSourceHandler(NodeType, AudioNode&, float sampleRate);
60
61   // Scheduling.
62   void start(double when, ExceptionState&);
63   void stop(double when, ExceptionState&);
64
65   PlaybackState playbackState() const {
66     return static_cast<PlaybackState>(acquireLoad(&m_playbackState));
67   }
68
69   void setPlaybackState(PlaybackState newState) {
70     releaseStore(&m_playbackState, newState);
71   }
72
73   bool isPlayingOrScheduled() const {
74     PlaybackState state = playbackState();
75     return state == PLAYING_STATE || state == SCHEDULED_STATE;
76   }
77
78   bool hasFinished() const { return playbackState() == FINISHED_STATE; }
79
80  protected:
81   // Get frame information for the current time quantum.
82   // We handle the transition into PLAYING_STATE and FINISHED_STATE here,
83   // zeroing out portions of the outputBus which are outside the range of
84   // startFrame and endFrame.
85   //
86   // Each frame time is relative to the context's currentSampleFrame().
87   // quantumFrameOffset    : Offset frame in this time quantum to start
88   //                         rendering.
89   // nonSilentFramesToProcess : Number of frames rendering non-silence (will be
90   //                            <= quantumFrameSize).
91   void updateSchedulingInfo(size_t quantumFrameSize,
92                             AudioBus* outputBus,
93                             size_t& quantumFrameOffset,
94                             size_t& nonSilentFramesToProcess);
95
96   // Called when we have no more sound to play or the stop() time has been
97   // reached. No onEnded event is called.
98   virtual void finishWithoutOnEnded();
99
100   // Like finishWithoutOnEnded(), but an onEnded (if specified) is called.
101   virtual void finish();
102
103   void notifyEnded();
104
105   // This synchronizes with process() and any other method that needs to be
106   // synchronized like setBuffer for AudioBufferSource.
107   mutable Mutex m_processLock;
108
109   // m_startTime is the time to start playing based on the context's timeline (0
110   // or a time less than the context's current time means "now").
111   double m_startTime;  // in seconds
112
113   // m_endTime is the time to stop playing based on the context's timeline (0 or
114   // a time less than the context's current time means "now").  If it hasn't
115   // been set explicitly, then the sound will not stop playing (if looping) or
116   // will stop when the end of the AudioBuffer has been reached.
117   double m_endTime;  // in seconds
118
119   static const double UnknownTime;
120
121  private:
122   // This is accessed by both the main thread and audio thread.  Use the setter
123   // and getter to protect the access to this.
124   int m_playbackState;
125 };
126
127 class AudioScheduledSourceNode : public AudioSourceNode,
128                                  public ActiveScriptWrappable {
129   USING_GARBAGE_COLLECTED_MIXIN(AudioScheduledSourceNode);
130
131  public:
132   void start(ExceptionState&);
133   void start(double when, ExceptionState&);
134   void stop(ExceptionState&);
135   void stop(double when, ExceptionState&);
136
137   EventListener* onended();
138   void setOnended(EventListener*);
139
140   // ScriptWrappable:
141   bool hasPendingActivity() const final;
142
143   DEFINE_INLINE_VIRTUAL_TRACE() { AudioSourceNode::trace(visitor); }
144
145  protected:
146   explicit AudioScheduledSourceNode(BaseAudioContext&);
147   AudioScheduledSourceHandler& audioScheduledSourceHandler() const;
148 };
149
150 }  // namespace blink
151
152 #endif  // AudioScheduledSourceNode_h