OLD | NEW |
(Empty) | |
| 1 // 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 |
| 5 #include "ppapi/shared_impl/audio_input_impl.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 namespace ppapi { |
| 10 |
| 11 AudioInputImpl::AudioInputImpl() |
| 12 : capturing_(false), |
| 13 shared_memory_size_(0), |
| 14 callback_(NULL), |
| 15 user_data_(NULL) { |
| 16 } |
| 17 |
| 18 AudioInputImpl::~AudioInputImpl() { |
| 19 // 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 |
| 28 void AudioInputImpl::SetCallback(PPB_AudioInput_Callback callback, void* user_da
ta) { |
| 29 callback_ = callback; |
| 30 user_data_ = user_data; |
| 31 } |
| 32 |
| 33 void AudioInputImpl::SetStartCaptureState() { |
| 34 DCHECK(!capturing_); |
| 35 DCHECK(!audio_input_thread_.get()); |
| 36 |
| 37 // If the socket doesn't exist, that means that the plugin has started before |
| 38 // the browser has had a chance to create all the shared memory info and |
| 39 // notify us. This is a common case. In this case, we just set the playing_ |
| 40 // flag and the playback will automatically start when that data is available |
| 41 // in SetStreamInfo. |
| 42 if (socket_.get()) |
| 43 StartThread(); |
| 44 capturing_ = true; |
| 45 } |
| 46 |
| 47 void AudioInputImpl::SetStopCaptureState() { |
| 48 DCHECK(capturing_); |
| 49 |
| 50 if (audio_input_thread_.get()) { |
| 51 audio_input_thread_->Join(); |
| 52 audio_input_thread_.reset(); |
| 53 } |
| 54 capturing_ = false; |
| 55 } |
| 56 |
| 57 void AudioInputImpl::SetStreamInfo(base::SharedMemoryHandle shared_memory_handle
, |
| 58 size_t shared_memory_size, |
| 59 base::SyncSocket::Handle socket_handle) { |
| 60 socket_.reset(new base::SyncSocket(socket_handle)); |
| 61 shared_memory_.reset(new base::SharedMemory(shared_memory_handle, false)); |
| 62 shared_memory_size_ = shared_memory_size; |
| 63 |
| 64 if (callback_) { |
| 65 shared_memory_->Map(shared_memory_size_); |
| 66 |
| 67 // In common case StartPlayback() was called before StreamCreated(). |
| 68 if (capturing_) |
| 69 StartThread(); |
| 70 } |
| 71 } |
| 72 |
| 73 void AudioInputImpl::StartThread() { |
| 74 DCHECK(callback_); |
| 75 DCHECK(!audio_input_thread_.get()); |
| 76 audio_input_thread_.reset(new base::DelegateSimpleThread( |
| 77 this, "plugin_audio_thread")); |
| 78 audio_input_thread_->Start(); |
| 79 } |
| 80 |
| 81 void AudioInputImpl::Run() { |
| 82 int pending_data; |
| 83 void* buffer = shared_memory_->memory(); |
| 84 |
| 85 |
| 86 while (sizeof(pending_data) == |
| 87 socket_->Receive(&pending_data, sizeof(pending_data)) && |
| 88 pending_data >= 0) { |
| 89 callback_(buffer, shared_memory_size_, user_data_); |
| 90 } |
| 91 } |
| 92 |
| 93 } // namespace ppapi |
OLD | NEW |