Skip to content

Commit

Permalink
Device management commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Apr 15, 2023
1 parent 7aff0a6 commit 8fc6974
Show file tree
Hide file tree
Showing 43 changed files with 4,014 additions and 577 deletions.
3 changes: 2 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ UseTab: Never
IndentWidth: 4
AccessModifierOffset: -4
ColumnLimit: 90
SpacesBeforeTrailingComments: 1

MaxEmptyLinesToKeep: 1
EmptyLineBeforeAccessModifier: Always
Expand All @@ -24,6 +23,8 @@ BreakConstructorInitializers: BeforeComma
ConstructorInitializerAllOnOneLineOrOnePerLine: false
IndentCaseLabels: false
PenaltyBreakComment: 0
SpaceBeforeCpp11BracedList: true
SpacesBeforeTrailingComments: 1

BreakBeforeBraces: Custom
BraceWrapping:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "3rdparty/spdlog"]
path = 3rdparty/spdlog
url = https://github.com/gabime/spdlog.git
[submodule "3rdparty/fmt"]
path = 3rdparty/fmt
url = https://github.com/fmtlib/fmt.git
26 changes: 26 additions & 0 deletions 3rdparty/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,38 @@ get_filename_component(GRPC_BIN_DIR
ABSOLUTE
)

# fmt
ExternalProject_Add(fmt
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/fmt
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/3rdparty/fmt-prefix
CMAKE_ARGS
-DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DBUILD_TESTING=OFF
-DFMT_DOC=OFF
-DFMT_INSTALL=ON
-DFMT_TEST=OFF
LOG_DOWNLOAD YES
LOG_CONFIGURE YES
LOG_BUILD YES
LOG_INSTALL YES
)
include_directories(SYSTEM
${CMAKE_CURRENT_BINARY_DIR}/3rdparty/fmt-prefix/include
)
list(PREPEND CMAKE_PREFIX_PATH
${CMAKE_CURRENT_BINARY_DIR}/3rdparty/fmt-prefix/lib/cmake
)

