提交 d729b8b9 编写于 作者: S Simon Fels

Implement audio support

Audio playback still has a delay about 1-2 seconds but it's usable.

This also moves the platform policy class into its own namespace to be
not specific to just window management.
上级 23392b97
...@@ -73,13 +73,27 @@ LOCAL_SRC_FILES := \ ...@@ -73,13 +73,27 @@ LOCAL_SRC_FILES := \
LOCAL_MODULE := hwcomposer.anbox LOCAL_MODULE := hwcomposer.anbox
LOCAL_CFLAGS:= -DLOG_TAG=\"hwcomposer\" LOCAL_CFLAGS:= -DLOG_TAG=\"hwcomposer\"
LOCAL_C_INCLUDES += \ LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/../device/generic/goldfish/opengl/host/include/libOpenglRender \ $(LOCAL_PATH)/android/opengl/host/include/libOpenglRender \
$(LOCAL_PATH)/../device/generic/goldfish/opengl/shared/OpenglCodecCommon \ $(LOCAL_PATH)/android/opengl/shared/OpenglCodecCommon \
$(LOCAL_PATH)/../device/generic/goldfish/opengl/system/renderControl_enc \ $(LOCAL_PATH)/android/opengl/system/renderControl_enc \
$(LOCAL_PATH)/../device/generic/goldfish/opengl/system/OpenglSystemCommon $(LOCAL_PATH)/android/opengl/system/OpenglSystemCommon
LOCAL_MODULE_TAGS := optional LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY) include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := audio.primary.goldfish
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_MODULE_TAGS := optional
LOCAL_SHARED_LIBRARIES := libcutils liblog
LOCAL_SRC_FILES := \
android/audio/audio_hw.cpp
LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/src
LOCAL_SHARED_LIBRARIES += libdl
LOCAL_CFLAGS := -Wno-unused-parameter
include $(BUILD_SHARED_LIBRARY)
# Include the Android.mk files below will override LOCAL_PATH so we # Include the Android.mk files below will override LOCAL_PATH so we
# have to take a copy of it here. # have to take a copy of it here.
TMP_PATH := $(LOCAL_PATH) TMP_PATH := $(LOCAL_PATH)
...@@ -89,7 +103,6 @@ include $(TMP_PATH)/android/fingerprint/Android.mk ...@@ -89,7 +103,6 @@ include $(TMP_PATH)/android/fingerprint/Android.mk
include $(TMP_PATH)/android/power/Android.mk include $(TMP_PATH)/android/power/Android.mk
include $(TMP_PATH)/android/qemu-props/Android.mk include $(TMP_PATH)/android/qemu-props/Android.mk
include $(TMP_PATH)/android/qemud/Android.mk include $(TMP_PATH)/android/qemud/Android.mk
include $(TMP_PATH)/android/audio/Android.mk
include $(TMP_PATH)/android/sensors/Android.mk include $(TMP_PATH)/android/sensors/Android.mk
include $(TMP_PATH)/android/opengl/Android.mk include $(TMP_PATH)/android/opengl/Android.mk
include $(TMP_PATH)/android/gps/Android.mk include $(TMP_PATH)/android/gps/Android.mk
......
...@@ -29,6 +29,10 @@ set(HWCOMPOSER_SOURCES ...@@ -29,6 +29,10 @@ set(HWCOMPOSER_SOURCES
add_library(hwcomposer.anbox SHARED ${HWCOMPOSER_SOURCES}) add_library(hwcomposer.anbox SHARED ${HWCOMPOSER_SOURCES})
set(AUDIO_SOURCES
audio/audio_hw.cpp)
add_library(audio.goldfish SHARED ${AUDIO_SOURCES})
# As we're adding Android specific bits in this project we can't # As we're adding Android specific bits in this project we can't
# build this safely within default build anymore. We keep this # build this safely within default build anymore. We keep this
# for easy integration into used IDEs. # for easy integration into used IDEs.
...@@ -38,3 +42,6 @@ set_target_properties(anboxd ...@@ -38,3 +42,6 @@ set_target_properties(anboxd
set_target_properties(hwcomposer.anbox set_target_properties(hwcomposer.anbox
PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1) PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
set_target_properties(audio.goldfish
PROPERTIES EXCLUDE_FROM_ALL 1 EXCLUDE_FROM_DEFAULT_BUILD 1)
#
# Copyright (C) 2011 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := audio.primary.goldfish
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_MODULE_TAGS := optional
LOCAL_SHARED_LIBRARIES := libcutils liblog
LOCAL_SRC_FILES := audio_hw.c
LOCAL_SHARED_LIBRARIES += libdl
LOCAL_CFLAGS := -Wno-unused-parameter
include $(BUILD_SHARED_LIBRARY)
此差异已折叠。
此差异已折叠。
...@@ -136,14 +136,20 @@ set(SOURCES ...@@ -136,14 +136,20 @@ set(SOURCES
anbox/graphics/emugl/TimeUtils.cpp anbox/graphics/emugl/TimeUtils.cpp
anbox/graphics/emugl/WindowSurface.cpp anbox/graphics/emugl/WindowSurface.cpp
anbox/audio/server.cpp
anbox/audio/client_info.h
anbox/audio/source.h
anbox/audio/sink.h
anbox/wm/display.cpp anbox/wm/display.cpp
anbox/wm/task.cpp anbox/wm/task.cpp
anbox/wm/stack.cpp anbox/wm/stack.cpp
anbox/wm/manager.cpp anbox/wm/manager.cpp
anbox/wm/window_state.cpp anbox/wm/window_state.cpp
anbox/wm/window.cpp anbox/wm/window.cpp
anbox/wm/platform_policy.cpp
anbox/wm/default_platform_policy.cpp anbox/platform/policy.cpp
anbox/platform/default_policy.cpp
anbox/input/manager.cpp anbox/input/manager.cpp
anbox/input/device.cpp anbox/input/device.cpp
...@@ -168,6 +174,7 @@ set(SOURCES ...@@ -168,6 +174,7 @@ set(SOURCES
anbox/ubuntu/window.cpp anbox/ubuntu/window.cpp
anbox/ubuntu/keycode_converter.cpp anbox/ubuntu/keycode_converter.cpp
anbox/ubuntu/platform_policy.cpp anbox/ubuntu/platform_policy.cpp
anbox/ubuntu/audio_sink.cpp
anbox/dbus/interface.h anbox/dbus/interface.h
anbox/dbus/skeleton/service.cpp anbox/dbus/skeleton/service.cpp
......
/*
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ANBOX_AUDIO_CLIENT_INFO_H_
#define ANBOX_AUDIO_CLIENT_INFO_H_
#include <cstdint>
namespace anbox {
namespace audio {
struct ClientInfo {
enum class Type : std::uint8_t {
Playback = 0,
Recording = 1,
Max = 2,
};
Type type;
};
} // namespace audio
} // namespace anbox
#endif
/*
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "anbox/audio/server.h"
#include "anbox/audio/sink.h"
#include "anbox/network/published_socket_connector.h"
#include "anbox/network/delegate_connection_creator.h"
#include "anbox/network/local_socket_messenger.h"
#include "anbox/network/message_processor.h"
#include "anbox/common/type_traits.h"
#include "anbox/config.h"
#include "anbox/utils.h"
#include "anbox/logger.h"
using namespace std::placeholders;
namespace {
class AudioForwarder : public anbox::network::MessageProcessor {
public:
AudioForwarder(const std::shared_ptr<anbox::audio::Sink> &sink) :
sink_(sink) {
}
bool process_data(const std::vector<std::uint8_t> &data) override {
sink_->write_data(data);
return true;
}
private:
std::shared_ptr<anbox::audio::Sink> sink_;
};
}
namespace anbox {
namespace audio {
Server::Server(const std::shared_ptr<Runtime>& rt, const std::shared_ptr<platform::Policy> &platform_policy) :
platform_policy_(platform_policy),
socket_file_(utils::string_format("%s/anbox_audio", config::socket_path())),
connector_(std::make_shared<network::PublishedSocketConnector>(
socket_file_, rt,
std::make_shared<network::DelegateConnectionCreator<boost::asio::local::stream_protocol>>(std::bind(&Server::create_connection_for, this, _1)))),
connections_(std::make_shared<network::Connections<network::SocketConnection>>()),
next_id_(0) {
// FIXME: currently creating the socket creates it with the rights of
// the user we're running as. As this one is mapped into the container
::chmod(socket_file_.c_str(), 0777);
}
Server::~Server() {}
void Server::create_connection_for(std::shared_ptr<boost::asio::basic_stream_socket<boost::asio::local::stream_protocol>> const& socket) {
auto const messenger =
std::make_shared<network::LocalSocketMessenger>(socket);
// We have to read the client flags first before we can continue
// processing the actual commands
ClientInfo client_info;
auto err = messenger->receive_msg(
boost::asio::buffer(&client_info, sizeof(ClientInfo)));
if (err) {
ERROR("Failed to read client info: %s", err.message());
return;
}
std::shared_ptr<network::MessageProcessor> processor;
switch (client_info.type) {
case ClientInfo::Type::Playback:
processor = std::make_shared<AudioForwarder>(platform_policy_->create_audio_sink());
break;
case ClientInfo::Type::Recording:
break;
default:
ERROR("Invalid client type %d", static_cast<int>(client_info.type));
return;
}
// Everything ok, so approve the client by sending the requesting client
// info back. Once we have more things to negotiate we will send a modified
// client info struct back.
messenger->send(reinterpret_cast<char*>(&client_info), sizeof(client_info));
auto connection = std::make_shared<network::SocketConnection>(
messenger, messenger, next_id(), connections_, processor);
connections_->add(connection);
connection->read_next_message();
}
int Server::next_id() {
return next_id_.fetch_add(1);
}
} // namespace audio
} // namespace anbox
/*
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ANBOX_AUDIO_SERVER_H_
#define ANBOX_AUDIO_SERVER_H_
#include "anbox/runtime.h"
#include "anbox/audio/client_info.h"
#include "anbox/network/socket_messenger.h"
#include "anbox/network/socket_connection.h"
#include "anbox/platform/policy.h"
#include <atomic>
namespace anbox {
namespace network {
class PublishedSocketConnector;
} // namespace network
namespace audio {
class Server {
public:
Server(const std::shared_ptr<Runtime>& rt, const std::shared_ptr<platform::Policy> &platform_policy);
~Server();
std::string socket_file() const { return socket_file_; }
private:
void create_connection_for(std::shared_ptr<boost::asio::basic_stream_socket<
boost::asio::local::stream_protocol>> const& socket);
int next_id();
std::shared_ptr<platform::Policy> platform_policy_;
std::string socket_file_;
std::shared_ptr<network::PublishedSocketConnector> connector_;
std::shared_ptr<network::Connections<network::SocketConnection>> const connections_;
std::atomic<int> next_id_;
};
} // namespace audio
} // namespace anbox
#endif
/*
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ANBOX_AUDIO_SINK_H_
#define ANBOX_AUDIO_SINK_H_
#include <cstdint>
#include <vector>
namespace anbox {
namespace audio {
class Sink {
public:
virtual ~Sink() {}
virtual void write_data(const std::vector<std::uint8_t> &data) = 0;
};
} // namespace audio
} // namespace anbox
#endif
/*
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ANBOX_AUDIO_SOURCE_H_
#define ANBOX_AUDIO_SOURCE_H_
#include <cstdint>
#include <vector>
namespace anbox {
namespace audio {
class Source {
public:
virtual ~Source() {}
virtual void read_data(std::vector<std::uint8_t> &data) = 0;
};
} // namespace audio
} // namespace anbox
#endif
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "core/posix/signal.h" #include "core/posix/signal.h"
#include "anbox/application/launcher_storage.h" #include "anbox/application/launcher_storage.h"
#include "anbox/audio/server.h"
#include "anbox/bridge/android_api_stub.h" #include "anbox/bridge/android_api_stub.h"
#include "anbox/bridge/platform_api_skeleton.h" #include "anbox/bridge/platform_api_skeleton.h"
#include "anbox/bridge/platform_message_processor.h" #include "anbox/bridge/platform_message_processor.h"
...@@ -122,6 +123,8 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory) ...@@ -122,6 +123,8 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory)
policy->set_renderer(gl_server->renderer()); policy->set_renderer(gl_server->renderer());
auto audio_server = std::make_shared<audio::Server>(rt, policy);
// The qemu pipe is used as a very fast communication channel between guest // The qemu pipe is used as a very fast communication channel between guest
// and host for things like the GLES emulation/translation, the RIL or ADB. // and host for things like the GLES emulation/translation, the RIL or ADB.
auto qemu_pipe_connector = auto qemu_pipe_connector =
...@@ -154,6 +157,7 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory) ...@@ -154,6 +157,7 @@ anbox::cmds::Run::Run(const BusFactory &bus_factory)
container_configuration.bind_mounts = { container_configuration.bind_mounts = {
{qemu_pipe_connector->socket_file(), "/dev/qemu_pipe"}, {qemu_pipe_connector->socket_file(), "/dev/qemu_pipe"},
{bridge_connector->socket_file(), "/dev/anbox_bridge"}, {bridge_connector->socket_file(), "/dev/anbox_bridge"},
{audio_server->socket_file(), "/dev/anbox_audio"},
{config::host_input_device_path(), "/dev/input"}, {config::host_input_device_path(), "/dev/input"},
{"/dev/binder", "/dev/binder"}, {"/dev/binder", "/dev/binder"},
{"/dev/ashmem", "/dev/ashmem"}, {"/dev/ashmem", "/dev/ashmem"},
......
...@@ -47,6 +47,17 @@ int BufferQueue::push_locked( ...@@ -47,6 +47,17 @@ int BufferQueue::push_locked(
return try_push_locked(std::move(buffer)); return try_push_locked(std::move(buffer));
} }
int BufferQueue::wait_until_not_empty_locked(std::unique_lock<std::mutex> &lock) {
while (count_ == 0) {
if (closed_)
// Closed queue is empty.
return -EIO;
can_pop_.wait(lock);
}
return 0;
}
int BufferQueue::try_pop_locked(Buffer *buffer) { int BufferQueue::try_pop_locked(Buffer *buffer) {
if (count_ == 0) if (count_ == 0)
return closed_ ? -EIO : -EAGAIN; return closed_ ? -EIO : -EAGAIN;
......
...@@ -33,6 +33,8 @@ class BufferQueue { ...@@ -33,6 +33,8 @@ class BufferQueue {
bool can_pop_locked() const { return count_ > 0U; } bool can_pop_locked() const { return count_ > 0U; }
bool is_closed_locked() const { return closed_; } bool is_closed_locked() const { return closed_; }
int wait_until_not_empty_locked(std::unique_lock<std::mutex> &lock);
int try_push_locked(Buffer &&buffer); int try_push_locked(Buffer &&buffer);
int push_locked(Buffer &&buffer, std::unique_lock<std::mutex> &lock); int push_locked(Buffer &&buffer, std::unique_lock<std::mutex> &lock);
int try_pop_locked(Buffer *buffer); int try_pop_locked(Buffer *buffer);
......
...@@ -115,7 +115,7 @@ void BufferedIOStream::thread_main() { ...@@ -115,7 +115,7 @@ void BufferedIOStream::thread_main() {
std::unique_lock<std::mutex> l(out_lock_); std::unique_lock<std::mutex> l(out_lock_);
Buffer buffer; Buffer buffer;
auto result = out_queue_.pop_locked(&buffer, l); const auto result = out_queue_.pop_locked(&buffer, l);
if (result != 0 && result != -EAGAIN) break; if (result != 0 && result != -EAGAIN) break;
auto bytes_left = buffer.size(); auto bytes_left = buffer.size();
......
...@@ -41,8 +41,7 @@ class PublishedSocketConnector : public DoNotCopyOrMove, public Connector { ...@@ -41,8 +41,7 @@ class PublishedSocketConnector : public DoNotCopyOrMove, public Connector {
private: private:
void start_accept(); void start_accept();
void on_new_connection( void on_new_connection(
std::shared_ptr<boost::asio::local::stream_protocol::socket> const& std::shared_ptr<boost::asio::local::stream_protocol::socket> const& socket,
socket,
boost::system::error_code const& err); boost::system::error_code const& err);
const std::string socket_file_; const std::string socket_file_;
......
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
* *
*/ */
#include "anbox/wm/default_platform_policy.h" #include "anbox/platform/default_policy.h"
#include "anbox/logger.h"
#include "anbox/wm/window.h" #include "anbox/wm/window.h"
#include "anbox/logger.h"
namespace { namespace {
class NullWindow : public anbox::wm::Window { class NullWindow : public anbox::wm::Window {
...@@ -29,12 +29,22 @@ class NullWindow : public anbox::wm::Window { ...@@ -29,12 +29,22 @@ class NullWindow : public anbox::wm::Window {
} }
namespace anbox { namespace anbox {
namespace wm { namespace platform {
DefaultPlatformPolicy::DefaultPlatformPolicy() {} DefaultPolicy::DefaultPolicy() {}
std::shared_ptr<Window> DefaultPlatformPolicy::create_window( std::shared_ptr<wm::Window> DefaultPolicy::create_window(
const anbox::wm::Task::Id &task, const anbox::graphics::Rect &frame) { const anbox::wm::Task::Id &task, const anbox::graphics::Rect &frame) {
return std::make_shared<::NullWindow>(task, frame); return std::make_shared<::NullWindow>(task, frame);
} }
std::shared_ptr<audio::Sink> DefaultPolicy::create_audio_sink() {
ERROR("Not implemented");
return nullptr;
}
std::shared_ptr<audio::Source> DefaultPolicy::create_audio_source() {
ERROR("Not implemented");
return nullptr;
}
} // namespace wm } // namespace wm
} // namespace anbox } // namespace anbox
...@@ -15,19 +15,21 @@ ...@@ -15,19 +15,21 @@
* *
*/ */
#ifndef ANBOX_WM_DEFAULT_PLATFORM_POLICY_H_ #ifndef ANBOX_PLATFORM_DEFAULT_POLICY_H_
#define ANBOX_WM_DEFAULT_PLATFORM_POLICY_H_ #define ANBOX_PLATFORM_DEFAULT_POLICY_H_
#include "anbox/wm/platform_policy.h" #include "anbox/platform/policy.h"
namespace anbox { namespace anbox {
namespace wm { namespace platform {
class DefaultPlatformPolicy : public PlatformPolicy { class DefaultPolicy : public Policy {
public: public:
DefaultPlatformPolicy(); DefaultPolicy();
std::shared_ptr<Window> create_window( std::shared_ptr<wm::Window> create_window(
const anbox::wm::Task::Id &task, const anbox::wm::Task::Id &task,
const anbox::graphics::Rect &frame) override; const anbox::graphics::Rect &frame) override;
std::shared_ptr<audio::Sink> create_audio_sink() override;
std::shared_ptr<audio::Source> create_audio_source() override;
}; };
} // namespace wm } // namespace wm
} // namespace anbox } // namespace anbox
......
...@@ -15,10 +15,10 @@ ...@@ -15,10 +15,10 @@
* *
*/ */
#include "anbox/wm/platform_policy.h" #include "anbox/platform/policy.h"
namespace anbox { namespace anbox {
namespace wm { namespace platform {
PlatformPolicy::~PlatformPolicy() {} Policy::~Policy() {}
} // namespace wm } // namespace wm
} // namespace anbox } // namespace anbox
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
* *
*/ */
#ifndef ANBOX_WM_PLATFORM_POLICY_H_ #ifndef ANBOX_PLATFORM_POLICY_H_
#define ANBOX_WM_PLATFORM_POLICY_H_ #define ANBOX_PLATFORM_POLICY_H_
#include "anbox/graphics/rect.h" #include "anbox/graphics/rect.h"
#include "anbox/wm/window_state.h" #include "anbox/wm/window_state.h"
...@@ -24,14 +24,23 @@ ...@@ -24,14 +24,23 @@
#include <memory> #include <memory>
namespace anbox { namespace anbox {
namespace audio {
class Sink;
class Source;
} // namespace audio
namespace wm { namespace wm {
class Window; class Window;
class PlatformPolicy { } // namespace wm
namespace platform {
class Policy {
public: public:
virtual ~PlatformPolicy(); virtual ~Policy();
virtual std::shared_ptr<Window> create_window( virtual std::shared_ptr<wm::Window> create_window(
const anbox::wm::Task::Id &task, const anbox::graphics::Rect &frame) = 0; const anbox::wm::Task::Id &task, const anbox::graphics::Rect &frame) = 0;
virtual std::shared_ptr<audio::Sink> create_audio_sink() = 0;
virtual std::shared_ptr<audio::Source> create_audio_source() = 0;
}; };
} // namespace wm } // namespace wm
} // namespace anbox } // namespace anbox
......
/*
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "anbox/ubuntu/audio_sink.h"
#include "anbox/logger.h"
#include <stdexcept>
#include <boost/throw_exception.hpp>
namespace {
const constexpr size_t max_queue_size{16};
}
namespace anbox {
namespace ubuntu {
AudioSink::AudioSink() :
device_id_(0),
queue_(max_queue_size) {
}
AudioSink::~AudioSink() {}
void AudioSink::on_data_requested(void *user_data, std::uint8_t *buffer, int size) {
auto thiz = static_cast<AudioSink*>(user_data);
thiz->read_data(buffer, size);
}
bool AudioSink::connect_audio() {
if (device_id_ > 0)
return true;
SDL_memset(&spec_, 0, sizeof(spec_));
spec_.freq = 44100;
spec_.format = AUDIO_S16;
spec_.channels = 2;
spec_.samples = 4096;
spec_.callback = &AudioSink::on_data_requested;
spec_.userdata = this;
device_id_ = SDL_OpenAudioDevice(nullptr, 0, &spec_, nullptr, 0);
if (!device_id_)
return false;
SDL_PauseAudioDevice(device_id_, 0);
return true;
}
void AudioSink::disconnect_audio() {
if (device_id_ == 0)
return;
SDL_CloseAudioDevice(device_id_);
device_id_ = 0;
}
void AudioSink::read_data(std::uint8_t *buffer, int size) {
std::unique_lock<std::mutex> l(lock_);
const auto wanted = size;
size_t count = 0;
auto dst = buffer;
while (count < wanted) {
if (read_buffer_left_ > 0) {
size_t avail = std::min<size_t>(wanted - count, read_buffer_left_);
memcpy(dst + count,
read_buffer_.data() + (read_buffer_.size() - read_buffer_left_),
avail);
count += avail;
read_buffer_left_ -= avail;
continue;
}
bool blocking = (count == 0);
auto result = -EIO;
if (blocking)
result = queue_.pop_locked(&read_buffer_, l);
else
result = queue_.try_pop_locked(&read_buffer_);
if (result == 0) {
read_buffer_left_ = read_buffer_.size();
continue;
}
if (count > 0) break;
return;
}
}
void AudioSink::write_data(const std::vector<std::uint8_t> &data) {
std::unique_lock<std::mutex> l(lock_);
if (!connect_audio()) {
WARNING("Audio server not connected, skipping %d bytes", data.size());
return;
}
graphics::Buffer buffer{data.data(), data.data() + data.size()};
queue_.push_locked(std::move(buffer), l);
}
} // namespace ubuntu
} // namespace anbox
/*
* Copyright (C) 2017 Simon Fels <morphis@gravedo.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3, as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranties of
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef ANBOX_UBUNTU_AUDIO_SINK_H_
#define ANBOX_UBUNTU_AUDIO_SINK_H_
#include "anbox/audio/sink.h"
#include "anbox/graphics/buffer_queue.h"
#include <SDL2/SDL_audio.h>
#include <thread>
namespace anbox {
namespace ubuntu {
class AudioSink : public audio::Sink {
public:
AudioSink();
~AudioSink();
void write_data(const std::vector<std::uint8_t> &data) override;
private:
bool connect_audio();
void disconnect_audio();
void read_data(std::uint8_t *buffer, int size);
static void on_data_requested(void *user_data, std::uint8_t *buffer, int size);
std::mutex lock_;
SDL_AudioSpec spec_;
SDL_AudioDeviceID device_id_;
graphics::BufferQueue queue_;
graphics::Buffer read_buffer_;
size_t read_buffer_left_ = 0;
};
} // namespace ubuntu
} // namespace anbox
#endif
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "anbox/logger.h" #include "anbox/logger.h"
#include "anbox/ubuntu/keycode_converter.h" #include "anbox/ubuntu/keycode_converter.h"
#include "anbox/ubuntu/window.h" #include "anbox/ubuntu/window.h"
#include "anbox/ubuntu/audio_sink.h"
#include <boost/throw_exception.hpp> #include <boost/throw_exception.hpp>
...@@ -36,7 +37,7 @@ PlatformPolicy::PlatformPolicy( ...@@ -36,7 +37,7 @@ PlatformPolicy::PlatformPolicy(
: input_manager_(input_manager), : input_manager_(input_manager),
android_api_(android_api), android_api_(android_api),
event_thread_running_(false) { event_thread_running_(false) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS) < 0)
BOOST_THROW_EXCEPTION(std::runtime_error("Failed to initialize SDL")); BOOST_THROW_EXCEPTION(std::runtime_error("Failed to initialize SDL"));
auto display_frame = graphics::Rect::Invalid; auto display_frame = graphics::Rect::Invalid;
...@@ -263,5 +264,14 @@ void PlatformPolicy::window_resized(const Window::Id &id, ...@@ -263,5 +264,14 @@ void PlatformPolicy::window_resized(const Window::Id &id,
DisplayManager::DisplayInfo PlatformPolicy::display_info() const { DisplayManager::DisplayInfo PlatformPolicy::display_info() const {
return display_info_; return display_info_;
} }
std::shared_ptr<audio::Sink> PlatformPolicy::create_audio_sink() {
return std::make_shared<AudioSink>();
}
std::shared_ptr<audio::Source> PlatformPolicy::create_audio_source() {
ERROR("Not implemented");
return nullptr;
}
} // namespace wm } // namespace wm
} // namespace anbox } // namespace anbox
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
#define ANBOX_UBUNTU_PLATFORM_POLICY_H_ #define ANBOX_UBUNTU_PLATFORM_POLICY_H_
#include "anbox/ubuntu/window.h" #include "anbox/ubuntu/window.h"
#include "anbox/wm/platform_policy.h" #include "anbox/platform/policy.h"
#include "anbox/graphics/emugl/DisplayManager.h" #include "anbox/graphics/emugl/DisplayManager.h"
...@@ -40,7 +40,7 @@ class AndroidApiStub; ...@@ -40,7 +40,7 @@ class AndroidApiStub;
} // namespace bridge } // namespace bridge
namespace ubuntu { namespace ubuntu {
class PlatformPolicy : public std::enable_shared_from_this<PlatformPolicy>, class PlatformPolicy : public std::enable_shared_from_this<PlatformPolicy>,
public wm::PlatformPolicy, public platform::Policy,
public Window::Observer, public Window::Observer,
public DisplayManager { public DisplayManager {
public: public:
...@@ -63,6 +63,9 @@ class PlatformPolicy : public std::enable_shared_from_this<PlatformPolicy>, ...@@ -63,6 +63,9 @@ class PlatformPolicy : public std::enable_shared_from_this<PlatformPolicy>,
void set_renderer(const std::shared_ptr<Renderer> &renderer); void set_renderer(const std::shared_ptr<Renderer> &renderer);
std::shared_ptr<audio::Sink> create_audio_sink() override;
std::shared_ptr<audio::Source> create_audio_source() override;
private: private:
void process_events(); void process_events();
void process_input_event(const SDL_Event &event); void process_input_event(const SDL_Event &event);
......
...@@ -16,15 +16,15 @@ ...@@ -16,15 +16,15 @@
*/ */
#include "anbox/wm/manager.h" #include "anbox/wm/manager.h"
#include "anbox/platform/policy.h"
#include "anbox/logger.h" #include "anbox/logger.h"
#include "anbox/wm/platform_policy.h"
#include <algorithm> #include <algorithm>
namespace anbox { namespace anbox {
namespace wm { namespace wm {
Manager::Manager(const std::shared_ptr<PlatformPolicy> &platform) Manager::Manager(const std::shared_ptr<platform::Policy> &policy)
: platform_(platform) {} : platform_policy_(policy) {}
Manager::~Manager() {} Manager::~Manager() {}
...@@ -61,7 +61,7 @@ void Manager::apply_window_state_update(const WindowState::List &updated, ...@@ -61,7 +61,7 @@ void Manager::apply_window_state_update(const WindowState::List &updated,
} }
auto platform_window = auto platform_window =
platform_->create_window(window.task(), window.frame()); platform_policy_->create_window(window.task(), window.frame());
platform_window->attach(); platform_window->attach();
windows_.insert({window.task(), platform_window}); windows_.insert({window.task(), platform_window});
} }
......
...@@ -26,11 +26,13 @@ ...@@ -26,11 +26,13 @@
#include <mutex> #include <mutex>
namespace anbox { namespace anbox {
namespace platform {
class Policy;
} // namespace platform
namespace wm { namespace wm {
class PlatformPolicy;
class Manager { class Manager {
public: public:
Manager(const std::shared_ptr<PlatformPolicy> &platform); Manager(const std::shared_ptr<platform::Policy> &policy);
~Manager(); ~Manager();
void apply_window_state_update(const WindowState::List &updated, void apply_window_state_update(const WindowState::List &updated,
...@@ -40,7 +42,7 @@ class Manager { ...@@ -40,7 +42,7 @@ class Manager {
private: private:
std::mutex mutex_; std::mutex mutex_;
std::shared_ptr<PlatformPolicy> platform_; std::shared_ptr<platform::Policy> platform_policy_;
std::map<Task::Id, std::shared_ptr<Window>> windows_; std::map<Task::Id, std::shared_ptr<Window>> windows_;
}; };
} // namespace wm } // namespace wm
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
#include <gmock/gmock.h> #include <gmock/gmock.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "anbox/wm/default_platform_policy.h" #include "anbox/platform/default_policy.h"
#include "anbox/wm/manager.h" #include "anbox/wm/manager.h"
#include "anbox/wm/window_state.h" #include "anbox/wm/window_state.h"
...@@ -43,7 +43,7 @@ TEST(LayerComposer, FindsNoSuitableWindowForLayer) { ...@@ -43,7 +43,7 @@ TEST(LayerComposer, FindsNoSuitableWindowForLayer) {
// The default policy will create a dumb window instance when requested // The default policy will create a dumb window instance when requested
// from the manager. // from the manager.
auto platform_policy = std::make_shared<wm::DefaultPlatformPolicy>(); auto platform_policy = std::make_shared<platform::DefaultPolicy>();
auto wm = std::make_shared<wm::Manager>(platform_policy); auto wm = std::make_shared<wm::Manager>(platform_policy);
auto single_window = wm::WindowState{ auto single_window = wm::WindowState{
...@@ -76,7 +76,7 @@ TEST(LayerComposer, MapsLayersToWindows) { ...@@ -76,7 +76,7 @@ TEST(LayerComposer, MapsLayersToWindows) {
// The default policy will create a dumb window instance when requested // The default policy will create a dumb window instance when requested
// from the manager. // from the manager.
auto platform_policy = std::make_shared<wm::DefaultPlatformPolicy>(); auto platform_policy = std::make_shared<platform::DefaultPolicy>();
auto wm = std::make_shared<wm::Manager>(platform_policy); auto wm = std::make_shared<wm::Manager>(platform_policy);
auto first_window = wm::WindowState{ auto first_window = wm::WindowState{
...@@ -135,7 +135,7 @@ TEST(LayerComposer, WindowPartiallyOffscreen) { ...@@ -135,7 +135,7 @@ TEST(LayerComposer, WindowPartiallyOffscreen) {
// The default policy will create a dumb window instance when requested // The default policy will create a dumb window instance when requested
// from the manager. // from the manager.
auto platform_policy = std::make_shared<wm::DefaultPlatformPolicy>(); auto platform_policy = std::make_shared<platform::DefaultPolicy>();
auto wm = std::make_shared<wm::Manager>(platform_policy); auto wm = std::make_shared<wm::Manager>(platform_policy);
auto window = wm::WindowState{ auto window = wm::WindowState{
...@@ -179,7 +179,7 @@ TEST(LayerComposer, PopupShouldNotCauseWindowLayerOffset) { ...@@ -179,7 +179,7 @@ TEST(LayerComposer, PopupShouldNotCauseWindowLayerOffset) {
// The default policy will create a dumb window instance when requested // The default policy will create a dumb window instance when requested
// from the manager. // from the manager.
auto platform_policy = std::make_shared<wm::DefaultPlatformPolicy>(); auto platform_policy = std::make_shared<platform::DefaultPolicy>();
auto wm = std::make_shared<wm::Manager>(platform_policy); auto wm = std::make_shared<wm::Manager>(platform_policy);
auto window = wm::WindowState{ auto window = wm::WindowState{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册