Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / test / webrtc_audio_device_test.h
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
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "content/browser/renderer_host/media/mock_media_observer.h"
14 #include "content/public/renderer/content_renderer_client.h"
15 #include "ipc/ipc_listener.h"
16 #include "media/base/audio_hardware_config.h"
17 #include "media/base/channel_layout.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/webrtc/common_types.h"
20
21 namespace IPC {
22 class Channel;
23 }
24
25 namespace media {
26 class AudioManager;
27 }
28
29 namespace net {
30 class URLRequestContext;
31 }
32
33 namespace webrtc {
34 class MediaStreamInterface;
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
48 class AudioMirroringManager;
49 class ContentRendererClient;
50 class MediaStreamManager;
51 class RenderThreadImpl;
52 class ResourceContext;
53 class TestAudioInputRendererHost;
54 class TestAudioRendererHost;
55 class TestBrowserThread;
56 class WebRtcAudioRenderer;
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
113 // Implemented and defined in the cc file.
114 class ReplaceContentClientRenderer;
115
116 // Temporarily disabled in LeakSanitizer builds due to memory leaks.
117 // http://crbug.com/148865
118 // Disabling all tests for now since they are flaky.
119 // http://crbug.com/167298
120 #define MAYBE_WebRTCAudioDeviceTest DISABLED_WebRTCAudioDeviceTest
121
122 class MAYBE_WebRTCAudioDeviceTest : public ::testing::Test,
123                                     public IPC::Listener {
124  public:
125   MAYBE_WebRTCAudioDeviceTest();
126   virtual ~MAYBE_WebRTCAudioDeviceTest();
127
128   virtual void SetUp() OVERRIDE;
129   virtual void TearDown() OVERRIDE;
130
131   // Sends an IPC message to the IO thread channel.
132   bool Send(IPC::Message* message);
133
134   void SetAudioHardwareConfig(media::AudioHardwareConfig* hardware_config);
135
136   scoped_refptr<WebRtcAudioRenderer> CreateDefaultWebRtcAudioRenderer(
137       int render_view_id,
138       const scoped_refptr<webrtc::MediaStreamInterface>& media_stream);
139
140  protected:
141   void InitializeIOThread(const char* thread_name);
142   void UninitializeIOThread();
143   void CreateChannel(const char* name);
144   void DestroyChannel();
145
146   void OnGetAudioHardwareConfig(media::AudioParameters* input_params,
147                                 media::AudioParameters* output_params);
148
149   // IPC::Listener implementation.
150   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
151
152   // Posts a final task to the IO message loop and waits for completion.
153   void WaitForIOThreadCompletion();
154   void WaitForAudioManagerCompletion();
155   void WaitForTaskRunnerCompletion(
156       const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
157
158   std::string GetTestDataPath(const base::FilePath::StringType& file_name);
159
160   scoped_ptr<ReplaceContentClientRenderer> saved_content_renderer_;
161   base::MessageLoopForUI message_loop_;
162   ContentRendererClient content_renderer_client_;
163   RenderThreadImpl* render_thread_;  // Owned by mock_process_.
164   scoped_ptr<WebRTCMockRenderProcess> mock_process_;
165   scoped_ptr<MediaStreamManager> media_stream_manager_;
166   scoped_ptr<media::AudioManager> audio_manager_;
167   scoped_ptr<AudioMirroringManager> mirroring_manager_;
168   scoped_ptr<net::URLRequestContext> test_request_context_;
169   scoped_ptr<ResourceContext> resource_context_;
170   scoped_ptr<IPC::Channel> channel_;
171   scoped_refptr<TestAudioRendererHost> audio_render_host_;
172   scoped_refptr<TestAudioInputRendererHost> audio_input_renderer_host_;
173
174   media::AudioHardwareConfig* audio_hardware_config_;  // Weak reference.
175
176   // Initialized on the main test thread that we mark as the UI thread.
177   scoped_ptr<TestBrowserThread> ui_thread_;
178   // Initialized on our IO thread to satisfy BrowserThread::IO checks.
179   scoped_ptr<TestBrowserThread> io_thread_;
180
181 #if defined(OS_WIN)
182   // COM initialization on the IO thread.
183   scoped_ptr<base::win::ScopedCOMInitializer> initialize_com_;
184 #endif
185
186   // These are initialized when we set up our IO thread.
187   bool has_input_devices_;
188   bool has_output_devices_;
189
190   // The previous state for whether sandbox support was enabled in
191   // RenderViewWebKitPlatformSupportImpl.
192   bool sandbox_was_enabled_;
193 };
194
195 // A very basic implementation of webrtc::Transport that acts as a transport
196 // but just forwards all calls to a local webrtc::VoENetwork implementation.
197 // Ownership of the VoENetwork object lies outside the class.
198 class WebRTCTransportImpl : public webrtc::Transport {
199  public:
200   explicit WebRTCTransportImpl(webrtc::VoENetwork* network);
201   virtual ~WebRTCTransportImpl();
202
203   virtual int SendPacket(int channel, const void* data, int len) OVERRIDE;
204   virtual int SendRTCPPacket(int channel, const void* data, int len) OVERRIDE;
205
206  private:
207   webrtc::VoENetwork* network_;
208 };
209
210 }  // namespace content
211
212 #endif  // CONTENT_TEST_WEBRTC_AUDIO_DEVICE_TEST_H_