#include "media/midi/midi_manager_alsa.h"
#include <errno.h>
#include <poll.h>
#include <stddef.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
#include "base/posix/eintr_wrapper.h"
#include "base/posix/safe_strerror.h"
#include "base/ranges/algorithm.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "crypto/sha2.h"
#include "media/midi/midi_service.h"
#include "media/midi/midi_service.mojom.h"
#include "media/midi/task_service.h"
namespace midi {
namespace {
PortState;
Result;
enum { … };
constexpr size_t kSendBufferSize = …;
constexpr int kMinimumClientIdForCards = …;
const char kAlsaHw[] = …;
const char kUdev[] = …;
const char kUdevSubsystemSound[] = …;
const char kUdevPropertySoundInitialized[] = …;
const char kUdevActionChange[] = …;
const char kUdevActionRemove[] = …;
const char kUdevIdVendor[] = …;
const char kUdevIdVendorEnc[] = …;
const char kUdevIdVendorFromDatabase[] = …;
const char kUdevIdVendorId[] = …;
const char kUdevIdModelId[] = …;
const char kUdevIdBus[] = …;
const char kUdevIdPath[] = …;
const char kUdevIdUsbInterfaceNum[] = …;
const char kUdevIdSerialShort[] = …;
const char kSysattrVendorName[] = …;
const char kSysattrVendor[] = …;
const char kSysattrModel[] = …;
const char kSysattrGuid[] = …;
const char kCardSyspath[] = …;
constexpr unsigned int kRequiredInputPortCaps = …;
constexpr unsigned int kRequiredOutputPortCaps = …;
constexpr unsigned int kCreateOutputPortCaps = …;
constexpr unsigned int kCreateInputPortCaps = …;
constexpr unsigned int kCreatePortType = …;
int AddrToInt(int client, int port) { … }
bool IsCardClient(snd_seq_client_type_t type, int client_id) { … }
const std::string UdevDeviceGetPropertyOrSysattr(
struct udev_device* udev_device,
const char* property_key,
const char* sysattr_key) { … }
int GetCardNumber(udev_device* dev) { … }
std::string GetVendor(udev_device* dev) { … }
void SetStringIfNonEmpty(base::Value::Dict* value,
const std::string& path,
const std::string& in_value) { … }
}
MidiManagerAlsa::MidiManagerAlsa(MidiService* service) : … { … }
MidiManagerAlsa::~MidiManagerAlsa() { … }
void MidiManagerAlsa::StartInitialization() { … }
void MidiManagerAlsa::DispatchSendMidiData(MidiManagerClient* client,
uint32_t port_index,
const std::vector<uint8_t>& data,
base::TimeTicks timestamp) { … }
MidiManagerAlsa::MidiPort::Id::Id() = default;
MidiManagerAlsa::MidiPort::Id::Id(const std::string& bus,
const std::string& vendor_id,
const std::string& model_id,
const std::string& usb_interface_num,
const std::string& serial)
: … { … }
MidiManagerAlsa::MidiPort::Id::Id(const Id&) = default;
MidiManagerAlsa::MidiPort::Id::~Id() = default;
bool MidiManagerAlsa::MidiPort::Id::operator==(const Id& rhs) const { … }
bool MidiManagerAlsa::MidiPort::Id::empty() const { … }
MidiManagerAlsa::MidiPort::MidiPort(const std::string& path,
const Id& id,
int client_id,
int port_id,
int midi_device,
const std::string& client_name,
const std::string& port_name,
const std::string& manufacturer,
const std::string& version,
Type type)
: … { … }
MidiManagerAlsa::MidiPort::~MidiPort() = default;
std::unique_ptr<base::Value::Dict> MidiManagerAlsa::MidiPort::Value() const { … }
std::string MidiManagerAlsa::MidiPort::JSONValue() const { … }
std::string MidiManagerAlsa::MidiPort::OpaqueKey() const { … }
bool MidiManagerAlsa::MidiPort::MatchConnected(const MidiPort& query) const { … }
bool MidiManagerAlsa::MidiPort::MatchCardPass1(const MidiPort& query) const { … }
bool MidiManagerAlsa::MidiPort::MatchCardPass2(const MidiPort& query) const { … }
bool MidiManagerAlsa::MidiPort::MatchNoCardPass1(const MidiPort& query) const { … }
bool MidiManagerAlsa::MidiPort::MatchNoCardPass2(const MidiPort& query) const { … }
MidiManagerAlsa::MidiPortStateBase::~MidiPortStateBase() = default;
MidiManagerAlsa::MidiPortStateBase::iterator
MidiManagerAlsa::MidiPortStateBase::Find(
const MidiManagerAlsa::MidiPort& port) { … }
MidiManagerAlsa::MidiPortStateBase::iterator
MidiManagerAlsa::MidiPortStateBase::FindConnected(
const MidiManagerAlsa::MidiPort& port) { … }
MidiManagerAlsa::MidiPortStateBase::iterator
MidiManagerAlsa::MidiPortStateBase::FindDisconnected(
const MidiManagerAlsa::MidiPort& port) { … }
MidiManagerAlsa::MidiPortStateBase::MidiPortStateBase() = default;
MidiManagerAlsa::MidiPortState::MidiPortState() = default;
uint32_t MidiManagerAlsa::MidiPortState::push_back(
std::unique_ptr<MidiPort> port) { … }
MidiManagerAlsa::AlsaSeqState::AlsaSeqState() = default;
MidiManagerAlsa::AlsaSeqState::~AlsaSeqState() = default;
void MidiManagerAlsa::AlsaSeqState::ClientStart(int client_id,
const std::string& client_name,
snd_seq_client_type_t type) { … }
bool MidiManagerAlsa::AlsaSeqState::ClientStarted(int client_id) { … }
void MidiManagerAlsa::AlsaSeqState::ClientExit(int client_id) { … }
void MidiManagerAlsa::AlsaSeqState::PortStart(
int client_id,
int port_id,
const std::string& port_name,
MidiManagerAlsa::AlsaSeqState::PortDirection direction,
bool midi) { … }
void MidiManagerAlsa::AlsaSeqState::PortExit(int client_id, int port_id) { … }
snd_seq_client_type_t MidiManagerAlsa::AlsaSeqState::ClientType(
int client_id) const { … }
std::unique_ptr<MidiManagerAlsa::TemporaryMidiPortState>
MidiManagerAlsa::AlsaSeqState::ToMidiPortState(const AlsaCardMap& alsa_cards) { … }
MidiManagerAlsa::AlsaSeqState::Port::Port(
const std::string& name,
MidiManagerAlsa::AlsaSeqState::PortDirection direction,
bool midi)
: … { … }
MidiManagerAlsa::AlsaSeqState::Port::~Port() = default;
MidiManagerAlsa::AlsaSeqState::Client::Client(const std::string& name,
snd_seq_client_type_t type)
: … { … }
MidiManagerAlsa::AlsaSeqState::Client::~Client() = default;
void MidiManagerAlsa::AlsaSeqState::Client::AddPort(
int addr,
std::unique_ptr<Port> port) { … }
void MidiManagerAlsa::AlsaSeqState::Client::RemovePort(int addr) { … }
MidiManagerAlsa::AlsaSeqState::Client::PortMap::const_iterator
MidiManagerAlsa::AlsaSeqState::Client::begin() const { … }
MidiManagerAlsa::AlsaSeqState::Client::PortMap::const_iterator
MidiManagerAlsa::AlsaSeqState::Client::end() const { … }
MidiManagerAlsa::AlsaCard::AlsaCard(udev_device* dev,
const std::string& name,
const std::string& longname,
const std::string& driver,
int midi_device_count)
: … { … }
MidiManagerAlsa::AlsaCard::~AlsaCard() = default;
std::string MidiManagerAlsa::AlsaCard::ExtractManufacturerString(
const std::string& udev_id_vendor,
const std::string& udev_id_vendor_id,
const std::string& udev_id_vendor_from_database,
const std::string& alsa_name,
const std::string& alsa_longname) { … }
void MidiManagerAlsa::SendMidiData(MidiManagerClient* client,
uint32_t port_index,
const std::vector<uint8_t>& data) { … }
void MidiManagerAlsa::EventLoop() { … }
void MidiManagerAlsa::ProcessSingleEvent(snd_seq_event_t* event,
base::TimeTicks timestamp) { … }
void MidiManagerAlsa::ProcessClientStartEvent(int client_id) { … }
void MidiManagerAlsa::ProcessPortStartEvent(const snd_seq_addr_t& addr) { … }
void MidiManagerAlsa::ProcessClientExitEvent(const snd_seq_addr_t& addr) { … }
void MidiManagerAlsa::ProcessPortExitEvent(const snd_seq_addr_t& addr) { … }
void MidiManagerAlsa::ProcessUdevEvent(udev_device* dev) { … }
void MidiManagerAlsa::AddCard(udev_device* dev) { … }
void MidiManagerAlsa::RemoveCard(int number) { … }
void MidiManagerAlsa::UpdatePortStateAndGenerateEvents() { … }
void MidiManagerAlsa::EnumerateAlsaPorts() { … }
bool MidiManagerAlsa::EnumerateUdevCards() { … }
bool MidiManagerAlsa::CreateAlsaOutputPort(uint32_t port_index,
int client_id,
int port_id) { … }
void MidiManagerAlsa::DeleteAlsaOutputPort(uint32_t port_index) { … }
bool MidiManagerAlsa::Subscribe(uint32_t port_index,
int client_id,
int port_id) { … }
MidiManagerAlsa::ScopedSndMidiEventPtr
MidiManagerAlsa::CreateScopedSndMidiEventPtr(size_t size) { … }
MidiManager* MidiManager::Create(MidiService* service) { … }
}