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

Unified Diff: content/renderer/pepper/pepper_media_stream_audio_track_host.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: Update Created 6 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: content/renderer/pepper/pepper_media_stream_audio_track_host.cc
diff --git a/content/renderer/pepper/pepper_media_stream_audio_track_host.cc b/content/renderer/pepper/pepper_media_stream_audio_track_host.cc
new file mode 100644
index 0000000000000000000000000000000000000000..822ff862f4c2c3ca4b2a83a1c3f6a00e35c955c7
--- /dev/null
+++ b/content/renderer/pepper/pepper_media_stream_audio_track_host.cc
@@ -0,0 +1,109 @@
+// Copyright (c) 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 "content/renderer/pepper/pepper_media_stream_audio_track_host.h"
+
+#include "base/bind.h"
+#include "base/location.h"
+#include "base/logging.h"
+#include "base/message_loop/message_loop_proxy.h"
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/c/ppb_audio_frame.h"
+#include "ppapi/shared_impl/media_stream_frame.h"
+
+namespace {
+
+// TODO(penghuang): make it configurable.
+const int32_t kNumberOfFrames = 4;
+
+} // namespace
+
+namespace content {
+
+PepperMediaStreamAudioTrackHost::PepperMediaStreamAudioTrackHost(
+ RendererPpapiHost* host,
+ PP_Instance instance,
+ PP_Resource resource,
+ const blink::WebMediaStreamTrack& track)
+ : PepperMediaStreamTrackHostBase(host, instance, resource),
+ track_(track),
+ connected_(false),
+ frame_data_size_(0),
+ main_message_loop_proxy_(base::MessageLoopProxy::current()),
+ weak_factory_(this) {
+ DCHECK(!track_.isNull());
+}
+
+PepperMediaStreamAudioTrackHost::~PepperMediaStreamAudioTrackHost() {
+ OnClose();
+}
+
+void PepperMediaStreamAudioTrackHost::InitFramesOnMainThread(
+ uint32_t number_of_frames, uint32_t frame_size) {
+ bool result = InitFrames(number_of_frames, frame_size);
+ DCHECK(result);
+}
+
+void PepperMediaStreamAudioTrackHost::OnClose() {
+ if (connected_) {
+ MediaStreamAudioSink::RemoveFromAudioTrack(this, track_);
+ connected_ = false;
+ }
+}
+
+void PepperMediaStreamAudioTrackHost::OnData(const int16* audio_data,
+ int sample_rate,
+ int number_of_channels,
+ int number_of_frames) {
+ DCHECK(audio_data);
+ DCHECK_EQ(sample_rate, audio_params_.sample_rate());
+ DCHECK_EQ(number_of_channels, audio_params_.channels());
+ DCHECK_EQ(number_of_frames, audio_params_.frames_per_buffer());
+
+ int32_t index = frame_buffer()->DequeueFrame();
+ // Drop frames if the underlying buffer is full or not initialized.
+ if (index >= 0) {
+ // TODO(penghuang): support re-sampling, etc.
+ ppapi::MediaStreamFrame::Audio* frame =
+ &(frame_buffer()->GetFramePointer(index)->audio);
+ frame->header.size = frame_buffer()->frame_size();
+ frame->header.type = ppapi::MediaStreamFrame::TYPE_AUDIO;
+ frame->timestamp = timestamp_.InMillisecondsF();
+ frame->sample_rate = static_cast<PP_AudioSampleRate>(sample_rate);
+ frame->number_of_channels = number_of_channels;
+ frame->number_of_samples = number_of_channels * number_of_frames;
+ frame->data_size = frame_data_size_;
+
+ memcpy(frame->data, audio_data, frame_data_size_);
+ SendEnqueueFrameMessageToPlugin(index);
+ }
+ timestamp_ += frame_duration_;
+}
+
+void PepperMediaStreamAudioTrackHost::OnSetFormat(
+ const media::AudioParameters& params) {
+ DCHECK(!audio_params_.IsValid());
+ DCHECK(params.IsValid());
+ DCHECK_EQ(params.bits_per_sample(), 16);
+
+ // TODO(penghuang): support setting format more than once.
+ audio_params_ = params;
+ frame_duration_ = audio_params_.GetBufferDuration();
+ frame_data_size_ = audio_params_.GetBytesPerBuffer();
+ int32_t size = sizeof(ppapi::MediaStreamFrame::Audio) + frame_data_size_;
dmichael (off chromium) 2014/01/16 22:34:20 I don't know if we care, but this size calculation
Peng 2014/01/17 00:23:49 Done.
+
+ main_message_loop_proxy_->PostTask(
dmichael (off chromium) 2014/01/16 22:34:20 What thread is this coming in on? Do you know the
Peng 2014/01/17 00:23:49 OnData() & OnSetFormat() are called from an Audio
+ FROM_HERE,
+ base::Bind(&PepperMediaStreamAudioTrackHost::InitFramesOnMainThread,
+ weak_factory_.GetWeakPtr(), kNumberOfFrames, size));
+}
+
+void PepperMediaStreamAudioTrackHost::DidConnectPendingHostToResource() {
+ if (!connected_) {
+ MediaStreamAudioSink::AddToAudioTrack(this, track_);
+ connected_ = true;
+ }
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698