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 "ppapi/host/host_message_context.h"
19 #include "ppapi/shared_impl/media_stream_audio_track_shared.h"
20 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
23 class MessageLoopProxy
;
28 class PepperMediaStreamAudioTrackHost
: public PepperMediaStreamTrackHostBase
{
30 PepperMediaStreamAudioTrackHost(RendererPpapiHost
* host
,
33 const blink::WebMediaStreamTrack
& track
);
36 // A helper class for receiving audio samples in the audio thread.
37 // This class is created and destroyed on the renderer main thread.
38 class AudioSink
: public MediaStreamAudioSink
{
40 explicit AudioSink(PepperMediaStreamAudioTrackHost
* host
);
41 ~AudioSink() override
;
43 // Enqueues a free buffer index into |buffers_| which will be used for
44 // sending audio samples to plugin.
45 // This function is called on the main thread.
46 void EnqueueBuffer(int32_t index
);
48 // This function is called on the main thread.
49 int32_t Configure(int32_t number_of_buffers
, int32_t duration
,
50 const ppapi::host::ReplyMessageContext
& context
);
52 // Send a reply to the currently pending |Configure()| request.
53 void SendConfigureReply(int32_t result
);
55 // MediaStreamAudioSink overrides:
56 // These two functions should be called on the audio thread.
57 // NOTE: For this specific instance, |OnSetFormat()| is also called on the
58 // main thread. However, the call to |OnSetFormat()| happens before this
59 // sink is added to an audio track, also on the main thread, which should
60 // avoid any potential races.
61 void OnData(const media::AudioBus
& audio_bus
,
62 base::TimeTicks estimated_capture_time
) override
;
63 void OnSetFormat(const media::AudioParameters
& params
) override
;
66 // Initializes buffers on the main thread.
67 void SetFormatOnMainThread(int bytes_per_second
, int bytes_per_frame
);
71 // Send enqueue buffer message on the main thread.
72 void SendEnqueueBufferMessageOnMainThread(int32_t index
,
73 int32_t buffers_generation
);
75 // Unowned host which is available during the AudioSink's lifespan.
76 // It is mainly used in the main thread. But the audio thread will use
77 // host_->buffer_manager() to read some buffer properties. It is safe
78 // because the buffer_manager()'s properties will not be changed after
80 PepperMediaStreamAudioTrackHost
* host_
;
82 // The estimated capture time of the first sample frame of audio. This is
83 // used as the timebase to compute the buffer timestamps.
84 // Access only on the audio thread.
85 base::TimeTicks first_frame_capture_time_
;
87 // The current audio parameters.
88 // Access only on the audio thread.
89 media::AudioParameters audio_params_
;
91 // Index of the currently active buffer.
92 // Access only on the audio thread.
93 int active_buffer_index_
;
95 // Generation of buffers corresponding to the currently active
96 // buffer. Used to make sure the active buffer is still valid.
97 // Access only on the audio thread.
98 int32_t active_buffers_generation_
;
100 // Current offset, in sample frames, within the currently active buffer.
101 // Access only on the audio thread.
102 int active_buffer_frame_offset_
;
104 // A lock to protect the index queue |buffers_|, |buffers_generation_|,
105 // buffers in |host_->buffer_manager()|, and |output_buffer_size_|.
108 // A queue for free buffer indices.
109 std::deque
<int32_t> buffers_
;
111 // Generation of buffers. It is increased by every |InitBuffers()| call.
112 int32_t buffers_generation_
;
114 // Intended size of each output buffer.
115 int32_t output_buffer_size_
;
117 scoped_refptr
<base::MessageLoopProxy
> main_message_loop_proxy_
;
119 base::ThreadChecker audio_thread_checker_
;
121 // Number of buffers.
122 int32_t number_of_buffers_
;
124 // Number of bytes per second.
125 int bytes_per_second_
;
127 // Number of bytes per frame = channels * bytes per sample.
128 int bytes_per_frame_
;
130 // User-configured buffer duration, in milliseconds.
131 int32_t user_buffer_duration_
;
133 // Pending |Configure()| reply context.
134 ppapi::host::ReplyMessageContext pending_configure_reply_
;
136 base::WeakPtrFactory
<AudioSink
> weak_factory_
;
138 DISALLOW_COPY_AND_ASSIGN(AudioSink
);
141 ~PepperMediaStreamAudioTrackHost() override
;
143 // ResourceMessageHandler overrides:
144 int32_t OnResourceMessageReceived(
145 const IPC::Message
& msg
,
146 ppapi::host::HostMessageContext
* context
) override
;
149 int32_t OnHostMsgConfigure(
150 ppapi::host::HostMessageContext
* context
,
151 const ppapi::MediaStreamAudioTrackShared::Attributes
& attributes
);
153 // PepperMediaStreamTrackHostBase overrides:
154 void OnClose() override
;
156 // MediaStreamBufferManager::Delegate overrides:
157 void OnNewBufferEnqueued() override
;
159 // ResourceHost overrides:
160 void DidConnectPendingHostToResource() override
;
162 blink::WebMediaStreamTrack track_
;
164 // True if |audio_sink_| has been added to |blink::WebMediaStreamTrack|
168 AudioSink audio_sink_
;
170 DISALLOW_COPY_AND_ASSIGN(PepperMediaStreamAudioTrackHost
);
173 } // namespace content
175 #endif // CONTENT_RENDERER_PEPPER_PEPPER_MEDIA_STREAM_AUDIO_TRACK_HOST_H_