sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/protocol/sdp_message.h" |
| 6 | |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame] | 7 | #include <algorithm> |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 8 | #include <utility> |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame] | 9 | |
Hans Wennborg | 828a097 | 2020-04-27 18:10:09 | [diff] [blame] | 10 | #include "base/notreached.h" |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame] | 11 | #include "base/strings/string_piece.h" |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 12 | #include "base/strings/string_split.h" |
| 13 | #include "base/strings/string_util.h" |
| 14 | |
| 15 | namespace remoting { |
| 16 | namespace protocol { |
| 17 | |
| 18 | SdpMessage::SdpMessage(const std::string& sdp) { |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame] | 19 | sdp_lines_ = base::SplitString( |
| 20 | sdp, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 21 | for (const auto& line : sdp_lines_) { |
| 22 | if (base::StartsWith(line, "m=audio", base::CompareCase::SENSITIVE)) |
| 23 | has_audio_ = true; |
| 24 | if (base::StartsWith(line, "m=video", base::CompareCase::SENSITIVE)) |
| 25 | has_video_ = true; |
| 26 | } |
| 27 | } |
| 28 | |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 29 | SdpMessage::~SdpMessage() = default; |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 30 | |
| 31 | std::string SdpMessage::ToString() const { |
Lambros Lambrou | 34aaa2b2 | 2018-04-10 18:03:21 | [diff] [blame] | 32 | return base::JoinString(sdp_lines_, "\r\n") + "\r\n"; |
| 33 | } |
| 34 | |
| 35 | std::string SdpMessage::NormalizedForSignature() const { |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 36 | return base::JoinString(sdp_lines_, "\n") + "\n"; |
| 37 | } |
| 38 | |
| 39 | bool SdpMessage::AddCodecParameter(const std::string& codec, |
| 40 | const std::string& parameters_to_add) { |
Zijie He | f912ff7 | 2017-10-10 18:11:40 | [diff] [blame] | 41 | std::vector<std::pair<int, std::string>> payload_types = FindCodec(codec); |
| 42 | if (payload_types.empty()) { |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 43 | return false; |
| 44 | } |
Zijie He | f912ff7 | 2017-10-10 18:11:40 | [diff] [blame] | 45 | |
| 46 | for (size_t i = 0; i < payload_types.size(); i++) { |
| 47 | sdp_lines_.insert( |
| 48 | sdp_lines_.begin() + payload_types[i].first + i + 1, |
| 49 | "a=fmtp:" + payload_types[i].second + ' ' + parameters_to_add); |
| 50 | } |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame] | 54 | bool SdpMessage::PreferVideoCodec(const std::string& codec) { |
| 55 | if (!has_video_) { |
| 56 | return false; |
| 57 | } |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 58 | std::vector<std::pair<int, std::string>> payload_types = FindCodec(codec); |
| 59 | if (payload_types.empty()) { |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | |
| 63 | for (size_t i = 0; i < sdp_lines_.size(); i++) { |
| 64 | if (!base::StartsWith(sdp_lines_[i], |
| 65 | "m=video", |
| 66 | base::CompareCase::SENSITIVE)) { |
| 67 | continue; |
| 68 | } |
| 69 | |
| 70 | // A valid SDP contains only one "m=video" line. So instead of continue, if |
| 71 | // this line is invalid, we should return false immediately. |
| 72 | std::vector<base::StringPiece> fields = base::SplitStringPiece( |
| 73 | sdp_lines_[i], " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 74 | // The first three fields are "m=video", port and proto. |
| 75 | static constexpr int kSkipFields = 3; |
| 76 | if (fields.size() <= kSkipFields) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | const auto first_codec_pos = fields.begin() + kSkipFields; |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 81 | |
| 82 | for (const auto& payload : payload_types) { |
| 83 | auto pos = std::find(first_codec_pos, |
| 84 | fields.end(), |
| 85 | base::StringPiece(payload.second)); |
| 86 | // The codec has not been found in codec list. |
| 87 | if (pos == fields.end()) { |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | std::rotate(first_codec_pos, pos, pos + 1); |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame] | 92 | } |
| 93 | |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame] | 94 | sdp_lines_[i] = base::JoinString(fields, " "); |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | // If has_video_ is true (tested at the very beginning of the function), we |
| 99 | // should always return within the for-loop above. |
| 100 | NOTREACHED(); |
| 101 | return false; |
| 102 | } |
| 103 | |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 104 | std::vector<std::pair<int, std::string>> SdpMessage::FindCodec( |
| 105 | const std::string& codec) const { |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 106 | const std::string kRtpMapPrefix = "a=rtpmap:"; |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 107 | std::vector<std::pair<int, std::string>> results; |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 108 | for (size_t i = 0; i < sdp_lines_.size(); ++i) { |
| 109 | const auto& line = sdp_lines_[i]; |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 110 | if (!base::StartsWith(line, kRtpMapPrefix, base::CompareCase::SENSITIVE)) { |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 111 | continue; |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 112 | } |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 113 | size_t space_pos = line.find(' '); |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 114 | if (space_pos == std::string::npos) { |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 115 | continue; |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 116 | } |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 117 | if (line.substr(space_pos + 1, codec.size()) == codec && |
| 118 | line[space_pos + 1 + codec.size()] == '/') { |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 119 | std::string payload_type = |
| 120 | line.substr(kRtpMapPrefix.size(), space_pos - kRtpMapPrefix.size()); |
| 121 | results.push_back(std::make_pair(i, std::move(payload_type))); |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 122 | } |
| 123 | } |
Zijie He | 455b8d6 | 2017-10-09 22:52:04 | [diff] [blame] | 124 | return results; |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | } // namespace protocol |
| 128 | } // namespace remoting |