blob: 7f7995b3edbb781cf45570aa2011499dbcd1ba9d [file] [log] [blame]
[email protected]028b90f62012-04-05 01:44:131// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]5d84d012010-12-02 17:17:212// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]9a578392011-12-07 18:59:275#ifndef PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_
6#define PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_
[email protected]5d84d012010-12-02 17:17:217
avie029c4132015-12-23 06:45:228#include <stddef.h>
9#include <stdint.h>
10
dchengced92242016-04-07 00:00:1211#include <memory>
12
avie029c4132015-12-23 06:45:2213#include "base/macros.h"
[email protected]8299b712013-07-17 19:55:2314#include "base/memory/shared_memory.h"
[email protected]5d84d012010-12-02 17:17:2115#include "base/sync_socket.h"
[email protected]ac9ba8fe2010-12-30 18:08:3616#include "base/threading/simple_thread.h"
[email protected]dfc48e4c2012-09-05 13:39:2117#include "media/base/audio_bus.h"
[email protected]b9a59842011-01-15 01:04:0018#include "ppapi/c/ppb_audio.h"
[email protected]6b1511262013-09-06 21:46:3419#include "ppapi/c/ppb_audio_config.h"
[email protected]7f8b26b2011-08-18 15:41:0120#include "ppapi/shared_impl/resource.h"
[email protected]55cdf6052011-05-13 19:22:5321#include "ppapi/thunk/ppb_audio_api.h"
[email protected]5d84d012010-12-02 17:17:2122
[email protected]d40ece12014-04-23 16:11:3923struct PP_ThreadFunctions;
[email protected]420ade42012-07-25 21:47:3124
[email protected]55cdf6052011-05-13 19:22:5325namespace ppapi {
[email protected]5d84d012010-12-02 17:17:2126
[email protected]6b1511262013-09-06 21:46:3427class PPAPI_SHARED_EXPORT AudioCallbackCombined {
28 public:
29 AudioCallbackCombined();
30 explicit AudioCallbackCombined(PPB_Audio_Callback_1_0 callback_1_0);
31 explicit AudioCallbackCombined(PPB_Audio_Callback callback);
32
33 ~AudioCallbackCombined();
34
35 bool IsValid() const;
36
37 void Run(void* sample_buffer,
38 uint32_t buffer_size_in_bytes,
39 PP_TimeDelta latency,
40 void* user_data) const;
41
42 private:
43 PPB_Audio_Callback_1_0 callback_1_0_;
44 PPB_Audio_Callback callback_;
45};
46
[email protected]5d84d012010-12-02 17:17:2147// Implements the logic to map shared memory and run the audio thread signaled
48// from the sync socket. Both the proxy and the renderer implementation use
49// this code.
[email protected]9a578392011-12-07 18:59:2750class PPAPI_SHARED_EXPORT PPB_Audio_Shared
[email protected]f0a04c42011-08-26 22:43:2051 : public thunk::PPB_Audio_API,
52 public base::DelegateSimpleThread::Delegate {
[email protected]5d84d012010-12-02 17:17:2153 public:
[email protected]9a578392011-12-07 18:59:2754 PPB_Audio_Shared();
55 virtual ~PPB_Audio_Shared();
[email protected]5d84d012010-12-02 17:17:2156
57 bool playing() const { return playing_; }
58
59 // Sets the callback information that the background thread will use. This
60 // is optional. Without a callback, the thread will not be run. This
61 // non-callback mode is used in the renderer with the proxy, since the proxy
62 // handles the callback entirely within the plugin process.
[email protected]6b1511262013-09-06 21:46:3463 void SetCallback(const AudioCallbackCombined& callback, void* user_data);
[email protected]5d84d012010-12-02 17:17:2164
65 // Configures the current state to be playing or not. The caller is
66 // responsible for ensuring the new state is the opposite of the current one.
67 //
68 // This is the implementation for PPB_Audio.Start/StopPlayback, except that
69 // it does not actually notify the audio system to stop playback, it just
70 // configures our object to stop generating callbacks. The actual stop
71 // playback request will be done in the derived classes and will be different
72 // from the proxy and the renderer.
73 void SetStartPlaybackState();
74 void SetStopPlaybackState();
75
76 // Sets the shared memory and socket handles. This will automatically start
77 // playback if we're currently set to play.
[email protected]96724682012-04-26 20:53:3778 void SetStreamInfo(PP_Instance instance,
79 base::SharedMemoryHandle shared_memory_handle,
[email protected]5d84d012010-12-02 17:17:2180 size_t shared_memory_size,
[email protected]dfc48e4c2012-09-05 13:39:2181 base::SyncSocket::Handle socket_handle,
[email protected]6b1511262013-09-06 21:46:3482 PP_AudioSampleRate sample_rate,
[email protected]dfc48e4c2012-09-05 13:39:2183 int sample_frame_count);
[email protected]5d84d012010-12-02 17:17:2184
[email protected]a4a01e42014-04-21 10:36:2185 // Returns whether a thread can be created on the client context.
86 // In trusted plugin, this should always return true, as it uses Chrome's
87 // thread library. In NaCl plugin, this returns whether SetThreadFunctions
88 // was invoked properly.
89 static bool IsThreadFunctionReady();
90
[email protected]d40ece12014-04-23 16:11:3991 // Configures this class to run in a NaCl plugin.
92 // If called, SetThreadFunctions() must be called before calling
93 // SetStartPlaybackState() on any instance of this class.
94 static void SetNaClMode();
95
[email protected]420ade42012-07-25 21:47:3196 // NaCl has a special API for IRT code to create threads that can call back
97 // into user code.
98 static void SetThreadFunctions(const struct PP_ThreadFunctions* functions);
[email protected]246fc492012-08-27 20:28:1899
[email protected]5d84d012010-12-02 17:17:21100 private:
101 // Starts execution of the audio thread.
102 void StartThread();
103
[email protected]0e8d17b12012-05-15 21:32:25104 // Stop execution of the audio thread.
105 void StopThread();
106
[email protected]5d84d012010-12-02 17:17:21107 // DelegateSimpleThread::Delegate implementation. Run on the audio thread.
[email protected]78994ab02010-12-08 18:06:44108 virtual void Run();
[email protected]5d84d012010-12-02 17:17:21109
110 // True if playing the stream.
111 bool playing_;
112
113 // Socket used to notify us when audio is ready to accept new samples. This
114 // pointer is created in StreamCreated().
dchengced92242016-04-07 00:00:12115 std::unique_ptr<base::CancelableSyncSocket> socket_;
[email protected]5d84d012010-12-02 17:17:21116
117 // Sample buffer in shared memory. This pointer is created in
118 // StreamCreated(). The memory is only mapped when the audio thread is
119 // created.
dchengced92242016-04-07 00:00:12120 std::unique_ptr<base::SharedMemory> shared_memory_;
[email protected]5d84d012010-12-02 17:17:21121
122 // The size of the sample buffer in bytes.
123 size_t shared_memory_size_;
124
125 // When the callback is set, this thread is spawned for calling it.
dchengced92242016-04-07 00:00:12126 std::unique_ptr<base::DelegateSimpleThread> audio_thread_;
[email protected]d40ece12014-04-23 16:11:39127 uintptr_t nacl_thread_id_;
128 bool nacl_thread_active_;
[email protected]420ade42012-07-25 21:47:31129
130 static void CallRun(void* self);
[email protected]5d84d012010-12-02 17:17:21131
132 // Callback to call when audio is ready to accept new samples.
[email protected]6b1511262013-09-06 21:46:34133 AudioCallbackCombined callback_;
[email protected]5d84d012010-12-02 17:17:21134
135 // User data pointer passed verbatim to the callback function.
136 void* user_data_;
[email protected]9a578392011-12-07 18:59:27137
[email protected]dfc48e4c2012-09-05 13:39:21138 // AudioBus for shuttling data across the shared memory.
dchengced92242016-04-07 00:00:12139 std::unique_ptr<media::AudioBus> audio_bus_;
[email protected]dfc48e4c2012-09-05 13:39:21140
141 // Internal buffer for client's integer audio data.
142 int client_buffer_size_bytes_;
dchengced92242016-04-07 00:00:12143 std::unique_ptr<uint8_t[]> client_buffer_;
[email protected]dfc48e4c2012-09-05 13:39:21144
[email protected]6b1511262013-09-06 21:46:34145 // The size (in bytes) of one second of audio data. Used to calculate latency.
146 size_t bytes_per_second_;
147
[email protected]1caf0aa2013-10-31 07:58:02148 // Buffer index used to coordinate with the browser side audio receiver.
149 uint32_t buffer_index_;
150
[email protected]9a578392011-12-07 18:59:27151 DISALLOW_COPY_AND_ASSIGN(PPB_Audio_Shared);
[email protected]5d84d012010-12-02 17:17:21152};
153
[email protected]55cdf6052011-05-13 19:22:53154} // namespace ppapi
[email protected]5d84d012010-12-02 17:17:21155
[email protected]9a578392011-12-07 18:59:27156#endif // PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_