OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 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 CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_OUTPUT_DEV_H_ |
| 6 #define CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_OUTPUT_DEV_H_ |
| 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "media/audio/audio_output_ipc.h" |
| 13 #include "media/base/audio_parameters.h" |
| 14 #include "media/base/output_device_info.h" |
| 15 |
| 16 namespace base { |
| 17 class OneShotTimer; |
| 18 class SingleThreadTaskRunner; |
| 19 } |
| 20 |
| 21 namespace { |
| 22 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 23 const int64_t kMaxAuthorizationTimeoutMs = 4000; |
| 24 #else |
| 25 const int64_t kMaxAuthorizationTimeoutMs = 0; // No timeout. |
| 26 #endif |
| 27 } |
| 28 |
| 29 namespace content { |
| 30 class PepperAudioOutputHost; |
| 31 |
| 32 // This class is used to support new PPAPI |PPB_AudioOutput_Dev|, while |
| 33 // |PepperPlatformAudioOutput| is to support old PPAPI |PPB_Audio|. |
| 34 class PepperPlatformAudioOutputDev |
| 35 : public media::AudioOutputIPCDelegate, |
| 36 public base::RefCountedThreadSafe<PepperPlatformAudioOutputDev> { |
| 37 public: |
| 38 // Factory function, returns NULL on failure. StreamCreated() will be called |
| 39 // when the stream is created. |
| 40 static PepperPlatformAudioOutputDev* Create(int render_frame_id, |
| 41 const std::string& device_id, |
| 42 const GURL& document_url, |
| 43 int sample_rate, |
| 44 int frames_per_buffer, |
| 45 PepperAudioOutputHost* client); |
| 46 |
| 47 // The following three methods are all called on main thread. |
| 48 |
| 49 // Request authorization to use the device. |
| 50 void RequestDeviceAuthorization(); |
| 51 |
| 52 // Starts the playback. Returns false on error or if called before the |
| 53 // stream is created or after the stream is closed. |
| 54 bool StartPlayback(); |
| 55 |
| 56 // Stops the playback. Returns false on error or if called before the stream |
| 57 // is created or after the stream is closed. |
| 58 bool StopPlayback(); |
| 59 |
| 60 // Sets the volume. Returns false on error or if called before the stream |
| 61 // is created or after the stream is closed. |
| 62 bool SetVolume(double volume); |
| 63 |
| 64 // Closes the stream. Make sure to call this before the object is |
| 65 // destructed. |
| 66 void ShutDown(); |
| 67 |
| 68 // media::AudioOutputIPCDelegate implementation. |
| 69 void OnError() override; |
| 70 void OnDeviceAuthorized(media::OutputDeviceStatus device_status, |
| 71 const media::AudioParameters& output_params, |
| 72 const std::string& matched_device_id) override; |
| 73 void OnStreamCreated(base::SharedMemoryHandle handle, |
| 74 base::SyncSocket::Handle socket_handle, |
| 75 int length) override; |
| 76 void OnIPCClosed() override; |
| 77 |
| 78 protected: |
| 79 ~PepperPlatformAudioOutputDev() override; |
| 80 |
| 81 private: |
| 82 enum State { |
| 83 IPC_CLOSED, // No more IPCs can take place. |
| 84 IDLE, // Not started. |
| 85 AUTHORIZING, // Sent device authorization request, waiting for reply. |
| 86 AUTHORIZED, // Successful device authorization received. |
| 87 CREATING_STREAM, // Waiting for OnStreamCreated() to be called back. |
| 88 PAUSED, // Paused. OnStreamCreated() has been called. Can Play()/Stop(). |
| 89 PLAYING, // Playing back. Can Pause()/Stop(). |
| 90 }; |
| 91 |
| 92 friend class base::RefCountedThreadSafe<PepperPlatformAudioOutputDev>; |
| 93 |
| 94 PepperPlatformAudioOutputDev(); |
| 95 PepperPlatformAudioOutputDev(int render_frame_id, |
| 96 const std::string& device_id, |
| 97 const GURL& document_url, |
| 98 base::TimeDelta authorization_timeout); |
| 99 |
| 100 // Creates audio stream. Used for new Pepper audio output interface |
| 101 // |PPB_AudioOutput_Dev|. |
| 102 bool Initialize(int sample_rate, |
| 103 int frames_per_buffer, |
| 104 PepperAudioOutputHost* client); |
| 105 |
| 106 void RequestDeviceAuthorizationOnIOThread(); |
| 107 void CreateStreamOnIOThread(const media::AudioParameters& params); |
| 108 void StartPlaybackOnIOThread(); |
| 109 void StopPlaybackOnIOThread(); |
| 110 void SetVolumeOnIOThread(double volume); |
| 111 void ShutDownOnIOThread(); |
| 112 |
| 113 void NotifyStreamCreationFailed(); |
| 114 |
| 115 PepperAudioOutputHost* client_; |
| 116 |
| 117 // Used to send/receive IPC. THIS MUST ONLY BE ACCESSED ON THE |
| 118 // I/O thread except to send messages and get the message loop. |
| 119 std::unique_ptr<media::AudioOutputIPC> ipc_; |
| 120 |
| 121 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; |
| 122 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 123 |
| 124 // The frame containing the Pepper widget. |
| 125 int render_frame_id_; |
| 126 |
| 127 // Initialized on the main thread and accessed on the I/O thread afterwards. |
| 128 media::AudioParameters params_; |
| 129 |
| 130 // Current state (must only be accessed from the IO thread). |
| 131 State state_; |
| 132 |
| 133 // State of Start() calls before OnDeviceAuthorized() is called. |
| 134 bool start_on_authorized_; |
| 135 |
| 136 // State of StartPlayback() calls before OnStreamCreated() is called. |
| 137 bool play_on_start_; |
| 138 |
| 139 // The media session ID used to identify which output device to be started. |
| 140 int session_id_; |
| 141 |
| 142 // ID of hardware output device to be used (provided session_id_ is zero) |
| 143 const std::string device_id_; |
| 144 const url::Origin security_origin_; |
| 145 |
| 146 // If |device_id_| is empty and |session_id_| is not, |matched_device_id_| is |
| 147 // received in OnDeviceAuthorized(). |
| 148 std::string matched_device_id_; |
| 149 |
| 150 base::WaitableEvent did_receive_auth_; |
| 151 media::AudioParameters output_params_; |
| 152 media::OutputDeviceStatus device_status_; |
| 153 |
| 154 const base::TimeDelta auth_timeout_; |
| 155 std::unique_ptr<base::OneShotTimer> auth_timeout_action_; |
| 156 |
| 157 DISALLOW_COPY_AND_ASSIGN(PepperPlatformAudioOutputDev); |
| 158 }; |
| 159 |
| 160 } // namespace content |
| 161 |
| 162 #endif // CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_OUTPUT_DEV_H_ |
OLD | NEW |