Skip to content

Commit 36bcab8

Browse files
committed
Add FrameInfo
Depacketizers will include metadata about assembled frame.
1 parent 437a758 commit 36bcab8

5 files changed

+85
-10
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ set(LIBDATACHANNEL_SOURCES
6767
${CMAKE_CURRENT_SOURCE_DIR}/src/mediahandler.cpp
6868
${CMAKE_CURRENT_SOURCE_DIR}/src/global.cpp
6969
${CMAKE_CURRENT_SOURCE_DIR}/src/message.cpp
70+
${CMAKE_CURRENT_SOURCE_DIR}/src/frameinfo.cpp
7071
${CMAKE_CURRENT_SOURCE_DIR}/src/peerconnection.cpp
7172
${CMAKE_CURRENT_SOURCE_DIR}/src/rtcpreceivingsession.cpp
7273
${CMAKE_CURRENT_SOURCE_DIR}/src/track.cpp
@@ -99,6 +100,7 @@ set(LIBDATACHANNEL_HEADERS
99100
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/common.hpp
100101
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/global.hpp
101102
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/message.hpp
103+
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/frameinfo.hpp
102104
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/peerconnection.hpp
103105
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/reliability.hpp
104106
${CMAKE_CURRENT_SOURCE_DIR}/include/rtc/rtc.h

include/rtc/frameinfo.hpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) 2019-2020 Paul-Louis Ageneau
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#ifndef RTC_FRAMEINFO_H
10+
#define RTC_FRAMEINFO_H
11+
12+
#if RTC_ENABLE_MEDIA
13+
14+
#include "common.hpp"
15+
#include "message.hpp"
16+
17+
namespace rtc {
18+
19+
struct RTC_CPP_EXPORT FrameInfo : Message {
20+
FrameInfo(binary &&data, uint32_t timestamp) : Message(std::move(data)), timestamp(timestamp) {}
21+
22+
template <typename Iterator>
23+
FrameInfo(Iterator begin_, Iterator end_, uint32_t timestamp)
24+
: Message(begin_, end_, Binary), timestamp(timestamp) {}
25+
26+
uint32_t timestamp = 0; // RTP Timestamp
27+
};
28+
29+
using frame_info_ptr = shared_ptr<FrameInfo>;
30+
using frame_info_callback = std::function<void(frame_info_ptr)>;
31+
using frame_info_vector = std::vector<frame_info_ptr>;
32+
33+
template <typename Iterator>
34+
frame_info_ptr make_frame_info(Iterator begin, Iterator end, uint32_t timestamp) {
35+
auto frame_info = std::make_shared<FrameInfo>(begin, end, timestamp);
36+
frame_info->stream = 0;
37+
frame_info->reliability = nullptr;
38+
return frame_info;
39+
}
40+
41+
RTC_CPP_EXPORT frame_info_ptr make_frame_info(binary &&data, uint32_t timestamp);
42+
43+
} // namespace rtc
44+
45+
#endif // RTC_ENABLE_MEDIA
46+
#endif // RTC_FRAMEINFO_H

include/rtc/h264rtpdepacketizer.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#if RTC_ENABLE_MEDIA
1414

1515
#include "common.hpp"
16+
#include "frameinfo.hpp"
1617
#include "mediahandler.hpp"
17-
#include "message.hpp"
1818
#include "rtp.hpp"
1919

2020
#include <iterator>
@@ -32,7 +32,8 @@ class RTC_CPP_EXPORT H264RtpDepacketizer : public MediaHandler {
3232
private:
3333
std::vector<message_ptr> mRtpBuffer;
3434

35-
message_vector buildFrames(message_vector::iterator firstPkt, message_vector::iterator lastPkt);
35+
frame_info_vector buildFrameInfos(message_vector::iterator firstPkt,
36+
message_vector::iterator lastPkt, uint32_t timestamp);
3637
};
3738

3839
} // namespace rtc

src/frameinfo.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2019-2020 Paul-Louis Ageneau
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
7+
*/
8+
9+
#if RTC_ENABLE_MEDIA
10+
11+
#include "frameinfo.hpp"
12+
13+
namespace rtc {
14+
15+
frame_info_ptr make_frame_info(binary &&data, uint32_t timestamp) {
16+
auto frame_info = std::make_shared<FrameInfo>(std::move(data), timestamp);
17+
frame_info->stream = 0;
18+
frame_info->reliability = nullptr;
19+
return frame_info;
20+
}
21+
22+
} // namespace rtc
23+
24+
#endif // RTC_ENABLE_MEDIA
25+

src/h264rtpdepacketizer.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ const auto fuaHeaderSize = 2;
3131
const uint8_t naluTypeSTAPA = 24;
3232
const uint8_t naluTypeFUA = 28;
3333

34-
message_vector H264RtpDepacketizer::buildFrames(message_vector::iterator begin,
35-
message_vector::iterator end) {
36-
message_vector out = {};
34+
frame_info_vector H264RtpDepacketizer::buildFrameInfos(message_vector::iterator begin,
35+
message_vector::iterator end,
36+
uint32_t timestamp) {
37+
frame_info_vector out = {};
3738
auto fua_buffer = std::vector<std::byte>{};
3839

3940
for (auto it = begin; it != end; it++) {
@@ -58,11 +59,11 @@ message_vector H264RtpDepacketizer::buildFrames(message_vector::iterator begin,
5859
fua_buffer.at(0) =
5960
std::byte(nalUnitHeader.idc() | nalUnitFragmentHeader.unitType());
6061

61-
out.push_back(make_message(std::move(fua_buffer)));
62+
out.push_back(make_frame_info(std::move(fua_buffer), timestamp));
6263
fua_buffer.clear();
6364
}
6465
} else if (nalUnitHeader.unitType() > 0 && nalUnitHeader.unitType() < 24) {
65-
out.push_back(make_message(pkt->begin() + headerSize, pkt->end()));
66+
out.push_back(make_frame_info(pkt->begin() + headerSize, pkt->end(), timestamp));
6667
} else if (nalUnitHeader.unitType() == naluTypeSTAPA) {
6768
auto currOffset = stapaHeaderSize + headerSize;
6869

@@ -76,8 +77,8 @@ message_vector H264RtpDepacketizer::buildFrames(message_vector::iterator begin,
7677
throw std::runtime_error("STAP-A declared size is larger then buffer");
7778
}
7879

79-
out.push_back(
80-
make_message(pkt->begin() + currOffset, pkt->begin() + currOffset + naluSize));
80+
out.push_back(make_frame_info(pkt->begin() + currOffset,
81+
pkt->begin() + currOffset + naluSize, timestamp));
8182
currOffset += naluSize;
8283
}
8384

@@ -128,7 +129,7 @@ void H264RtpDepacketizer::incoming(message_vector &messages, const message_callb
128129
auto begin = mRtpBuffer.begin();
129130
auto end = mRtpBuffer.begin() + (packets_in_timestamp - 1);
130131

131-
auto frames = buildFrames(begin, end + 1);
132+
auto frames = buildFrameInfos(begin, end + 1, current_timestamp);
132133
messages.insert(messages.end(), frames.begin(), frames.end());
133134
mRtpBuffer.erase(mRtpBuffer.begin(), mRtpBuffer.begin() + packets_in_timestamp);
134135
}

0 commit comments

Comments
 (0)