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 <webrtc/RTPSession.h> 20 #include <webrtc/RTPSocketHandler.h> 21 #include <webrtc/SDP.h> 22 #include <webrtc/ServerState.h> 23 24 #include <https/WebSocketHandler.h> 25 #include <https/RunLoop.h> 26 #include <source/KeyboardSink.h> 27 #include <source/TouchSink.h> 28 29 #include <memory> 30 #include <optional> 31 #include <sstream> 32 #include <string> 33 #include <vector> 34 35 struct MyWebSocketHandler 36 : public WebSocketHandler, 37 public std::enable_shared_from_this<MyWebSocketHandler> { 38 39 explicit MyWebSocketHandler( 40 std::shared_ptr<RunLoop> runLoop, 41 std::shared_ptr<ServerState> serverState, 42 size_t handlerId); 43 44 ~MyWebSocketHandler() override; 45 46 int handleMessage( 47 uint8_t headerByte, const uint8_t *msg, size_t len) override; 48 49 private: 50 enum OptionBits : uint32_t { 51 disableAudio = 1, 52 bundleTracks = 2, 53 enableData = 4, 54 useSingleCertificateForAllTracks = 8, 55 useTCP = 16, 56 }; 57 58 using TouchSink = android::TouchSink; 59 using KeyboardSink = android::KeyboardSink; 60 61 std::shared_ptr<RunLoop> mRunLoop; 62 std::shared_ptr<ServerState> mServerState; 63 size_t mId; 64 uint32_t mOptions; 65 66 // Vector has the same ordering as the media entries in the SDP, i.e. 67 // vector index is "mlineIndex". (unless we are bundling, in which case 68 // there is only a single session). 69 std::vector<std::shared_ptr<RTPSession>> mSessions; 70 71 SDP mOfferedSDP; 72 std::vector<std::shared_ptr<RTPSocketHandler>> mRTPs; 73 74 std::shared_ptr<TouchSink> mTouchSink; 75 std::shared_ptr<KeyboardSink> mKeyboardSink; 76 77 std::pair<std::shared_ptr<X509>, std::shared_ptr<EVP_PKEY>> 78 mCertificateAndKey; 79 80 // Pass -1 for mlineIndex to access the "general" section. 81 std::optional<std::string> getSDPValue( 82 ssize_t mlineIndex, 83 std::string_view key, 84 bool fallthroughToGeneralSection) const; 85 86 std::string getRemotePassword(size_t mlineIndex) const; 87 std::string getRemoteUFrag(size_t mlineIndex) const; 88 std::string getRemoteFingerprint(size_t mlineIndex) const; 89 90 bool getCandidate(int32_t mid); 91 92 static std::pair<std::shared_ptr<X509>, std::shared_ptr<EVP_PKEY>> 93 CreateDTLSCertificateAndKey(); 94 95 std::pair<std::string, std::string> createUniqueUFragAndPassword(); 96 97 void parseOptions(const std::string &pathAndQuery); 98 size_t countTracks() const; 99 100 void prepareSessions(); 101 102 void emitTrackIceOptionsAndFingerprint( 103 std::stringstream &ss, size_t mlineIndex) const; 104 105 // Returns -1 on error. 106 ssize_t mlineIndexForMid(int32_t mid) const; 107 108 static void CreateRandomIceCharSequence(char *dst, size_t size); 109 }; 110 111 112