Skip to content

Commit

Permalink
Add AAC extend Support
Browse files Browse the repository at this point in the history
Signed-off-by: tstan <[email protected]>
  • Loading branch information
tstan123 committed Aug 28, 2023
1 parent a3b2fce commit 4b9c414
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ set(LIBDATACHANNEL_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/rtppacketizer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/opusrtppacketizer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/opuspacketizationhandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/aacrtppacketizer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/h264rtppacketizer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/nalunit.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/h264packetizationhandler.cpp
Expand Down Expand Up @@ -112,6 +113,7 @@ set(LIBDATACHANNEL_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtppacketizer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/opusrtppacketizer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/opuspacketizationhandler.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/aacrtppacketizer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/h264rtppacketizer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/nalunit.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/h264packetizationhandler.hpp
Expand Down
61 changes: 61 additions & 0 deletions include/rtc/aacrtppacketizer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright (c) 2020 Filip Klembara (in2core)
*
* 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 https://mozilla.org/MPL/2.0/.
*/

#ifndef RTC_AAC_RTP_PACKETIZER_H
#define RTC_AAC_RTP_PACKETIZER_H

#if RTC_ENABLE_MEDIA

#include "mediahandlerrootelement.hpp"
#include "mediachainablehandler.hpp"
#include "rtppacketizer.hpp"

namespace rtc {

/// RTP packetizer for aac
class RTC_CPP_EXPORT AACRtpPacketizer final : public RtpPacketizer,
public MediaHandlerRootElement {
public:
/// default clock rate used in aac RTP communication
inline static const uint32_t defaultClockRate = 48 * 1000;

/// Constructs aac packetizer with given RTP configuration.
/// @note RTP configuration is used in packetization process which may change some configuration
/// properties such as sequence number.
/// @param rtpConfig RTP configuration
AACRtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig);

/// Creates RTP packet for given payload based on `rtpConfig`.
/// @note This function increase sequence number after packetization.
/// @param payload RTP payload
/// @param setMark This needs to be `false` for all RTP packets with aac payload
binary_ptr packetize(binary_ptr payload, bool setMark) override;

/// Creates RTP packet for given samples (all samples share same RTP timesamp)
/// @param messages aac samples
/// @param control RTCP
/// @returns RTP packets and unchanged `control`
ChainedOutgoingProduct processOutgoingBinaryMessage(ChainedMessagesProduct messages,
message_ptr control) override;
};

/// Handler for aac packetization
class RTC_CPP_EXPORT AACPacketizationHandler final : public MediaChainableHandler {

public:
/// Construct handler for aac packetization.
/// @param packetizer RTP packetizer for aac
AACPacketizationHandler(shared_ptr<AACRtpPacketizer> packetizer)
: MediaChainableHandler(packetizer) {}
};

} // namespace rtc

#endif /* RTC_ENABLE_MEDIA */

#endif /* RTC_AAC_RTP_PACKETIZER_H */
2 changes: 2 additions & 0 deletions include/rtc/description.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ class RTC_CPP_EXPORT Description {
void addPCMACodec(int payloadType, optional<string> profile = std::nullopt);

void addPCMUCodec(int payloadType, optional<string> profile = std::nullopt);

void addAacCodec(int payloadType, optional<string> latmMuxCfg = std::nullopt);
};

class RTC_CPP_EXPORT Video : public Media {
Expand Down
9 changes: 8 additions & 1 deletion include/rtc/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ typedef enum {
// audio
RTC_CODEC_OPUS = 128,
RTC_CODEC_PCMU = 129,
RTC_CODEC_PCMA = 130
RTC_CODEC_PCMA = 130,
RTC_CODEC_AAC = 131,
} rtcCodec;

typedef enum {
Expand Down Expand Up @@ -273,6 +274,9 @@ typedef struct {
const char *name; // optional
const char *msid; // optional
const char *trackId; // optional, track ID used in MSID

//AAC
const char *latmMuxCfg;
} rtcTrackInit;

RTC_C_EXPORT int rtcSetTrackCallback(int pc, rtcTrackCallbackFunc cb);
Expand Down Expand Up @@ -343,6 +347,9 @@ RTC_C_EXPORT int rtcSetH264PacketizationHandler(int tr, const rtcPacketizationHa
// Set OpusPacketizationHandler for track
RTC_C_EXPORT int rtcSetOpusPacketizationHandler(int tr, const rtcPacketizationHandlerInit *init);

// Set AACPacketizationHandler for track
RTC_C_EXPORT int rtcSetAACPacketizationHandler(int tr, const rtcPacketizationHandlerInit *init);

// Chain RtcpSrReporter to handler chain for given track
RTC_C_EXPORT int rtcChainRtcpSrReporter(int tr);

Expand Down
1 change: 1 addition & 0 deletions include/rtc/rtc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@
#include "h264packetizationhandler.hpp"
#include "av1packetizationhandler.hpp"
#include "opuspacketizationhandler.hpp"
#include "aacrtppacketizer.hpp"

#endif // RTC_ENABLE_MEDIA
38 changes: 38 additions & 0 deletions src/aacrtppacketizer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2020 Filip Klembara (in2core)
*
* 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 https://mozilla.org/MPL/2.0/.
*/

#if RTC_ENABLE_MEDIA

#include "aacrtppacketizer.hpp"

#include <cassert>

namespace rtc {

AACRtpPacketizer::AACRtpPacketizer(shared_ptr<RtpPacketizationConfig> rtpConfig)
: RtpPacketizer(rtpConfig), MediaHandlerRootElement() {}

binary_ptr AACRtpPacketizer::packetize(binary_ptr payload, [[maybe_unused]] bool setMark) {
assert(!setMark);
return RtpPacketizer::packetize(payload, false);
}

ChainedOutgoingProduct
AACRtpPacketizer::processOutgoingBinaryMessage(ChainedMessagesProduct messages,
message_ptr control) {
ChainedMessagesProduct packets = make_chained_messages_product();
packets->reserve(messages->size());
for (auto message : *messages) {
packets->push_back(packetize(message, false));
}
return {packets, control};
}

} // namespace rtc

#endif /* RTC_ENABLE_MEDIA */
24 changes: 23 additions & 1 deletion src/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,7 @@ int rtcAddTrackEx(int pc, const rtcTrackInit *init) {
case RTC_CODEC_OPUS:
case RTC_CODEC_PCMU:
case RTC_CODEC_PCMA:
case RTC_CODEC_AAC:
mid = "audio";
break;
default:
Expand Down Expand Up @@ -1059,7 +1060,8 @@ int rtcAddTrackEx(int pc, const rtcTrackInit *init) {
}
case RTC_CODEC_OPUS:
case RTC_CODEC_PCMU:
case RTC_CODEC_PCMA: {
case RTC_CODEC_PCMA:
case RTC_CODEC_AAC: {
auto desc = Description::Audio(mid, direction);
switch (init->codec) {
case RTC_CODEC_OPUS:
Expand All @@ -1071,6 +1073,9 @@ int rtcAddTrackEx(int pc, const rtcTrackInit *init) {
case RTC_CODEC_PCMA:
desc.addPCMACodec(init->payloadType);
break;
case RTC_CODEC_AAC:
desc.addAacCodec(init->payloadType, init->latmMuxCfg);
break;
default:
break;
}
Expand Down Expand Up @@ -1227,6 +1232,23 @@ int rtcSetOpusPacketizationHandler(int tr, const rtcPacketizationHandlerInit *in
});
}

int rtcSetAACPacketizationHandler(int tr, const rtcPacketizationHandlerInit *init) {
return wrap([&] {
auto track = getTrack(tr);
// create RTP configuration
auto rtpConfig = createRtpPacketizationConfig(init);
// create packetizer
auto packetizer = std::make_shared<AACRtpPacketizer>(rtpConfig);
// create AAC handler
auto aacHandler = std::make_shared<AACPacketizationHandler>(packetizer);
emplaceMediaChainableHandler(aacHandler, tr);
emplaceRtpConfig(rtpConfig, tr);
// set handler
track->setMediaHandler(aacHandler);
return RTC_ERR_SUCCESS;
});
}

int rtcChainRtcpSrReporter(int tr) {
return wrap([tr] {
auto config = getRtpConfig(tr);
Expand Down
11 changes: 11 additions & 0 deletions src/description.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,17 @@ void Description::Audio::addPCMUCodec(int payloadType, optional<string> profile)
addAudioCodec(payloadType, "PCMU", profile);
}

void Description::Audio::addAacCodec(int payloadType, optional<string> latmMuxCfg) {
if (latmMuxCfg) {
string profile = "cpresent=0;config=";
profile += *latmMuxCfg;
addAudioCodec(payloadType, "MP4A-LATM", profile);
} else {
addAudioCodec(payloadType, "MP4A-LATM", "cpresent=1");
}

}

Description::Video::Video(string mid, Direction dir)
: Media("video 9 UDP/TLS/RTP/SAVPF", std::move(mid), dir) {}

Expand Down

0 comments on commit 4b9c414

Please sign in to comment.