Chromium Code Reviews| Index: ppapi/examples/media_stream_audio/media_stream_audio.cc |
| diff --git a/ppapi/examples/media_stream_audio/media_stream_audio.cc b/ppapi/examples/media_stream_audio/media_stream_audio.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9227979cfc93bc5a76d6101a2e40f70752b4b472 |
| --- /dev/null |
| +++ b/ppapi/examples/media_stream_audio/media_stream_audio.cc |
| @@ -0,0 +1,197 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <stdlib.h> |
| +#include <string.h> |
| + |
| +#include <algorithm> |
| +#include <limits> |
| + |
| +#include "ppapi/cpp/audio_frame.h" |
| +#include "ppapi/cpp/dev/var_resource_dev.h" |
| +#include "ppapi/cpp/graphics_2d.h" |
| +#include "ppapi/cpp/image_data.h" |
| +#include "ppapi/cpp/instance.h" |
| +#include "ppapi/cpp/logging.h" |
| +#include "ppapi/cpp/media_stream_audio_track.h" |
| +#include "ppapi/cpp/module.h" |
| +#include "ppapi/cpp/rect.h" |
| +#include "ppapi/cpp/size.h" |
| +#include "ppapi/utility/completion_callback_factory.h" |
| + |
| +// When compiling natively on Windows, PostMessage can be #define-d to |
| +// something else. |
| +#ifdef PostMessage |
| +#undef PostMessage |
| +#endif |
| + |
| +class MediaStreamAudioInstance : public pp::Instance { |
| + public: |
| + explicit MediaStreamAudioInstance(PP_Instance instance) |
| + : pp::Instance(instance), |
| + callback_factory_(this), |
| + sample_count_(0), |
| + channel_count_(0), |
| + samples_(NULL), |
| + timer_interval_(0), |
| + pending_paint_(false), |
| + waiting_for_flush_completion_(false) { |
| + } |
| + virtual ~MediaStreamAudioInstance() { |
| + delete[] samples_; |
| + } |
| + |
| + virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) { |
| + if (position.size() == size_) |
| + return; |
| + |
| + size_ = position.size(); |
| + device_context_ = pp::Graphics2D(this, size_, false); |
| + if (!BindGraphics(device_context_)) |
| + return; |
| + |
| + Paint(); |
| + } |
| + |
| + virtual void HandleMessage(const pp::Var& var_message) { |
| + if (!var_message.is_dictionary()) |
| + return; |
| + pp::VarDictionary var_dictionary_message(var_message); |
| + pp::Var var_track = var_dictionary_message.Get("track"); |
| + if (!var_track.is_resource()) |
| + return; |
| + |
| + pp::Resource resource_track = pp::VarResource_Dev(var_track).AsResource(); |
| + audio_track_ = pp::MediaStreamAudioTrack(resource_track); |
| + audio_track_.GetFrame(callback_factory_.NewCallbackWithOutput( |
| + &MediaStreamAudioInstance::OnGetFrame)); |
| + } |
| + |
| + private: |
| + void ScheduleNextTimer() { |
| + PP_DCHECK(timer_interval_ > 0); |
| + pp::Module::Get()->core()->CallOnMainThread( |
| + timer_interval_, |
| + callback_factory_.NewCallback(&MediaStreamAudioInstance::OnTimer), |
| + 0); |
| + } |
| + |
| + void OnTimer(int32_t) { |
| + ScheduleNextTimer(); |
| + Paint(); |
| + } |
| + |
| + void DidFlush(int32_t result) { |
| + waiting_for_flush_completion_ = false; |
| + if (pending_paint_) |
| + Paint(); |
| + } |
| + |
| + void Paint() { |
| + if (waiting_for_flush_completion_) { |
| + pending_paint_ = true; |
| + return; |
| + } |
| + |
| + pending_paint_ = false; |
| + |
| + if (size_.IsEmpty()) |
| + return; // Nothing to do. |
| + |
| + pp::ImageData image = PaintImage(size_); |
| + if (!image.is_null()) { |
| + device_context_.ReplaceContents(&image); |
| + waiting_for_flush_completion_ = true; |
| + device_context_.Flush( |
| + callback_factory_.NewCallback(&MediaStreamAudioInstance::DidFlush)); |
| + } |
| + } |
| + |
| + pp::ImageData PaintImage(const pp::Size& size) { |
| + pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false); |
| + if (image.is_null()) |
| + return image; |
| + |
| + // Clear to dark grey. |
| + for (int y = 0; y < size.height(); y++) { |
| + for (int x = 0; x < size.width(); x++) |
| + *image.GetAddr32(pp::Point(x, y)) = 0xff202020; |
|
bbudge
2014/01/31 22:13:01
It might be nice to define constants to indicate w
Peng
2014/02/02 02:11:26
Done.
|
| + } |
| + |
| + int mid_height = size.height() / 2; |
| + int max_amplitude = size.height() * 4 / 10; |
| + |
| + // Draw some lines. |
| + for (int x = 0; x < size.width(); x++) { |
| + *image.GetAddr32(pp::Point(x, mid_height)) = 0xff606060; |
| + *image.GetAddr32(pp::Point(x, mid_height + max_amplitude)) = 0xff404040; |
| + *image.GetAddr32(pp::Point(x, mid_height - max_amplitude)) = 0xff404040; |
| + } |
| + |
| + // Draw our samples. |
| + for (int x = 0, i = 0; |
| + x < std::min(size.width(), static_cast<int>(sample_count_)); |
| + x++, i += channel_count_) { |
|
bbudge
2014/01/31 22:13:01
Could we draw all the channels?
Peng
2014/02/02 02:11:26
Done.
|
| + int y = samples_[i] * max_amplitude / |
| + (std::numeric_limits<int16_t>::max() + 1) + mid_height; |
| + *image.GetAddr32(pp::Point(x, y)) = 0xffffffff; |
| + } |
| + |
| + return image; |
| + } |
| + |
| + // Callback that is invoked when new frames are recevied. |
|
bbudge
2014/01/31 22:13:01
s/recevied/received
Peng
2014/02/02 02:11:26
Done.
|
| + void OnGetFrame(int32_t result, pp::AudioFrame frame) { |
| + if (result != PP_OK) |
| + return; |
| + const char* data = static_cast<const char*>(frame.GetDataBuffer()); |
| + |
| + if (!samples_) { |
| + channel_count_ = frame.GetNumberOfChannels(); |
| + sample_count_ = frame.GetNumberOfSamples() / channel_count_; |
| + samples_ = new int16_t[sample_count_ * channel_count_]; |
| + timer_interval_ = (sample_count_ * 1000) / frame.GetSampleRate() + 5; |
| + // Start the timer |
| + ScheduleNextTimer(); |
| + } |
| + |
| + memcpy(samples_, data, sample_count_ * channel_count_ * sizeof(int16_t)); |
| + |
| + audio_track_.RecycleFrame(frame); |
| + audio_track_.GetFrame(callback_factory_.NewCallbackWithOutput( |
| + &MediaStreamAudioInstance::OnGetFrame)); |
| + |
| + } |
| + |
| + pp::MediaStreamAudioTrack audio_track_; |
| + pp::CompletionCallbackFactory<MediaStreamAudioInstance> callback_factory_; |
| + |
| + uint32_t sample_count_; |
| + uint32_t channel_count_; |
| + int16_t* samples_; |
| + |
| + int32_t timer_interval_; |
| + |
| + // Painting stuff. |
| + pp::Size size_; |
| + pp::Graphics2D device_context_; |
| + bool pending_paint_; |
| + bool waiting_for_flush_completion_; |
| +}; |
| + |
| +class MediaStreamAudioModule : public pp::Module { |
| + public: |
| + virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| + return new MediaStreamAudioInstance(instance); |
| + } |
| +}; |
| + |
| +namespace pp { |
| + |
| +// Factory function for your specialization of the Module object. |
| +Module* CreateModule() { |
| + return new MediaStreamAudioModule(); |
| +} |
| + |
| +} // namespace pp |