blob: ae5b37ea5d79551d193a08852eb09cc5d5ee5f97 [file] [log] [blame]
[email protected]1f8a0892011-11-18 00:14:241// Copyright (c) 2011 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
[email protected]9a578392011-12-07 18:59:275#include "ppapi/shared_impl/ppb_audio_input_shared.h"
[email protected]1f8a0892011-11-18 00:14:246
7#include "base/logging.h"
8
9namespace ppapi {
10
[email protected]9a578392011-12-07 18:59:2711PPB_AudioInput_Shared::PPB_AudioInput_Shared()
[email protected]1f8a0892011-11-18 00:14:2412 : capturing_(false),
13 shared_memory_size_(0),
14 callback_(NULL),
15 user_data_(NULL) {
16}
17
[email protected]9a578392011-12-07 18:59:2718PPB_AudioInput_Shared::~PPB_AudioInput_Shared() {
[email protected]1f8a0892011-11-18 00:14:2419 // Closing the socket causes the thread to exit - wait for it.
20 if (socket_.get())
21 socket_->Close();
22 if (audio_input_thread_.get()) {
23 audio_input_thread_->Join();
24 audio_input_thread_.reset();
25 }
26}
27
[email protected]9a578392011-12-07 18:59:2728void PPB_AudioInput_Shared::SetCallback(PPB_AudioInput_Callback callback,
29 void* user_data) {
[email protected]1f8a0892011-11-18 00:14:2430 callback_ = callback;
31 user_data_ = user_data;
32}
33
[email protected]9a578392011-12-07 18:59:2734void PPB_AudioInput_Shared::SetStartCaptureState() {
[email protected]1f8a0892011-11-18 00:14:2435 DCHECK(!capturing_);
36 DCHECK(!audio_input_thread_.get());
37
38 // If the socket doesn't exist, that means that the plugin has started before
39 // the browser has had a chance to create all the shared memory info and
40 // notify us. This is a common case. In this case, we just set the playing_
41 // flag and the capture will automatically start when that data is available
42 // in SetStreamInfo.
43 if (socket_.get())
44 StartThread();
45 capturing_ = true;
46}
47
[email protected]9a578392011-12-07 18:59:2748void PPB_AudioInput_Shared::SetStopCaptureState() {
[email protected]1f8a0892011-11-18 00:14:2449 DCHECK(capturing_);
50
51 if (audio_input_thread_.get()) {
52 audio_input_thread_->Join();
53 audio_input_thread_.reset();
54 }
55 capturing_ = false;
56}
57
[email protected]9a578392011-12-07 18:59:2758void PPB_AudioInput_Shared::SetStreamInfo(
[email protected]1f8a0892011-11-18 00:14:2459 base::SharedMemoryHandle shared_memory_handle,
60 size_t shared_memory_size,
61 base::SyncSocket::Handle socket_handle) {
62 socket_.reset(new base::SyncSocket(socket_handle));
63 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false));
64 shared_memory_size_ = shared_memory_size;
65
66 if (callback_) {
67 shared_memory_->Map(shared_memory_size_);
68
69 // In common case StartCapture() was called before StreamCreated().
70 if (capturing_)
71 StartThread();
72 }
73}
74
[email protected]9a578392011-12-07 18:59:2775void PPB_AudioInput_Shared::StartThread() {
[email protected]1f8a0892011-11-18 00:14:2476 DCHECK(callback_);
77 DCHECK(!audio_input_thread_.get());
78 audio_input_thread_.reset(new base::DelegateSimpleThread(
79 this, "plugin_audio_input_thread"));
80 audio_input_thread_->Start();
81}
82
[email protected]9a578392011-12-07 18:59:2783void PPB_AudioInput_Shared::Run() {
[email protected]1f8a0892011-11-18 00:14:2484 int pending_data;
85 void* buffer = shared_memory_->memory();
86
87 while (sizeof(pending_data) == socket_->Receive(&pending_data,
88 sizeof(pending_data)) &&
89 pending_data >= 0) {
90 callback_(buffer, shared_memory_size_, user_data_);
91 }
92}
93
94} // namespace ppapi