Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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_TEST_WEBRTC_AUDIO_DEVICE_TEST_H_ |
| 6 | #define CONTENT_TEST_WEBRTC_AUDIO_DEVICE_TEST_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
| 12 | #include "base/memory/scoped_ptr.h" |
Ben Murdoch | 9ab5563 | 2013-07-18 11:57:30 +0100 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 14 | #include "content/browser/renderer_host/media/mock_media_observer.h" |
| 15 | #include "content/public/renderer/content_renderer_client.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 16 | #include "ipc/ipc_listener.h" |
| 17 | #include "media/base/audio_hardware_config.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | #include "media/base/channel_layout.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | #include "third_party/webrtc/common_types.h" |
| 21 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 22 | namespace IPC { |
| 23 | class Channel; |
| 24 | } |
| 25 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 26 | namespace media { |
| 27 | class AudioManager; |
| 28 | } |
| 29 | |
| 30 | namespace net { |
| 31 | class URLRequestContext; |
| 32 | } |
| 33 | |
| 34 | namespace webrtc { |
| 35 | class VoENetwork; |
| 36 | } |
| 37 | |
| 38 | #if defined(OS_WIN) |
| 39 | namespace base { |
| 40 | namespace win { |
| 41 | class ScopedCOMInitializer; |
| 42 | } |
| 43 | } |
| 44 | #endif |
| 45 | |
| 46 | namespace content { |
| 47 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 48 | class AudioMirroringManager; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 49 | class ContentRendererClient; |
| 50 | class MediaStreamManager; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 51 | class RenderThreadImpl; |
| 52 | class ResourceContext; |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame^] | 53 | class TestAudioInputRendererHost; |
| 54 | class TestAudioRendererHost; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 55 | class TestBrowserThread; |
Torne (Richard Coles) | 58537e2 | 2013-09-12 12:10:22 +0100 | [diff] [blame] | 56 | class WebRtcAudioRenderer; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 57 | class WebRTCMockRenderProcess; |
| 58 | |
| 59 | // Scoped class for WebRTC interfaces. Fetches the wrapped interface |
| 60 | // in the constructor via WebRTC's GetInterface mechanism and then releases |
| 61 | // the reference in the destructor. |
| 62 | template<typename T> |
| 63 | class ScopedWebRTCPtr { |
| 64 | public: |
| 65 | template<typename Engine> |
| 66 | explicit ScopedWebRTCPtr(Engine* e) |
| 67 | : ptr_(T::GetInterface(e)) {} |
| 68 | explicit ScopedWebRTCPtr(T* p) : ptr_(p) {} |
| 69 | ~ScopedWebRTCPtr() { reset(); } |
| 70 | T* operator->() const { return ptr_; } |
| 71 | T* get() const { return ptr_; } |
| 72 | |
| 73 | // Releases the current pointer. |
| 74 | void reset() { |
| 75 | if (ptr_) { |
| 76 | ptr_->Release(); |
| 77 | ptr_ = NULL; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool valid() const { return ptr_ != NULL; } |
| 82 | |
| 83 | private: |
| 84 | T* ptr_; |
| 85 | }; |
| 86 | |
| 87 | // Wrapper to automatically calling T::Delete in the destructor. |
| 88 | // This is useful for some WebRTC objects that have their own Create/Delete |
| 89 | // methods and we can't use our our scoped_* classes. |
| 90 | template <typename T> |
| 91 | class WebRTCAutoDelete { |
| 92 | public: |
| 93 | WebRTCAutoDelete() : ptr_(NULL) {} |
| 94 | explicit WebRTCAutoDelete(T* ptr) : ptr_(ptr) {} |
| 95 | ~WebRTCAutoDelete() { reset(); } |
| 96 | |
| 97 | void reset() { |
| 98 | if (ptr_) { |
| 99 | T::Delete(ptr_); |
| 100 | ptr_ = NULL; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | T* operator->() { return ptr_; } |
| 105 | T* get() const { return ptr_; } |
| 106 | |
| 107 | bool valid() const { return ptr_ != NULL; } |
| 108 | |
| 109 | protected: |
| 110 | T* ptr_; |
| 111 | }; |
| 112 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 113 | // Implemented and defined in the cc file. |
| 114 | class ReplaceContentClientRenderer; |
| 115 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 116 | // Temporarily disabled in LeakSanitizer builds due to memory leaks. |
| 117 | // http://crbug.com/148865 |
| 118 | #if defined(LEAK_SANITIZER) |
| 119 | #define MAYBE_WebRTCAudioDeviceTest DISABLED_WebRTCAudioDeviceTest |
| 120 | #else |
| 121 | #define MAYBE_WebRTCAudioDeviceTest WebRTCAudioDeviceTest |
| 122 | #endif |
| 123 | |
| 124 | class MAYBE_WebRTCAudioDeviceTest : public ::testing::Test, |
| 125 | public IPC::Listener { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 126 | public: |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 127 | MAYBE_WebRTCAudioDeviceTest(); |
| 128 | virtual ~MAYBE_WebRTCAudioDeviceTest(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 129 | |
| 130 | virtual void SetUp() OVERRIDE; |
| 131 | virtual void TearDown() OVERRIDE; |
| 132 | |
| 133 | // Sends an IPC message to the IO thread channel. |
| 134 | bool Send(IPC::Message* message); |
| 135 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 136 | void SetAudioHardwareConfig(media::AudioHardwareConfig* hardware_config); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 137 | |
Torne (Richard Coles) | 58537e2 | 2013-09-12 12:10:22 +0100 | [diff] [blame] | 138 | scoped_refptr<WebRtcAudioRenderer> CreateDefaultWebRtcAudioRenderer( |
| 139 | int render_view_id); |
| 140 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 141 | protected: |
| 142 | void InitializeIOThread(const char* thread_name); |
| 143 | void UninitializeIOThread(); |
| 144 | void CreateChannel(const char* name); |
| 145 | void DestroyChannel(); |
| 146 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 147 | void OnGetAudioHardwareConfig(media::AudioParameters* input_params, |
| 148 | media::AudioParameters* output_params); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 149 | |
| 150 | // IPC::Listener implementation. |
| 151 | virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 152 | |
| 153 | // Posts a final task to the IO message loop and waits for completion. |
| 154 | void WaitForIOThreadCompletion(); |
| 155 | void WaitForAudioManagerCompletion(); |
| 156 | void WaitForMessageLoopCompletion(base::MessageLoopProxy* loop); |
| 157 | |
| 158 | // Convenience getter for gmock. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 159 | MockMediaInternals& media_observer() const { |
| 160 | return *media_internals_.get(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 163 | std::string GetTestDataPath(const base::FilePath::StringType& file_name); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 164 | |
| 165 | scoped_ptr<ReplaceContentClientRenderer> saved_content_renderer_; |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 166 | base::MessageLoopForUI message_loop_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 167 | ContentRendererClient content_renderer_client_; |
| 168 | RenderThreadImpl* render_thread_; // Owned by mock_process_. |
| 169 | scoped_ptr<WebRTCMockRenderProcess> mock_process_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 170 | scoped_ptr<MockMediaInternals> media_internals_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 171 | scoped_ptr<MediaStreamManager> media_stream_manager_; |
| 172 | scoped_ptr<media::AudioManager> audio_manager_; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 173 | scoped_ptr<AudioMirroringManager> mirroring_manager_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 174 | scoped_ptr<net::URLRequestContext> test_request_context_; |
| 175 | scoped_ptr<ResourceContext> resource_context_; |
| 176 | scoped_ptr<IPC::Channel> channel_; |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame^] | 177 | scoped_refptr<TestAudioRendererHost> audio_render_host_; |
| 178 | scoped_refptr<TestAudioInputRendererHost> audio_input_renderer_host_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 179 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 180 | media::AudioHardwareConfig* audio_hardware_config_; // Weak reference. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 181 | |
| 182 | // Initialized on the main test thread that we mark as the UI thread. |
| 183 | scoped_ptr<TestBrowserThread> ui_thread_; |
| 184 | // Initialized on our IO thread to satisfy BrowserThread::IO checks. |
| 185 | scoped_ptr<TestBrowserThread> io_thread_; |
| 186 | |
| 187 | #if defined(OS_WIN) |
| 188 | // COM initialization on the IO thread. |
| 189 | scoped_ptr<base::win::ScopedCOMInitializer> initialize_com_; |
| 190 | #endif |
| 191 | |
| 192 | // These are initialized when we set up our IO thread. |
| 193 | bool has_input_devices_; |
| 194 | bool has_output_devices_; |
| 195 | |
| 196 | // The previous state for whether sandbox support was enabled in |
| 197 | // RenderViewWebKitPlatformSupportImpl. |
| 198 | bool sandbox_was_enabled_; |
| 199 | }; |
| 200 | |
| 201 | // A very basic implementation of webrtc::Transport that acts as a transport |
| 202 | // but just forwards all calls to a local webrtc::VoENetwork implementation. |
| 203 | // Ownership of the VoENetwork object lies outside the class. |
| 204 | class WebRTCTransportImpl : public webrtc::Transport { |
| 205 | public: |
| 206 | explicit WebRTCTransportImpl(webrtc::VoENetwork* network); |
| 207 | virtual ~WebRTCTransportImpl(); |
| 208 | |
| 209 | virtual int SendPacket(int channel, const void* data, int len) OVERRIDE; |
| 210 | virtual int SendRTCPPacket(int channel, const void* data, int len) OVERRIDE; |
| 211 | |
| 212 | private: |
| 213 | webrtc::VoENetwork* network_; |
| 214 | }; |
| 215 | |
| 216 | } // namespace content |
| 217 | |
| 218 | #endif // CONTENT_TEST_WEBRTC_AUDIO_DEVICE_TEST_H_ |