xref: /aosp_15_r20/external/webrtc/test/peer_scenario/signaling_route.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 #include "test/peer_scenario/signaling_route.h"
11 
12 #include <memory>
13 
14 #include "test/network/network_emulation_manager.h"
15 
16 namespace webrtc {
17 namespace test {
18 namespace {
19 constexpr size_t kIcePacketSize = 400;
20 constexpr size_t kSdpPacketSize = 1200;
21 
22 struct IceMessage {
23   IceMessage() = default;
IceMessagewebrtc::test::__anonc49b6e180111::IceMessage24   explicit IceMessage(const IceCandidateInterface* candidate)
25       : sdp_mid(candidate->sdp_mid()),
26         sdp_mline_index(candidate->sdp_mline_index()) {
27     RTC_CHECK(candidate->ToString(&sdp_line));
28   }
AsCandidatewebrtc::test::__anonc49b6e180111::IceMessage29   std::unique_ptr<IceCandidateInterface> AsCandidate() const {
30     SdpParseError err;
31     std::unique_ptr<IceCandidateInterface> candidate(
32         CreateIceCandidate(sdp_mid, sdp_mline_index, sdp_line, &err));
33     RTC_CHECK(candidate) << "Failed to parse: \"" << err.line
34                          << "\". Reason: " << err.description;
35     return candidate;
36   }
37   std::string sdp_mid;
38   int sdp_mline_index;
39   std::string sdp_line;
40 };
41 
StartIceSignalingForRoute(PeerScenarioClient * caller,PeerScenarioClient * callee,CrossTrafficRoute * send_route)42 void StartIceSignalingForRoute(PeerScenarioClient* caller,
43                                PeerScenarioClient* callee,
44                                CrossTrafficRoute* send_route) {
45   caller->handlers()->on_ice_candidate.push_back(
46       [=](const IceCandidateInterface* candidate) {
47         IceMessage msg(candidate);
48         send_route->NetworkDelayedAction(kIcePacketSize, [callee, msg]() {
49           callee->thread()->PostTask(
50               [callee, msg]() { callee->AddIceCandidate(msg.AsCandidate()); });
51         });
52       });
53 }
54 
StartSdpNegotiation(PeerScenarioClient * caller,PeerScenarioClient * callee,CrossTrafficRoute * send_route,CrossTrafficRoute * ret_route,std::function<void (SessionDescriptionInterface * offer)> munge_offer,std::function<void (SessionDescriptionInterface *)> modify_offer,std::function<void (const SessionDescriptionInterface &)> exchange_finished)55 void StartSdpNegotiation(
56     PeerScenarioClient* caller,
57     PeerScenarioClient* callee,
58     CrossTrafficRoute* send_route,
59     CrossTrafficRoute* ret_route,
60     std::function<void(SessionDescriptionInterface* offer)> munge_offer,
61     std::function<void(SessionDescriptionInterface*)> modify_offer,
62     std::function<void(const SessionDescriptionInterface&)> exchange_finished) {
63   caller->CreateAndSetSdp(munge_offer, [=](std::string sdp_offer) {
64     if (modify_offer) {
65       auto offer = CreateSessionDescription(SdpType::kOffer, sdp_offer);
66       modify_offer(offer.get());
67       RTC_CHECK(offer->ToString(&sdp_offer));
68     }
69     send_route->NetworkDelayedAction(kSdpPacketSize, [=] {
70       callee->SetSdpOfferAndGetAnswer(sdp_offer, [=](std::string answer) {
71         ret_route->NetworkDelayedAction(kSdpPacketSize, [=] {
72           caller->SetSdpAnswer(std::move(answer), std::move(exchange_finished));
73         });
74       });
75     });
76   });
77 }
78 }  // namespace
79 
SignalingRoute(PeerScenarioClient * caller,PeerScenarioClient * callee,CrossTrafficRoute * send_route,CrossTrafficRoute * ret_route)80 SignalingRoute::SignalingRoute(PeerScenarioClient* caller,
81                                PeerScenarioClient* callee,
82                                CrossTrafficRoute* send_route,
83                                CrossTrafficRoute* ret_route)
84     : caller_(caller),
85       callee_(callee),
86       send_route_(send_route),
87       ret_route_(ret_route) {}
88 
StartIceSignaling()89 void SignalingRoute::StartIceSignaling() {
90   StartIceSignalingForRoute(caller_, callee_, send_route_);
91   StartIceSignalingForRoute(callee_, caller_, ret_route_);
92 }
93 
NegotiateSdp(std::function<void (SessionDescriptionInterface *)> munge_offer,std::function<void (SessionDescriptionInterface *)> modify_offer,std::function<void (const SessionDescriptionInterface &)> exchange_finished)94 void SignalingRoute::NegotiateSdp(
95     std::function<void(SessionDescriptionInterface*)> munge_offer,
96     std::function<void(SessionDescriptionInterface*)> modify_offer,
97     std::function<void(const SessionDescriptionInterface&)> exchange_finished) {
98   StartSdpNegotiation(caller_, callee_, send_route_, ret_route_, munge_offer,
99                       modify_offer, exchange_finished);
100 }
101 
NegotiateSdp(std::function<void (SessionDescriptionInterface *)> modify_offer,std::function<void (const SessionDescriptionInterface &)> exchange_finished)102 void SignalingRoute::NegotiateSdp(
103     std::function<void(SessionDescriptionInterface*)> modify_offer,
104     std::function<void(const SessionDescriptionInterface&)> exchange_finished) {
105   NegotiateSdp({}, modify_offer, exchange_finished);
106 }
107 
NegotiateSdp(std::function<void (const SessionDescriptionInterface &)> exchange_finished)108 void SignalingRoute::NegotiateSdp(
109     std::function<void(const SessionDescriptionInterface&)> exchange_finished) {
110   NegotiateSdp({}, {}, exchange_finished);
111 }
112 
113 }  // namespace test
114 }  // namespace webrtc
115