# spdlog
ExternalProject_Add(spdlog
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/spdlog
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/3rdparty/spdlog-prefix
CMAKE_ARGS
-DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER}
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_PREFIX_PATH=${CMAKE_CURRENT_BINARY_DIR}/3rdparty/fmt-prefix/lib/cmake
-DSPDLOG_FMT_EXTERNAL=ON
LOG_DOWNLOAD YES
LOG_CONFIGURE YES
LOG_BUILD YES
Expand Down Expand Up @@ -118,6 +143,7 @@ set(ALL_DEPENDENCIES
roc
libASPL
gRPC
fmt
spdlog
CLI11
)
Expand Down
1 change: 1 addition & 0 deletions 3rdparty/fmt
Submodule fmt added at 8ec94a
3 changes: 2 additions & 1 deletion HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ The project uses these libraries:
* [gRPC](https://github.com/grpc/grpc) - control protocol for virtual device
* [CLI11](https://github.com/CLIUtils/CLI11) - command-line parsing library
* [spdlog](https://github.com/gabime/spdlog) - logging library
* [{fmt}](https://github.com/fmtlib/fmt) - formatting library

All dependencies listed above are shipped as submodules. Besides them, project needs some standard frameworks, build tools installed system-wide, and Xcode or Xcode command-line tools.
All dependencies listed above are shipped as submodules. Besides them, project needs some standard frameworks, build tools installed system-wide, and Xcode or Xcode command-line tools with C++17 support.

## Build system

Expand Down
8 changes: 6 additions & 2 deletions driver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
find_package(libASPL CONFIG REQUIRED)
find_package(Protobuf CONFIG REQUIRED)
find_package(gRPC CONFIG REQUIRED)
find_package(spdlog CONFIG REQUIRED)
find_package(libASPL CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED)
find_library(LIB_CoreFoundation CoreFoundation REQUIRED)

add_library(${DRIVER_NAME} MODULE
Expand All @@ -10,10 +11,12 @@ add_library(${DRIVER_NAME} MODULE
"driver.cpp"
"driver_service.cpp"
"entry_point.cpp"
"index_allocator.cpp"
"log_manager.cpp"
"log_sender.cpp"
"log_syslog.cpp"
"tracer.cpp"
"uid_generator.cpp"
)

target_link_libraries(${DRIVER_NAME} PRIVATE
Expand All @@ -22,9 +25,10 @@ target_link_libraries(${DRIVER_NAME} PRIVATE
${RPC_LIB}

# third-party libs
aspl::libASPL
gRPC::grpc++_unsecure
spdlog::spdlog
aspl::libASPL
fmt::fmt

# system libs
${LIB_CoreFoundation}
Expand Down
74 changes: 62 additions & 12 deletions driver/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,84 @@

#include "device.hpp"

#include <fmt/core.h>
#include <spdlog/spdlog.h>

namespace rocvad {

Device::Device(std::shared_ptr<aspl::Plugin> plugin)
: plugin_(plugin)
namespace {

void populate_default_values(DeviceInfo& info)
{
if (info.config.name.empty()) {
info.config.name = fmt::format("Roc Virtual Device #{}", info.index);
}
}

aspl::DeviceParameters make_device_params(const DeviceConfig& config)
{
aspl::DeviceParameters device_params;

device_params.Name = config.name;
device_params.SampleRate = 44100; // TODO
device_params.ChannelCount = 2; // TODO
device_params.EnableMixing = true;
device_params.EnableRealtimeTracing = false;

return device_params;
}

} // namespace

Device::Device(std::shared_ptr<aspl::Plugin> plugin,
IndexAllocator& index_allocator,
UidGenerator& uid_generator,
const DeviceConfig& config)
: index_allocator_(index_allocator)
, uid_generator_(uid_generator)
, plugin_(plugin)
, info_(config)
{
spdlog::info("creating device object");
info_.index = index_allocator_.allocate();

if (info_.config.uid.empty()) {
info_.config.uid = uid_generator_.generate();
}

populate_default_values(info_);

aspl::DeviceParameters deviceParams;
deviceParams.Name = "Roc Device";
deviceParams.SampleRate = 44100;
deviceParams.ChannelCount = 2;
deviceParams.EnableMixing = true;
deviceParams.EnableRealtimeTracing = false;
spdlog::info("creating device object, index={} uid={} type={} name=\"{}\"",
info_.index,
info_.config.uid,
info_.config.type,
info_.config.name);

device_ = std::make_shared<aspl::Device>(plugin->GetContext(), deviceParams);
device_ = std::make_shared<aspl::Device>(
plugin->GetContext(), make_device_params(info_.config));

device_->AddStreamWithControlsAsync(aspl::Direction::Output);
device_->AddStreamWithControlsAsync(info_.config.type == DeviceType::Sender
? aspl::Direction::Output
: aspl::Direction::Input);

plugin_->AddDevice(device_);
}

Device::~Device()
{
spdlog::info("destroying device object");
spdlog::info("destroying device object, index={} uid={} type={} name=\"{}\"",
info_.index,
info_.config.uid,
info_.config.type,
info_.config.name);

plugin_->RemoveDevice(device_);

index_allocator_.release(info_.index);
}

DeviceInfo Device::info()
{
return info_;
}

} // namespace rocvad
18 changes: 17 additions & 1 deletion driver/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,40 @@

#include <memory>

#include "device_defs.hpp"
#include "index_allocator.hpp"
#include "uid_generator.hpp"

namespace rocvad {

// Correspond to one virtual device.
class Device
{
public:
Device(std::shared_ptr<aspl::Plugin> plugin);
Device(std::shared_ptr<aspl::Plugin> plugin,
IndexAllocator& index_allocator,
UidGenerator& uid_generator,
const DeviceConfig& config);
~Device();

Device(const Device&) = delete;
Device& operator=(const Device&) = delete;

DeviceInfo info();

private:
// to generate new device index and uid
IndexAllocator& index_allocator_;
UidGenerator& uid_generator_;

// objects registered in coreaudiod
// aspl::Plugin is parent object for all devices
// aspl::Device correspond to this specific device
std::shared_ptr<aspl::Plugin> plugin_;
std::shared_ptr<aspl::Device> device_;

// run-time device info
DeviceInfo info_;
};

} // namespace rocvad
61 changes: 61 additions & 0 deletions driver/device_defs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#pragma once

#include <fmt/core.h>

#include <string>
#include <string_view>

#include "index_allocator.hpp"

namespace rocvad {

// Device type.
enum class DeviceType
{
Sender,
Receiver,
};

// Device creation parameters.
struct DeviceConfig
{
DeviceType type = DeviceType::Sender;
std::string name;
std::string uid;
};

// Device run-time information
struct DeviceInfo
{
IndexAllocator::index_t index = 0;

DeviceConfig config;

DeviceInfo() = default;

DeviceInfo(const DeviceConfig& config)
: config(config)
{
}
};

} // namespace rocvad

//! Device type formatter.
template <>
struct fmt::formatter<rocvad::DeviceType> : fmt::formatter<std::string_view>
{
auto format(rocvad::DeviceType device_type, fmt::format_context& ctx) const
{
return fmt::formatter<std::string_view>::format(
device_type == rocvad::DeviceType::Sender ? "sender" : "receiver", ctx);
}
};
Loading

0 comments on commit 8fc6974

Please sign in to comment.