Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(79)

Side by Side Diff: content/renderer/pepper/pepper_media_stream_audio_track_host.cc

Issue 140783004: [PPAPI] Pepper MediaStream API audio track implementation and example. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix review issues Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 const uint32_t kMaxNumbferOfFrames = 64u;
21 // TODO(penghuang): make it configurable.
bbudge 2014/01/31 20:29:35 s/it/this
Peng 2014/01/31 21:09:25 Done.
22 const int32_t kNumberOfFrames = 4;
23
24 } // namespace
25
26 namespace content {
27
28 PepperMediaStreamAudioTrackHost::AudioSink::AudioSink(
29 PepperMediaStreamAudioTrackHost* host)
30 : host_(host),
31 frame_data_size_(0),
32 frames_(0ll),
bbudge 2014/01/31 20:29:35 This is confusing at first. Can we use 0LL?
Peng 2014/01/31 21:09:25 Done.
33 main_message_loop_proxy_(base::MessageLoopProxy::current()) {
34 }
35
36 PepperMediaStreamAudioTrackHost::AudioSink::~AudioSink() {
37 DCHECK_EQ(main_message_loop_proxy_, base::MessageLoopProxy::current());
38 }
39
40 void PepperMediaStreamAudioTrackHost::AudioSink::EnqueueFrame(int32_t index) {
41 DCHECK_EQ(main_message_loop_proxy_, base::MessageLoopProxy::current());
42 DCHECK_GE(index, 0);
43 DCHECK_LT(index, host_->frame_buffer()->number_of_frames());
44 DCHECK(!(base::subtle::NoBarrier_Load(&frames_) & (1 << index)));
45
46 // Set the bit for the frame index. Automic does not support and/or
bbudge 2014/01/31 20:29:35 s/Automic/Atomic
Peng 2014/01/31 21:09:25 Done.
47 // operations, so we use add/sub instead.
48 base::subtle::NoBarrier_AtomicIncrement(&frames_, 1 << index);
bbudge 2014/01/31 20:29:35 Using AtomicIncrement here makes me nervous, since
Peng 2014/01/31 21:09:25 Fixed by using base::Lock
49 }
50
51 void PepperMediaStreamAudioTrackHost::AudioSink::InitFramesOnMainThread(
52 uint32_t number_of_frames, uint32_t frame_size) {
53 DCHECK_LE(number_of_frames, kMaxNumbferOfFrames);
54 bool result = host_->InitFrames(number_of_frames, frame_size);
55 DCHECK(result);
56 for (uint32_t i = 0; i < number_of_frames; ++i) {
57 int32_t index = host_->frame_buffer()->DequeueFrame();
58 DCHECK_GE(index, 0);
59 }
60
61 base::subtle::Atomic64 frames = base::subtle::NoBarrier_CompareAndSwap(
62 &frames_, 0ll, (1ll << number_of_frames) - 1ll);
63 DCHECK(!frames);
64 }
65
66 void PepperMediaStreamAudioTrackHost::AudioSink::OnData(const int16* audio_data,
67 int sample_rate,
68 int number_of_channels,
69 int number_of_frames) {
70 DCHECK(audio_data);
71 DCHECK_EQ(sample_rate, audio_params_.sample_rate());
72 DCHECK_EQ(number_of_channels, audio_params_.channels());
73 DCHECK_EQ(number_of_frames, audio_params_.frames_per_buffer());
74
75 base::subtle::Atomic64 frames = base::subtle::NoBarrier_Load(&frames_);
76 if (frames) {
77 // If frames isn't zero, the |frame_buffer()| should be initialized already,
78 // and |frame_buffer()|'s attributes (|number_of_frames()|, |frame_size()|,
79 // etc) will not be changed. So it is safe to read them in the audio thread.
80 int32_t index = 0;
81 int32_t n = host_->frame_buffer()->number_of_frames();
82
83 // Find a free frame.
84 while(index < n && !(frames & (1 << index)))
85 index++;
86
87 if (index < n) {
88 // TODO(penghuang): support re-sampling, etc.
89 ppapi::MediaStreamFrame::Audio* frame =
90 &(host_->frame_buffer()->GetFramePointer(index)->audio);
91 frame->header.size = host_->frame_buffer()->frame_size();
92 frame->header.type = ppapi::MediaStreamFrame::TYPE_AUDIO;
93 frame->timestamp = timestamp_.InMillisecondsF();
94 frame->sample_rate = static_cast<PP_AudioFrame_SampleRate>(sample_rate);
95 frame->number_of_channels = number_of_channels;
96 frame->number_of_samples = number_of_channels * number_of_frames;
97 frame->data_size = frame_data_size_;
98 memcpy(frame->data, audio_data, frame_data_size_);
99
100 // Clear the bit for the frame index. Automic does not support and/or
101 // operations, so we use add/sub instead.
102 base::subtle::NoBarrier_AtomicIncrement(&frames_, -(1ll << index));
103
104 // This function is called from the audio thread, but
105 // |SendEnqueueFrameMessageToPlugin()| doesn't use any sync IPC,
106 // so it is safe to call it from the audio thread directly.
107 host_->SendEnqueueFrameMessageToPlugin(index);
108 }
109 }
110 timestamp_ += frame_duration_;
111 }
112
113 void PepperMediaStreamAudioTrackHost::AudioSink::OnSetFormat(
114 const AudioParameters& params) {
115 DCHECK(!audio_params_.IsValid());
116 DCHECK(params.IsValid());
117 DCHECK_EQ(params.bits_per_sample(), 16);
118 DCHECK((params.sample_rate() == AudioParameters::kTelephoneSampleRate) ||
119 (params.sample_rate() == AudioParameters::kAudioCDSampleRate));
120
121 COMPILE_ASSERT(AudioParameters::kTelephoneSampleRate == 8000,
122 audio_sample_rate_does_not_match);
123 COMPILE_ASSERT(AudioParameters::kAudioCDSampleRate == 44100,
124 audio_sample_rate_does_not_match);
125
126 // TODO(penghuang): support setting format more than once.
127 audio_params_ = params;
128 frame_duration_ = audio_params_.GetBufferDuration();
129 frame_data_size_ = audio_params_.GetBytesPerBuffer();
130
131 // The size is slightly bigger than necessary, because 8 extra bytes are added
132 // into the struct. Also see |MediaStreamFrame|.
133 int32_t size = sizeof(ppapi::MediaStreamFrame::Audio) + frame_data_size_;
134
135 // This function is called from the audio thread and |InitFrames()| uses some
136 // sync IPC. So we have to call |InitFrames()| from the main thread.
137 main_message_loop_proxy_->PostTask(
138 FROM_HERE,
139 base::Bind(&AudioSink::InitFramesOnMainThread,
140 AsWeakPtr(), kNumberOfFrames, size));
141 }
142
143 PepperMediaStreamAudioTrackHost::PepperMediaStreamAudioTrackHost(
144 RendererPpapiHost* host,
145 PP_Instance instance,
146 PP_Resource resource,
147 const blink::WebMediaStreamTrack& track)
148 : PepperMediaStreamTrackHostBase(host, instance, resource),
149 track_(track),
150 connected_(false),
151 audio_sink_(this) {
152 DCHECK(!track_.isNull());
153 }
154
155 PepperMediaStreamAudioTrackHost::~PepperMediaStreamAudioTrackHost() {
156 OnClose();
157 }
158
159 void PepperMediaStreamAudioTrackHost::OnClose() {
160 if (connected_) {
161 MediaStreamAudioSink::RemoveFromAudioTrack(&audio_sink_, track_);
162 connected_ = false;
163 }
164 }
165
166 void PepperMediaStreamAudioTrackHost::OnNewFrameEnqueued() {
167 int32_t index = frame_buffer()->DequeueFrame();
168 DCHECK_GE(index, 0);
169 audio_sink_.EnqueueFrame(index);
170 }
171
172 void PepperMediaStreamAudioTrackHost::DidConnectPendingHostToResource() {
173 if (!connected_) {
174 MediaStreamAudioSink::AddToAudioTrack(&audio_sink_, track_);
175 connected_ = true;
176 }
177 }
178
179 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698