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 <https/PlainSocket.h>
20 #include <https/RunLoop.h>
21 #include <webrtc/DTLS.h>
22 #include <webrtc/RTPSender.h>
23 #include <webrtc/RTPSession.h>
24 #include <webrtc/SCTPHandler.h>
25 #include <webrtc/ServerState.h>
26 #include <webrtc/STUNMessage.h>
27 
28 #include <memory>
29 #include <string_view>
30 #include <vector>
31 
32 struct MyWebSocketHandler;
33 
34 struct RTPSocketHandler
35     : public std::enable_shared_from_this<RTPSocketHandler> {
36 
37     static constexpr size_t kMaxUDPPayloadSize = 1536;
38 
39     static constexpr uint32_t TRACK_VIDEO = 1;
40     static constexpr uint32_t TRACK_AUDIO = 2;
41     static constexpr uint32_t TRACK_DATA  = 4;
42 
43     enum class TransportType {
44         UDP,
45         TCP,
46     };
47 
48     explicit RTPSocketHandler(
49             std::shared_ptr<RunLoop> runLoop,
50             std::shared_ptr<ServerState> serverState,
51             TransportType type,
52             int domain,
53             uint32_t trackMask,
54             std::shared_ptr<RTPSession> session);
55 
56     uint16_t getLocalPort() const;
57     std::string getLocalUFrag() const;
58     std::string getLocalIPString() const;
59 
60     void run();
61 
62     void queueDatagram(
63             const sockaddr_storage &addr, const void *data, size_t size);
64 
65     void queueRTCPDatagram(const void *data, size_t size);
66     void queueRTPDatagram(const void *data, size_t size);
67 
68     void notifyDTLSConnected();
69 
70 private:
71     struct Datagram {
72         explicit Datagram(
73                 const sockaddr_storage &addr, const void *data, size_t size);
74 
75         const void *data() const;
76         size_t size() const;
77 
78         const sockaddr_storage &remoteAddress() const;
79 
80     private:
81         std::vector<uint8_t> mData;
82         sockaddr_storage mAddr;
83     };
84 
85     std::shared_ptr<RunLoop> mRunLoop;
86     std::shared_ptr<ServerState> mServerState;
87     TransportType mTransportType;
88     uint16_t mLocalPort;
89     uint32_t mTrackMask;
90     std::shared_ptr<RTPSession> mSession;
91 
92     std::shared_ptr<BufferedSocket> mSocket;
93     std::shared_ptr<DTLS> mDTLS;
94     std::shared_ptr<SCTPHandler> mSCTPHandler;
95 
96     std::deque<std::shared_ptr<Datagram>> mOutQueue;
97     bool mSendPending;
98     bool mDTLSConnected;
99 
100     std::shared_ptr<RTPSender> mRTPSender;
101 
102     // for TransportType TCP:
103     std::shared_ptr<PlainSocket> mServerSocket;
104     sockaddr_storage mClientAddr;
105     socklen_t mClientAddrLen;
106 
107     std::vector<uint8_t> mInBuffer;
108     size_t mInBufferLength;
109 
110     std::vector<uint8_t> mOutBuffer;
111 
112     void onReceive();
113     void onDTLSReceive(const uint8_t *data, size_t size);
114 
115     void pingRemote(std::shared_ptr<RTPSession> session);
116 
117     bool matchesSession(const STUNMessage &msg) const;
118 
119     void scheduleDrainOutQueue();
120     void drainOutQueue();
121 
122     int onSRTPReceive(uint8_t *data, size_t size);
123 
124     void onTCPConnect();
125     void onTCPReceive();
126 
127     void onPacketReceived(
128             const sockaddr_storage &addr,
129             socklen_t addrLen,
130             uint8_t *data,
131             size_t size);
132 
133     void queueTCPOutputPacket(const uint8_t *data, size_t size);
134     void sendTCPOutputData();
135 };
136 
137 
138