blob: 809c0dbe119d32d4befd94669ebbdf8b5adf8cdc [file] [log] [blame]
[email protected]77b55502012-11-08 22:20:201// Copyright (c) 2012 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#ifndef PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_
6#define PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_
7
avie029c4132015-12-23 06:45:228#include <stddef.h>
9#include <stdint.h>
10
dchengced92242016-04-07 00:00:1211#include <memory>
12
[email protected]77b55502012-11-08 22:20:2013#include "base/compiler_specific.h"
avie029c4132015-12-23 06:45:2214#include "base/macros.h"
[email protected]77b55502012-11-08 22:20:2015#include "base/memory/ref_counted.h"
[email protected]8299b712013-07-17 19:55:2316#include "base/memory/shared_memory.h"
[email protected]77b55502012-11-08 22:20:2017#include "base/sync_socket.h"
18#include "base/threading/simple_thread.h"
[email protected]4f01c762012-12-05 02:44:1819#include "ppapi/proxy/device_enumeration_resource_helper.h"
[email protected]77b55502012-11-08 22:20:2020#include "ppapi/proxy/plugin_resource.h"
21#include "ppapi/shared_impl/scoped_pp_resource.h"
22#include "ppapi/thunk/ppb_audio_input_api.h"
23
[email protected]5955c9f2014-07-10 16:44:0124namespace media {
25class AudioBus;
26}
27
[email protected]77b55502012-11-08 22:20:2028namespace ppapi {
[email protected]77b55502012-11-08 22:20:2029namespace proxy {
30
31class ResourceMessageReplyParams;
[email protected]77b55502012-11-08 22:20:2032
33class AudioInputResource : public PluginResource,
34 public thunk::PPB_AudioInput_API,
35 public base::DelegateSimpleThread::Delegate {
36 public:
37 AudioInputResource(Connection connection, PP_Instance instance);
nicke4784432015-04-23 14:01:4838 ~AudioInputResource() override;
[email protected]77b55502012-11-08 22:20:2039
40 // Resource overrides.
nicke4784432015-04-23 14:01:4841 thunk::PPB_AudioInput_API* AsPPB_AudioInput_API() override;
42 void OnReplyReceived(const ResourceMessageReplyParams& params,
43 const IPC::Message& msg) override;
[email protected]77b55502012-11-08 22:20:2044
45 // PPB_AudioInput_API implementation.
nicke4784432015-04-23 14:01:4846 int32_t EnumerateDevices(const PP_ArrayOutput& output,
47 scoped_refptr<TrackedCallback> callback) override;
48 int32_t MonitorDeviceChange(PP_MonitorDeviceChangeCallback callback,
49 void* user_data) override;
50 int32_t Open0_3(PP_Resource device_ref,
51 PP_Resource config,
52 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3,
53 void* user_data,
54 scoped_refptr<TrackedCallback> callback) override;
55 int32_t Open(PP_Resource device_ref,
56 PP_Resource config,
57 PPB_AudioInput_Callback audio_input_callback,
58 void* user_data,
59 scoped_refptr<TrackedCallback> callback) override;
60 PP_Resource GetCurrentConfig() override;
61 PP_Bool StartCapture() override;
62 PP_Bool StopCapture() override;
63 void Close() override;
[email protected]77b55502012-11-08 22:20:2064
[email protected]4f01c762012-12-05 02:44:1865 protected:
66 // Resource override.
nicke4784432015-04-23 14:01:4867 void LastPluginRefWasDeleted() override;
[email protected]4f01c762012-12-05 02:44:1868
[email protected]77b55502012-11-08 22:20:2069 private:
70 enum OpenState {
71 BEFORE_OPEN,
72 OPENED,
73 CLOSED
74 };
75
[email protected]77b55502012-11-08 22:20:2076 void OnPluginMsgOpenReply(const ResourceMessageReplyParams& params);
77
78 // Sets the shared memory and socket handles. This will automatically start
79 // capture if we're currently set to capture.
80 void SetStreamInfo(base::SharedMemoryHandle shared_memory_handle,
81 size_t shared_memory_size,
82 base::SyncSocket::Handle socket_handle);
83
84 // Starts execution of the audio input thread.
85 void StartThread();
86
87 // Stops execution of the audio input thread.
88 void StopThread();
89
90 // DelegateSimpleThread::Delegate implementation.
91 // Run on the audio input thread.
nicke4784432015-04-23 14:01:4892 void Run() override;
[email protected]77b55502012-11-08 22:20:2093
[email protected]c90ffec72013-07-03 11:57:4594 int32_t CommonOpen(PP_Resource device_ref,
95 PP_Resource config,
[email protected]a23089d02014-01-06 23:51:4096 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3,
[email protected]c90ffec72013-07-03 11:57:4597 PPB_AudioInput_Callback audio_input_callback,
98 void* user_data,
99 scoped_refptr<TrackedCallback> callback);
100
[email protected]77b55502012-11-08 22:20:20101 OpenState open_state_;
102
103 // True if capturing the stream.
104 bool capturing_;
105
106 // Socket used to notify us when new samples are available. This pointer is
107 // created in SetStreamInfo().
dchengced92242016-04-07 00:00:12108 std::unique_ptr<base::CancelableSyncSocket> socket_;
[email protected]77b55502012-11-08 22:20:20109
110 // Sample buffer in shared memory. This pointer is created in
111 // SetStreamInfo(). The memory is only mapped when the audio thread is
112 // created.
dchengced92242016-04-07 00:00:12113 std::unique_ptr<base::SharedMemory> shared_memory_;
[email protected]77b55502012-11-08 22:20:20114
115 // The size of the sample buffer in bytes.
116 size_t shared_memory_size_;
117
118 // When the callback is set, this thread is spawned for calling it.
dchengced92242016-04-07 00:00:12119 std::unique_ptr<base::DelegateSimpleThread> audio_input_thread_;
[email protected]77b55502012-11-08 22:20:20120
121 // Callback to call when new samples are available.
[email protected]a23089d02014-01-06 23:51:40122 PPB_AudioInput_Callback_0_3 audio_input_callback_0_3_;
[email protected]77b55502012-11-08 22:20:20123 PPB_AudioInput_Callback audio_input_callback_;
124
125 // User data pointer passed verbatim to the callback function.
126 void* user_data_;
127
[email protected]77b55502012-11-08 22:20:20128 // The callback is not directly passed to OnPluginMsgOpenReply() because we
129 // would like to be able to cancel it early in Close().
130 scoped_refptr<TrackedCallback> open_callback_;
131
132 // Owning reference to the current config object. This isn't actually used,
133 // we just dish it out as requested by the plugin.
134 ScopedPPResource config_;
135
[email protected]4f01c762012-12-05 02:44:18136 DeviceEnumerationResourceHelper enumeration_helper_;
137
[email protected]c90ffec72013-07-03 11:57:45138 // The data size (in bytes) of one second of audio input. Used to calculate
139 // latency.
140 size_t bytes_per_second_;
141
[email protected]5955c9f2014-07-10 16:44:01142 // AudioBus for shuttling data across the shared memory.
dchengced92242016-04-07 00:00:12143 std::unique_ptr<media::AudioBus> audio_bus_;
[email protected]5955c9f2014-07-10 16:44:01144 int sample_frame_count_;
145
146 // Internal buffer for client's integer audio data.
147 int client_buffer_size_bytes_;
dchengced92242016-04-07 00:00:12148 std::unique_ptr<uint8_t[]> client_buffer_;
[email protected]5955c9f2014-07-10 16:44:01149
[email protected]77b55502012-11-08 22:20:20150 DISALLOW_COPY_AND_ASSIGN(AudioInputResource);
151};
152
153} // namespace proxy
154} // namespace ppapi
155
156#endif // PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_