~aleteoryx/muditaos

ref: sign_test muditaos/module-audio/Audio/Endpoint.cpp -rw-r--r-- 2.0 KiB
a217eeb3 — Dawid Wojtas [BH-2024] Fix lack of alarm directory after updating software 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "Endpoint.hpp"

#include <algorithm>
#include <vector>

#include <cassert> // assert

using audio::AbstractStream;
using audio::Endpoint;
using audio::Sink;
using audio::Source;
using audio::StreamConnection;

void Endpoint::connectStream(AbstractStream &stream)
{
    assert(_stream == nullptr);
    _stream = &stream;
}

void Endpoint::disconnectStream()
{
    assert(_stream != nullptr);
    _stream = nullptr;
}

bool Endpoint::isConnected() const noexcept
{
    return _stream != nullptr;
}

auto Endpoint::isFormatSupported(const AudioFormat &format) -> bool
{
    const auto &formats = getSupportedFormats();
    return std::find(std::begin(formats), std::end(formats), format) != std::end(formats);
}

StreamConnection::StreamConnection(Source *source, Sink *sink, AbstractStream *stream)
    : _sink(sink), _source(source), _stream(stream)
{
    assert(_sink != nullptr);
    assert(_source != nullptr);
    assert(_stream != nullptr);

    _sink->connectStream(*_stream);
    _source->connectStream(*_stream);
}

StreamConnection::~StreamConnection()
{
    destroy();
}

void StreamConnection::destroy()
{
    disable();
    _sink->disconnectStream();
    _source->disconnectStream();
}

void StreamConnection::enable()
{
    if (enabled) {
        return;
    }

    _stream->reset();
    _sink->enableOutput();
    _source->enableInput();

    enabled = true;
}

void StreamConnection::disable()
{
    if (!enabled) {
        return;
    }

    _source->disableInput();
    _sink->disableOutput();
    _stream->reset();

    enabled = false;
}

bool StreamConnection::isEnabled() const noexcept
{
    return enabled;
}

Source *StreamConnection::getSource() const noexcept
{
    return _source;
}

Sink *StreamConnection::getSink() const noexcept
{
    return _sink;
}

AbstractStream *StreamConnection::getStream() const noexcept
{
    return _stream;
}