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.
5 #ifndef CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_AUDIO_TRACK_HOST_H_
6 #define CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_AUDIO_TRACK_HOST_H_
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"
21 class MessageLoopProxy;
26 class PepperMediaStreamAudioTrackHost : public PepperMediaStreamTrackHostBase {
28 PepperMediaStreamAudioTrackHost(RendererPpapiHost* host,
31 const blink::WebMediaStreamTrack& track);
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 {
38 explicit AudioSink(PepperMediaStreamAudioTrackHost* host);
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);
47 // Initializes buffers on the main thread.
48 void InitBuffersOnMainThread(int32_t number_of_buffers,
51 // Send enqueue buffer message on the main thread.
52 void SendEnqueueBufferMessageOnMainThread(int32_t index);
54 // MediaStreamAudioSink overrides:
55 // These two functions should be called on the audio thread.
56 virtual void OnData(const int16* audio_data,
58 int number_of_channels,
59 int number_of_frames) OVERRIDE;
60 virtual void OnSetFormat(const media::AudioParameters& params) OVERRIDE;
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
67 PepperMediaStreamAudioTrackHost* host_;
69 // Timestamp of the next received audio buffer.
70 // Access only on the audio thread.
71 base::TimeDelta timestamp_;
73 // Duration of one audio buffer.
74 // Access only on the audio thread.
75 base::TimeDelta buffer_duration_;
77 // The current audio parameters.
78 // Access only on the audio thread.
79 media::AudioParameters audio_params_;
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_;
86 // The audio data size of one audio buffer in bytes.
87 // Access only on the audio thread.
88 uint32_t buffer_data_size_;
90 // A lock to protect the index queue |buffers_|.
93 // A queue for free buffer indices.
94 std::deque<int32_t> buffers_;
96 scoped_refptr<base::MessageLoopProxy> main_message_loop_proxy_;
98 base::ThreadChecker audio_thread_checker_;
100 base::WeakPtrFactory<AudioSink> weak_factory_;
102 DISALLOW_COPY_AND_ASSIGN(AudioSink);
105 virtual ~PepperMediaStreamAudioTrackHost();
107 // PepperMediaStreamTrackHostBase overrides:
108 virtual void OnClose() OVERRIDE;
110 // MediaStreamBufferManager::Delegate overrides:
111 virtual void OnNewBufferEnqueued() OVERRIDE;
113 // ResourceHost overrides:
114 virtual void DidConnectPendingHostToResource() OVERRIDE;
116 blink::WebMediaStreamTrack track_;
118 // True if |audio_sink_| has been added to |blink::WebMediaStreamTrack|
122 AudioSink audio_sink_;
124 DISALLOW_COPY_AND_ASSIGN(PepperMediaStreamAudioTrackHost);
127 } // namespace content
129 #endif // CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_AUDIO_TRACK_HOST_H_