xref: /aosp_15_r20/external/webrtc/test/call_config_utils.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "test/call_config_utils.h"
12 
13 #include <string>
14 #include <vector>
15 
16 namespace webrtc {
17 namespace test {
18 
19 // Deserializes a JSON representation of the VideoReceiveStreamInterface::Config
20 // back into a valid object. This will not initialize the decoders or the
21 // renderer.
ParseVideoReceiveStreamJsonConfig(webrtc::Transport * transport,const Json::Value & json)22 VideoReceiveStreamInterface::Config ParseVideoReceiveStreamJsonConfig(
23     webrtc::Transport* transport,
24     const Json::Value& json) {
25   auto receive_config = VideoReceiveStreamInterface::Config(transport);
26   for (const auto& decoder_json : json["decoders"]) {
27     VideoReceiveStreamInterface::Decoder decoder;
28     decoder.video_format =
29         SdpVideoFormat(decoder_json["payload_name"].asString());
30     decoder.payload_type = decoder_json["payload_type"].asInt64();
31     for (const auto& params_json : decoder_json["codec_params"]) {
32       std::vector<std::string> members = params_json.getMemberNames();
33       RTC_CHECK_EQ(members.size(), 1);
34       decoder.video_format.parameters[members[0]] =
35           params_json[members[0]].asString();
36     }
37     receive_config.decoders.push_back(decoder);
38   }
39   receive_config.render_delay_ms = json["render_delay_ms"].asInt64();
40   receive_config.rtp.remote_ssrc = json["rtp"]["remote_ssrc"].asInt64();
41   receive_config.rtp.local_ssrc = json["rtp"]["local_ssrc"].asInt64();
42   receive_config.rtp.rtcp_mode =
43       json["rtp"]["rtcp_mode"].asString() == "RtcpMode::kCompound"
44           ? RtcpMode::kCompound
45           : RtcpMode::kReducedSize;
46   receive_config.rtp.transport_cc = json["rtp"]["transport_cc"].asBool();
47   receive_config.rtp.lntf.enabled = json["rtp"]["lntf"]["enabled"].asInt64();
48   receive_config.rtp.nack.rtp_history_ms =
49       json["rtp"]["nack"]["rtp_history_ms"].asInt64();
50   receive_config.rtp.ulpfec_payload_type =
51       json["rtp"]["ulpfec_payload_type"].asInt64();
52   receive_config.rtp.red_payload_type =
53       json["rtp"]["red_payload_type"].asInt64();
54   receive_config.rtp.rtx_ssrc = json["rtp"]["rtx_ssrc"].asInt64();
55 
56   for (const auto& pl_json : json["rtp"]["rtx_payload_types"]) {
57     std::vector<std::string> members = pl_json.getMemberNames();
58     RTC_CHECK_EQ(members.size(), 1);
59     Json::Value rtx_payload_type = pl_json[members[0]];
60     receive_config.rtp.rtx_associated_payload_types[std::stoi(members[0])] =
61         rtx_payload_type.asInt64();
62   }
63   for (const auto& ext_json : json["rtp"]["extensions"]) {
64     receive_config.rtp.extensions.emplace_back(ext_json["uri"].asString(),
65                                                ext_json["id"].asInt64(),
66                                                ext_json["encrypt"].asBool());
67   }
68   return receive_config;
69 }
70 
GenerateVideoReceiveStreamJsonConfig(const VideoReceiveStreamInterface::Config & config)71 Json::Value GenerateVideoReceiveStreamJsonConfig(
72     const VideoReceiveStreamInterface::Config& config) {
73   Json::Value root_json;
74 
75   root_json["decoders"] = Json::Value(Json::arrayValue);
76   for (const auto& decoder : config.decoders) {
77     Json::Value decoder_json;
78     decoder_json["payload_type"] = decoder.payload_type;
79     decoder_json["payload_name"] = decoder.video_format.name;
80     decoder_json["codec_params"] = Json::Value(Json::arrayValue);
81     for (const auto& codec_param_entry : decoder.video_format.parameters) {
82       Json::Value codec_param_json;
83       codec_param_json[codec_param_entry.first] = codec_param_entry.second;
84       decoder_json["codec_params"].append(codec_param_json);
85     }
86     root_json["decoders"].append(decoder_json);
87   }
88 
89   Json::Value rtp_json;
90   rtp_json["remote_ssrc"] = config.rtp.remote_ssrc;
91   rtp_json["local_ssrc"] = config.rtp.local_ssrc;
92   rtp_json["rtcp_mode"] = config.rtp.rtcp_mode == RtcpMode::kCompound
93                               ? "RtcpMode::kCompound"
94                               : "RtcpMode::kReducedSize";
95   rtp_json["transport_cc"] = config.rtp.transport_cc;
96   rtp_json["lntf"]["enabled"] = config.rtp.lntf.enabled;
97   rtp_json["nack"]["rtp_history_ms"] = config.rtp.nack.rtp_history_ms;
98   rtp_json["ulpfec_payload_type"] = config.rtp.ulpfec_payload_type;
99   rtp_json["red_payload_type"] = config.rtp.red_payload_type;
100   rtp_json["rtx_ssrc"] = config.rtp.rtx_ssrc;
101   rtp_json["rtx_payload_types"] = Json::Value(Json::arrayValue);
102 
103   for (auto& kv : config.rtp.rtx_associated_payload_types) {
104     Json::Value val;
105     val[std::to_string(kv.first)] = kv.second;
106     rtp_json["rtx_payload_types"].append(val);
107   }
108 
109   rtp_json["extensions"] = Json::Value(Json::arrayValue);
110   for (auto& ext : config.rtp.extensions) {
111     Json::Value ext_json;
112     ext_json["uri"] = ext.uri;
113     ext_json["id"] = ext.id;
114     ext_json["encrypt"] = ext.encrypt;
115     rtp_json["extensions"].append(ext_json);
116   }
117   root_json["rtp"] = rtp_json;
118 
119   root_json["render_delay_ms"] = config.render_delay_ms;
120 
121   return root_json;
122 }
123 
124 }  // namespace test.
125 }  // namespace webrtc.
126