[email protected] | 028b90f6 | 2012-04-05 01:44:13 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 5 | #ifndef PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_ |
| 6 | #define PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_ |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 7 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 9 | #include "base/shared_memory.h" |
| 10 | #include "base/sync_socket.h" |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 11 | #include "base/threading/simple_thread.h" |
[email protected] | b9a5984 | 2011-01-15 01:04:00 | [diff] [blame] | 12 | #include "ppapi/c/ppb_audio.h" |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 13 | #include "ppapi/shared_impl/resource.h" |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 14 | #include "ppapi/thunk/ppb_audio_api.h" |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 15 | |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 16 | namespace ppapi { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 17 | |
| 18 | // Implements the logic to map shared memory and run the audio thread signaled |
| 19 | // from the sync socket. Both the proxy and the renderer implementation use |
| 20 | // this code. |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 21 | class PPAPI_SHARED_EXPORT PPB_Audio_Shared |
[email protected] | f0a04c4 | 2011-08-26 22:43:20 | [diff] [blame] | 22 | : public thunk::PPB_Audio_API, |
| 23 | public base::DelegateSimpleThread::Delegate { |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 24 | public: |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 25 | PPB_Audio_Shared(); |
| 26 | virtual ~PPB_Audio_Shared(); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 27 | |
[email protected] | 028b90f6 | 2012-04-05 01:44:13 | [diff] [blame] | 28 | // Keep in sync with media::AudioOutputController::kPauseMark. |
| 29 | static const int kPauseMark; |
| 30 | |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 31 | bool playing() const { return playing_; } |
| 32 | |
| 33 | // Sets the callback information that the background thread will use. This |
| 34 | // is optional. Without a callback, the thread will not be run. This |
| 35 | // non-callback mode is used in the renderer with the proxy, since the proxy |
| 36 | // handles the callback entirely within the plugin process. |
| 37 | void SetCallback(PPB_Audio_Callback callback, void* user_data); |
| 38 | |
| 39 | // Configures the current state to be playing or not. The caller is |
| 40 | // responsible for ensuring the new state is the opposite of the current one. |
| 41 | // |
| 42 | // This is the implementation for PPB_Audio.Start/StopPlayback, except that |
| 43 | // it does not actually notify the audio system to stop playback, it just |
| 44 | // configures our object to stop generating callbacks. The actual stop |
| 45 | // playback request will be done in the derived classes and will be different |
| 46 | // from the proxy and the renderer. |
| 47 | void SetStartPlaybackState(); |
| 48 | void SetStopPlaybackState(); |
| 49 | |
| 50 | // Sets the shared memory and socket handles. This will automatically start |
| 51 | // playback if we're currently set to play. |
[email protected] | 9672468 | 2012-04-26 20:53:37 | [diff] [blame^] | 52 | void SetStreamInfo(PP_Instance instance, |
| 53 | base::SharedMemoryHandle shared_memory_handle, |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 54 | size_t shared_memory_size, |
| 55 | base::SyncSocket::Handle socket_handle); |
| 56 | |
| 57 | private: |
| 58 | // Starts execution of the audio thread. |
| 59 | void StartThread(); |
| 60 | |
| 61 | // DelegateSimpleThread::Delegate implementation. Run on the audio thread. |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 62 | virtual void Run(); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 63 | |
| 64 | // True if playing the stream. |
| 65 | bool playing_; |
| 66 | |
| 67 | // Socket used to notify us when audio is ready to accept new samples. This |
| 68 | // pointer is created in StreamCreated(). |
| 69 | scoped_ptr<base::SyncSocket> socket_; |
| 70 | |
| 71 | // Sample buffer in shared memory. This pointer is created in |
| 72 | // StreamCreated(). The memory is only mapped when the audio thread is |
| 73 | // created. |
| 74 | scoped_ptr<base::SharedMemory> shared_memory_; |
| 75 | |
| 76 | // The size of the sample buffer in bytes. |
| 77 | size_t shared_memory_size_; |
| 78 | |
| 79 | // When the callback is set, this thread is spawned for calling it. |
| 80 | scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
| 81 | |
| 82 | // Callback to call when audio is ready to accept new samples. |
| 83 | PPB_Audio_Callback callback_; |
| 84 | |
| 85 | // User data pointer passed verbatim to the callback function. |
| 86 | void* user_data_; |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(PPB_Audio_Shared); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 89 | }; |
| 90 | |
[email protected] | 55cdf605 | 2011-05-13 19:22:53 | [diff] [blame] | 91 | } // namespace ppapi |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 92 | |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 93 | #endif // PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_ |