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

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 // TODO(penghuang): make this 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 main_message_loop_proxy_(base::MessageLoopProxy::current()) {
32 }
33
34 PepperMediaStreamAudioTrackHost::AudioSink::~AudioSink() {
35 DCHECK_EQ(main_message_loop_proxy_, base::MessageLoopProxy::current());
36 }
37
38 void PepperMediaStreamAudioTrackHost::AudioSink::EnqueueFrame(int32_t index) {
no longer working on chromium 2014/02/03 09:50:44 thread check for this method as well?
Peng 2014/02/03 14:38:54 Done.
39 DCHECK_GE(index, 0);
40 DCHECK_LT(index, host_->frame_buffer()->number_of_frames());
41 base::AutoLock lock(lock_);
42 frames_.push_back(index);
43 }
44
45 void PepperMediaStreamAudioTrackHost::AudioSink::InitFramesOnMainThread(
46 int32_t number_of_frames, int32_t frame_size) {
47 DCHECK_EQ(main_message_loop_proxy_, base::MessageLoopProxy::current());
48 bool result = host_->InitFrames(number_of_frames, frame_size);
49 DCHECK(result);
50 base::AutoLock lock(lock_);
51 for (int32_t i = 0; i < number_of_frames; ++i) {
52 int32_t index = host_->frame_buffer()->DequeueFrame();
53 DCHECK_GE(index, 0);
54 frames_.push_back(index);
55 }
56 }
57
58 void PepperMediaStreamAudioTrackHost::AudioSink::OnData(const int16* audio_data,
59 int sample_rate,
60 int number_of_channels,
61 int number_of_frames) {
62 DCHECK(capture_thread_checker_.CalledOnValidThread());
63 DCHECK(audio_data);
64 DCHECK_EQ(sample_rate, audio_params_.sample_rate());
65 DCHECK_EQ(number_of_channels, audio_params_.channels());
66 DCHECK_EQ(number_of_frames, audio_params_.frames_per_buffer());
67 int32_t index = -1;
68 {
69 base::AutoLock lock(lock_);
70 if (!frames_.empty()) {
71 index = frames_.front();
72 frames_.pop_front();
73 }
74 }
75
76 if (index != -1) {
77 // TODO(penghuang): support re-sampling, etc.
78 ppapi::MediaStreamFrame::Audio* frame =
79 &(host_->frame_buffer()->GetFramePointer(index)->audio);
80 frame->header.size = host_->frame_buffer()->frame_size();
81 frame->header.type = ppapi::MediaStreamFrame::TYPE_AUDIO;
82 frame->timestamp = timestamp_.InMillisecondsF();
83 frame->sample_rate = static_cast<PP_AudioFrame_SampleRate>(sample_rate);
84 frame->number_of_channels = number_of_channels;
85 frame->number_of_samples = number_of_channels * number_of_frames;
86 frame->data_size = frame_data_size_;
87 memcpy(frame->data, audio_data, frame_data_size_);
88
89 // This function is called from the audio thread, but
90 // |SendEnqueueFrameMessageToPlugin()| doesn't use any sync IPC,
91 // so it is safe to call it from the audio thread directly.
92 host_->SendEnqueueFrameMessageToPlugin(index);
93 }
94 timestamp_ += frame_duration_;
95 }
96
97 void PepperMediaStreamAudioTrackHost::AudioSink::OnSetFormat(
98 const AudioParameters& params) {
99 DCHECK(!audio_params_.IsValid());
100 DCHECK(params.IsValid());
101 DCHECK_EQ(params.bits_per_sample(), 16);
102 DCHECK((params.sample_rate() == AudioParameters::kTelephoneSampleRate) ||
103 (params.sample_rate() == AudioParameters::kAudioCDSampleRate));
104
105 COMPILE_ASSERT(AudioParameters::kTelephoneSampleRate == 8000,
106 audio_sample_rate_does_not_match);
107 COMPILE_ASSERT(AudioParameters::kAudioCDSampleRate == 44100,
108 audio_sample_rate_does_not_match);
109
110 capture_thread_checker_.DetachFromThread();
111
112 // TODO(penghuang): support setting format more than once.
113 audio_params_ = params;
114 frame_duration_ = audio_params_.GetBufferDuration();
115 frame_data_size_ = audio_params_.GetBytesPerBuffer();
116
117 // The size is slightly bigger than necessary, because 8 extra bytes are added
118 // into the struct. Also see |MediaStreamFrame|.
119 size_t size = sizeof(ppapi::MediaStreamFrame::Audio) + frame_data_size_;
120
121 // This function is called from the audio thread and |InitFrames()| uses some
122 // sync IPC. So we have to call |InitFrames()| from the main thread.
123 main_message_loop_proxy_->PostTask(
124 FROM_HERE,
125 base::Bind(&AudioSink::InitFramesOnMainThread,
126 AsWeakPtr(), kNumberOfFrames, static_cast<int32_t>(size)));
127 }
128
129 PepperMediaStreamAudioTrackHost::PepperMediaStreamAudioTrackHost(
130 RendererPpapiHost* host,
131 PP_Instance instance,
132 PP_Resource resource,
133 const blink::WebMediaStreamTrack& track)
134 : PepperMediaStreamTrackHostBase(host, instance, resource),
135 track_(track),
136 connected_(false),
137 audio_sink_(this) {
138 DCHECK(!track_.isNull());
139 }
140
141 PepperMediaStreamAudioTrackHost::~PepperMediaStreamAudioTrackHost() {
142 OnClose();
143 }
144
145 void PepperMediaStreamAudioTrackHost::OnClose() {
146 if (connected_) {
147 MediaStreamAudioSink::RemoveFromAudioTrack(&audio_sink_, track_);
148 connected_ = false;
149 }
150 }
151
152 void PepperMediaStreamAudioTrackHost::OnNewFrameEnqueued() {
153 int32_t index = frame_buffer()->DequeueFrame();
154 DCHECK_GE(index, 0);
155 audio_sink_.EnqueueFrame(index);
156 }
157
158 void PepperMediaStreamAudioTrackHost::DidConnectPendingHostToResource() {
159 if (!connected_) {
160 MediaStreamAudioSink::AddToAudioTrack(&audio_sink_, track_);
161 connected_ = true;
162 }
163 }
164
165 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698