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::__anonc2619d220111::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::__anonc2619d220111::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,TrafficRoute * send_route)42 void StartIceSignalingForRoute(PeerScenarioClient* caller,
43                                PeerScenarioClient* callee,
44                                TrafficRoute* 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(RTC_FROM_HERE, [callee, msg]() {
50             callee->AddIceCandidate(msg.AsCandidate());
51           });
52         });
53       });
54 }
55 
StartSdpNegotiation(PeerScenarioClient * caller,PeerScenarioClient * callee,TrafficRoute * send_route,TrafficRoute * ret_route,std::function<void (SessionDescriptionInterface *)> modify_offer,std::function<void (const SessionDescriptionInterface &)> exchange_finished)56 void StartSdpNegotiation(
57     PeerScenarioClient* caller,
58     PeerScenarioClient* callee,
59     TrafficRoute* send_route,
60     TrafficRoute* ret_route,
61     std::function<void(SessionDescriptionInterface*)> modify_offer,
62     std::function<void(const SessionDescriptionInterface&)> exchange_finished) {
63   caller->CreateAndSetSdp([=](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,TrafficRoute * send_route,TrafficRoute * ret_route)80 SignalingRoute::SignalingRoute(PeerScenarioClient* caller,
81                                PeerScenarioClient* callee,
82                                TrafficRoute* send_route,
83                                TrafficRoute* 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 *)> modify_offer,std::function<void (const SessionDescriptionInterface &)> exchange_finished)94 void SignalingRoute::NegotiateSdp(
95     std::function<void(SessionDescriptionInterface*)> modify_offer,
96     std::function<void(const SessionDescriptionInterface&)> exchange_finished) {
97   StartSdpNegotiation(caller_, callee_, send_route_, ret_route_, modify_offer,
98                       exchange_finished);
99 }
100 
NegotiateSdp(std::function<void (const SessionDescriptionInterface &)> exchange_finished)101 void SignalingRoute::NegotiateSdp(
102     std::function<void(const SessionDescriptionInterface&)> exchange_finished) {
103   NegotiateSdp({}, exchange_finished);
104 }
105 
106 }  // namespace test
107 }  // namespace webrtc
108