tizen beta release
[framework/web/webkit-efl.git] / Source / WebKit / chromium / src / AudioDestinationChromium.h
1 /*
2  * Copyright (C) 2010 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 AudioDestinationChromium_h
30 #define AudioDestinationChromium_h
31
32 #include "AudioBus.h"
33 #include "AudioDestination.h"
34 #include "AudioSourceProvider.h"
35 #include "WebAudioDevice.h"
36 #include "WebVector.h"
37
38 namespace WebKit { class WebAudioDevice; }
39
40 namespace WebCore { 
41
42 // An AudioDestination using Chromium's audio system
43
44 class AudioDestinationChromium : public AudioDestination, public WebKit::WebAudioDevice::RenderCallback {
45 public:
46     AudioDestinationChromium(AudioSourceProvider&, float sampleRate);
47     virtual ~AudioDestinationChromium();
48
49     virtual void start();
50     virtual void stop();
51     bool isPlaying() { return m_isPlaying; }
52
53     float sampleRate() const { return m_sampleRate; }
54
55     // WebKit::WebAudioDevice::RenderCallback
56     virtual void render(const WebKit::WebVector<float*>& audioData, size_t numberOfFrames);
57
58 private:
59     // A FIFO (First In First Out) buffer to handle mismatches in the
60     // audio backend hardware buffer size and the Web Audio render size.
61     class FIFO {
62     public:
63         // Create a FIFO that gets data from |provider|. The FIFO will
64         // be large enough to hold |fifoLength| frames of data of
65         // |numberOfChannels| channels. The AudioSourceProvider will
66         // be asked to produce |providerSize| frames when the FIFO
67         // needs more data.
68         FIFO(AudioSourceProvider& provider, unsigned numberOfChannels, size_t fifoLength, size_t providerSize);
69
70         // Read |framesToConsume| frames from the FIFO into the
71         // destination. If the FIFO does not have enough data, we ask
72         // the |provider| to get more data to fulfill the request.
73         void consume(AudioBus* destination, size_t framesToConsume);
74
75     private:
76         // Update the FIFO index by the step, with appropriate
77         // wrapping around the endpoint.
78         int updateIndex(int index, int step) { return (index + step) % m_fifoLength; }
79
80         void findWrapLengths(size_t index, size_t providerSize, size_t& part1Length, size_t& part2Length);
81         
82         // Fill the FIFO buffer with at least |numberOfFrames| more data.
83         void fillBuffer(size_t numberOfFrames);
84
85         // The provider of the data in our FIFO.
86         AudioSourceProvider& m_provider;
87
88         // The FIFO itself. In reality, the FIFO is a circular buffer.
89         AudioBus m_fifoAudioBus;
90
91         // The total available space in the FIFO.
92         size_t m_fifoLength;
93
94         // The number of actual elements in the FIFO
95         size_t m_framesInFifo;
96
97         // Where to start reading from the FIFO.
98         size_t m_readIndex;
99
100         // Where to start writing to the FIFO.
101         size_t m_writeIndex;
102
103         // Number of frames of data that the provider will produce per call.
104         unsigned int m_providerSize;
105
106         // Temporary workspace to hold the data from the provider.
107         AudioBus m_tempBus;
108     };
109
110 AudioSourceProvider& m_provider;
111     AudioBus m_renderBus;
112     float m_sampleRate;
113     bool m_isPlaying;
114     OwnPtr<WebKit::WebAudioDevice> m_audioDevice;
115     size_t m_callbackBufferSize;
116     OwnPtr<FIFO> m_fifo;
117 };
118
119 } // namespace WebCore
120
121 #endif // AudioDestinationChromium_h