Index: content/test/webrtc_audio_device_test.cc |
=================================================================== |
--- content/test/webrtc_audio_device_test.cc (revision 0) |
+++ content/test/webrtc_audio_device_test.cc (working copy) |
@@ -0,0 +1,302 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/test/webrtc_audio_device_test.h" |
Paweł Hajdan Jr.
2011/11/02 18:00:24
Ooops, one thing I didn't initially notice is that
tommi (sloooow) - chröme
2011/11/03 07:56:52
You didn't notice before because this file wasn't
Paweł Hajdan Jr.
2011/11/04 07:51:32
Ah, I see. Do you expect more tests to use webrtc_
tommi (sloooow) - chröme
2011/11/04 08:59:00
More tests are going to be added but I don't know
|
+ |
+#include "base/bind.h" |
+#include "base/message_loop.h" |
+#include "base/synchronization/waitable_event.h" |
+#include "base/test/signaling_task.h" |
+#include "base/test/test_timeouts.h" |
+#include "content/browser/renderer_host/media/audio_renderer_host.h" |
+#include "content/browser/renderer_host/media/mock_media_observer.h" |
+#include "content/browser/resource_context.h" |
+#include "content/common/view_messages.h" |
+#include "content/public/common/content_paths.h" |
+#include "content/renderer/media/webrtc_audio_device_impl.h" |
+#include "content/renderer/render_process.h" |
+#include "content/renderer/render_thread_impl.h" |
+#include "content/test/test_browser_thread.h" |
+#include "net/url_request/url_request_test_util.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/webrtc/voice_engine/main/interface/voe_audio_processing.h" |
+#include "third_party/webrtc/voice_engine/main/interface/voe_base.h" |
+#include "third_party/webrtc/voice_engine/main/interface/voe_file.h" |
+#include "third_party/webrtc/voice_engine/main/interface/voe_network.h" |
+ |
+using testing::_; |
+using testing::InvokeWithoutArgs; |
+using testing::Return; |
+using testing::StrEq; |
+ |
+// This class is a mock of the child process singleton which is needed |
+// to be able to create a RenderThread object. |
+class WebRTCMockRenderProcess : public RenderProcess { |
+ public: |
+ WebRTCMockRenderProcess() {} |
+ virtual ~WebRTCMockRenderProcess() {} |
+ |
+ // RenderProcess implementation. |
+ virtual skia::PlatformCanvas* GetDrawingCanvas(TransportDIB** memory, |
+ const gfx::Rect& rect) { return NULL; } |
+ virtual void ReleaseTransportDIB(TransportDIB* memory) {} |
+ virtual bool UseInProcessPlugins() const { return false; } |
+ virtual bool HasInitializedMediaLibrary() const { return false; } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(WebRTCMockRenderProcess); |
+}; |
+ |
+namespace { |
+ |
+class WebRTCMockResourceContext : public content::ResourceContext { |
+ public: |
+ WebRTCMockResourceContext() {} |
+ virtual ~WebRTCMockResourceContext() {} |
+ virtual void EnsureInitialized() const OVERRIDE {} |
+}; |
+ |
+ACTION_P(QuitMessageLoop, loop_or_proxy) { |
+ LOG(WARNING) << __FUNCTION__; |
Paweł Hajdan Jr.
2011/11/02 18:00:24
nit: Isn't this cluttering the logs too much? Idea
tommi (sloooow) - chröme
2011/11/03 07:56:52
This has been removed. It was there just for my be
|
+ loop_or_proxy->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
+} |
+ |
+} // end namespace |
+ |
+WebRTCAudioDeviceTest::WebRTCAudioDeviceTest() |
+ : render_thread_(NULL), event_(false, false), audio_util_callback_(NULL) {} |
+WebRTCAudioDeviceTest::~WebRTCAudioDeviceTest() {} |
+ |
+void WebRTCAudioDeviceTest::SetUp() { |
+ // Set low latency mode, as it soon would be on by default. |
+ if (AudioRendererImpl::latency_type() != AudioRendererImpl::kLowLatency) |
+ AudioRendererImpl::set_latency_type(AudioRendererImpl::kLowLatency); |
+ |
+ DCHECK_EQ(AudioRendererImpl::kLowLatency, |
Paweł Hajdan Jr.
2011/11/02 18:00:24
nit: I think ASSERT_EQ would fit better (not crash
tommi (sloooow) - chröme
2011/11/03 07:56:52
agreed.
|
+ AudioRendererImpl::latency_type()); |
+ |
+ // This part sets up a RenderThread environment to ensure that |
+ // RenderThread::current() (<=> TLS pointer) is valid. |
+ // Main parts are inspired by the RenderViewFakeResourcesTest. |
+ // Note that, the IPC part is not utilized in this test. |
+ saved_content_renderer_ = content::GetContentClient()->renderer(); |
Paweł Hajdan Jr.
2011/11/02 18:00:24
Could you create a scoped object for this and put
tommi (sloooow) - chröme
2011/11/03 07:56:52
Will do. (I'm at home right now, will post an upda
|
+ content::GetContentClient()->set_renderer(&mock_content_renderer_client_); |
+ mock_process_.reset(new WebRTCMockRenderProcess()); |
+ ui_thread_.reset(new content::TestBrowserThread(BrowserThread::UI, |
+ MessageLoop::current())); |
+ |
+ // Construct the resource context on the UI thread. |
+ resource_context_.reset(new WebRTCMockResourceContext()); |
+ |
+ static const char kThreadName[] = "RenderThread"; |
+ ChildProcess::current()->io_message_loop()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&SetupTask::InitializeIOThread, new SetupTask(this), |
+ kThreadName)); |
+ WaitForIOThreadCompletion(); |
+ |
+ render_thread_ = new RenderThreadImpl(kThreadName); |
+ mock_process_->set_main_thread(render_thread_); |
+} |
+ |
+void WebRTCAudioDeviceTest::TearDown() { |
+ ChildProcess::current()->io_message_loop()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&SetupTask::UninitializeIOThread, new SetupTask(this))); |
+ WaitForIOThreadCompletion(); |
+ mock_process_.reset(); |
+ |
+ content::GetContentClient()->set_renderer(saved_content_renderer_); |
+} |
+ |
+bool WebRTCAudioDeviceTest::Send(IPC::Message* message) { |
+ return channel_->Send(message); |
+} |
+ |
+void WebRTCAudioDeviceTest::InitializeIOThread(const char* thread_name) { |
+ // Set the current thread as the IO thread. |
+ io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO, |
+ MessageLoop::current())); |
+ test_request_context_ = new TestURLRequestContext(); |
+ resource_context_->set_request_context(test_request_context_.get()); |
+ media_observer_.reset(new MockMediaObserver()); |
+ resource_context_->set_media_observer(media_observer_.get()); |
+ |
+ CreateChannel(thread_name, resource_context_.get()); |
+} |
+ |
+void WebRTCAudioDeviceTest::UninitializeIOThread() { |
+ DestroyChannel(); |
+ resource_context_.reset(); |
+ test_request_context_ = NULL; |
+} |
+ |
+void WebRTCAudioDeviceTest::CreateChannel(const char* name, |
+ content::ResourceContext* resource_context) { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ audio_render_host_ = new AudioRendererHost(resource_context); |
+ audio_render_host_->OnChannelConnected(base::GetCurrentProcId()); |
+ |
+ channel_.reset(new IPC::Channel(name, IPC::Channel::MODE_SERVER, this)); |
+ ASSERT_TRUE(channel_->Connect()); |
+ |
+ audio_render_host_->OnFilterAdded(channel_.get()); |
+} |
+ |
+void WebRTCAudioDeviceTest::DestroyChannel() { |
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ channel_.reset(); |
+ audio_render_host_ = NULL; |
+} |
+ |
+void WebRTCAudioDeviceTest::OnGetHardwareSampleRate(double* sample_rate) { |
+ DLOG_IF(WARNING, !audio_util_callback_) << "Can't get output sample rate"; |
+ *sample_rate = audio_util_callback_ ? |
+ audio_util_callback_->GetAudioHardwareSampleRate() : 0.0; |
+} |
+ |
+void WebRTCAudioDeviceTest::OnGetHardwareInputSampleRate(double* sample_rate) { |
+ DLOG_IF(WARNING, !audio_util_callback_) << "Can't get input sample rate"; |
Paweł Hajdan Jr.
2011/11/02 18:00:24
Should that possibly be EXCEPT_TRUE? I generally d
tommi (sloooow) - chröme
2011/11/03 07:56:52
Yes, that makes sense as things are right now.
|
+ *sample_rate = audio_util_callback_ ? |
+ audio_util_callback_->GetAudioInputHardwareSampleRate() : 0.0; |
+} |
+ |
+// IPC::Channel::Listener implementation. |
+bool WebRTCAudioDeviceTest::OnMessageReceived(const IPC::Message& message) { |
+ if (render_thread_) { |
+ IPC::ChannelProxy::MessageFilter* filter = |
+ render_thread_->audio_input_message_filter(); |
+ if (filter->OnMessageReceived(message)) |
+ return true; |
+ |
+ filter = render_thread_->audio_message_filter(); |
+ if (filter->OnMessageReceived(message)) |
+ return true; |
+ } |
+ |
+ if (audio_render_host_.get()) { |
+ bool message_was_ok = false; |
+ if (audio_render_host_->OnMessageReceived(message, &message_was_ok)) |
+ return true; |
+ } |
+ |
+ bool handled = true; |
+ bool message_is_ok = true; |
Paweł Hajdan Jr.
2011/11/02 18:00:24
Now could you EXPECT_TRUE(message_is_ok) after END
tommi (sloooow) - chröme
2011/11/03 07:56:52
Sure
|
+ IPC_BEGIN_MESSAGE_MAP_EX(WebRTCAudioDeviceTest, message, message_is_ok) |
+ IPC_MESSAGE_HANDLER(ViewHostMsg_GetHardwareSampleRate, |
+ OnGetHardwareSampleRate) |
+ IPC_MESSAGE_HANDLER(ViewHostMsg_GetHardwareInputSampleRate, |
+ OnGetHardwareInputSampleRate) |
+ IPC_MESSAGE_UNHANDLED(handled = false) |
+ IPC_END_MESSAGE_MAP_EX() |
+ |
+ if (!handled) { |
+ // We don't do a NOTREACHED/NOTIMPLEMENTED here since this is FYI |
Paweł Hajdan Jr.
2011/11/02 18:00:24
I think it'd be much simpler and cleaner to just s
tommi (sloooow) - chröme
2011/11/03 07:56:52
Right now there are 1-2 such cases for each unit t
|
+ // for the developer in case the test stops working. If new IPC messages |
+ // are added but not handled in the test for some reason, the test might |
+ // break. This DLOG can save time in tracking down why. |
+ DLOG(WARNING) << "Unhandled IPC message"; |
+ } |
+ |
+ return true; |
+} |
+ |
+// Posts a final task to the IO message loop and waits for completion. |
+void WebRTCAudioDeviceTest::WaitForIOThreadCompletion() { |
+ ChildProcess::current()->io_message_loop()->PostTask( |
+ FROM_HERE, new base::SignalingTask(&event_)); |
+ EXPECT_TRUE(event_.TimedWait( |
+ base::TimeDelta::FromMilliseconds(TestTimeouts::action_timeout_ms()))); |
+} |
+ |
+std::string WebRTCAudioDeviceTest::GetTestDataPath( |
+ const FilePath::StringType& file_name) { |
+ FilePath path; |
+ EXPECT_TRUE(PathService::Get(content::DIR_TEST_DATA, &path)); |
+ path = path.Append(file_name); |
+#ifdef OS_WIN |
+ return WideToUTF8(path.value()); |
+#else |
+ return path.value(); |
+#endif |
+} |
+ |
+void WebRTCAudioDeviceTest::PlayLocalFile(int duration) { |
+ EXPECT_GE(duration, 0); |
+ EXPECT_CALL(media_observer(), |
+ OnSetAudioStreamStatus(_, 1, StrEq("created"))).Times(1); |
+ |
+ EXPECT_CALL(media_observer(), |
+ OnSetAudioStreamPlaying(_, 1, true)).Times(1); |
+ |
+ // When the "closed" event is triggered, we end the test. |
+ EXPECT_CALL(media_observer(), |
+ OnSetAudioStreamStatus(_, 1, StrEq("closed"))) |
+ .WillOnce(QuitMessageLoop(message_loop_.message_loop_proxy())); |
+ |
+ EXPECT_CALL(media_observer(), |
+ OnDeleteAudioStream(_, 1)).Times(1); |
+ |
+ scoped_refptr<WebRtcAudioDeviceImpl> audio_device( |
+ new WebRtcAudioDeviceImpl()); |
+ audio_device->SetSessionId(1); |
+ |
+ WebRTCAutoDelete<webrtc::VoiceEngine> engine(webrtc::VoiceEngine::Create()); |
+ |
+ ScopedWebRTCPtr<webrtc::VoEBase> base(engine.get()); |
+ int err = base->Init(audio_device); |
+ EXPECT_EQ(0, err); |
+ if (err == 0) { |
+ ScopedWebRTCPtr<webrtc::VoEAudioProcessing> audio_processing(engine.get()); |
+ EXPECT_EQ(0, audio_processing->SetAgcStatus(true, |
+ webrtc::kAgcAdaptiveDigital)); |
+ |
+ int ch = base->CreateChannel(); |
+ EXPECT_NE(-1, ch); |
+ |
+ ScopedWebRTCPtr<webrtc::VoENetwork> network(engine.get()); |
+ scoped_ptr<WebRTCTransportImpl> transport( |
+ new WebRTCTransportImpl(network.get())); |
+ EXPECT_EQ(0, network->RegisterExternalTransport(ch, *transport.get())); |
+ EXPECT_EQ(0, base->StartReceive(ch)); |
+ EXPECT_EQ(0, base->StartPlayout(ch)); |
+ EXPECT_EQ(0, base->StartSend(ch)); |
+ |
+ std::string file_path( |
+ GetTestDataPath(FILE_PATH_LITERAL("speechmusic_mono_16kHz.pcm"))); |
+ |
+ ScopedWebRTCPtr<webrtc::VoEFile> file(engine.get()); |
+ if (duration == 0) { |
+ EXPECT_EQ(0, file->GetFileDuration(file_path.c_str(), duration, |
+ webrtc::kFileFormatPcm16kHzFile)); |
+ EXPECT_NE(0, duration); |
+ } |
+ |
+ EXPECT_EQ(0, file->StartPlayingFileLocally(ch, file_path.c_str(), false, |
+ webrtc::kFileFormatPcm16kHzFile)); |
+ |
+ message_loop_.PostDelayedTask(FROM_HERE, |
+ new MessageLoop::QuitTask(), duration); |
+ message_loop_.Run(); |
+ |
+ EXPECT_EQ(0, network->DeRegisterExternalTransport(ch)); |
+ } |
+} |
+ |
+WebRTCTransportImpl::WebRTCTransportImpl(webrtc::VoENetwork* network) |
+ : network_(network) { |
+} |
+ |
+WebRTCTransportImpl::~WebRTCTransportImpl() { |
+} |
+ |
+int WebRTCTransportImpl::SendPacket(int channel, const void* data, int len) { |
+ return network_->ReceivedRTPPacket(channel, data, len); |
+} |
+ |
+int WebRTCTransportImpl::SendRTCPPacket(int channel, const void* data, |
+ int len) { |
+ return network_->ReceivedRTCPPacket(channel, data, len); |
+} |
Property changes on: content/test/webrtc_audio_device_test.cc |
___________________________________________________________________ |
Added: svn:eol-style |
## -0,0 +1 ## |
+LF |