Chromium Code Reviews| 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/macros.h" | |
| 11 #include "base/message_loop/message_loop_proxy.h" | |
| 12 #include "ppapi/c/pp_errors.h" | |
| 13 #include "ppapi/c/ppb_audio_frame.h" | |
| 14 #include "ppapi/shared_impl/media_stream_frame.h" | |
| 15 | |
| 16 using media::AudioParameters; | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // TODO(penghuang): make it configurable. | |
| 21 const int32_t kNumberOfFrames = 4; | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 PepperMediaStreamAudioTrackHost::AudioSink::AudioSink( | |
| 28 PepperMediaStreamAudioTrackHost* host) | |
| 29 : host_(host), | |
| 30 frame_data_size_(0), | |
| 31 frames_(0), | |
| 32 main_message_loop_proxy_(base::MessageLoopProxy::current()) { | |
| 33 } | |
| 34 | |
| 35 PepperMediaStreamAudioTrackHost::AudioSink::~AudioSink() {} | |
| 36 | |
| 37 void PepperMediaStreamAudioTrackHost::AudioSink::EnqueueFrame(int32_t index) { | |
| 38 DCHECK_GE(index, 0); | |
|
yzshen1
2014/01/29 21:28:06
Please also also DCHECK that this is called on the
Peng
2014/01/31 18:54:43
Done.
| |
| 39 DCHECK_LT(index, host_->frame_buffer()->number_of_frames()); | |
| 40 DCHECK(!(base::subtle::NoBarrier_Load(&frames_) & (1 << index))); | |
| 41 | |
| 42 // Set the bit for the frame index. Automic does not support and/or | |
| 43 // operations, so we use add/sub instead. | |
| 44 base::subtle::NoBarrier_AtomicIncrement(&frames_, 1 << index); | |
|
yzshen1
2014/01/29 21:28:06
This kind of code is, as the name suggested, subtl
Peng
2014/01/31 18:54:43
Atomic's performance is slightly better than lock.
yzshen1
2014/01/31 19:50:26
IMO, the biggest reason that we avoid lock is to m
dmichael (off chromium)
2014/01/31 20:14:28
Yuzhu's exactly right; I was only concerned about
Peng
2014/01/31 21:09:25
Done.
Peng
2014/01/31 21:09:25
Fixed by using base::Lock. Done
| |
| 45 } | |
| 46 | |
| 47 void PepperMediaStreamAudioTrackHost::AudioSink::InitFramesOnMainThread( | |
| 48 uint32_t number_of_frames, uint32_t frame_size) { | |
| 49 // Maybe use Atomic64 to support more frames. | |
| 50 DCHECK_LE(number_of_frames, 32u); | |
| 51 bool result = host_->InitFrames(number_of_frames, frame_size); | |
| 52 DCHECK(result); | |
| 53 for (uint32_t i = 0; i < number_of_frames; ++i) { | |
| 54 int32_t index = host_->frame_buffer()->DequeueFrame(); | |
| 55 DCHECK_GE(index, 0); | |
| 56 } | |
| 57 | |
| 58 base::subtle::Atomic32 frames = base::subtle::NoBarrier_CompareAndSwap( | |
| 59 &frames_, 0, (1 << number_of_frames) - 1); | |
| 60 DCHECK(!frames); | |
| 61 } | |
| 62 | |
| 63 void PepperMediaStreamAudioTrackHost::AudioSink::OnData(const int16* audio_data, | |
| 64 int sample_rate, | |
| 65 int number_of_channels, | |
| 66 int number_of_frames) { | |
| 67 DCHECK(audio_data); | |
| 68 DCHECK_EQ(sample_rate, audio_params_.sample_rate()); | |
| 69 DCHECK_EQ(number_of_channels, audio_params_.channels()); | |
| 70 DCHECK_EQ(number_of_frames, audio_params_.frames_per_buffer()); | |
| 71 | |
| 72 base::subtle::Atomic32 frames = base::subtle::NoBarrier_Load(&frames_); | |
| 73 if (frames) { | |
| 74 // If frames isn't zero, the |frame_buffer()| should be initialized already, | |
| 75 // and |frame_buffer()|'s attributes (|number_of_frames()|, |frame_size()|, | |
| 76 // etc) will not be changed. So it is safe to read them in the audio thread. | |
| 77 int32_t index = 0; | |
| 78 int32_t n = host_->frame_buffer()->number_of_frames(); | |
| 79 | |
| 80 // Find a free frame. | |
| 81 while(index < n && !(frames & (1 << index))) | |
| 82 index ++; | |
|
yzshen1
2014/01/29 21:28:06
no space in the middle, please.
Peng
2014/01/31 18:54:43
Done.
| |
| 83 | |
| 84 if (index < n) { | |
| 85 // TODO(penghuang): support re-sampling, etc. | |
| 86 ppapi::MediaStreamFrame::Audio* frame = | |
| 87 &(host_->frame_buffer()->GetFramePointer(index)->audio); | |
|
yzshen1
2014/01/29 21:28:06
We are accessing the frame buffer from multiple th
Peng
2014/01/31 18:54:43
Yes. After the frame_buffer() being initialized, t
| |
| 88 frame->header.size = host_->frame_buffer()->frame_size(); | |
| 89 frame->header.type = ppapi::MediaStreamFrame::TYPE_AUDIO; | |
| 90 frame->timestamp = timestamp_.InMillisecondsF(); | |
| 91 frame->sample_rate = static_cast<PP_AudioFrame_SampleRate>(sample_rate); | |
| 92 frame->number_of_channels = number_of_channels; | |
| 93 frame->number_of_samples = number_of_channels * number_of_frames; | |
| 94 frame->data_size = frame_data_size_; | |
| 95 memcpy(frame->data, audio_data, frame_data_size_); | |
| 96 | |
| 97 // Clear the bit for the frame index. Automic does not support and/or | |
| 98 // operations, so we use add/sub instead. | |
| 99 base::subtle::NoBarrier_AtomicIncrement(&frames_, -(1 << index)); | |
|
yzshen1
2014/01/29 21:28:06
what if index == 31?
Peng
2014/01/31 18:54:43
I think (1 << 32) will be 0x80000000. Because it i
| |
| 100 | |
| 101 // This function is called from the audio thread, but | |
| 102 // |SendEnqueueFrameMessageToPlugin()| doesn't use any sync IPC, | |
| 103 // so it is safe to call it from the audio thread directly. | |
| 104 host_->SendEnqueueFrameMessageToPlugin(index); | |
|
yzshen1
2014/01/29 21:28:06
Unless we said explicitly comment at the declarati
Peng
2014/01/31 18:54:43
I understand. But I really don't have any good ide
| |
| 105 } | |
| 106 } | |
| 107 timestamp_ += frame_duration_; | |
| 108 } | |
| 109 | |
| 110 void PepperMediaStreamAudioTrackHost::AudioSink::OnSetFormat( | |
| 111 const AudioParameters& params) { | |
| 112 DCHECK(!audio_params_.IsValid()); | |
| 113 DCHECK(params.IsValid()); | |
| 114 DCHECK_EQ(params.bits_per_sample(), 16); | |
| 115 DCHECK((params.sample_rate() == AudioParameters::kTelephoneSampleRate) || | |
| 116 (params.sample_rate() == AudioParameters::kAudioCDSampleRate)); | |
| 117 | |
| 118 COMPILE_ASSERT(AudioParameters::kTelephoneSampleRate == 8000, | |
| 119 audio_sample_rate_does_not_match); | |
| 120 COMPILE_ASSERT(AudioParameters::kAudioCDSampleRate == 44100, | |
| 121 audio_sample_rate_does_not_match); | |
| 122 | |
| 123 // TODO(penghuang): support setting format more than once. | |
| 124 audio_params_ = params; | |
| 125 frame_duration_ = audio_params_.GetBufferDuration(); | |
| 126 frame_data_size_ = audio_params_.GetBytesPerBuffer(); | |
| 127 | |
| 128 // The size is slightly bigger than necessary, because 8 extra bytes are added | |
| 129 // into the struct. Also see |MediaStreamFrame|. | |
| 130 int32_t size = sizeof(ppapi::MediaStreamFrame::Audio) + frame_data_size_; | |
| 131 | |
| 132 // This function is called from the audio thread and |InitFrames()| uses some | |
| 133 // sync IPC. So we have to call |InitFrames()| from the main thread. | |
|
yzshen1
2014/01/29 21:28:06
I have no problem with running InitFrames() on the
Peng
2014/01/31 18:54:43
Currently, InitFrames() uses content::RenderThread
yzshen1
2014/02/03 18:14:36
Ah, I see. :)
I thought you meant that you added
| |
| 134 main_message_loop_proxy_->PostTask( | |
| 135 FROM_HERE, | |
| 136 base::Bind(&AudioSink::InitFramesOnMainThread, | |
| 137 AsWeakPtr(), kNumberOfFrames, size)); | |
| 138 } | |
| 139 | |
| 140 PepperMediaStreamAudioTrackHost::PepperMediaStreamAudioTrackHost( | |
| 141 RendererPpapiHost* host, | |
| 142 PP_Instance instance, | |
| 143 PP_Resource resource, | |
| 144 const blink::WebMediaStreamTrack& track) | |
| 145 : PepperMediaStreamTrackHostBase(host, instance, resource), | |
| 146 track_(track), | |
| 147 connected_(false), | |
| 148 audio_sink_(this) { | |
| 149 DCHECK(!track_.isNull()); | |
| 150 } | |
| 151 | |
| 152 PepperMediaStreamAudioTrackHost::~PepperMediaStreamAudioTrackHost() { | |
| 153 OnClose(); | |
|
yzshen1
2014/01/29 21:28:06
(I haven't read the relevant backend code, but wan
Peng
2014/01/31 18:54:43
https://code.google.com/p/chromium/codesearch#chro
| |
| 154 } | |
| 155 | |
| 156 void PepperMediaStreamAudioTrackHost::OnClose() { | |
| 157 if (connected_) { | |
| 158 MediaStreamAudioSink::RemoveFromAudioTrack(&audio_sink_, track_); | |
| 159 connected_ = false; | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 bool PepperMediaStreamAudioTrackHost::OnNewFramePreEnqueued(int32_t index) { | |
| 164 audio_sink_.EnqueueFrame(index); | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 168 void PepperMediaStreamAudioTrackHost::DidConnectPendingHostToResource() { | |
| 169 if (!connected_) { | |
| 170 MediaStreamAudioSink::AddToAudioTrack(&audio_sink_, track_); | |
| 171 connected_ = true; | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 } // namespace content | |
| OLD | NEW |