blob: b6ee1eee3fc939a2d4869a2beb097041d075f2b4 [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
[email protected]3b63f8f42011-03-28 01:54:158#include "base/memory/scoped_ptr.h"
[email protected]5d84d012010-12-02 17:17:219#include "base/shared_memory.h"
10#include "base/sync_socket.h"
[email protected]ac9ba8fe2010-12-30 18:08:3611#include "base/threading/simple_thread.h"
[email protected]b9a59842011-01-15 01:04:0012#include "ppapi/c/ppb_audio.h"
[email protected]7f8b26b2011-08-18 15:41:0113#include "ppapi/shared_impl/resource.h"
[email protected]55cdf6052011-05-13 19:22:5314#include "ppapi/thunk/ppb_audio_api.h"
[email protected]5d84d012010-12-02 17:17:2115
[email protected]55cdf6052011-05-13 19:22:5316namespace ppapi {
[email protected]5d84d012010-12-02 17:17:2117
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]9a578392011-12-07 18:59:2721class PPAPI_SHARED_EXPORT PPB_Audio_Shared
[email protected]f0a04c42011-08-26 22:43:2022 : public thunk::PPB_Audio_API,
23 public base::DelegateSimpleThread::Delegate {
[email protected]5d84d012010-12-02 17:17:2124 public:
[email protected]9a578392011-12-07 18:59:2725 PPB_Audio_Shared();
26 virtual ~PPB_Audio_Shared();
[email protected]5d84d012010-12-02 17:17:2127
[email protected]028b90f62012-04-05 01:44:1328 // Keep in sync with media::AudioOutputController::kPauseMark.
29 static const int kPauseMark;
30
[email protected]5d84d012010-12-02 17:17:2131 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]96724682012-04-26 20:53:3752 void SetStreamInfo(PP_Instance instance,
53 base::SharedMemoryHandle shared_memory_handle,
[email protected]5d84d012010-12-02 17:17:2154 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]78994ab02010-12-08 18:06:4462 virtual void Run();
[email protected]5d84d012010-12-02 17:17:2163
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]9a578392011-12-07 18:59:2787
88 DISALLOW_COPY_AND_ASSIGN(PPB_Audio_Shared);
[email protected]5d84d012010-12-02 17:17:2189};
90
[email protected]55cdf6052011-05-13 19:22:5391} // namespace ppapi
[email protected]5d84d012010-12-02 17:17:2192
[email protected]9a578392011-12-07 18:59:2793#endif // PPAPI_SHARED_IMPL_PPB_AUDIO_SHARED_H_