-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrtp_packer.cpp
69 lines (61 loc) · 1.73 KB
/
rtp_packer.cpp
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
//
// Created by hijiang on 2019/8/8.
//
#include <assert.h>
#include "rtp_packer.h"
RtpPacker::RtpPacker()
{
}
uint16_t RtpPacker::getSequenceNum()
{
if (++rtp_seqnum_ >= 65535)
{
rtp_seqnum_ = 0;
}
return rtp_seqnum_;
}
std::vector<std::shared_ptr<RtpPacket>> RtpPacker::pack(std::unique_ptr<char[]> data, size_t len, uint8_t pt, int ssrc, int64_t pts)
{
assert(len > 0);
data_ = std::move(data);//hold the data
len_ = len;
std::vector<std::shared_ptr<RtpPacket>> pkts;
size_t left_len = len;
int curr_pos = 0;
while(left_len > 0)
{
int consume_count = 0;
std::shared_ptr<RtpPacket> pkt;
if(left_len < NAL_RTP_PACKET_SIZE)
{
pkt = std::make_shared<RtpPacket>();
pkt->header_.pt = pt;
pkt->header_.timestamp = pts;
pkt->header_.marker = 1;
pkt->header_.seqnum = getSequenceNum();
pkt->header_.ssrc = ssrc;
pkt->payload_ = data_.get() + curr_pos;
pkt->payload_len_ = left_len;
consume_count = left_len;
}
else
{
pkt = std::make_shared<RtpPacket>();
pkt->header_.pt = pt;
pkt->header_.timestamp = pts;
pkt->header_.marker = 0;
pkt->header_.seqnum = getSequenceNum();
pkt->header_.ssrc = ssrc;
pkt->payload_ = data_.get() + curr_pos;
pkt->payload_len_ = NAL_RTP_PACKET_SIZE;
consume_count = NAL_RTP_PACKET_SIZE;
}
pkts.push_back(pkt);
curr_pos += consume_count;
left_len -= consume_count;
}
return pkts;
}
RtpPacker::~RtpPacker()
{
}