Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / pepper_media_stream_audio_track_host.h
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_AUDIO_TRACK_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_AUDIO_TRACK_HOST_H_
7
8 #include <deque>
9
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/thread_checker.h"
15 #include "content/public/renderer/media_stream_audio_sink.h"
16 #include "content/renderer/pepper/pepper_media_stream_track_host_base.h"
17 #include "media/audio/audio_parameters.h"
18 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
19
20 namespace base {
21 class MessageLoopProxy;
22 }  // namespace base
23
24 namespace content {
25
26 class PepperMediaStreamAudioTrackHost : public PepperMediaStreamTrackHostBase {
27  public:
28   PepperMediaStreamAudioTrackHost(RendererPpapiHost* host,
29                                   PP_Instance instance,
30                                   PP_Resource resource,
31                                   const blink::WebMediaStreamTrack& track);
32
33  private:
34   // A helper class for receiving audio samples in the audio thread.
35   // This class is created and destroyed on the renderer main thread.
36   class AudioSink : public MediaStreamAudioSink {
37    public:
38     explicit AudioSink(PepperMediaStreamAudioTrackHost* host);
39     virtual ~AudioSink();
40
41     // Enqueues a free buffer index into |buffers_| which will be used for
42     // sending audio samples to plugin.
43     // This function is called on the main thread.
44     void EnqueueBuffer(int32_t index);
45
46    private:
47     // Initializes buffers on the main thread.
48     void InitBuffersOnMainThread(int32_t number_of_buffers,
49                                  int32_t buffer_size);
50
51     // Send enqueue buffer message on the main thread.
52     void SendEnqueueBufferMessageOnMainThread(int32_t index);
53
54     // MediaStreamAudioSink overrides:
55     // These two functions should be called on the audio thread.
56     virtual void OnData(const int16* audio_data,
57                         int sample_rate,
58                         int number_of_channels,
59                         int number_of_frames) OVERRIDE;
60     virtual void OnSetFormat(const media::AudioParameters& params) OVERRIDE;
61
62     // Unowned host which is available during the AudioSink's lifespan.
63     // It is mainly used in the main thread. But the audio thread will use
64     // host_->buffer_manager() to read some buffer properties. It is safe
65     // because the buffer_manager()'s properties will not be changed after
66     // initialization.
67     PepperMediaStreamAudioTrackHost* host_;
68
69     // Timestamp of the next received audio buffer.
70     // Access only on the audio thread.
71     base::TimeDelta timestamp_;
72
73     // Duration of one audio buffer.
74     // Access only on the audio thread.
75     base::TimeDelta buffer_duration_;
76
77     // The current audio parameters.
78     // Access only on the audio thread.
79     media::AudioParameters audio_params_;
80
81     // The original audio parameters which is set in the first time of
82     // OnSetFormat being called.
83     // Access only on the audio thread.
84     media::AudioParameters original_audio_params_;
85
86     // The audio data size of one audio buffer in bytes.
87     // Access only on the audio thread.
88     uint32_t buffer_data_size_;
89
90     // A lock to protect the index queue |buffers_|.
91     base::Lock lock_;
92
93     // A queue for free buffer indices.
94     std::deque<int32_t> buffers_;
95
96     scoped_refptr<base::MessageLoopProxy> main_message_loop_proxy_;
97
98     base::ThreadChecker audio_thread_checker_;
99
100     base::WeakPtrFactory<AudioSink> weak_factory_;
101
102     DISALLOW_COPY_AND_ASSIGN(AudioSink);
103   };
104
105   virtual ~PepperMediaStreamAudioTrackHost();
106
107   // PepperMediaStreamTrackHostBase overrides:
108   virtual void OnClose() OVERRIDE;
109
110   // MediaStreamBufferManager::Delegate overrides:
111   virtual void OnNewBufferEnqueued() OVERRIDE;
112
113   // ResourceHost overrides:
114   virtual void DidConnectPendingHostToResource() OVERRIDE;
115
116   blink::WebMediaStreamTrack track_;
117
118   // True if |audio_sink_| has been added to |blink::WebMediaStreamTrack|
119   // as a sink.
120   bool connected_;
121
122   AudioSink audio_sink_;
123
124   DISALLOW_COPY_AND_ASSIGN(PepperMediaStreamAudioTrackHost);
125 };
126
127 }  // namespace content
128
129 #endif  // CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_AUDIO_TRACK_HOST_H_