Files
MuditaOS/module-audio/Audio/Endpoint.cpp
Marcin Smoczyński 40bf381eca [EGD-5086] Fix voice not starting when calling
Due to a race condition between source and sink voice is not always
starting when calling. Introduce audio stream connections to avoid
race condition and improve handling of audio start and stop operations.

Signed-off-by: Marcin Smoczyński <smoczynski.marcin@gmail.com>
2021-01-14 11:03:47 +01:00

103 lines
1.8 KiB
C++

// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "Endpoint.hpp"
#include <cassert> // assert
using namespace audio;
Endpoint::Endpoint(const Capabilities &caps) : _caps(caps)
{}
const Endpoint::Capabilities &Endpoint::getCapabilities() const noexcept
{
return _caps;
}
void Endpoint::connectStream(Stream &stream)
{
assert(_stream == nullptr);
_stream = &stream;
}
void Endpoint::disconnectStream()
{
assert(_stream != nullptr);
_stream = nullptr;
}
bool Endpoint::isConnected() const noexcept
{
return _stream != nullptr;
}
StreamConnection::StreamConnection(Source *source, Sink *sink, Stream *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;
}
Stream *StreamConnection::getStream() const noexcept
{
return _stream;
}