1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "Packetizer.h"
20 
21 #include <https/RunLoop.h>
22 
23 #include <memory>
24 #include <optional>
25 #include <unordered_map>
26 #include <vector>
27 
28 struct RTPSocketHandler;
29 
30 struct RTPSender : public std::enable_shared_from_this<RTPSender> {
31 
32     explicit RTPSender(
33             std::shared_ptr<RunLoop> runLoop,
34             RTPSocketHandler *parent,
35             std::shared_ptr<Packetizer> videoPacketizer,
36             std::shared_ptr<Packetizer> audioPacketizer);
37 
38     void addSource(uint32_t ssrc);
39 
40     void addRetransInfo(
41             uint32_t ssrc, uint8_t PT, uint32_t retransSSRC, uint8_t retransPT);
42 
43     int injectRTCP(uint8_t *data, size_t size);
44     void queueRTPDatagram(std::vector<uint8_t> *packet);
45 
46     void run();
47 
48     void requestIDRFrame();
49 
50 private:
51     struct SourceInfo {
SourceInfoRTPSender::SourceInfo52         explicit SourceInfo()
53             : mNumPacketsSent(0),
54               mNumBytesSent(0) {
55         }
56 
57         size_t mNumPacketsSent;
58         size_t mNumBytesSent;
59 
60         // (ssrc, PT) by PT.
61         std::unordered_map<uint8_t, std::pair<uint32_t, uint8_t>> mRetrans;
62 
63         std::deque<std::vector<uint8_t>> mRecentPackets;
64     };
65 
66     std::shared_ptr<RunLoop> mRunLoop;
67     RTPSocketHandler *mParent;
68 
69     // Sources by ssrc.
70     std::unordered_map<uint32_t, SourceInfo> mSources;
71 
72     std::shared_ptr<Packetizer> mVideoPacketizer;
73     std::shared_ptr<Packetizer> mAudioPacketizer;
74 
75     void appendSR(std::vector<uint8_t> *buffer, uint32_t localSSRC);
76     void appendSDES(std::vector<uint8_t> *buffer, uint32_t localSSRC);
77 
78     void queueSR(uint32_t localSSRC);
79     void sendSR(uint32_t localSSRC);
80 
81     void queueDLRR(
82             uint32_t localSSRC,
83             uint32_t remoteSSRC,
84             uint32_t ntpHi,
85             uint32_t ntpLo);
86 
87     void appendDLRR(
88             std::vector<uint8_t> *buffer,
89             uint32_t localSSRC,
90             uint32_t remoteSSRC,
91             uint32_t ntpHi,
92             uint32_t ntpLo);
93 
94     int processRTCP(const uint8_t *data, size_t size);
95 
96     void retransmitPackets(uint32_t localSSRC, uint16_t PID, uint16_t BLP);
97 };
98 
99