OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 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_OUTPUT_RESOURCE_H_ |
| 6 #define PPAPI_PROXY_AUDIO_OUTPUT_RESOURCE_H_ |
| 7 |
| 8 #include <stddef.h> |
| 9 #include <stdint.h> |
| 10 |
| 11 #include <memory> |
| 12 |
| 13 #include "base/compiler_specific.h" |
| 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/shared_memory.h" |
| 17 #include "base/sync_socket.h" |
| 18 #include "base/threading/simple_thread.h" |
| 19 #include "ppapi/c/ppb_audio_config.h" |
| 20 #include "ppapi/proxy/device_enumeration_resource_helper.h" |
| 21 #include "ppapi/proxy/plugin_resource.h" |
| 22 #include "ppapi/shared_impl/scoped_pp_resource.h" |
| 23 #include "ppapi/thunk/ppb_audio_output_api.h" |
| 24 |
| 25 namespace media { |
| 26 class AudioBus; |
| 27 } |
| 28 |
| 29 namespace ppapi { |
| 30 namespace proxy { |
| 31 |
| 32 class ResourceMessageReplyParams; |
| 33 |
| 34 class AudioOutputResource : public PluginResource, |
| 35 public thunk::PPB_AudioOutput_API, |
| 36 public base::DelegateSimpleThread::Delegate { |
| 37 public: |
| 38 AudioOutputResource(Connection connection, PP_Instance instance); |
| 39 ~AudioOutputResource() override; |
| 40 |
| 41 // Resource overrides. |
| 42 thunk::PPB_AudioOutput_API* AsPPB_AudioOutput_API() override; |
| 43 void OnReplyReceived(const ResourceMessageReplyParams& params, |
| 44 const IPC::Message& msg) override; |
| 45 |
| 46 // PPB_AudioOutput_API implementation. |
| 47 int32_t EnumerateDevices(const PP_ArrayOutput& output, |
| 48 scoped_refptr<TrackedCallback> callback) override; |
| 49 int32_t MonitorDeviceChange(PP_MonitorDeviceChangeCallback callback, |
| 50 void* user_data) override; |
| 51 int32_t Open(PP_Resource device_ref, |
| 52 PP_Resource config, |
| 53 PPB_AudioOutput_Callback audio_output_callback, |
| 54 void* user_data, |
| 55 scoped_refptr<TrackedCallback> callback) override; |
| 56 |
| 57 PP_Resource GetCurrentConfig() override; |
| 58 |
| 59 bool playing() const { return playing_; } |
| 60 |
| 61 PP_Bool StartPlayback() override; |
| 62 PP_Bool StopPlayback() override; |
| 63 void Close() override; |
| 64 |
| 65 protected: |
| 66 // Resource override. |
| 67 void LastPluginRefWasDeleted() override; |
| 68 |
| 69 private: |
| 70 enum OpenState { BEFORE_OPEN, OPENED, CLOSED }; |
| 71 |
| 72 void OnPluginMsgOpenReply(const ResourceMessageReplyParams& params); |
| 73 |
| 74 // Sets the shared memory and socket handles. |
| 75 void SetStreamInfo(base::SharedMemoryHandle shared_memory_handle, |
| 76 size_t shared_memory_size, |
| 77 base::SyncSocket::Handle socket_handle); |
| 78 |
| 79 // Starts execution of the audio output thread. |
| 80 void StartThread(); |
| 81 |
| 82 // Stops execution of the audio output thread. |
| 83 void StopThread(); |
| 84 |
| 85 // DelegateSimpleThread::Delegate implementation. |
| 86 // Run on the audio output thread. |
| 87 void Run() override; |
| 88 |
| 89 int32_t CommonOpen(PP_Resource device_ref, |
| 90 PP_Resource config, |
| 91 PPB_AudioOutput_Callback audio_output_callback, |
| 92 void* user_data, |
| 93 scoped_refptr<TrackedCallback> callback); |
| 94 |
| 95 OpenState open_state_; |
| 96 |
| 97 // True if playing the stream. |
| 98 bool playing_; |
| 99 |
| 100 // Socket used to notify us when new samples are available. This pointer is |
| 101 // created in SetStreamInfo(). |
| 102 std::unique_ptr<base::CancelableSyncSocket> socket_; |
| 103 |
| 104 // Sample buffer in shared memory. This pointer is created in |
| 105 // SetStreamInfo(). The memory is only mapped when the audio thread is |
| 106 // created. |
| 107 std::unique_ptr<base::SharedMemory> shared_memory_; |
| 108 |
| 109 // The size of the sample buffer in bytes. |
| 110 size_t shared_memory_size_; |
| 111 |
| 112 // When the callback is set, this thread is spawned for calling it. |
| 113 std::unique_ptr<base::DelegateSimpleThread> audio_output_thread_; |
| 114 |
| 115 // Callback to call when new samples are available. |
| 116 PPB_AudioOutput_Callback audio_output_callback_; |
| 117 |
| 118 // User data pointer passed verbatim to the callback function. |
| 119 void* user_data_; |
| 120 |
| 121 // The callback is not directly passed to OnPluginMsgOpenReply() because we |
| 122 // would like to be able to cancel it early in Close(). |
| 123 scoped_refptr<TrackedCallback> open_callback_; |
| 124 |
| 125 // Owning reference to the current config object. This isn't actually used, |
| 126 // we just dish it out as requested by the plugin. |
| 127 ScopedPPResource config_; |
| 128 |
| 129 DeviceEnumerationResourceHelper enumeration_helper_; |
| 130 |
| 131 // The data size (in bytes) of one second of audio output. Used to calculate |
| 132 // latency. |
| 133 size_t bytes_per_second_; |
| 134 |
| 135 // AudioBus for shuttling data across the shared memory. |
| 136 std::unique_ptr<media::AudioBus> audio_bus_; |
| 137 int sample_frame_count_; |
| 138 |
| 139 // Internal buffer for client's integer audio data. |
| 140 int client_buffer_size_bytes_; |
| 141 std::unique_ptr<uint8_t[]> client_buffer_; |
| 142 |
| 143 DISALLOW_COPY_AND_ASSIGN(AudioOutputResource); |
| 144 }; |
| 145 |
| 146 } // namespace proxy |
| 147 } // namespace ppapi |
| 148 |
| 149 #endif // PPAPI_PROXY_AUDIO_OUTPUT_RESOURCE_H_ |
OLD | NEW |