Commit 4ab948cd authored by Max Morin's avatar Max Morin Committed by Commit Bot

Add factory for streams in audio service.

Just testing the factory is a bit tricky, so I test it as part of the
audio::OutputStream tests.

Doc: go/audio-service-streams-design

Bug: 803102

Change-Id: I463e7adbda7de1a8a5724fa23fc98015cb425a3c
Reviewed-on: https://chromium-review.googlesource.com/939179
Commit-Queue: Max Morin <maxmorin@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarYuri Wiitala <miu@chromium.org>
Reviewed-by: default avatarGuido Urdaneta <guidou@chromium.org>
Cr-Commit-Position: refs/heads/master@{#541785}
parent 505f0d3a
......@@ -25,6 +25,8 @@ source_set("lib") {
"service.h",
"service_factory.cc",
"service_factory.h",
"stream_factory.cc",
"stream_factory.h",
"system_info.cc",
"system_info.h",
]
......
This diff is collapsed.
......@@ -8,6 +8,7 @@ mojom("mojom") {
sources = [
"audio_device_description.mojom",
"debug_recording.mojom",
"stream_factory.mojom",
"system_info.mojom",
]
......
module audio.mojom;
import "media/mojo/interfaces/audio_data_pipe.mojom";
import "media/mojo/interfaces/audio_logging.mojom";
import "media/mojo/interfaces/audio_output_stream.mojom";
import "media/mojo/interfaces/audio_parameters.mojom";
import "mojo/common/unguessable_token.mojom";
// This interface is exposed by the audio service to allow trusted clients
// (like the browser process) to create streams. Note that while the factory
// interface itself is only for trusted clients, the created streams and data
// pipes may be forwarded to untrusted clients.
// TODO(803102): Add other stream creation functionality to this interface.
interface StreamFactory {
// Creates an AudioOutputStream and returns the AudioDataPipe it reads data
// from. |data_pipe| is null in case stream creation failed.
// |device_id| is either the |unique_id| field from an AudioDeviceDescription
// obtained from the audio.mojom.SystemInfo interface, or "default".
// |stream_group_id| will later be used for muting streams or capturing them
// for loopback.
CreateOutputStream(
media.mojom.AudioOutputStream& stream,
media.mojom.AudioOutputStreamClient client,
associated media.mojom.AudioOutputStreamObserver observer,
media.mojom.AudioLog log,
string device_id, media.mojom.AudioParameters params,
mojo.common.mojom.UnguessableToken stream_group_id)
=> (media.mojom.AudioDataPipe? data_pipe);
};
// 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/stream_factory.h"
#include <utility>
#include "services/audio/output_stream.h"
namespace audio {
StreamFactory::StreamFactory(media::AudioManager* audio_manager)
: audio_manager_(audio_manager) {}
StreamFactory::~StreamFactory() {
DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
}
void StreamFactory::BindRequest(mojom::StreamFactoryRequest request) {
DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
bindings_.AddBinding(this, std::move(request));
}
void StreamFactory::CreateOutputStream(
media::mojom::AudioOutputStreamRequest stream_request,
media::mojom::AudioOutputStreamClientPtr client,
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) {
DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
media::mojom::AudioOutputStreamObserverAssociatedPtr observer;
observer.Bind(std::move(observer_info));
// Unretained is safe since |this| indirectly owns the OutputStream.
auto deleter_callback = base::BindOnce(&StreamFactory::RemoveOutputStream,
base::Unretained(this));
output_streams_.insert(std::make_unique<OutputStream>(
std::move(created_callback), std::move(deleter_callback),
std::move(stream_request), std::move(client), std::move(observer),
std::move(log), audio_manager_, output_device_id, params));
}
void StreamFactory::RemoveOutputStream(OutputStream* stream) {
DCHECK_CALLED_ON_VALID_SEQUENCE(owning_sequence_);
size_t erased = output_streams_.erase(stream);
DCHECK_EQ(1u, erased);
}
} // 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_STREAM_FACTORY_H_
#define SERVICES_AUDIO_STREAM_FACTORY_H_
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/containers/flat_set.h"
#include "base/containers/unique_ptr_adapters.h"
#include "base/macros.h"
#include "base/sequence_checker.h"
#include "media/mojo/interfaces/audio_logging.mojom.h"
#include "media/mojo/interfaces/audio_output_stream.mojom.h"
#include "mojo/public/cpp/bindings/binding_set.h"
#include "services/audio/public/mojom/stream_factory.mojom.h"
namespace base {
class UnguessableToken;
}
namespace media {
class AudioManager;
class AudioParameters;
} // namespace media
namespace audio {
class OutputStream;
// This class is used to provide the StreamFactory interface. It will typically
// be instantiated when needed and remain for the lifetime of the service.
// Destructing the factory will also destroy all the streams it has created.
// |audio_manager| must outlive the factory.
class StreamFactory final : public mojom::StreamFactory {
public:
explicit StreamFactory(media::AudioManager* audio_manager);
~StreamFactory() final;
void BindRequest(mojom::StreamFactoryRequest request);
// StreamFactory implementation.
void CreateOutputStream(
media::mojom::AudioOutputStreamRequest stream_request,
media::mojom::AudioOutputStreamClientPtr client,
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) final;
private:
using OutputStreamSet =
base::flat_set<std::unique_ptr<OutputStream>, base::UniquePtrComparator>;
void RemoveOutputStream(OutputStream* stream);
SEQUENCE_CHECKER(owning_sequence_);
media::AudioManager* const audio_manager_;
mojo::BindingSet<mojom::StreamFactory> bindings_;
OutputStreamSet output_streams_;
DISALLOW_COPY_AND_ASSIGN(StreamFactory);
};
} // namespace audio
#endif // SERVICES_AUDIO_STREAM_FACTORY_H_
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