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 CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_INPUT_H_ 6 #define CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_INPUT_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/weak_ptr.h" 15 #include "media/audio/audio_input_ipc.h" 16 #include "media/audio/audio_parameters.h" 17 18 class GURL; 19 20 namespace base { 21 class MessageLoopProxy; 22 } 23 24 namespace media { 25 class AudioParameters; 26 } 27 28 namespace content { 29 30 class PepperAudioInputHost; 31 class PepperMediaDeviceManager; 32 class RenderViewImpl; 33 34 // PepperPlatformAudioInput is operated on two threads: the main thread (the 35 // thread on which objects are created) and the I/O thread. All public methods, 36 // except the destructor, must be called on the main thread. The notifications 37 // to the users of this class (i.e. PepperAudioInputHost) are also sent on the 38 // main thread. Internally, this class sends audio input IPC messages and 39 // receives media::AudioInputIPCDelegate notifications on the I/O thread. 40 41 class PepperPlatformAudioInput 42 : public media::AudioInputIPCDelegate, 43 public base::RefCountedThreadSafe<PepperPlatformAudioInput> { 44 public: 45 // Factory function, returns NULL on failure. StreamCreated() will be called 46 // when the stream is created. 47 static PepperPlatformAudioInput* Create( 48 const base::WeakPtr<RenderViewImpl>& render_view, 49 const std::string& device_id, 50 const GURL& document_url, 51 int sample_rate, 52 int frames_per_buffer, 53 PepperAudioInputHost* client); 54 55 // Called on main thread. 56 void StartCapture(); 57 void StopCapture(); 58 // Closes the stream. Make sure to call this before the object is destructed. 59 void ShutDown(); 60 61 // media::AudioInputIPCDelegate. 62 virtual void OnStreamCreated(base::SharedMemoryHandle handle, 63 base::SyncSocket::Handle socket_handle, 64 int length, 65 int total_segments) OVERRIDE; 66 virtual void OnVolume(double volume) OVERRIDE; 67 virtual void OnStateChanged( 68 media::AudioInputIPCDelegate::State state) OVERRIDE; 69 virtual void OnIPCClosed() OVERRIDE; 70 71 protected: 72 virtual ~PepperPlatformAudioInput(); 73 74 private: 75 friend class base::RefCountedThreadSafe<PepperPlatformAudioInput>; 76 77 PepperPlatformAudioInput(); 78 79 bool Initialize( 80 const base::WeakPtr<RenderViewImpl>& render_view, 81 const std::string& device_id, 82 const GURL& document_url, 83 int sample_rate, 84 int frames_per_buffer, 85 PepperAudioInputHost* client); 86 87 // I/O thread backends to above functions. 88 void InitializeOnIOThread(int session_id); 89 void StartCaptureOnIOThread(); 90 void StopCaptureOnIOThread(); 91 void ShutDownOnIOThread(); 92 93 void OnDeviceOpened(int request_id, 94 bool succeeded, 95 const std::string& label); 96 void CloseDevice(); 97 void NotifyStreamCreationFailed(); 98 99 PepperMediaDeviceManager* GetMediaDeviceManager(); 100 101 // The client to notify when the stream is created. THIS MUST ONLY BE 102 // ACCESSED ON THE MAIN THREAD. 103 PepperAudioInputHost* client_; 104 105 // Used to send/receive IPC. THIS MUST ONLY BE ACCESSED ON THE 106 // I/O THREAD. 107 scoped_ptr<media::AudioInputIPC> ipc_; 108 109 scoped_refptr<base::MessageLoopProxy> main_message_loop_proxy_; 110 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 111 112 // THIS MUST ONLY BE ACCESSED ON THE MAIN THREAD. 113 base::WeakPtr<RenderViewImpl> render_view_; 114 115 // The unique ID to identify the opened device. THIS MUST ONLY BE ACCESSED ON 116 // THE MAIN THREAD. 117 std::string label_; 118 119 // Initialized on the main thread and accessed on the I/O thread afterwards. 120 media::AudioParameters params_; 121 122 // Whether we have tried to create an audio stream. THIS MUST ONLY BE ACCESSED 123 // ON THE I/O THREAD. 124 bool create_stream_sent_; 125 126 // Whether we have a pending request to open a device. We have to make sure 127 // there isn't any pending request before this object goes away. 128 // THIS MUST ONLY BE ACCESSED ON THE MAIN THREAD. 129 bool pending_open_device_; 130 // THIS MUST ONLY BE ACCESSED ON THE MAIN THREAD. 131 int pending_open_device_id_; 132 133 DISALLOW_COPY_AND_ASSIGN(PepperPlatformAudioInput); 134 }; 135 136 } // namespace content 137 138 #endif // CONTENT_RENDERER_PEPPER_PEPPER_PLATFORM_AUDIO_INPUT_H_ 139