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.
5 #ifndef PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_
6 #define PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_
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"
19 #include "ppapi/nacl_irt/irt_ppapi.h"
24 class PPAPI_SHARED_EXPORT AudioCallbackCombined {
26 AudioCallbackCombined();
27 explicit AudioCallbackCombined(PPB_Audio_Callback_1_0 callback_1_0);
28 explicit AudioCallbackCombined(PPB_Audio_Callback callback);
30 ~AudioCallbackCombined();
34 void Run(void* sample_buffer,
35 uint32_t buffer_size_in_bytes,
37 void* user_data) const;
40 PPB_Audio_Callback_1_0 callback_1_0_;
41 PPB_Audio_Callback callback_;
44 // Implements the logic to map shared memory and run the audio thread signaled
45 // from the sync socket. Both the proxy and the renderer implementation use
47 class PPAPI_SHARED_EXPORT PPB_Audio_Shared
48 : public thunk::PPB_Audio_API,
49 public base::DelegateSimpleThread::Delegate {
52 virtual ~PPB_Audio_Shared();
54 bool playing() const { return playing_; }
56 // Sets the callback information that the background thread will use. This
57 // is optional. Without a callback, the thread will not be run. This
58 // non-callback mode is used in the renderer with the proxy, since the proxy
59 // handles the callback entirely within the plugin process.
60 void SetCallback(const AudioCallbackCombined& callback, void* user_data);
62 // Configures the current state to be playing or not. The caller is
63 // responsible for ensuring the new state is the opposite of the current one.
65 // This is the implementation for PPB_Audio.Start/StopPlayback, except that
66 // it does not actually notify the audio system to stop playback, it just
67 // configures our object to stop generating callbacks. The actual stop
68 // playback request will be done in the derived classes and will be different
69 // from the proxy and the renderer.
70 void SetStartPlaybackState();
71 void SetStopPlaybackState();
73 // Sets the shared memory and socket handles. This will automatically start
74 // playback if we're currently set to play.
75 void SetStreamInfo(PP_Instance instance,
76 base::SharedMemoryHandle shared_memory_handle,
77 size_t shared_memory_size,
78 base::SyncSocket::Handle socket_handle,
79 PP_AudioSampleRate sample_rate,
80 int sample_frame_count);
83 // NaCl has a special API for IRT code to create threads that can call back
85 static void SetThreadFunctions(const struct PP_ThreadFunctions* functions);
89 // Starts execution of the audio thread.
92 // Stop execution of the audio thread.
95 // DelegateSimpleThread::Delegate implementation. Run on the audio thread.
98 // True if playing the stream.
101 // Socket used to notify us when audio is ready to accept new samples. This
102 // pointer is created in StreamCreated().
103 scoped_ptr<base::CancelableSyncSocket> socket_;
105 // Sample buffer in shared memory. This pointer is created in
106 // StreamCreated(). The memory is only mapped when the audio thread is
108 scoped_ptr<base::SharedMemory> shared_memory_;
110 // The size of the sample buffer in bytes.
111 size_t shared_memory_size_;
113 #if !defined(OS_NACL)
114 // When the callback is set, this thread is spawned for calling it.
115 scoped_ptr<base::DelegateSimpleThread> audio_thread_;
117 uintptr_t thread_id_;
120 static void CallRun(void* self);
123 // Callback to call when audio is ready to accept new samples.
124 AudioCallbackCombined callback_;
126 // User data pointer passed verbatim to the callback function.
129 // AudioBus for shuttling data across the shared memory.
130 scoped_ptr<media::AudioBus> audio_bus_;
132 // Internal buffer for client's integer audio data.
133 int client_buffer_size_bytes_;
134 scoped_ptr<uint8_t[]> client_buffer_;
136 // The size (in bytes) of one second of audio data. Used to calculate latency.
137 size_t bytes_per_second_;
139 // Buffer index used to coordinate with the browser side audio receiver.
140 uint32_t buffer_index_;
142 DISALLOW_COPY_AND_ASSIGN(PPB_Audio_Shared);
147 #endif // PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_