OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stdlib.h> |
| 6 #include <string.h> |
| 7 |
| 8 #include <algorithm> |
| 9 #include <limits> |
| 10 #include <vector> |
| 11 |
| 12 #include "ppapi/cpp/audio_frame.h" |
| 13 #include "ppapi/cpp/dev/var_resource_dev.h" |
| 14 #include "ppapi/cpp/graphics_2d.h" |
| 15 #include "ppapi/cpp/image_data.h" |
| 16 #include "ppapi/cpp/instance.h" |
| 17 #include "ppapi/cpp/logging.h" |
| 18 #include "ppapi/cpp/media_stream_audio_track.h" |
| 19 #include "ppapi/cpp/module.h" |
| 20 #include "ppapi/cpp/rect.h" |
| 21 #include "ppapi/cpp/size.h" |
| 22 #include "ppapi/utility/completion_callback_factory.h" |
| 23 |
| 24 // When compiling natively on Windows, PostMessage can be #define-d to |
| 25 // something else. |
| 26 #ifdef PostMessage |
| 27 #undef PostMessage |
| 28 #endif |
| 29 |
| 30 // This example demonstrates receiving audio samples from an AndioMediaTrack |
| 31 // and visualizing them. |
| 32 |
| 33 namespace { |
| 34 |
| 35 uint32_t kColorRed = 0xFFFF0000; |
| 36 uint32_t kColorGreen = 0xFF00FF00; |
| 37 uint32_t kColorGrey1 = 0xFF202020; |
| 38 uint32_t kColorGrey2 = 0xFF404040; |
| 39 uint32_t kColorGrey3 = 0xFF606060; |
| 40 |
| 41 class MediaStreamAudioInstance : public pp::Instance { |
| 42 public: |
| 43 explicit MediaStreamAudioInstance(PP_Instance instance) |
| 44 : pp::Instance(instance), |
| 45 callback_factory_(this), |
| 46 sample_count_(0), |
| 47 channel_count_(0), |
| 48 timer_interval_(0), |
| 49 pending_paint_(false), |
| 50 waiting_for_flush_completion_(false) { |
| 51 } |
| 52 |
| 53 virtual ~MediaStreamAudioInstance() { |
| 54 } |
| 55 |
| 56 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { |
| 57 if (position.size() == size_) |
| 58 return; |
| 59 |
| 60 size_ = position.size(); |
| 61 device_context_ = pp::Graphics2D(this, size_, false); |
| 62 if (!BindGraphics(device_context_)) |
| 63 return; |
| 64 |
| 65 Paint(); |
| 66 } |
| 67 |
| 68 virtual void HandleMessage(const pp::Var& var_message) { |
| 69 if (!var_message.is_dictionary()) |
| 70 return; |
| 71 pp::VarDictionary var_dictionary_message(var_message); |
| 72 pp::Var var_track = var_dictionary_message.Get("track"); |
| 73 if (!var_track.is_resource()) |
| 74 return; |
| 75 |
| 76 pp::Resource resource_track = pp::VarResource_Dev(var_track).AsResource(); |
| 77 audio_track_ = pp::MediaStreamAudioTrack(resource_track); |
| 78 audio_track_.GetFrame(callback_factory_.NewCallbackWithOutput( |
| 79 &MediaStreamAudioInstance::OnGetFrame)); |
| 80 } |
| 81 |
| 82 private: |
| 83 void ScheduleNextTimer() { |
| 84 PP_DCHECK(timer_interval_ > 0); |
| 85 pp::Module::Get()->core()->CallOnMainThread( |
| 86 timer_interval_, |
| 87 callback_factory_.NewCallback(&MediaStreamAudioInstance::OnTimer), |
| 88 0); |
| 89 } |
| 90 |
| 91 void OnTimer(int32_t) { |
| 92 ScheduleNextTimer(); |
| 93 Paint(); |
| 94 } |
| 95 |
| 96 void DidFlush(int32_t result) { |
| 97 waiting_for_flush_completion_ = false; |
| 98 if (pending_paint_) |
| 99 Paint(); |
| 100 } |
| 101 |
| 102 void Paint() { |
| 103 if (waiting_for_flush_completion_) { |
| 104 pending_paint_ = true; |
| 105 return; |
| 106 } |
| 107 |
| 108 pending_paint_ = false; |
| 109 |
| 110 if (size_.IsEmpty()) |
| 111 return; // Nothing to do. |
| 112 |
| 113 pp::ImageData image = PaintImage(size_); |
| 114 if (!image.is_null()) { |
| 115 device_context_.ReplaceContents(&image); |
| 116 waiting_for_flush_completion_ = true; |
| 117 device_context_.Flush( |
| 118 callback_factory_.NewCallback(&MediaStreamAudioInstance::DidFlush)); |
| 119 } |
| 120 } |
| 121 |
| 122 pp::ImageData PaintImage(const pp::Size& size) { |
| 123 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false); |
| 124 if (image.is_null()) |
| 125 return image; |
| 126 |
| 127 // Clear to dark grey. |
| 128 for (int y = 0; y < size.height(); y++) { |
| 129 for (int x = 0; x < size.width(); x++) |
| 130 *image.GetAddr32(pp::Point(x, y)) = kColorGrey1; |
| 131 } |
| 132 |
| 133 int mid_height = size.height() / 2; |
| 134 int max_amplitude = size.height() * 4 / 10; |
| 135 |
| 136 // Draw some lines. |
| 137 for (int x = 0; x < size.width(); x++) { |
| 138 *image.GetAddr32(pp::Point(x, mid_height)) = kColorGrey3; |
| 139 *image.GetAddr32(pp::Point(x, mid_height + max_amplitude)) = kColorGrey2; |
| 140 *image.GetAddr32(pp::Point(x, mid_height - max_amplitude)) = kColorGrey2; |
| 141 } |
| 142 |
| 143 |
| 144 // Draw our samples. |
| 145 for (int x = 0, i = 0; |
| 146 x < std::min(size.width(), static_cast<int>(sample_count_)); |
| 147 x++, i += channel_count_) { |
| 148 for (uint32_t ch = 0; ch < std::min(channel_count_, 2U); ++ch) { |
| 149 int y = samples_[i + ch] * max_amplitude / |
| 150 (std::numeric_limits<int16_t>::max() + 1) + mid_height; |
| 151 *image.GetAddr32(pp::Point(x, y)) = (ch == 0 ? kColorRed : kColorGreen); |
| 152 } |
| 153 } |
| 154 |
| 155 return image; |
| 156 } |
| 157 |
| 158 // Callback that is invoked when new frames are received. |
| 159 void OnGetFrame(int32_t result, pp::AudioFrame frame) { |
| 160 if (result != PP_OK) |
| 161 return; |
| 162 const char* data = static_cast<const char*>(frame.GetDataBuffer()); |
| 163 |
| 164 if (samples_.empty()) { |
| 165 channel_count_ = frame.GetNumberOfChannels(); |
| 166 sample_count_ = frame.GetNumberOfSamples() / channel_count_; |
| 167 |
| 168 samples_.resize(sample_count_ * channel_count_); |
| 169 timer_interval_ = (sample_count_ * 1000) / frame.GetSampleRate() + 5; |
| 170 // Start the timer |
| 171 ScheduleNextTimer(); |
| 172 } |
| 173 |
| 174 memcpy(samples_.data(), data, |
| 175 sample_count_ * channel_count_ * sizeof(int16_t)); |
| 176 |
| 177 audio_track_.RecycleFrame(frame); |
| 178 audio_track_.GetFrame(callback_factory_.NewCallbackWithOutput( |
| 179 &MediaStreamAudioInstance::OnGetFrame)); |
| 180 |
| 181 } |
| 182 |
| 183 pp::MediaStreamAudioTrack audio_track_; |
| 184 pp::CompletionCallbackFactory<MediaStreamAudioInstance> callback_factory_; |
| 185 |
| 186 uint32_t sample_count_; |
| 187 uint32_t channel_count_; |
| 188 std::vector<int16_t> samples_; |
| 189 |
| 190 int32_t timer_interval_; |
| 191 |
| 192 // Painting stuff. |
| 193 pp::Size size_; |
| 194 pp::Graphics2D device_context_; |
| 195 bool pending_paint_; |
| 196 bool waiting_for_flush_completion_; |
| 197 }; |
| 198 |
| 199 class MediaStreamAudioModule : public pp::Module { |
| 200 public: |
| 201 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 202 return new MediaStreamAudioInstance(instance); |
| 203 } |
| 204 }; |
| 205 |
| 206 } // namespace |
| 207 |
| 208 namespace pp { |
| 209 |
| 210 // Factory function for your specialization of the Module object. |
| 211 Module* CreateModule() { |
| 212 return new MediaStreamAudioModule(); |
| 213 } |
| 214 |
| 215 } // namespace pp |
OLD | NEW |