OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdlib.h> | 5 #include <stdlib.h> |
6 #include <string.h> | 6 #include <string.h> |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <limits> | 9 #include <limits> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "ppapi/cpp/audio_frame.h" | 12 #include "ppapi/cpp/audio_buffer.h" |
13 #include "ppapi/cpp/dev/var_resource_dev.h" | 13 #include "ppapi/cpp/dev/var_resource_dev.h" |
14 #include "ppapi/cpp/graphics_2d.h" | 14 #include "ppapi/cpp/graphics_2d.h" |
15 #include "ppapi/cpp/image_data.h" | 15 #include "ppapi/cpp/image_data.h" |
16 #include "ppapi/cpp/instance.h" | 16 #include "ppapi/cpp/instance.h" |
17 #include "ppapi/cpp/logging.h" | 17 #include "ppapi/cpp/logging.h" |
18 #include "ppapi/cpp/media_stream_audio_track.h" | 18 #include "ppapi/cpp/media_stream_audio_track.h" |
19 #include "ppapi/cpp/module.h" | 19 #include "ppapi/cpp/module.h" |
20 #include "ppapi/cpp/rect.h" | 20 #include "ppapi/cpp/rect.h" |
21 #include "ppapi/cpp/size.h" | 21 #include "ppapi/cpp/size.h" |
22 #include "ppapi/utility/completion_callback_factory.h" | 22 #include "ppapi/utility/completion_callback_factory.h" |
(...skipping 13 matching lines...) Expand all Loading... |
36 const uint32_t kColorGreen = 0xFF00FF00; | 36 const uint32_t kColorGreen = 0xFF00FF00; |
37 const uint32_t kColorGrey1 = 0xFF202020; | 37 const uint32_t kColorGrey1 = 0xFF202020; |
38 const uint32_t kColorGrey2 = 0xFF404040; | 38 const uint32_t kColorGrey2 = 0xFF404040; |
39 const uint32_t kColorGrey3 = 0xFF606060; | 39 const uint32_t kColorGrey3 = 0xFF606060; |
40 | 40 |
41 class MediaStreamAudioInstance : public pp::Instance { | 41 class MediaStreamAudioInstance : public pp::Instance { |
42 public: | 42 public: |
43 explicit MediaStreamAudioInstance(PP_Instance instance) | 43 explicit MediaStreamAudioInstance(PP_Instance instance) |
44 : pp::Instance(instance), | 44 : pp::Instance(instance), |
45 callback_factory_(this), | 45 callback_factory_(this), |
46 first_frame_(true), | 46 first_buffer_(true), |
47 sample_count_(0), | 47 sample_count_(0), |
48 channel_count_(0), | 48 channel_count_(0), |
49 timer_interval_(0), | 49 timer_interval_(0), |
50 pending_paint_(false), | 50 pending_paint_(false), |
51 waiting_for_flush_completion_(false) { | 51 waiting_for_flush_completion_(false) { |
52 } | 52 } |
53 | 53 |
54 virtual ~MediaStreamAudioInstance() { | 54 virtual ~MediaStreamAudioInstance() { |
55 } | 55 } |
56 | 56 |
(...skipping 12 matching lines...) Expand all Loading... |
69 virtual void HandleMessage(const pp::Var& var_message) { | 69 virtual void HandleMessage(const pp::Var& var_message) { |
70 if (!var_message.is_dictionary()) | 70 if (!var_message.is_dictionary()) |
71 return; | 71 return; |
72 pp::VarDictionary var_dictionary_message(var_message); | 72 pp::VarDictionary var_dictionary_message(var_message); |
73 pp::Var var_track = var_dictionary_message.Get("track"); | 73 pp::Var var_track = var_dictionary_message.Get("track"); |
74 if (!var_track.is_resource()) | 74 if (!var_track.is_resource()) |
75 return; | 75 return; |
76 | 76 |
77 pp::Resource resource_track = pp::VarResource_Dev(var_track).AsResource(); | 77 pp::Resource resource_track = pp::VarResource_Dev(var_track).AsResource(); |
78 audio_track_ = pp::MediaStreamAudioTrack(resource_track); | 78 audio_track_ = pp::MediaStreamAudioTrack(resource_track); |
79 audio_track_.GetFrame(callback_factory_.NewCallbackWithOutput( | 79 audio_track_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
80 &MediaStreamAudioInstance::OnGetFrame)); | 80 &MediaStreamAudioInstance::OnGetBuffer)); |
81 } | 81 } |
82 | 82 |
83 private: | 83 private: |
84 void ScheduleNextTimer() { | 84 void ScheduleNextTimer() { |
85 PP_DCHECK(timer_interval_ > 0); | 85 PP_DCHECK(timer_interval_ > 0); |
86 pp::Module::Get()->core()->CallOnMainThread( | 86 pp::Module::Get()->core()->CallOnMainThread( |
87 timer_interval_, | 87 timer_interval_, |
88 callback_factory_.NewCallback(&MediaStreamAudioInstance::OnTimer), | 88 callback_factory_.NewCallback(&MediaStreamAudioInstance::OnTimer), |
89 0); | 89 0); |
90 } | 90 } |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 for (uint32_t ch = 0; ch < std::min(channel_count_, 2U); ++ch) { | 149 for (uint32_t ch = 0; ch < std::min(channel_count_, 2U); ++ch) { |
150 int y = samples_[i + ch] * max_amplitude / | 150 int y = samples_[i + ch] * max_amplitude / |
151 (std::numeric_limits<int16_t>::max() + 1) + mid_height; | 151 (std::numeric_limits<int16_t>::max() + 1) + mid_height; |
152 *image.GetAddr32(pp::Point(x, y)) = (ch == 0 ? kColorRed : kColorGreen); | 152 *image.GetAddr32(pp::Point(x, y)) = (ch == 0 ? kColorRed : kColorGreen); |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
156 return image; | 156 return image; |
157 } | 157 } |
158 | 158 |
159 // Callback that is invoked when new frames are received. | 159 // Callback that is invoked when new buffers are received. |
160 void OnGetFrame(int32_t result, pp::AudioFrame frame) { | 160 void OnGetBuffer(int32_t result, pp::AudioBuffer buffer) { |
161 if (result != PP_OK) | 161 if (result != PP_OK) |
162 return; | 162 return; |
163 | 163 |
164 PP_DCHECK(frame.GetSampleSize() == PP_AUDIOFRAME_SAMPLESIZE_16_BITS); | 164 PP_DCHECK(buffer.GetSampleSize() == PP_AUDIOBUFFER_SAMPLESIZE_16_BITS); |
165 const char* data = static_cast<const char*>(frame.GetDataBuffer()); | 165 const char* data = static_cast<const char*>(buffer.GetDataBuffer()); |
166 uint32_t channels = frame.GetNumberOfChannels(); | 166 uint32_t channels = buffer.GetNumberOfChannels(); |
167 uint32_t samples = frame.GetNumberOfSamples() / channels; | 167 uint32_t samples = buffer.GetNumberOfSamples() / channels; |
168 | 168 |
169 if (channel_count_ != channels || sample_count_ != samples) { | 169 if (channel_count_ != channels || sample_count_ != samples) { |
170 channel_count_ = channels; | 170 channel_count_ = channels; |
171 sample_count_ = samples; | 171 sample_count_ = samples; |
172 | 172 |
173 samples_.resize(sample_count_ * channel_count_); | 173 samples_.resize(sample_count_ * channel_count_); |
174 timer_interval_ = (sample_count_ * 1000) / frame.GetSampleRate() + 5; | 174 timer_interval_ = (sample_count_ * 1000) / buffer.GetSampleRate() + 5; |
175 // Start the timer for the first frame. | 175 // Start the timer for the first buffer. |
176 if (first_frame_) { | 176 if (first_buffer_) { |
177 first_frame_ = false; | 177 first_buffer_ = false; |
178 ScheduleNextTimer(); | 178 ScheduleNextTimer(); |
179 } | 179 } |
180 } | 180 } |
181 | 181 |
182 memcpy(samples_.data(), data, | 182 memcpy(samples_.data(), data, |
183 sample_count_ * channel_count_ * sizeof(int16_t)); | 183 sample_count_ * channel_count_ * sizeof(int16_t)); |
184 | 184 |
185 audio_track_.RecycleFrame(frame); | 185 audio_track_.RecycleBuffer(buffer); |
186 audio_track_.GetFrame(callback_factory_.NewCallbackWithOutput( | 186 audio_track_.GetBuffer(callback_factory_.NewCallbackWithOutput( |
187 &MediaStreamAudioInstance::OnGetFrame)); | 187 &MediaStreamAudioInstance::OnGetBuffer)); |
188 | 188 |
189 } | 189 } |
190 | 190 |
191 pp::MediaStreamAudioTrack audio_track_; | 191 pp::MediaStreamAudioTrack audio_track_; |
192 pp::CompletionCallbackFactory<MediaStreamAudioInstance> callback_factory_; | 192 pp::CompletionCallbackFactory<MediaStreamAudioInstance> callback_factory_; |
193 | 193 |
194 bool first_frame_; | 194 bool first_buffer_; |
195 uint32_t sample_count_; | 195 uint32_t sample_count_; |
196 uint32_t channel_count_; | 196 uint32_t channel_count_; |
197 std::vector<int16_t> samples_; | 197 std::vector<int16_t> samples_; |
198 | 198 |
199 int32_t timer_interval_; | 199 int32_t timer_interval_; |
200 | 200 |
201 // Painting stuff. | 201 // Painting stuff. |
202 pp::Size size_; | 202 pp::Size size_; |
203 pp::Graphics2D device_context_; | 203 pp::Graphics2D device_context_; |
204 bool pending_paint_; | 204 bool pending_paint_; |
(...skipping 10 matching lines...) Expand all Loading... |
215 } // namespace | 215 } // namespace |
216 | 216 |
217 namespace pp { | 217 namespace pp { |
218 | 218 |
219 // Factory function for your specialization of the Module object. | 219 // Factory function for your specialization of the Module object. |
220 Module* CreateModule() { | 220 Module* CreateModule() { |
221 return new MediaStreamAudioModule(); | 221 return new MediaStreamAudioModule(); |
222 } | 222 } |
223 | 223 |
224 } // namespace pp | 224 } // namespace pp |
OLD | NEW |