[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 1 | // 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 | |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 11 | #include <memory> |
| 12 | |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
avi | e029c413 | 2015-12-23 06:45:22 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
[email protected] | 8299b71 | 2013-07-17 19:55:23 | [diff] [blame] | 16 | #include "base/memory/shared_memory.h" |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 17 | #include "base/sync_socket.h" |
| 18 | #include "base/threading/simple_thread.h" |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 19 | #include "ppapi/proxy/device_enumeration_resource_helper.h" |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 20 | #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] | 5955c9f | 2014-07-10 16:44:01 | [diff] [blame] | 24 | namespace media { |
| 25 | class AudioBus; |
| 26 | } |
| 27 | |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 28 | namespace ppapi { |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 29 | namespace proxy { |
| 30 | |
| 31 | class ResourceMessageReplyParams; |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 32 | |
| 33 | class AudioInputResource : public PluginResource, |
| 34 | public thunk::PPB_AudioInput_API, |
| 35 | public base::DelegateSimpleThread::Delegate { |
| 36 | public: |
| 37 | AudioInputResource(Connection connection, PP_Instance instance); |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 38 | ~AudioInputResource() override; |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 39 | |
| 40 | // Resource overrides. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 41 | thunk::PPB_AudioInput_API* AsPPB_AudioInput_API() override; |
| 42 | void OnReplyReceived(const ResourceMessageReplyParams& params, |
| 43 | const IPC::Message& msg) override; |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 44 | |
| 45 | // PPB_AudioInput_API implementation. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 46 | 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] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 64 | |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 65 | protected: |
| 66 | // Resource override. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 67 | void LastPluginRefWasDeleted() override; |
[email protected] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 68 | |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 69 | private: |
| 70 | enum OpenState { |
| 71 | BEFORE_OPEN, |
| 72 | OPENED, |
| 73 | CLOSED |
| 74 | }; |
| 75 | |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 76 | 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. |
nick | e478443 | 2015-04-23 14:01:48 | [diff] [blame] | 92 | void Run() override; |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 93 | |
[email protected] | c90ffec7 | 2013-07-03 11:57:45 | [diff] [blame] | 94 | int32_t CommonOpen(PP_Resource device_ref, |
| 95 | PP_Resource config, |
[email protected] | a23089d0 | 2014-01-06 23:51:40 | [diff] [blame] | 96 | PPB_AudioInput_Callback_0_3 audio_input_callback_0_3, |
[email protected] | c90ffec7 | 2013-07-03 11:57:45 | [diff] [blame] | 97 | PPB_AudioInput_Callback audio_input_callback, |
| 98 | void* user_data, |
| 99 | scoped_refptr<TrackedCallback> callback); |
| 100 | |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 101 | 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(). |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 108 | std::unique_ptr<base::CancelableSyncSocket> socket_; |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 109 | |
| 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. |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 113 | std::unique_ptr<base::SharedMemory> shared_memory_; |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 114 | |
| 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. |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 119 | std::unique_ptr<base::DelegateSimpleThread> audio_input_thread_; |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 120 | |
| 121 | // Callback to call when new samples are available. |
[email protected] | a23089d0 | 2014-01-06 23:51:40 | [diff] [blame] | 122 | PPB_AudioInput_Callback_0_3 audio_input_callback_0_3_; |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 123 | PPB_AudioInput_Callback audio_input_callback_; |
| 124 | |
| 125 | // User data pointer passed verbatim to the callback function. |
| 126 | void* user_data_; |
| 127 | |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 128 | // 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] | 4f01c76 | 2012-12-05 02:44:18 | [diff] [blame] | 136 | DeviceEnumerationResourceHelper enumeration_helper_; |
| 137 | |
[email protected] | c90ffec7 | 2013-07-03 11:57:45 | [diff] [blame] | 138 | // 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] | 5955c9f | 2014-07-10 16:44:01 | [diff] [blame] | 142 | // AudioBus for shuttling data across the shared memory. |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 143 | std::unique_ptr<media::AudioBus> audio_bus_; |
[email protected] | 5955c9f | 2014-07-10 16:44:01 | [diff] [blame] | 144 | int sample_frame_count_; |
| 145 | |
| 146 | // Internal buffer for client's integer audio data. |
| 147 | int client_buffer_size_bytes_; |
dcheng | ced9224 | 2016-04-07 00:00:12 | [diff] [blame] | 148 | std::unique_ptr<uint8_t[]> client_buffer_; |
[email protected] | 5955c9f | 2014-07-10 16:44:01 | [diff] [blame] | 149 | |
[email protected] | 77b5550 | 2012-11-08 22:20:20 | [diff] [blame] | 150 | DISALLOW_COPY_AND_ASSIGN(AudioInputResource); |
| 151 | }; |
| 152 | |
| 153 | } // namespace proxy |
| 154 | } // namespace ppapi |
| 155 | |
| 156 | #endif // PPAPI_PROXY_AUDIO_INPUT_RESOURCE_H_ |