Commit 8c97dbc3 authored by Jonas Olsson's avatar Jonas Olsson Committed by Commit Bot

Audio-service-based media::AudioInputIPC, to make AudioInputDevice work with the audio service.

Internally it uses a new, slightly simplified copy of MojoAudioInputIPC.

Bug: 834702
Change-Id: I878ada69d82c72a431b7fdfe1905ae79b3d8963d
Reviewed-on: https://chromium-review.googlesource.com/1041686Reviewed-by: default avatarOlga Sharonova <olka@chromium.org>
Reviewed-by: default avatarMax Morin <maxmorin@chromium.org>
Commit-Queue: Jonas Olsson <jonasolsson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#557545}
parent fa9130fb
...@@ -87,6 +87,7 @@ source_set("tests") { ...@@ -87,6 +87,7 @@ source_set("tests") {
"loopback_stream_unittest.cc", "loopback_stream_unittest.cc",
"output_controller_unittest.cc", "output_controller_unittest.cc",
"output_stream_unittest.cc", "output_stream_unittest.cc",
"public/cpp/input_ipc_unittest.cc",
"snooper_node_unittest.cc", "snooper_node_unittest.cc",
"stream_factory_unittest.cc", "stream_factory_unittest.cc",
"sync_reader_unittest.cc", "sync_reader_unittest.cc",
......
...@@ -12,6 +12,10 @@ source_set("cpp") { ...@@ -12,6 +12,10 @@ source_set("cpp") {
"debug_recording_session.h", "debug_recording_session.h",
"debug_recording_session_factory.cc", "debug_recording_session_factory.cc",
"debug_recording_session_factory.h", "debug_recording_session_factory.h",
"device_factory.cc",
"device_factory.h",
"input_ipc.cc",
"input_ipc.h",
] ]
public_deps = [ public_deps = [
......
// Copyright 2018 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 "services/audio/public/cpp/device_factory.h"
#include <memory>
#include "base/bind.h"
#include "services/audio/public/cpp/input_ipc.h"
namespace audio {
scoped_refptr<media::AudioCapturerSource> CreateInputDevice(
std::unique_ptr<service_manager::Connector> connector,
const std::string& device_id,
media::mojom::AudioLogPtr log) {
std::unique_ptr<media::AudioInputIPC> ipc = std::make_unique<InputIPC>(
std::move(connector), device_id, std::move(log));
return base::MakeRefCounted<media::AudioInputDevice>(std::move(ipc));
}
scoped_refptr<media::AudioCapturerSource> CreateInputDevice(
std::unique_ptr<service_manager::Connector> connector,
const std::string& device_id) {
std::unique_ptr<media::AudioInputIPC> ipc =
std::make_unique<InputIPC>(std::move(connector), device_id, nullptr);
return base::MakeRefCounted<media::AudioInputDevice>(std::move(ipc));
}
} // namespace audio
// Copyright 2018 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.
#ifndef SERVICES_AUDIO_PUBLIC_CPP_DEVICE_FACTORY_H_
#define SERVICES_AUDIO_PUBLIC_CPP_DEVICE_FACTORY_H_
#include "media/audio/audio_input_device.h"
#include "services/audio/public/mojom/stream_factory.mojom.h"
#include "services/service_manager/public/cpp/connector.h"
namespace audio {
scoped_refptr<media::AudioCapturerSource> CreateInputDevice(
std::unique_ptr<service_manager::Connector> connector,
const std::string& device_id);
scoped_refptr<media::AudioCapturerSource> CreateInputDevice(
std::unique_ptr<service_manager::Connector> connector,
const std::string& device_id,
media::mojom::AudioLogPtr);
} // namespace audio
#endif // SERVICES_AUDIO_PUBLIC_CPP_DEVICE_FACTORY_H_
// Copyright 2018 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 "services/audio/public/cpp/input_ipc.h"
#include <utility>
#include "base/bind_helpers.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "services/audio/public/mojom/constants.mojom.h"
namespace audio {
InputIPC::InputIPC(std::unique_ptr<service_manager::Connector> connector,
const std::string& device_id,
media::mojom::AudioLogPtr log)
: stream_(),
stream_client_binding_(this),
device_id_(std::move(device_id)),
stream_factory_(),
stream_factory_info_(),
log_(std::move(log)),
weak_factory_(this) {
DETACH_FROM_SEQUENCE(sequence_checker_);
DCHECK(connector);
connector->BindInterface(audio::mojom::kServiceName,
mojo::MakeRequest(&stream_factory_info_));
}
InputIPC::~InputIPC() = default;
void InputIPC::CreateStream(media::AudioInputIPCDelegate* delegate,
const media::AudioParameters& params,
bool automatic_gain_control,
uint32_t total_segments) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(delegate);
DCHECK(!delegate_);
delegate_ = delegate;
if (!stream_factory_.is_bound())
stream_factory_.Bind(std::move(stream_factory_info_));
media::mojom::AudioInputStreamRequest stream_request =
mojo::MakeRequest(&stream_);
media::mojom::AudioInputStreamClientPtr client;
stream_client_binding_.Bind(mojo::MakeRequest(&client));
// Unretained is safe because we own the binding.
stream_client_binding_.set_connection_error_handler(
base::BindOnce(&InputIPC::OnError, base::Unretained(this)));
// For now we don't care about key presses, so we pass a invalid buffer.
mojo::ScopedSharedBufferHandle invalid_key_press_count_buffer;
stream_factory_->CreateInputStream(
std::move(stream_request), std::move(client), nullptr,
log_ ? std::move(log_) : nullptr, device_id_, params, total_segments,
automatic_gain_control, std::move(invalid_key_press_count_buffer),
base::BindOnce(&InputIPC::StreamCreated, weak_factory_.GetWeakPtr()));
}
void InputIPC::StreamCreated(media::mojom::AudioDataPipePtr data_pipe,
bool initially_muted) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(delegate_);
if (data_pipe.is_null()) {
OnError();
return;
}
base::PlatformFile socket_handle;
auto result =
mojo::UnwrapPlatformFile(std::move(data_pipe->socket), &socket_handle);
DCHECK_EQ(result, MOJO_RESULT_OK);
base::SharedMemoryHandle memory_handle;
mojo::UnwrappedSharedMemoryHandleProtection protection;
result = mojo::UnwrapSharedMemoryHandle(std::move(data_pipe->shared_memory),
&memory_handle, nullptr, &protection);
DCHECK_EQ(result, MOJO_RESULT_OK);
DCHECK_EQ(protection, mojo::UnwrappedSharedMemoryHandleProtection::kReadOnly);
delegate_->OnStreamCreated(memory_handle, socket_handle, initially_muted);
}
void InputIPC::RecordStream() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(stream_.is_bound());
stream_->Record();
}
void InputIPC::SetVolume(double volume) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(stream_.is_bound());
stream_->SetVolume(volume);
}
void InputIPC::CloseStream() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
delegate_ = nullptr;
if (stream_client_binding_.is_bound())
stream_client_binding_.Close();
stream_.reset();
}
void InputIPC::OnError() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(delegate_);
delegate_->OnError();
}
void InputIPC::OnMutedStateChanged(bool is_muted) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(delegate_);
delegate_->OnMuted(is_muted);
}
} // namespace audio
// Copyright 2018 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.
#ifndef SERVICES_AUDIO_PUBLIC_CPP_INPUT_IPC_H_
#define SERVICES_AUDIO_PUBLIC_CPP_INPUT_IPC_H_
#include <string>
#include "base/callback_helpers.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "media/audio/audio_input_ipc.h"
#include "media/mojo/interfaces/audio_input_stream.mojom.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "services/audio/public/mojom/stream_factory.mojom.h"
#include "services/service_manager/public/cpp/connector.h"
namespace audio {
// InputIPC is a client-side class for handling creation,
// initialization and control of an input stream. May only be used on a single
// thread.
class InputIPC : public media::AudioInputIPC,
public media::mojom::AudioInputStreamClient {
public:
explicit InputIPC(std::unique_ptr<service_manager::Connector> connector,
const std::string& device_id,
media::mojom::AudioLogPtr log);
~InputIPC() override;
// AudioInputIPC implementation
void CreateStream(media::AudioInputIPCDelegate* delegate,
const media::AudioParameters& params,
bool automatic_gain_control,
uint32_t total_segments) override;
void RecordStream() override;
void SetVolume(double volume) override;
void CloseStream() override;
private:
// AudioInputStreamClient implementation.
void OnError() override;
void OnMutedStateChanged(bool is_muted) override;
void StreamCreated(media::mojom::AudioDataPipePtr data_pipe, bool is_muted);
SEQUENCE_CHECKER(sequence_checker_);
media::mojom::AudioInputStreamPtr stream_;
mojo::Binding<AudioInputStreamClient> stream_client_binding_;
media::AudioInputIPCDelegate* delegate_ = nullptr;
const std::string& device_id_;
// stream_factory_info_ is bound in the constructor, and later used to
// bind stream_factory_. This is done because the constructor may be called
// from a different thread than the other functions.
audio::mojom::StreamFactoryPtr stream_factory_;
audio::mojom::StreamFactoryPtrInfo stream_factory_info_;
media::mojom::AudioLogPtr log_;
base::WeakPtrFactory<InputIPC> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(InputIPC);
};
} // namespace audio
#endif // SERVICES_AUDIO_PUBLIC_CPP_INPUT_IPC_H_
// Copyright 2018 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 "services/audio/public/cpp/input_ipc.h"
#include "base/test/scoped_task_environment.h"
#include "media/mojo/interfaces/audio_data_pipe.mojom.h"
#include "mojo/public/cpp/system/buffer.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "services/audio/public/cpp/device_factory.h"
#include "services/audio/public/mojom/constants.mojom.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::StrictMock;
namespace audio {
namespace {
const std::string& kDeviceId = "1234";
const size_t kShMemSize = 456;
const double kNewVolume = 0.271828;
class MockStream : public media::mojom::AudioInputStream {
public:
MOCK_METHOD0(Record, void());
MOCK_METHOD1(SetVolume, void(double));
};
class FakeStreamFactory : public audio::mojom::StreamFactory {
public:
FakeStreamFactory() : binding_(this), stream_(), stream_binding_(&stream_) {}
~FakeStreamFactory() override = default;
void CreateInputStream(media::mojom::AudioInputStreamRequest stream_request,
media::mojom::AudioInputStreamClientPtr client,
media::mojom::AudioInputStreamObserverPtr observer,
media::mojom::AudioLogPtr log,
const std::string& device_id,
const media::AudioParameters& params,
uint32_t shared_memory_count,
bool enable_agc,
mojo::ScopedSharedBufferHandle key_press_count_buffer,
CreateInputStreamCallback created_callback) {
if (should_fail_) {
std::move(created_callback).Run(nullptr, initially_muted_);
return;
}
// Keep the client alive to avoid binding errors.
client_ = std::move(client);
if (stream_binding_.is_bound())
stream_binding_.Unbind();
stream_binding_.Bind(std::move(stream_request));
base::SyncSocket socket1, socket2;
base::SyncSocket::CreatePair(&socket1, &socket2);
auto h = mojo::SharedBufferHandle::Create(kShMemSize);
std::move(created_callback)
.Run({base::in_place,
h->Clone(mojo::SharedBufferHandle::AccessMode::READ_ONLY),
mojo::WrapPlatformFile(socket1.Release())},
initially_muted_);
}
MOCK_METHOD7(CreateOutputStream,
void(media::mojom::AudioOutputStreamRequest stream_request,
media::mojom::AudioOutputStreamObserverAssociatedPtrInfo
observer_info,
media::mojom::AudioLogPtr log,
const std::string& output_device_id,
const media::AudioParameters& params,
const base::UnguessableToken& group_id,
CreateOutputStreamCallback created_callback));
MOCK_METHOD2(BindMuter,
void(mojom::LocalMuterAssociatedRequest request,
const base::UnguessableToken& group_id));
void Bind(mojo::ScopedMessagePipeHandle handle) {
binding_.Bind(audio::mojom::StreamFactoryRequest(std::move(handle)));
}
mojo::Binding<audio::mojom::StreamFactory> binding_;
StrictMock<MockStream> stream_;
media::mojom::AudioInputStreamClientPtr client_;
mojo::Binding<media::mojom::AudioInputStream> stream_binding_;
bool initially_muted_ = true;
bool should_fail_ = false;
};
class MockDelegate : public media::AudioInputIPCDelegate {
public:
MockDelegate() {}
~MockDelegate() override {}
void OnStreamCreated(base::SharedMemoryHandle mem_handle,
base::SyncSocket::Handle socket_handle,
bool initially_muted) override {
base::SharedMemory sh_mem(
mem_handle, /*read_only*/ true); // Releases the shared memory handle.
base::SyncSocket socket(socket_handle); // Releases the socket descriptor.
GotOnStreamCreated(initially_muted);
}
MOCK_METHOD1(GotOnStreamCreated, void(bool initially_muted));
MOCK_METHOD0(OnError, void());
MOCK_METHOD1(OnMuted, void(bool));
MOCK_METHOD0(OnIPCClosed, void());
};
class InputIPCTest : public ::testing::Test {
public:
base::test::ScopedTaskEnvironment scoped_task_environment;
std::unique_ptr<audio::InputIPC> ipc;
const media::AudioParameters audioParameters =
media::AudioParameters(media::AudioParameters::AUDIO_PCM_LINEAR,
media::CHANNEL_LAYOUT_STEREO,
16000,
1600);
protected:
InputIPCTest()
: scoped_task_environment(
base::test::ScopedTaskEnvironment::MainThreadType::DEFAULT,
base::test::ScopedTaskEnvironment::ExecutionMode::QUEUED) {}
std::unique_ptr<StrictMock<FakeStreamFactory>> factory_;
void SetUp() override {
service_manager::mojom::ConnectorRequest request;
std::unique_ptr<service_manager::Connector> connector =
service_manager::Connector::Create(&request);
factory_ = std::make_unique<StrictMock<FakeStreamFactory>>();
{
service_manager::Connector::TestApi test_api(connector.get());
test_api.OverrideBinderForTesting(
service_manager::Identity(audio::mojom::kServiceName),
audio::mojom::StreamFactory::Name_,
base::BindRepeating(&FakeStreamFactory::Bind,
base::Unretained(factory_.get())));
}
ipc = std::make_unique<InputIPC>(std::move(connector), kDeviceId, nullptr);
}
};
} // namespace
TEST_F(InputIPCTest, CreateStreamPropagates) {
StrictMock<MockDelegate> delegate;
EXPECT_CALL(delegate, GotOnStreamCreated(_));
ipc->CreateStream(&delegate, audioParameters, false, 0);
scoped_task_environment.RunUntilIdle();
}
TEST_F(InputIPCTest, CreateStreamPropagatesInitiallyMuted) {
StrictMock<MockDelegate> delegate;
factory_->initially_muted_ = true;
EXPECT_CALL(delegate, GotOnStreamCreated(true));
ipc->CreateStream(&delegate, audioParameters, false, 0);
scoped_task_environment.RunUntilIdle();
ipc->CloseStream();
scoped_task_environment.RunUntilIdle();
factory_->initially_muted_ = false;
EXPECT_CALL(delegate, GotOnStreamCreated(false));
ipc->CreateStream(&delegate, audioParameters, false, 0);
scoped_task_environment.RunUntilIdle();
ipc->CloseStream();
scoped_task_environment.RunUntilIdle();
}
TEST_F(InputIPCTest, MutedStateChangesPropagates) {
StrictMock<MockDelegate> delegate;
EXPECT_CALL(delegate, GotOnStreamCreated(_));
ipc->CreateStream(&delegate, audioParameters, false, 0);
scoped_task_environment.RunUntilIdle();
EXPECT_CALL(delegate, OnMuted(true));
factory_->client_->OnMutedStateChanged(true);
scoped_task_environment.RunUntilIdle();
EXPECT_CALL(delegate, OnMuted(false));
factory_->client_->OnMutedStateChanged(false);
scoped_task_environment.RunUntilIdle();
}
TEST_F(InputIPCTest, Record_Records) {
StrictMock<MockDelegate> delegate;
EXPECT_CALL(delegate, GotOnStreamCreated(_));
ipc->CreateStream(&delegate, audioParameters, false, 0);
scoped_task_environment.RunUntilIdle();
EXPECT_CALL(factory_->stream_, Record());
ipc->RecordStream();
scoped_task_environment.RunUntilIdle();
}
TEST_F(InputIPCTest, IsReusable) {
for (int i = 0; i < 5; i++) {
StrictMock<MockDelegate> delegate;
EXPECT_CALL(delegate, GotOnStreamCreated(_));
ipc->CreateStream(&delegate, audioParameters, false, 0);
scoped_task_environment.RunUntilIdle();
ipc->CloseStream();
scoped_task_environment.RunUntilIdle();
testing::Mock::VerifyAndClearExpectations(&delegate);
}
}
TEST_F(InputIPCTest, SetVolume_SetsVolume) {
StrictMock<MockDelegate> delegate;
EXPECT_CALL(delegate, GotOnStreamCreated(_));
ipc->CreateStream(&delegate, audioParameters, false, 0);
scoped_task_environment.RunUntilIdle();
EXPECT_CALL(factory_->stream_, SetVolume(kNewVolume));
ipc->SetVolume(kNewVolume);
scoped_task_environment.RunUntilIdle();
}
TEST_F(InputIPCTest, FailedStreamCreationNullCallback) {
StrictMock<MockDelegate> delegate;
EXPECT_CALL(delegate, OnError()).Times(2);
factory_->should_fail_ = true;
ipc->CreateStream(&delegate, audioParameters, false, 0);
scoped_task_environment.RunUntilIdle();
}
TEST_F(InputIPCTest, FailedStreamCreationDestuctedFactory) {
StrictMock<MockDelegate> delegate;
EXPECT_CALL(delegate, OnError());
factory_ = nullptr;
ipc->CreateStream(&delegate, audioParameters, false, 0);
scoped_task_environment.RunUntilIdle();
}
} // namespace audio
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment