1 /*
2  * Copyright (C) 2020 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 <map>
20 #include <memory>
21 #include <vector>
22 
23 #include <json/json.h>
24 
25 #include <api/peer_connection_interface.h>
26 
27 #include "common/libs/utils/result.h"
28 
29 namespace cuttlefish {
30 namespace webrtc_streaming {
31 
32 // Helper method to ensure a json object has the required fields convertible
33 // to the appropriate types.
34 Result<void> ValidateJsonObject(
35     const Json::Value& obj, const std::string& type,
36     const std::map<std::string, Json::ValueType>& required_fields,
37     const std::map<std::string, Json::ValueType>& optional_fields = {});
38 
39 // Parses a session description object from a JSON message.
40 Result<std::unique_ptr<webrtc::SessionDescriptionInterface>>
41 ParseSessionDescription(const std::string& type, const Json::Value& message,
42                         webrtc::SdpType sdp_type);
43 
44 // Parses an IceCandidate from a JSON message.
45 Result<std::unique_ptr<webrtc::IceCandidateInterface>> ParseIceCandidate(
46     const std::string& type, const Json::Value& message);
47 
48 // Parses a JSON error message.
49 Result<std::string> ParseError(const std::string& type,
50                                const Json::Value& message);
51 
52 // Checks if the message contains an "ice_servers" array field and parses it
53 // into a vector of webrtc ICE servers. Returns an empty vector if the field
54 // isn't present.
55 Result<std::vector<webrtc::PeerConnectionInterface::IceServer>>
56 ParseIceServersMessage(const Json::Value& message);
57 
58 // Generates a JSON message from a list of ICE servers.
59 Json::Value GenerateIceServersMessage(
60     const std::vector<webrtc::PeerConnectionInterface::IceServer>& ice_servers);
61 
62 }  // namespace webrtc_streaming
63 }  // namespace cuttlefish
64