File: media_route.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (118 lines) | stat: -rw-r--r-- 4,114 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/media_router/common/media_route.h"

#include <ostream>

#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "components/media_router/common/media_source.h"

namespace media_router {

constexpr char kRouteIdPrefix[] = "urn:x-org.chromium:media:route:";

namespace {

bool IsValidMediaRouteId(const MediaRoute::Id route_id) {
  if (!base::StartsWith(route_id, kRouteIdPrefix,
                        base::CompareCase::SENSITIVE)) {
    return false;
  }
  // return false if there are not at least two slashes in |route_id|.
  size_t pos;
  return ((pos = route_id.find("/")) != std::string::npos &&
          (pos = route_id.find("/", pos + 1)) != std::string::npos);
}

}  // namespace

// static
MediaRoute::Id MediaRoute::GetMediaRouteId(const std::string& presentation_id,
                                           const MediaSink::Id& sink_id,
                                           const MediaSource& source) {
  // TODO(crbug.com/40090609): Can the route ID just be the presentation
  // id?
  return base::StringPrintf("%s%s/%s/%s", kRouteIdPrefix,
                            presentation_id.c_str(), sink_id.c_str(),
                            source.id().c_str());
}

// static
std::string MediaRoute::GetPresentationIdFromMediaRouteId(
    const MediaRoute::Id route_id) {
  if (!IsValidMediaRouteId(route_id)) {
    return "";
  }
  return route_id.substr(strlen(kRouteIdPrefix),
                         route_id.find("/") - strlen(kRouteIdPrefix));
}

// static
std::string MediaRoute::GetSinkIdFromMediaRouteId(
    const MediaRoute::Id route_id) {
  if (!IsValidMediaRouteId(route_id)) {
    return "";
  }
  auto begin = route_id.find("/");
  auto end = route_id.find("/", begin + 1);
  return route_id.substr(begin + 1, (end - begin - 1));
}

// static
std::string MediaRoute::GetMediaSourceIdFromMediaRouteId(
    const MediaRoute::Id route_id) {
  if (!IsValidMediaRouteId(route_id)) {
    return "";
  }
  auto pos = route_id.find("/");
  pos = route_id.find("/", pos + 1);
  return route_id.substr(pos + 1);
}

MediaRoute::MediaRoute(const MediaRoute::Id& media_route_id,
                       const MediaSource& media_source,
                       const MediaSink::Id& media_sink_id,
                       const std::string& description,
                       bool is_local)
    : media_route_id_(media_route_id),
      media_source_(media_source),
      media_sink_id_(media_sink_id),
      description_(description),
      is_local_(is_local) {}

MediaRoute::MediaRoute() : media_source_("") {}
MediaRoute::MediaRoute(const MediaRoute&) = default;
MediaRoute& MediaRoute::operator=(const MediaRoute&) = default;
MediaRoute::MediaRoute(MediaRoute&&) = default;
MediaRoute& MediaRoute::operator=(MediaRoute&&) = default;

MediaRoute::~MediaRoute() = default;

bool MediaRoute::IsLocalMirroringRoute() const {
  return is_local_ && (media_source_.IsTabMirroringSource() ||
                       media_source_.IsDesktopMirroringSource() ||
                       controller_type_ == RouteControllerType::kMirroring);
}

bool MediaRoute::operator==(const MediaRoute& other) const {
  return media_route_id_ == other.media_route_id_ &&
         presentation_id_ == other.presentation_id_ &&
         media_source_ == other.media_source_ &&
         media_sink_id_ == other.media_sink_id_ &&
         media_sink_name_ == other.media_sink_name_ &&
         description_ == other.description_ && is_local_ == other.is_local_ &&
         controller_type_ == other.controller_type_ &&
         is_local_presentation_ == other.is_local_presentation_ &&
         is_connecting_ == other.is_connecting_;
}

std::ostream& operator<<(std::ostream& stream, const MediaRoute& route) {
  return stream << "MediaRoute{id=" << route.media_route_id_
                << ",source=" << route.media_source_.id()
                << ",sink=" << route.media_sink_id_ << "}";
}

}  // namespace media_router