• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_SHARING_RTCP_CONTEXT_H
17 #define OHOS_SHARING_RTCP_CONTEXT_H
18 
19 #include <map>
20 #include <cstdint>
21 #include "rtcp.h"
22 #include "rtcp_def.h"
23 #include "utils/data_buffer.h"
24 
25 namespace OHOS {
26 namespace Sharing {
27 class RtcpContext {
28 public:
29     virtual ~RtcpContext() = default;
30 
31     virtual void OnRtcp(RtcpHeader *rtcp) = 0;
32 
33     // seq: rtp seq, stamp: rtp stamp(samples), sampleRate: 90000 for video, bytes: rtp packet size
34     virtual void OnRtp(uint16_t seq, uint32_t stamp, uint64_t ntpStampMs, uint32_t sampleRate, size_t bytes);
35 
CreateRtcpSR(uint32_t rtcpSSRC)36     virtual DataBuffer::Ptr CreateRtcpSR(uint32_t rtcpSSRC)
37     {
38         return nullptr;
39     }
40 
CreateRtcpXRDLRR(uint32_t rtcpSSRC,uint32_t rtpSSRC)41     virtual DataBuffer::Ptr CreateRtcpXRDLRR(uint32_t rtcpSSRC, uint32_t rtpSSRC)
42     {
43         return nullptr;
44     }
45 
CreateRtcpRR(uint32_t rtcpSSRC,uint32_t rtpSSRC)46     virtual DataBuffer::Ptr CreateRtcpRR(uint32_t rtcpSSRC, uint32_t rtpSSRC)
47     {
48         return nullptr;
49     }
50 
GetLost()51     virtual size_t GetLost()
52     {
53         return 0;
54     }
55 
GetLostInterval()56     virtual size_t GetLostInterval()
57     {
58         return 0;
59     }
60 
GetExpectedPackets()61     virtual size_t GetExpectedPackets() const
62     {
63         return 0;
64     }
65 
GetExpectedPacketsInterval()66     virtual size_t GetExpectedPacketsInterval()
67     {
68         return 0;
69     }
70 
71 protected:
72     uint32_t lastRtpStamp_ = 0;
73     // size of rtp packets
74     size_t bytes_ = 0;
75     // number of rtp packets
76     size_t packets_ = 0;
77     uint64_t lastNtpStampMs_ = 0;
78 };
79 
80 class RtcpSenderContext : public RtcpContext {
81 public:
82     uint32_t GetRtt(uint32_t ssrc) const;
83     void OnRtcp(RtcpHeader *rtcp) override;
84 
85     DataBuffer::Ptr CreateRtcpSR(uint32_t rtcp_ssrc) override;
86     DataBuffer::Ptr CreateRtcpXRDLRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc) override;
87 
88 private:
89     std::map<uint32_t, uint32_t> rtt_; // ssrc, rtt
90     std::map<uint32_t, uint32_t> xrXrrtrRecvLastRr_; // ssrc, last rr
91     std::map<uint32_t, uint64_t> senderReportNtp_; // last sr, lsr ntp stamp
92     std::map<uint32_t, uint64_t> xrRrtrRecvSysStamp_; // ssrc, xr rrtr sys stamp
93 };
94 
95 class RtcpReceiverContext : public RtcpContext {
96 public:
97     using Ptr = std::shared_ptr<RtcpReceiverContext>;
98 
99     void OnRtcp(RtcpHeader *rtcp) override;
100     void OnRtp(uint16_t seq, uint32_t stamp, uint64_t ntpStampMs, uint32_t sampleRate, size_t bytes) override;
101 
102     DataBuffer::Ptr CreateRtcpRR(uint32_t rtcp_ssrc, uint32_t rtp_ssrc) override;
103 
104     size_t GetLost() override;
105     size_t GetLostInterval() override;
106     size_t GetExpectedPackets() const override;
107     size_t GetExpectedPacketsInterval() override;
108 
109 private:
110     // max rtp seq num
111     uint16_t seqMax_ = 0;
112     // first seq num
113     uint16_t seqBase_ = 0;
114     // rtp cycle num
115     uint16_t seqCycles_ = 0;
116     uint16_t lastRtpSeq_ = 0;
117 
118     // last SR timestamp
119     uint32_t lastSrLsr_ = 0;
120 
121     // timestamp jitter
122     double jitter_ = 0;
123     // ntp system time
124     uint64_t lastSrNtpSys_ = 0;
125     uint64_t lastRtpSysStamp_ = 0;
126 
127     size_t lastLost_ = 0;
128     // last expected rtp packet num
129     size_t lastExpected_ = 0;
130     // rtp packet num of last
131     size_t lastCyclePackets_ = 0;
132 };
133 
134 } // namespace Sharing
135 } // namespace OHOS
136 
137 #endif