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