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> |
| 8 | |
| 9 | #include "base/logging.h" |
| 10 | #include "base/strings/string_piece.h" |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 11 | #include "base/strings/string_split.h" |
| 12 | #include "base/strings/string_util.h" |
| 13 | |
| 14 | namespace remoting { |
| 15 | namespace protocol { |
| 16 | |
| 17 | SdpMessage::SdpMessage(const std::string& sdp) { |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame^] | 18 | sdp_lines_ = base::SplitString( |
| 19 | sdp, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 20 | for (const auto& line : sdp_lines_) { |
| 21 | if (base::StartsWith(line, "m=audio", base::CompareCase::SENSITIVE)) |
| 22 | has_audio_ = true; |
| 23 | if (base::StartsWith(line, "m=video", base::CompareCase::SENSITIVE)) |
| 24 | has_video_ = true; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | SdpMessage::~SdpMessage() {} |
| 29 | |
| 30 | std::string SdpMessage::ToString() const { |
| 31 | return base::JoinString(sdp_lines_, "\n") + "\n"; |
| 32 | } |
| 33 | |
| 34 | bool SdpMessage::AddCodecParameter(const std::string& codec, |
| 35 | const std::string& parameters_to_add) { |
| 36 | int line_num; |
| 37 | std::string payload_type; |
| 38 | bool codec_found = FindCodec(codec, &line_num, &payload_type); |
| 39 | if (!codec_found) { |
| 40 | return false; |
| 41 | } |
| 42 | sdp_lines_.insert(sdp_lines_.begin() + line_num + 1, |
| 43 | "a=fmtp:" + payload_type + ' ' + parameters_to_add); |
| 44 | return true; |
| 45 | } |
| 46 | |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame^] | 47 | bool SdpMessage::PreferVideoCodec(const std::string& codec) { |
| 48 | if (!has_video_) { |
| 49 | return false; |
| 50 | } |
| 51 | std::string payload_type; |
| 52 | if (!FindCodec(codec, nullptr, &payload_type)) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | for (size_t i = 0; i < sdp_lines_.size(); i++) { |
| 57 | if (!base::StartsWith(sdp_lines_[i], |
| 58 | "m=video", |
| 59 | base::CompareCase::SENSITIVE)) { |
| 60 | continue; |
| 61 | } |
| 62 | |
| 63 | // A valid SDP contains only one "m=video" line. So instead of continue, if |
| 64 | // this line is invalid, we should return false immediately. |
| 65 | std::vector<base::StringPiece> fields = base::SplitStringPiece( |
| 66 | sdp_lines_[i], " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 67 | // The first three fields are "m=video", port and proto. |
| 68 | static constexpr int kSkipFields = 3; |
| 69 | if (fields.size() <= kSkipFields) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | const auto first_codec_pos = fields.begin() + kSkipFields; |
| 74 | auto pos = std::find(first_codec_pos, |
| 75 | fields.end(), |
| 76 | base::StringPiece(payload_type)); |
| 77 | // The codec has not been found in codec list. |
| 78 | if (pos == fields.end()) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | std::rotate(first_codec_pos, pos, pos + 1); |
| 83 | sdp_lines_[i] = base::JoinString(fields, " "); |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | // If has_video_ is true (tested at the very beginning of the function), we |
| 88 | // should always return within the for-loop above. |
| 89 | NOTREACHED(); |
| 90 | return false; |
| 91 | } |
| 92 | |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 93 | bool SdpMessage::FindCodec(const std::string& codec, |
| 94 | int* line_num, |
| 95 | std::string* payload_type) const { |
| 96 | const std::string kRtpMapPrefix = "a=rtpmap:"; |
| 97 | for (size_t i = 0; i < sdp_lines_.size(); ++i) { |
| 98 | const auto& line = sdp_lines_[i]; |
| 99 | if (!base::StartsWith(line, kRtpMapPrefix, base::CompareCase::SENSITIVE)) |
| 100 | continue; |
| 101 | size_t space_pos = line.find(' '); |
| 102 | if (space_pos == std::string::npos) |
| 103 | continue; |
| 104 | if (line.substr(space_pos + 1, codec.size()) == codec && |
| 105 | line[space_pos + 1 + codec.size()] == '/') { |
Zijie He | cd945d73 | 2017-10-02 18:14:34 | [diff] [blame^] | 106 | if (line_num) { |
| 107 | *line_num = i; |
| 108 | } |
| 109 | if (payload_type) { |
| 110 | *payload_type = |
| 111 | line.substr(kRtpMapPrefix.size(), space_pos - kRtpMapPrefix.size()); |
| 112 | } |
sergeyu | 1ac2609e | 2016-11-29 01:08:12 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | } // namespace protocol |
| 120 | } // namespace remoting |