1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_
#define PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include "base/compiler_specific.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/scoped_refptr.h"
#include "base/sync_socket.h"
#include "base/threading/simple_thread.h"
#include "ppapi/proxy/device_enumeration_resource_helper.h"
#include "ppapi/proxy/plugin_resource.h"
#include "ppapi/shared_impl/scoped_pp_resource.h"
#include "ppapi/thunk/ppb_audio_input_api.h"
namespace media {
class AudioBus;
}
namespace ppapi {
namespace proxy {
class ResourceMessageReplyParams;
class AudioInputResource : public PluginResource,
public thunk::PPB_AudioInput_API,
public base::DelegateSimpleThread::Delegate {
public:
AudioInputResource(Connection connection, PP_Instance instance);
AudioInputResource(const AudioInputResource&) = delete;
AudioInputResource& operator=(const AudioInputResource&) = delete;
~AudioInputResource() override;
// Resource overrides.
thunk::PPB_AudioInput_API* AsPPB_AudioInput_API() override;
void OnReplyReceived(const ResourceMessageReplyParams& params,
const IPC::Message& msg) override;
// PPB_AudioInput_API implementation.
int32_t EnumerateDevices(const PP_ArrayOutput& output,
scoped_refptr<TrackedCallback> callback) override;
int32_t MonitorDeviceChange(PP_MonitorDeviceChangeCallback callback,
void* user_data) override;
int32_t Open0_3(PP_Resource device_ref,
PP_Resource config,
PPB_AudioInput_Callback_0_3 audio_input_callback_0_3,
void* user_data,
scoped_refptr<TrackedCallback> callback) override;
int32_t Open(PP_Resource device_ref,
PP_Resource config,
PPB_AudioInput_Callback audio_input_callback,
void* user_data,
scoped_refptr<TrackedCallback> callback) override;
PP_Resource GetCurrentConfig() override;
PP_Bool StartCapture() override;
PP_Bool StopCapture() override;
void Close() override;
protected:
// Resource override.
void LastPluginRefWasDeleted() override;
private:
enum OpenState {
BEFORE_OPEN,
OPENED,
CLOSED
};
void OnPluginMsgOpenReply(const ResourceMessageReplyParams& params);
// Sets the shared memory and socket handles. This will automatically start
// capture if we're currently set to capture.
void SetStreamInfo(base::ReadOnlySharedMemoryRegion shared_memory_region,
base::SyncSocket::Handle socket_handle);
// Starts execution of the audio input thread.
void StartThread();
// Stops execution of the audio input thread.
void StopThread();
// DelegateSimpleThread::Delegate implementation.
// Run on the audio input thread.
void Run() override;
int32_t CommonOpen(PP_Resource device_ref,
PP_Resource config,
PPB_AudioInput_Callback_0_3 audio_input_callback_0_3,
PPB_AudioInput_Callback audio_input_callback,
void* user_data,
scoped_refptr<TrackedCallback> callback);
OpenState open_state_;
// True if capturing the stream.
bool capturing_;
// Socket used to notify us when new samples are available. This pointer is
// created in SetStreamInfo().
std::unique_ptr<base::CancelableSyncSocket> socket_;
// Sample buffer in shared memory. This pointer is created in
// SetStreamInfo(). The memory is only mapped when the audio thread is
// created.
base::ReadOnlySharedMemoryMapping shared_memory_mapping_;
// The size of the sample buffer in bytes.
size_t shared_memory_size_;
// When the callback is set, this thread is spawned for calling it.
std::unique_ptr<base::DelegateSimpleThread> audio_input_thread_;
// Callback to call when new samples are available.
PPB_AudioInput_Callback_0_3 audio_input_callback_0_3_;
PPB_AudioInput_Callback audio_input_callback_;
// User data pointer passed verbatim to the callback function.
void* user_data_;
// The callback is not directly passed to OnPluginMsgOpenReply() because we
// would like to be able to cancel it early in Close().
scoped_refptr<TrackedCallback> open_callback_;
// Owning reference to the current config object. This isn't actually used,
// we just dish it out as requested by the plugin.
ScopedPPResource config_;
DeviceEnumerationResourceHelper enumeration_helper_;
// The data size (in bytes) of one second of audio input. Used to calculate
// latency.
size_t bytes_per_second_;
// AudioBus for shuttling data across the shared memory.
std::unique_ptr<const media::AudioBus> audio_bus_;
int sample_frame_count_;
// Internal buffer for client's integer audio data.
int client_buffer_size_bytes_;
std::unique_ptr<uint8_t[]> client_buffer_;
};
} // namespace proxy
} // namespace ppapi
#endif // PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_
|