OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 #include "content/renderer/pepper/pepper_media_stream_audio_track_host.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/location.h" | |
9 #include "base/logging.h" | |
10 #include "base/message_loop/message_loop_proxy.h" | |
11 #include "ppapi/c/pp_errors.h" | |
12 #include "ppapi/c/ppb_audio_frame.h" | |
13 #include "ppapi/shared_impl/media_stream_frame.h" | |
14 | |
15 namespace { | |
16 | |
17 // TODO(penghuang): make it configurable. | |
18 const int32_t kNumberOfFrames = 4; | |
19 | |
20 } // namespace | |
21 | |
22 namespace content { | |
23 | |
24 PepperMediaStreamAudioTrackHost::AudioSink::AudioSink( | |
25 PepperMediaStreamAudioTrackHost* host) | |
26 : host_(host), | |
27 frame_data_size_(0), | |
28 frames_(0), | |
29 main_message_loop_proxy_(base::MessageLoopProxy::current()) { | |
30 } | |
31 | |
32 PepperMediaStreamAudioTrackHost::AudioSink::~AudioSink() {} | |
33 | |
34 void PepperMediaStreamAudioTrackHost::AudioSink::EnqueueFrame(int32_t index) { | |
35 DCHECK_GE(index, 0); | |
36 DCHECK_LT(index, host_->frame_buffer()->number_of_frames()); | |
37 base::subtle::Atomic32 frames = base::subtle::NoBarrier_Load(&frames_); | |
38 DCHECK(!(frames & (1 << index))); | |
39 | |
bbudge
2014/01/22 22:06:37
I don't understand your use of atomics here. Betwe
Peng
2014/01/23 01:14:22
This method is only called from the main thread. A
| |
40 // Set the bit for the frame index. Automic does not support and/or | |
41 // operations, so we use add/sub instead. | |
42 base::subtle::NoBarrier_AtomicIncrement(&frames_, 1 << index); | |
43 } | |
44 | |
45 void PepperMediaStreamAudioTrackHost::AudioSink::InitFramesOnMainThread( | |
46 uint32_t number_of_frames, uint32_t frame_size) { | |
47 // Maybe use Atomic64 to support more frames. | |
48 DCHECK_LE(number_of_frames, 32u); | |
49 bool result = host_->InitFrames(number_of_frames, frame_size); | |
50 DCHECK(result); | |
51 for (uint32_t i = 0; i < number_of_frames; ++i) { | |
52 int32_t index = host_->frame_buffer()->DequeueFrame(); | |
53 DCHECK_GE(index, 0); | |
54 } | |
55 | |
56 base::subtle::Atomic32 frames = base::subtle::NoBarrier_CompareAndSwap( | |
57 &frames_, 0, (1 << number_of_frames) - 1); | |
58 DCHECK(!frames); | |
59 } | |
60 | |
61 void PepperMediaStreamAudioTrackHost::AudioSink::OnData(const int16* audio_data, | |
62 int sample_rate, | |
63 int number_of_channels, | |
64 int number_of_frames) { | |
65 DCHECK(audio_data); | |
66 DCHECK_EQ(sample_rate, audio_params_.sample_rate()); | |
67 DCHECK_EQ(number_of_channels, audio_params_.channels()); | |
68 DCHECK_EQ(number_of_frames, audio_params_.frames_per_buffer()); | |
69 | |
70 base::subtle::Atomic32 frames = base::subtle::NoBarrier_Load(&frames_); | |
71 if (frames) { | |
72 // If frames isn't zero, the |frame_buffer()| should be initialized already, | |
73 // and |frame_buffer()|'s attributes (|number_of_frames()|, |frame_size()|, | |
74 // etc) will not be changed. So it is safe to read them in the audio thread. | |
75 int32_t index = 0; | |
76 int32_t n = host_->frame_buffer()->number_of_frames(); | |
77 | |
78 // Find a free frame. | |
79 while(index < n && !(frames & (1 << index))) | |
80 index ++; | |
81 | |
82 if (index < n) { | |
83 // TODO(penghuang): support re-sampling, etc. | |
84 ppapi::MediaStreamFrame::Audio* frame = | |
85 &(host_->frame_buffer()->GetFramePointer(index)->audio); | |
86 frame->header.size = host_->frame_buffer()->frame_size(); | |
87 frame->header.type = ppapi::MediaStreamFrame::TYPE_AUDIO; | |
88 frame->timestamp = timestamp_.InMillisecondsF(); | |
89 frame->sample_rate = static_cast<PP_AudioSampleRate>(sample_rate); | |
90 frame->number_of_channels = number_of_channels; | |
91 frame->number_of_samples = number_of_channels * number_of_frames; | |
92 frame->data_size = frame_data_size_; | |
93 memcpy(frame->data, audio_data, frame_data_size_); | |
94 | |
95 // Clear the bit for the frame index. Automic does not support and/or | |
96 // operations, so we use add/sub instead. | |
97 base::subtle::NoBarrier_AtomicIncrement(&frames_, -(1 << index)); | |
98 | |
99 // This function is called from the audio thread, but | |
100 // |SendEnqueueFrameMessageToPlugin()| doesn't use any sync IPC, | |
101 // so it is safe to call it from the audio thread directly. | |
102 host_->SendEnqueueFrameMessageToPlugin(index); | |
103 } | |
104 } | |
105 timestamp_ += frame_duration_; | |
106 } | |
107 | |
108 void PepperMediaStreamAudioTrackHost::AudioSink::OnSetFormat( | |
109 const media::AudioParameters& params) { | |
110 DCHECK(!audio_params_.IsValid()); | |
111 DCHECK(params.IsValid()); | |
112 DCHECK_EQ(params.bits_per_sample(), 16); | |
113 | |
114 // TODO(penghuang): support setting format more than once. | |
115 audio_params_ = params; | |
116 frame_duration_ = audio_params_.GetBufferDuration(); | |
117 frame_data_size_ = audio_params_.GetBytesPerBuffer(); | |
118 | |
119 // The size is slightly bigger than necessary, because 8 extra bytes are added | |
120 // into the struct. Also see |MediaStreamFrame|. | |
121 int32_t size = sizeof(ppapi::MediaStreamFrame::Audio) + frame_data_size_; | |
122 | |
123 // This function is called from the audio thread and |InitFrames()| uses some | |
124 // sync IPC. So we have to call |InitFrames()| from the main thread. | |
125 main_message_loop_proxy_->PostTask( | |
126 FROM_HERE, | |
127 base::Bind(&AudioSink::InitFramesOnMainThread, | |
128 AsWeakPtr(), kNumberOfFrames, size)); | |
129 } | |
130 | |
131 PepperMediaStreamAudioTrackHost::PepperMediaStreamAudioTrackHost( | |
132 RendererPpapiHost* host, | |
133 PP_Instance instance, | |
134 PP_Resource resource, | |
135 const blink::WebMediaStreamTrack& track) | |
136 : PepperMediaStreamTrackHostBase(host, instance, resource), | |
137 track_(track), | |
138 connected_(false), | |
139 audio_sink_(this) { | |
140 DCHECK(!track_.isNull()); | |
141 } | |
142 | |
143 PepperMediaStreamAudioTrackHost::~PepperMediaStreamAudioTrackHost() { | |
144 OnClose(); | |
145 } | |
146 | |
147 void PepperMediaStreamAudioTrackHost::OnClose() { | |
148 if (connected_) { | |
149 MediaStreamAudioSink::RemoveFromAudioTrack(&audio_sink_, track_); | |
150 connected_ = false; | |
151 } | |
152 } | |
153 | |
154 bool PepperMediaStreamAudioTrackHost::OnNewFramePreEnqueued(int32_t index) { | |
155 audio_sink_.EnqueueFrame(index); | |
156 return true; | |
157 } | |
158 | |
159 void PepperMediaStreamAudioTrackHost::DidConnectPendingHostToResource() { | |
160 if (!connected_) { | |
161 MediaStreamAudioSink::AddToAudioTrack(&audio_sink_, track_); | |
162 connected_ = true; | |
163 } | |
164 } | |
165 | |
166 } // namespace content | |
OLD | NEW |