#include "track.hpp"
#include "dtlssrtptransport.hpp"
#include "include.hpp"
#include "logcounter.hpp"
static rtc::LogCounter
COUNTER_MEDIA_BAD_DIRECTION(plog::warning,
"Number of media packets sent in invalid directions");
static rtc::LogCounter COUNTER_QUEUE_FULL(plog::warning,
"Number of media packets dropped due to a full queue");
namespace rtc {
using std::shared_ptr;
using std::weak_ptr;
Track::Track(Description::Media description)
: mMediaDescription(std::move(description)), mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {}
string Track::mid() const { return mMediaDescription.mid(); }
Description::Media Track::description() const { return mMediaDescription; }
void Track::setDescription(Description::Media description) {
if (description.mid() != mMediaDescription.mid())
throw std::logic_error("Media description mid does not match track mid");
mMediaDescription = std::move(description);
}
void Track::close() {
mIsClosed = true;
resetCallbacks();
setRtcpHandler(nullptr);
}
bool Track::send(message_variant data) {
if (mIsClosed)
throw std::runtime_error("Track is closed");
auto direction = mMediaDescription.direction();
if ((direction == Description::Direction::RecvOnly ||
direction == Description::Direction::Inactive)) {
COUNTER_MEDIA_BAD_DIRECTION++;
return false;
}
auto message = make_message(std::move(data));
if (auto handler = getRtcpHandler()) {
message = handler->outgoing(message);
if (!message)
return false;
}
return outgoing(std::move(message));
}
bool Track::send(const byte *data, size_t size) { return send(binary(data, data + size)); }
std::optional<message_variant> Track::receive() {
if (auto next = mRecvQueue.tryPop())
return to_variant(std::move(**next));
return nullopt;
}
std::optional<message_variant> Track::peek() {
if (auto next = mRecvQueue.peek())
return to_variant(std::move(**next));
return nullopt;
}
bool Track::isOpen(void) const {
#if RTC_ENABLE_MEDIA
return !mIsClosed && mDtlsSrtpTransport.lock();
#else
return !mIsClosed;
#endif
}
bool Track::isClosed(void) const { return mIsClosed; }
size_t Track::maxMessageSize() const {
return 65535 - 12 - 4; }
size_t Track::availableAmount() const { return mRecvQueue.amount(); }
#if RTC_ENABLE_MEDIA
void Track::open(shared_ptr<DtlsSrtpTransport> transport) {
mDtlsSrtpTransport = transport;
triggerOpen();
}
#endif
void Track::incoming(message_ptr message) {
if (!message)
return;
auto direction = mMediaDescription.direction();
if ((direction == Description::Direction::SendOnly ||
direction == Description::Direction::Inactive) &&
message->type != Message::Control) {
COUNTER_MEDIA_BAD_DIRECTION++;
return;
}
if (auto handler = getRtcpHandler()) {
message = handler->incoming(message);
if (!message)
return;
}
if (mRecvQueue.full()) {
COUNTER_QUEUE_FULL++;
return;
}
mRecvQueue.push(message);
triggerAvailable(mRecvQueue.size());
}
bool Track::outgoing([[maybe_unused]] message_ptr message) {
#if RTC_ENABLE_MEDIA
auto transport = mDtlsSrtpTransport.lock();
if (!transport)
throw std::runtime_error("Track transport is not open");
if (mMediaDescription.type() == "audio")
message->dscp = 46; else
message->dscp = 36;
return transport->sendMedia(message);
#else
PLOG_WARNING << "Ignoring track send (not compiled with media support)";
return false;
#endif
}
void Track::setRtcpHandler(std::shared_ptr<MediaHandler> handler) {
std::unique_lock lock(mRtcpHandlerMutex);
mRtcpHandler = std::move(handler);
if (mRtcpHandler) {
auto copy = mRtcpHandler;
lock.unlock();
copy->onOutgoing(std::bind(&Track::outgoing, this, std::placeholders::_1));
}
}
bool Track::requestKeyframe() {
if (auto handler = getRtcpHandler()) {
return handler->requestKeyframe();
}
return false;
}
std::shared_ptr<MediaHandler> Track::getRtcpHandler() {
std::shared_lock lock(mRtcpHandlerMutex);
return mRtcpHandler;
}
}