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_SHARED_IMPL_PPB_AUDIO_SHARED_H_ 6 #define PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_ 7 8 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/shared_memory.h" 10 #include "base/sync_socket.h" 11 #include "base/threading/simple_thread.h" 12 #include "media/base/audio_bus.h" 13 #include "ppapi/c/ppb_audio.h" 14 #include "ppapi/c/ppb_audio_config.h" 15 #include "ppapi/shared_impl/resource.h" 16 #include "ppapi/thunk/ppb_audio_api.h" 17 18 struct PP_ThreadFunctions; 19 20 namespace ppapi { 21 22 class PPAPI_SHARED_EXPORT AudioCallbackCombined { 23 public: 24 AudioCallbackCombined(); 25 explicit AudioCallbackCombined(PPB_Audio_Callback_1_0 callback_1_0); 26 explicit AudioCallbackCombined(PPB_Audio_Callback callback); 27 28 ~AudioCallbackCombined(); 29 30 bool IsValid() const; 31 32 void Run(void* sample_buffer, 33 uint32_t buffer_size_in_bytes, 34 PP_TimeDelta latency, 35 void* user_data) const; 36 37 private: 38 PPB_Audio_Callback_1_0 callback_1_0_; 39 PPB_Audio_Callback callback_; 40 }; 41 42 // Implements the logic to map shared memory and run the audio thread signaled 43 // from the sync socket. Both the proxy and the renderer implementation use 44 // this code. 45 class PPAPI_SHARED_EXPORT PPB_Audio_Shared 46 : public thunk::PPB_Audio_API, 47 public base::DelegateSimpleThread::Delegate { 48 public: 49 PPB_Audio_Shared(); 50 virtual ~PPB_Audio_Shared(); 51 playing()52 bool playing() const { return playing_; } 53 54 // Sets the callback information that the background thread will use. This 55 // is optional. Without a callback, the thread will not be run. This 56 // non-callback mode is used in the renderer with the proxy, since the proxy 57 // handles the callback entirely within the plugin process. 58 void SetCallback(const AudioCallbackCombined& callback, void* user_data); 59 60 // Configures the current state to be playing or not. The caller is 61 // responsible for ensuring the new state is the opposite of the current one. 62 // 63 // This is the implementation for PPB_Audio.Start/StopPlayback, except that 64 // it does not actually notify the audio system to stop playback, it just 65 // configures our object to stop generating callbacks. The actual stop 66 // playback request will be done in the derived classes and will be different 67 // from the proxy and the renderer. 68 void SetStartPlaybackState(); 69 void SetStopPlaybackState(); 70 71 // Sets the shared memory and socket handles. This will automatically start 72 // playback if we're currently set to play. 73 void SetStreamInfo(PP_Instance instance, 74 base::SharedMemoryHandle shared_memory_handle, 75 size_t shared_memory_size, 76 base::SyncSocket::Handle socket_handle, 77 PP_AudioSampleRate sample_rate, 78 int sample_frame_count); 79 80 // Returns whether a thread can be created on the client context. 81 // In trusted plugin, this should always return true, as it uses Chrome's 82 // thread library. In NaCl plugin, this returns whether SetThreadFunctions 83 // was invoked properly. 84 static bool IsThreadFunctionReady(); 85 86 // Configures this class to run in a NaCl plugin. 87 // If called, SetThreadFunctions() must be called before calling 88 // SetStartPlaybackState() on any instance of this class. 89 static void SetNaClMode(); 90 91 // NaCl has a special API for IRT code to create threads that can call back 92 // into user code. 93 static void SetThreadFunctions(const struct PP_ThreadFunctions* functions); 94 95 private: 96 // Starts execution of the audio thread. 97 void StartThread(); 98 99 // Stop execution of the audio thread. 100 void StopThread(); 101 102 // DelegateSimpleThread::Delegate implementation. Run on the audio thread. 103 virtual void Run(); 104 105 // True if playing the stream. 106 bool playing_; 107 108 // Socket used to notify us when audio is ready to accept new samples. This 109 // pointer is created in StreamCreated(). 110 scoped_ptr<base::CancelableSyncSocket> socket_; 111 112 // Sample buffer in shared memory. This pointer is created in 113 // StreamCreated(). The memory is only mapped when the audio thread is 114 // created. 115 scoped_ptr<base::SharedMemory> shared_memory_; 116 117 // The size of the sample buffer in bytes. 118 size_t shared_memory_size_; 119 120 // When the callback is set, this thread is spawned for calling it. 121 scoped_ptr<base::DelegateSimpleThread> audio_thread_; 122 uintptr_t nacl_thread_id_; 123 bool nacl_thread_active_; 124 125 static void CallRun(void* self); 126 127 // Callback to call when audio is ready to accept new samples. 128 AudioCallbackCombined callback_; 129 130 // User data pointer passed verbatim to the callback function. 131 void* user_data_; 132 133 // AudioBus for shuttling data across the shared memory. 134 scoped_ptr<media::AudioBus> audio_bus_; 135 136 // Internal buffer for client's integer audio data. 137 int client_buffer_size_bytes_; 138 scoped_ptr<uint8_t[]> client_buffer_; 139 140 // The size (in bytes) of one second of audio data. Used to calculate latency. 141 size_t bytes_per_second_; 142 143 // Buffer index used to coordinate with the browser side audio receiver. 144 uint32_t buffer_index_; 145 146 DISALLOW_COPY_AND_ASSIGN(PPB_Audio_Shared); 147 }; 148 149 } // namespace ppapi 150 151 #endif // PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_ 152