Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(674)

Side by Side Diff: content/test/webrtc_audio_device_test.h

Issue 8427031: First unit tests for WebRTCAudioDevice. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Latest comments addressed Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
## -0,0 +1 ##
+LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 #pragma once
8
9 #include "base/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "content/browser/renderer_host/media/mock_media_observer.h"
13 #include "content/renderer/media/audio_renderer_impl.h"
14 #include "content/renderer/mock_content_renderer_client.h"
15 #include "ipc/ipc_channel.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/webrtc/common_types.h"
18
19 class AudioInputRendererHost;
20 class AudioRendererHost;
21 class RenderThreadImpl;
22 class WebRTCMockRenderProcess;
23
24 namespace content {
25 class ContentRendererClient;
26 class ResourceContext;
27 class TestBrowserThread;
28 }
29
30 namespace net {
31 class URLRequestContext;
32 }
33
34 namespace webrtc {
35 class VoENetwork;
36 }
37
38 // Scoped class for WebRTC interfaces. Fetches the wrapped interface
39 // in the constructor via WebRTC's GetInterface mechanism and then releases
40 // the reference in the destructor.
41 template<typename T>
42 class ScopedWebRTCPtr {
43 public:
44 template<typename Engine>
45 explicit ScopedWebRTCPtr(Engine* e)
46 : ptr_(T::GetInterface(e)) {}
47 explicit ScopedWebRTCPtr(T* p) : ptr_(p) {}
48 ~ScopedWebRTCPtr() { reset(); }
49 T* operator->() const { return ptr_; }
50 T* get() const { return ptr_; }
51
52 // Releases the current pointer.
53 void reset() {
54 if (ptr_) {
55 ptr_->Release();
56 ptr_ = NULL;
57 }
58 }
59
60 bool valid() const { return ptr_ != NULL; }
61
62 private:
63 T* ptr_;
64 };
65
66 // Wrapper to automatically calling T::Delete in the destructor.
67 // This is useful for some WebRTC objects that have their own Create/Delete
68 // methods and we can't use our our scoped_* classes.
69 template <typename T>
70 class WebRTCAutoDelete {
71 public:
72 WebRTCAutoDelete() : ptr_(NULL) {}
73 explicit WebRTCAutoDelete(T* ptr) : ptr_(ptr) {}
74 ~WebRTCAutoDelete() { reset(); }
75
76 void reset() {
77 if (ptr_) {
78 T::Delete(ptr_);
79 ptr_ = NULL;
80 }
81 }
82
83 T* operator->() { return ptr_; }
84 T* get() const { return ptr_; }
85
86 bool valid() const { return ptr_ != NULL; }
87
88 protected:
89 T* ptr_;
90 };
91
92 // Individual tests can provide an implementation (or mock) of this interface
93 // when the audio code queries for hardware capabilities on the IO thread.
94 class AudioUtilInterface {
95 public:
96 virtual double GetAudioHardwareSampleRate() = 0;
97 virtual double GetAudioInputHardwareSampleRate() = 0;
98 };
99
100 // Utility scoped class to replace the global content client's renderer for the
101 // duration of the test.
102 class ReplaceContentClientRenderer;
103
104 class WebRTCAudioDeviceTest
105 : public ::testing::Test,
106 public IPC::Channel::Listener {
107 public:
108 class SetupTask : public base::RefCountedThreadSafe<SetupTask> {
109 public:
110 explicit SetupTask(WebRTCAudioDeviceTest* test) : test_(test) {
111 DCHECK(test); // Catch this early since we dereference much later.
112 }
113 void InitializeIOThread(const char* thread_name) {
114 test_->InitializeIOThread(thread_name);
115 }
116 void UninitializeIOThread() { test_->UninitializeIOThread(); }
117 protected:
118 WebRTCAudioDeviceTest* test_;
119 };
120
121 WebRTCAudioDeviceTest();
122 virtual ~WebRTCAudioDeviceTest();
123
124 virtual void SetUp();
125 virtual void TearDown();
126
127 // Sends an IPC message to the IO thread channel.
128 bool Send(IPC::Message* message);
129
130 void set_audio_util_callback(AudioUtilInterface* callback) {
131 audio_util_callback_ = callback;
132 }
133
134 protected:
135 void InitializeIOThread(const char* thread_name);
136 void UninitializeIOThread();
137 void CreateChannel(const char* name,
138 content::ResourceContext* resource_context);
139 void DestroyChannel();
140
141 void OnGetHardwareSampleRate(double* sample_rate);
142 void OnGetHardwareInputSampleRate(double* sample_rate);
143
144 // IPC::Channel::Listener implementation.
145 virtual bool OnMessageReceived(const IPC::Message& message);
146
147 // Posts a final task to the IO message loop and waits for completion.
148 void WaitForIOThreadCompletion();
149
150 // Convenience getter for gmock.
151 MockMediaObserver& media_observer() const {
152 return *media_observer_.get();
153 }
154
155 std::string GetTestDataPath(const FilePath::StringType& file_name);
156
157 // Actual implementation of the short and long PlayFile tests.
158 // If the |duration| parameter is 0, then the function determines the length
159 // of the audio file and plays the entire file. Otherwise, |duration|
160 // specifies the number of milliseconds to allow the file to play.
161 void PlayLocalFile(int duration);
162
163 scoped_ptr<ReplaceContentClientRenderer> saved_content_renderer_;
164 MessageLoopForUI message_loop_;
165 content::MockContentRendererClient mock_content_renderer_client_;
166 RenderThreadImpl* render_thread_; // owned by mock_process_
henrika (OOO until Aug 14) 2011/11/04 10:35:12 nit. You mix initial captial letter in short comme
tommi (sloooow) - chröme 2011/11/07 10:27:28 Done.
167 scoped_ptr<WebRTCMockRenderProcess> mock_process_;
168 //media::MockFilterHost host_;
169 base::WaitableEvent event_;
170 scoped_ptr<MockMediaObserver> media_observer_;
171 scoped_ptr<content::ResourceContext> resource_context_;
172 scoped_refptr<net::URLRequestContext> test_request_context_;
173 scoped_ptr<IPC::Channel> channel_;
174 scoped_refptr<AudioRendererHost> audio_render_host_;
175 AudioUtilInterface* audio_util_callback_; // Weak reference.
176
177 // Initialized on the main test thread that we mark as the UI thread.
178 scoped_ptr<content::TestBrowserThread> ui_thread_;
179 // Initialized on our IO thread to satisfy BrowserThread::IO checks.
180 scoped_ptr<content::TestBrowserThread> io_thread_;
181 };
182
183 // A very basic implementation of webrtc::Transport that acts as a transport
184 // but just forwards all calls to a local webrtc::VoENetwork implementation.
185 // Ownership of the VoENetwork object lies outside the class.
186 class WebRTCTransportImpl : public webrtc::Transport {
187 public:
188 explicit WebRTCTransportImpl(webrtc::VoENetwork* network);
189 virtual ~WebRTCTransportImpl();
190
191 virtual int SendPacket(int channel, const void* data, int len);
192 virtual int SendRTCPPacket(int channel, const void* data, int len);
193
194 private:
195 webrtc::VoENetwork* network_;
196 };
197
198 #endif // CONTENT_TEST_WEBRTC_AUDIO_DEVICE_TEST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698