Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: ppapi/examples/media_stream_audio/media_stream_audio.cc

Issue 140783004: [PPAPI] Pepper MediaStream API audio track implementation and example. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix review issues Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ppapi/cpp/audio_frame.cc ('k') | ppapi/examples/media_stream_audio/media_stream_audio.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 const uint32_t kColorRed = 0xFFFF0000;
36 const uint32_t kColorGreen = 0xFF00FF00;
37 const uint32_t kColorGrey1 = 0xFF202020;
38 const uint32_t kColorGrey2 = 0xFF404040;
39 const 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 first_frame_(true),
47 sample_count_(0),
48 channel_count_(0),
49 timer_interval_(0),
50 pending_paint_(false),
51 waiting_for_flush_completion_(false) {
52 }
53
54 virtual ~MediaStreamAudioInstance() {
55 }
56
57 virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip) {
58 if (position.size() == size_)
59 return;
60
61 size_ = position.size();
62 device_context_ = pp::Graphics2D(this, size_, false);
63 if (!BindGraphics(device_context_))
64 return;
65
66 Paint();
67 }
68
69 virtual void HandleMessage(const pp::Var& var_message) {
70 if (!var_message.is_dictionary())
71 return;
72 pp::VarDictionary var_dictionary_message(var_message);
73 pp::Var var_track = var_dictionary_message.Get("track");
74 if (!var_track.is_resource())
75 return;
76
77 pp::Resource resource_track = pp::VarResource_Dev(var_track).AsResource();
78 audio_track_ = pp::MediaStreamAudioTrack(resource_track);
79 audio_track_.GetFrame(callback_factory_.NewCallbackWithOutput(
80 &MediaStreamAudioInstance::OnGetFrame));
81 }
82
83 private:
84 void ScheduleNextTimer() {
85 PP_DCHECK(timer_interval_ > 0);
86 pp::Module::Get()->core()->CallOnMainThread(
87 timer_interval_,
88 callback_factory_.NewCallback(&MediaStreamAudioInstance::OnTimer),
89 0);
90 }
91
92 void OnTimer(int32_t) {
93 ScheduleNextTimer();
94 Paint();
95 }
96
97 void DidFlush(int32_t result) {
98 waiting_for_flush_completion_ = false;
99 if (pending_paint_)
100 Paint();
101 }
102
103 void Paint() {
104 if (waiting_for_flush_completion_) {
105 pending_paint_ = true;
106 return;
107 }
108
109 pending_paint_ = false;
110
111 if (size_.IsEmpty())
112 return; // Nothing to do.
113
114 pp::ImageData image = PaintImage(size_);
115 if (!image.is_null()) {
116 device_context_.ReplaceContents(&image);
117 waiting_for_flush_completion_ = true;
118 device_context_.Flush(
119 callback_factory_.NewCallback(&MediaStreamAudioInstance::DidFlush));
120 }
121 }
122
123 pp::ImageData PaintImage(const pp::Size& size) {
124 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL, size, false);
125 if (image.is_null())
126 return image;
127
128 // Clear to dark grey.
129 for (int y = 0; y < size.height(); y++) {
130 for (int x = 0; x < size.width(); x++)
131 *image.GetAddr32(pp::Point(x, y)) = kColorGrey1;
132 }
133
134 int mid_height = size.height() / 2;
135 int max_amplitude = size.height() * 4 / 10;
136
137 // Draw some lines.
138 for (int x = 0; x < size.width(); x++) {
139 *image.GetAddr32(pp::Point(x, mid_height)) = kColorGrey3;
140 *image.GetAddr32(pp::Point(x, mid_height + max_amplitude)) = kColorGrey2;
141 *image.GetAddr32(pp::Point(x, mid_height - max_amplitude)) = kColorGrey2;
142 }
143
144
145 // Draw our samples.
146 for (int x = 0, i = 0;
147 x < std::min(size.width(), static_cast<int>(sample_count_));
148 x++, i += channel_count_) {
149 for (uint32_t ch = 0; ch < std::min(channel_count_, 2U); ++ch) {
150 int y = samples_[i + ch] * max_amplitude /
151 (std::numeric_limits<int16_t>::max() + 1) + mid_height;
152 *image.GetAddr32(pp::Point(x, y)) = (ch == 0 ? kColorRed : kColorGreen);
153 }
154 }
155
156 return image;
157 }
158
159 // Callback that is invoked when new frames are received.
160 void OnGetFrame(int32_t result, pp::AudioFrame frame) {
161 if (result != PP_OK)
162 return;
163
164 PP_DCHECK(frame.GetSampleSize() == PP_AUDIOFRAME_SAMPLESIZE_16_BITS);
165 const char* data = static_cast<const char*>(frame.GetDataBuffer());
166 uint32_t channels = frame.GetNumberOfChannels();
167 uint32_t samples = frame.GetNumberOfSamples() / channels;
168
169 if (channel_count_ != channels || sample_count_ != samples) {
170 channel_count_ = channels;
171 sample_count_ = samples;
172
173 samples_.resize(sample_count_ * channel_count_);
174 timer_interval_ = (sample_count_ * 1000) / frame.GetSampleRate() + 5;
175 // Start the timer for the first frame.
176 if (first_frame_) {
177 first_frame_ = false;
178 ScheduleNextTimer();
179 }
180 }
181
182 memcpy(samples_.data(), data,
183 sample_count_ * channel_count_ * sizeof(int16_t));
184
185 audio_track_.RecycleFrame(frame);
186 audio_track_.GetFrame(callback_factory_.NewCallbackWithOutput(
187 &MediaStreamAudioInstance::OnGetFrame));
188
189 }
190
191 pp::MediaStreamAudioTrack audio_track_;
192 pp::CompletionCallbackFactory<MediaStreamAudioInstance> callback_factory_;
193
194 bool first_frame_;
195 uint32_t sample_count_;
196 uint32_t channel_count_;
197 std::vector<int16_t> samples_;
198
199 int32_t timer_interval_;
200
201 // Painting stuff.
202 pp::Size size_;
203 pp::Graphics2D device_context_;
204 bool pending_paint_;
205 bool waiting_for_flush_completion_;
206 };
207
208 class MediaStreamAudioModule : public pp::Module {
209 public:
210 virtual pp::Instance* CreateInstance(PP_Instance instance) {
211 return new MediaStreamAudioInstance(instance);
212 }
213 };
214
215 } // namespace
216
217 namespace pp {
218
219 // Factory function for your specialization of the Module object.
220 Module* CreateModule() {
221 return new MediaStreamAudioModule();
222 }
223
224 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/cpp/audio_frame.cc ('k') | ppapi/examples/media_stream_audio/media_stream_audio.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698