1 /*
2  *  Copyright 2017 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 "pc/sdp_utils.h"
12 
13 #include <memory>
14 #include <string>
15 #include <utility>
16 
17 #include "api/jsep_session_description.h"
18 
19 namespace webrtc {
20 
CloneSessionDescription(const SessionDescriptionInterface * sdesc)21 std::unique_ptr<SessionDescriptionInterface> CloneSessionDescription(
22     const SessionDescriptionInterface* sdesc) {
23   RTC_DCHECK(sdesc);
24   return CloneSessionDescriptionAsType(sdesc, sdesc->GetType());
25 }
26 
CloneSessionDescriptionAsType(const SessionDescriptionInterface * sdesc,SdpType type)27 std::unique_ptr<SessionDescriptionInterface> CloneSessionDescriptionAsType(
28     const SessionDescriptionInterface* sdesc,
29     SdpType type) {
30   RTC_DCHECK(sdesc);
31   auto clone = std::make_unique<JsepSessionDescription>(type);
32   if (sdesc->description()) {
33     clone->Initialize(sdesc->description()->Clone(), sdesc->session_id(),
34                       sdesc->session_version());
35   }
36   // As of writing, our version of GCC does not allow returning a unique_ptr of
37   // a subclass as a unique_ptr of a base class. To get around this, we need to
38   // std::move the return value.
39   return std::move(clone);
40 }
41 
SdpContentsAll(SdpContentPredicate pred,const cricket::SessionDescription * desc)42 bool SdpContentsAll(SdpContentPredicate pred,
43                     const cricket::SessionDescription* desc) {
44   RTC_DCHECK(desc);
45   for (const auto& content : desc->contents()) {
46     const auto* transport_info = desc->GetTransportInfoByName(content.name);
47     if (!pred(&content, transport_info)) {
48       return false;
49     }
50   }
51   return true;
52 }
53 
SdpContentsNone(SdpContentPredicate pred,const cricket::SessionDescription * desc)54 bool SdpContentsNone(SdpContentPredicate pred,
55                      const cricket::SessionDescription* desc) {
56   return SdpContentsAll(
57       [pred](const cricket::ContentInfo* content_info,
58              const cricket::TransportInfo* transport_info) {
59         return !pred(content_info, transport_info);
60       },
61       desc);
62 }
63 
SdpContentsForEach(SdpContentMutator fn,cricket::SessionDescription * desc)64 void SdpContentsForEach(SdpContentMutator fn,
65                         cricket::SessionDescription* desc) {
66   RTC_DCHECK(desc);
67   for (auto& content : desc->contents()) {
68     auto* transport_info = desc->GetTransportInfoByName(content.name);
69     fn(&content, transport_info);
70   }
71 }
72 
73 }  // namespace webrtc
74