ppb_audio_shared.h revision 9ab5563a3196760eb381d102cbb2bc0f7abc6a50
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/shared_impl/resource.h"
15#include "ppapi/thunk/ppb_audio_api.h"
16
17#if defined(OS_NACL)
18#include "native_client/src/untrusted/irt/irt_ppapi.h"
19#endif
20
21namespace ppapi {
22
23// Implements the logic to map shared memory and run the audio thread signaled
24// from the sync socket. Both the proxy and the renderer implementation use
25// this code.
26class PPAPI_SHARED_EXPORT PPB_Audio_Shared
27    : public thunk::PPB_Audio_API,
28      public base::DelegateSimpleThread::Delegate {
29 public:
30  PPB_Audio_Shared();
31  virtual ~PPB_Audio_Shared();
32
33  bool playing() const { return playing_; }
34
35  // Sets the callback information that the background thread will use. This
36  // is optional. Without a callback, the thread will not be run. This
37  // non-callback mode is used in the renderer with the proxy, since the proxy
38  // handles the callback entirely within the plugin process.
39  void SetCallback(PPB_Audio_Callback callback, void* user_data);
40
41  // Configures the current state to be playing or not. The caller is
42  // responsible for ensuring the new state is the opposite of the current one.
43  //
44  // This is the implementation for PPB_Audio.Start/StopPlayback, except that
45  // it does not actually notify the audio system to stop playback, it just
46  // configures our object to stop generating callbacks. The actual stop
47  // playback request will be done in the derived classes and will be different
48  // from the proxy and the renderer.
49  void SetStartPlaybackState();
50  void SetStopPlaybackState();
51
52  // Sets the shared memory and socket handles. This will automatically start
53  // playback if we're currently set to play.
54  void SetStreamInfo(PP_Instance instance,
55                     base::SharedMemoryHandle shared_memory_handle,
56                     size_t shared_memory_size,
57                     base::SyncSocket::Handle socket_handle,
58                     int sample_frame_count);
59
60#if defined(OS_NACL)
61  // NaCl has a special API for IRT code to create threads that can call back
62  // into user code.
63  static void SetThreadFunctions(const struct PP_ThreadFunctions* functions);
64#endif
65
66 private:
67  // Starts execution of the audio thread.
68  void StartThread();
69
70  // Stop execution of the audio thread.
71  void StopThread();
72
73  // DelegateSimpleThread::Delegate implementation. Run on the audio thread.
74  virtual void Run();
75
76  // True if playing the stream.
77  bool playing_;
78
79  // Socket used to notify us when audio is ready to accept new samples. This
80  // pointer is created in StreamCreated().
81  scoped_ptr<base::CancelableSyncSocket> socket_;
82
83  // Sample buffer in shared memory. This pointer is created in
84  // StreamCreated(). The memory is only mapped when the audio thread is
85  // created.
86  scoped_ptr<base::SharedMemory> shared_memory_;
87
88  // The size of the sample buffer in bytes.
89  size_t shared_memory_size_;
90
91#if !defined(OS_NACL)
92  // When the callback is set, this thread is spawned for calling it.
93  scoped_ptr<base::DelegateSimpleThread> audio_thread_;
94#else
95  uintptr_t thread_id_;
96  bool thread_active_;
97
98  static void CallRun(void* self);
99#endif
100
101  // Callback to call when audio is ready to accept new samples.
102  PPB_Audio_Callback callback_;
103
104  // User data pointer passed verbatim to the callback function.
105  void* user_data_;
106
107  // AudioBus for shuttling data across the shared memory.
108  scoped_ptr<media::AudioBus> audio_bus_;
109
110  // Internal buffer for client's integer audio data.
111  int client_buffer_size_bytes_;
112  scoped_ptr<uint8_t[]> client_buffer_;
113
114  DISALLOW_COPY_AND_ASSIGN(PPB_Audio_Shared);
115};
116
117}  // namespace ppapi
118
119#endif  // PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_
120