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