1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
// Copyright (c) 2012 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 "base/bind.h"
#include "base/command_line.h"
#include "base/memory/scoped_ptr.h"
#include "base/run_loop.h"
#include "base/sync_socket.h"
#include "content/browser/media/capture/audio_mirroring_manager.h"
#include "content/browser/media/media_internals.h"
#include "content/browser/renderer_host/media/audio_input_device_manager.h"
#include "content/browser/renderer_host/media/audio_renderer_host.h"
#include "content/browser/renderer_host/media/media_stream_manager.h"
#include "content/common/media/audio_messages.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "ipc/ipc_message_utils.h"
#include "media/audio/audio_manager.h"
#include "media/base/bind_to_current_loop.h"
#include "media/base/media_switches.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::Assign;
using ::testing::DoAll;
using ::testing::NotNull;
namespace {
const int kRenderProcessId = 1;
const int kRenderViewId = 4;
const int kRenderFrameId = 5;
const int kStreamId = 50;
} // namespace
namespace content {
class MockAudioMirroringManager : public AudioMirroringManager {
public:
MockAudioMirroringManager() {}
virtual ~MockAudioMirroringManager() {}
MOCK_METHOD3(AddDiverter,
void(int render_process_id,
int render_frame_id,
Diverter* diverter));
MOCK_METHOD1(RemoveDiverter, void(Diverter* diverter));
private:
DISALLOW_COPY_AND_ASSIGN(MockAudioMirroringManager);
};
class MockAudioRendererHost : public AudioRendererHost {
public:
MockAudioRendererHost(media::AudioManager* audio_manager,
AudioMirroringManager* mirroring_manager,
MediaInternals* media_internals,
MediaStreamManager* media_stream_manager)
: AudioRendererHost(kRenderProcessId,
audio_manager,
mirroring_manager,
media_internals,
media_stream_manager),
shared_memory_length_(0) {}
// A list of mock methods.
MOCK_METHOD2(OnStreamCreated, void(int stream_id, int length));
MOCK_METHOD1(OnStreamPlaying, void(int stream_id));
MOCK_METHOD1(OnStreamPaused, void(int stream_id));
MOCK_METHOD1(OnStreamError, void(int stream_id));
private:
virtual ~MockAudioRendererHost() {
// Make sure all audio streams have been deleted.
EXPECT_TRUE(audio_entries_.empty());
}
// This method is used to dispatch IPC messages to the renderer. We intercept
// these messages here and dispatch to our mock methods to verify the
// conversation between this object and the renderer.
virtual bool Send(IPC::Message* message) {
CHECK(message);
// In this method we dispatch the messages to the according handlers as if
// we are the renderer.
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(MockAudioRendererHost, *message)
IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamCreated,
OnNotifyStreamCreated)
IPC_MESSAGE_HANDLER(AudioMsg_NotifyStreamStateChanged,
OnNotifyStreamStateChanged)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
EXPECT_TRUE(handled);
delete message;
return true;
}
void OnNotifyStreamCreated(
int stream_id, base::SharedMemoryHandle handle,
base::SyncSocket::TransitDescriptor socket_descriptor, uint32 length) {
// Maps the shared memory.
shared_memory_.reset(new base::SharedMemory(handle, false));
CHECK(shared_memory_->Map(length));
CHECK(shared_memory_->memory());
shared_memory_length_ = length;
// Create the SyncSocket using the handle.
base::SyncSocket::Handle sync_socket_handle =
base::SyncSocket::UnwrapHandle(socket_descriptor);
sync_socket_.reset(new base::SyncSocket(sync_socket_handle));
// And then delegate the call to the mock method.
OnStreamCreated(stream_id, length);
}
void OnNotifyStreamStateChanged(int stream_id,
media::AudioOutputIPCDelegate::State state) {
switch (state) {
case media::AudioOutputIPCDelegate::kPlaying:
OnStreamPlaying(stream_id);
break;
case media::AudioOutputIPCDelegate::kPaused:
OnStreamPaused(stream_id);
break;
case media::AudioOutputIPCDelegate::kError:
OnStreamError(stream_id);
break;
default:
FAIL() << "Unknown stream state";
break;
}
}
scoped_ptr<base::SharedMemory> shared_memory_;
scoped_ptr<base::SyncSocket> sync_socket_;
uint32 shared_memory_length_;
DISALLOW_COPY_AND_ASSIGN(MockAudioRendererHost);
};
class AudioRendererHostTest : public testing::Test {
public:
AudioRendererHostTest() {
audio_manager_.reset(media::AudioManager::CreateForTesting());
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kUseFakeDeviceForMediaStream);
media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
host_ = new MockAudioRendererHost(audio_manager_.get(),
&mirroring_manager_,
MediaInternals::GetInstance(),
media_stream_manager_.get());
// Simulate IPC channel connected.
host_->set_peer_pid_for_testing(base::GetCurrentProcId());
}
~AudioRendererHostTest() override {
// Simulate closing the IPC channel and give the audio thread time to close
// the underlying streams.
host_->OnChannelClosing();
SyncWithAudioThread();
// Release the reference to the mock object. The object will be destructed
// on message_loop_.
host_ = NULL;
}
protected:
void Create(bool unified_stream) {
EXPECT_CALL(*host_.get(), OnStreamCreated(kStreamId, _));
EXPECT_CALL(mirroring_manager_,
AddDiverter(kRenderProcessId, kRenderFrameId, NotNull()))
.RetiresOnSaturation();
// Send a create stream message to the audio output stream and wait until
// we receive the created message.
int session_id;
media::AudioParameters params;
if (unified_stream) {
// Use AudioInputDeviceManager::kFakeOpenSessionId as the session id to
// pass the permission check.
session_id = AudioInputDeviceManager::kFakeOpenSessionId;
params = media::AudioParameters(
media::AudioParameters::AUDIO_FAKE,
media::CHANNEL_LAYOUT_STEREO,
media::AudioParameters::kAudioCDSampleRate, 16,
media::AudioParameters::kAudioCDSampleRate / 10,
media::AudioParameters::NO_EFFECTS);
} else {
session_id = 0;
params = media::AudioParameters(
media::AudioParameters::AUDIO_FAKE,
media::CHANNEL_LAYOUT_STEREO,
media::AudioParameters::kAudioCDSampleRate, 16,
media::AudioParameters::kAudioCDSampleRate / 10);
}
host_->OnCreateStream(kStreamId, kRenderViewId, kRenderFrameId, session_id,
params);
// At some point in the future, a corresponding RemoveDiverter() call must
// be made.
EXPECT_CALL(mirroring_manager_, RemoveDiverter(NotNull()))
.RetiresOnSaturation();
SyncWithAudioThread();
}
void Close() {
// Send a message to AudioRendererHost to tell it we want to close the
// stream.
host_->OnCloseStream(kStreamId);
SyncWithAudioThread();
}
void Play() {
EXPECT_CALL(*host_.get(), OnStreamPlaying(kStreamId));
host_->OnPlayStream(kStreamId);
SyncWithAudioThread();
}
void Pause() {
EXPECT_CALL(*host_.get(), OnStreamPaused(kStreamId));
host_->OnPauseStream(kStreamId);
SyncWithAudioThread();
}
void SetVolume(double volume) {
host_->OnSetVolume(kStreamId, volume);
SyncWithAudioThread();
}
void SimulateError() {
EXPECT_EQ(1u, host_->audio_entries_.size())
<< "Calls Create() before calling this method";
// Expect an error signal sent through IPC.
EXPECT_CALL(*host_.get(), OnStreamError(kStreamId));
// Simulate an error sent from the audio device.
host_->ReportErrorAndClose(kStreamId);
SyncWithAudioThread();
// Expect the audio stream record is removed.
EXPECT_EQ(0u, host_->audio_entries_.size());
}
// SyncWithAudioThread() waits until all pending tasks on the audio thread
// are executed while also processing pending task in message_loop_ on the
// current thread. It is used to synchronize with the audio thread when we are
// closing an audio stream.
void SyncWithAudioThread() {
base::RunLoop().RunUntilIdle();
base::RunLoop run_loop;
audio_manager_->GetTaskRunner()->PostTask(
FROM_HERE, media::BindToCurrentLoop(run_loop.QuitClosure()));
run_loop.Run();
}
private:
// MediaStreamManager uses a DestructionObserver, so it must outlive the
// TestBrowserThreadBundle.
scoped_ptr<MediaStreamManager> media_stream_manager_;
TestBrowserThreadBundle thread_bundle_;
scoped_ptr<media::AudioManager> audio_manager_;
MockAudioMirroringManager mirroring_manager_;
scoped_refptr<MockAudioRendererHost> host_;
DISALLOW_COPY_AND_ASSIGN(AudioRendererHostTest);
};
TEST_F(AudioRendererHostTest, CreateAndClose) {
Create(false);
Close();
}
// Simulate the case where a stream is not properly closed.
TEST_F(AudioRendererHostTest, CreateAndShutdown) {
Create(false);
}
TEST_F(AudioRendererHostTest, CreatePlayAndClose) {
Create(false);
Play();
Close();
}
TEST_F(AudioRendererHostTest, CreatePlayPauseAndClose) {
Create(false);
Play();
Pause();
Close();
}
TEST_F(AudioRendererHostTest, SetVolume) {
Create(false);
SetVolume(0.5);
Play();
Pause();
Close();
}
// Simulate the case where a stream is not properly closed.
TEST_F(AudioRendererHostTest, CreatePlayAndShutdown) {
Create(false);
Play();
}
// Simulate the case where a stream is not properly closed.
TEST_F(AudioRendererHostTest, CreatePlayPauseAndShutdown) {
Create(false);
Play();
Pause();
}
TEST_F(AudioRendererHostTest, SimulateError) {
Create(false);
Play();
SimulateError();
}
// Simulate the case when an error is generated on the browser process,
// the audio device is closed but the render process try to close the
// audio stream again.
TEST_F(AudioRendererHostTest, SimulateErrorAndClose) {
Create(false);
Play();
SimulateError();
Close();
}
TEST_F(AudioRendererHostTest, CreateUnifiedStreamAndClose) {
Create(true);
Close();
}
// TODO(hclam): Add tests for data conversation in low latency mode.
} // namespace content
|