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 // Max audio buffer duration in milliseconds. |
| 21 const uint32_t kMaxDuration = 10; |
| 22 |
| 23 // TODO(penghuang): make this configurable. |
| 24 const int32_t kNumberOfFrames = 4; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 namespace content { |
| 29 |
| 30 PepperMediaStreamAudioTrackHost::AudioSink::AudioSink( |
| 31 PepperMediaStreamAudioTrackHost* host) |
| 32 : host_(host), |
| 33 frame_data_size_(0), |
| 34 main_message_loop_proxy_(base::MessageLoopProxy::current()) { |
| 35 } |
| 36 |
| 37 PepperMediaStreamAudioTrackHost::AudioSink::~AudioSink() { |
| 38 DCHECK_EQ(main_message_loop_proxy_, base::MessageLoopProxy::current()); |
| 39 } |
| 40 |
| 41 void PepperMediaStreamAudioTrackHost::AudioSink::EnqueueFrame(int32_t index) { |
| 42 DCHECK_EQ(main_message_loop_proxy_, base::MessageLoopProxy::current()); |
| 43 DCHECK_GE(index, 0); |
| 44 DCHECK_LT(index, host_->frame_buffer()->number_of_frames()); |
| 45 base::AutoLock lock(lock_); |
| 46 frames_.push_back(index); |
| 47 } |
| 48 |
| 49 void PepperMediaStreamAudioTrackHost::AudioSink::InitFramesOnMainThread( |
| 50 int32_t number_of_frames, int32_t frame_size) { |
| 51 DCHECK_EQ(main_message_loop_proxy_, base::MessageLoopProxy::current()); |
| 52 bool result = host_->InitFrames(number_of_frames, frame_size); |
| 53 DCHECK(result); |
| 54 DCHECK_GE(host_->frame_buffer()->frame_size(), frame_size); |
| 55 base::AutoLock lock(lock_); |
| 56 for (int32_t i = 0; i < number_of_frames; ++i) { |
| 57 int32_t index = host_->frame_buffer()->DequeueFrame(); |
| 58 DCHECK_GE(index, 0); |
| 59 frames_.push_back(index); |
| 60 } |
| 61 } |
| 62 |
| 63 void |
| 64 PepperMediaStreamAudioTrackHost::AudioSink::SendEnqueueFrameMessageOnMainThread( |
| 65 int32_t index) { |
| 66 DCHECK_EQ(main_message_loop_proxy_, base::MessageLoopProxy::current()); |
| 67 host_->SendEnqueueFrameMessageToPlugin(index); |
| 68 } |
| 69 |
| 70 void PepperMediaStreamAudioTrackHost::AudioSink::OnData(const int16* audio_data, |
| 71 int sample_rate, |
| 72 int number_of_channels, |
| 73 int number_of_frames) { |
| 74 DCHECK(audio_thread_checker_.CalledOnValidThread()); |
| 75 DCHECK(audio_data); |
| 76 DCHECK_EQ(sample_rate, audio_params_.sample_rate()); |
| 77 DCHECK_EQ(number_of_channels, audio_params_.channels()); |
| 78 DCHECK_EQ(number_of_frames, audio_params_.frames_per_buffer()); |
| 79 int32_t index = -1; |
| 80 { |
| 81 base::AutoLock lock(lock_); |
| 82 if (!frames_.empty()) { |
| 83 index = frames_.front(); |
| 84 frames_.pop_front(); |
| 85 } |
| 86 } |
| 87 |
| 88 if (index != -1) { |
| 89 // TODO(penghuang): support re-sampling, etc. |
| 90 ppapi::MediaStreamFrame::Audio* frame = |
| 91 &(host_->frame_buffer()->GetFramePointer(index)->audio); |
| 92 frame->header.size = host_->frame_buffer()->frame_size(); |
| 93 frame->header.type = ppapi::MediaStreamFrame::TYPE_AUDIO; |
| 94 frame->timestamp = timestamp_.InMillisecondsF(); |
| 95 frame->sample_rate = static_cast<PP_AudioFrame_SampleRate>(sample_rate); |
| 96 frame->number_of_channels = number_of_channels; |
| 97 frame->number_of_samples = number_of_channels * number_of_frames; |
| 98 frame->data_size = frame_data_size_; |
| 99 memcpy(frame->data, audio_data, frame_data_size_); |
| 100 |
| 101 main_message_loop_proxy_->PostTask( |
| 102 FROM_HERE, |
| 103 base::Bind(&AudioSink::SendEnqueueFrameMessageOnMainThread, |
| 104 AsWeakPtr(), index)); |
| 105 } |
| 106 timestamp_ += frame_duration_; |
| 107 } |
| 108 |
| 109 void PepperMediaStreamAudioTrackHost::AudioSink::OnSetFormat( |
| 110 const AudioParameters& params) { |
| 111 DCHECK(params.IsValid()); |
| 112 DCHECK_LE(params.GetBufferDuration().InMilliseconds(), kMaxDuration); |
| 113 DCHECK_EQ(params.bits_per_sample(), 16); |
| 114 DCHECK((params.sample_rate() == AudioParameters::kTelephoneSampleRate) || |
| 115 (params.sample_rate() == AudioParameters::kAudioCDSampleRate)); |
| 116 |
| 117 COMPILE_ASSERT(AudioParameters::kTelephoneSampleRate == 8000, |
| 118 audio_sample_rate_does_not_match); |
| 119 COMPILE_ASSERT(AudioParameters::kAudioCDSampleRate == 44100, |
| 120 audio_sample_rate_does_not_match); |
| 121 audio_params_ = params; |
| 122 |
| 123 // TODO(penghuang): support setting format more than once. |
| 124 frame_duration_ = params.GetBufferDuration(); |
| 125 frame_data_size_ = params.GetBytesPerBuffer(); |
| 126 |
| 127 if (original_audio_params_.IsValid()) { |
| 128 DCHECK_EQ(params.sample_rate(), original_audio_params_.sample_rate()); |
| 129 DCHECK_EQ(params.bits_per_sample(), |
| 130 original_audio_params_.bits_per_sample()); |
| 131 DCHECK_EQ(params.channels(), original_audio_params_.channels()); |
| 132 } else { |
| 133 audio_thread_checker_.DetachFromThread(); |
| 134 original_audio_params_ = params; |
| 135 // The size is slightly bigger than necessary, because 8 extra bytes are |
| 136 // added into the struct. Also see |MediaStreamFrame|. |
| 137 size_t max_frame_size = |
| 138 params.sample_rate() * params.bits_per_sample() / 8 * |
| 139 params.channels() * kMaxDuration / 1000; |
| 140 size_t size = sizeof(ppapi::MediaStreamFrame::Audio) + max_frame_size; |
| 141 |
| 142 main_message_loop_proxy_->PostTask( |
| 143 FROM_HERE, |
| 144 base::Bind(&AudioSink::InitFramesOnMainThread, |
| 145 AsWeakPtr(), kNumberOfFrames, static_cast<int32_t>(size))); |
| 146 } |
| 147 } |
| 148 |
| 149 PepperMediaStreamAudioTrackHost::PepperMediaStreamAudioTrackHost( |
| 150 RendererPpapiHost* host, |
| 151 PP_Instance instance, |
| 152 PP_Resource resource, |
| 153 const blink::WebMediaStreamTrack& track) |
| 154 : PepperMediaStreamTrackHostBase(host, instance, resource), |
| 155 track_(track), |
| 156 connected_(false), |
| 157 audio_sink_(this) { |
| 158 DCHECK(!track_.isNull()); |
| 159 } |
| 160 |
| 161 PepperMediaStreamAudioTrackHost::~PepperMediaStreamAudioTrackHost() { |
| 162 OnClose(); |
| 163 } |
| 164 |
| 165 void PepperMediaStreamAudioTrackHost::OnClose() { |
| 166 if (connected_) { |
| 167 MediaStreamAudioSink::RemoveFromAudioTrack(&audio_sink_, track_); |
| 168 connected_ = false; |
| 169 } |
| 170 } |
| 171 |
| 172 void PepperMediaStreamAudioTrackHost::OnNewFrameEnqueued() { |
| 173 int32_t index = frame_buffer()->DequeueFrame(); |
| 174 DCHECK_GE(index, 0); |
| 175 audio_sink_.EnqueueFrame(index); |
| 176 } |
| 177 |
| 178 void PepperMediaStreamAudioTrackHost::DidConnectPendingHostToResource() { |
| 179 if (!connected_) { |
| 180 MediaStreamAudioSink::AddToAudioTrack(&audio_sink_, track_); |
| 181 connected_ = true; |
| 182 } |
| 183 } |
| 184 |
| 185 } // namespace content |
OLD | NEW |