Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(378)

Side by Side Diff: content/public/browser/web_contents_media_capture_id.cc

Issue 2291893002: Let Contraints Controll Mute/Unmute Audio Local Playback For Desktop Sharing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/public/browser/web_contents_media_capture_id.h" 5 #include "content/public/browser/web_contents_media_capture_id.h"
6 6
7 #include <tuple> 7 #include <tuple>
8 8
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_piece.h" 10 #include "base/strings/string_piece.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 12
13 namespace {
14 const char kWebContentsCaptureScheme[] = "web-contents-media-stream://";
15 static char kEnableThrottlingFlag[] = "throttling=auto";
miu 2016/09/12 23:13:12 C++11 update: Please make these new strings conste
qiangchen 2016/09/13 21:46:57 Done. I think we can constexpr for kWebContentsCa
16 static char kMuteSourceFlag[] = "mute_source=true";
17 static char kOptionStart[] = "?";
miu 2016/09/12 23:13:12 For the separators, use a char instead of a char[]
qiangchen 2016/09/13 21:46:57 Done.
18 static char kOptionSeparator[] = "&";
19
20 bool ExtractTabCaptureTarget(const std::string& device_id_param,
21 int* render_process_id,
22 int* main_render_frame_id) {
23 const std::string device_scheme = kWebContentsCaptureScheme;
24 if (!base::StartsWith(device_id_param, device_scheme,
25 base::CompareCase::SENSITIVE))
26 return false;
27
28 const std::string device_id = device_id_param.substr(device_scheme.size());
29
30 const size_t sep_pos = device_id.find(':');
31 if (sep_pos == std::string::npos)
32 return false;
33
34 const base::StringPiece component1(device_id.data(), sep_pos);
35 size_t end_pos = device_id.find('?');
36 if (end_pos == std::string::npos)
37 end_pos = device_id.length();
38 const base::StringPiece component2(device_id.data() + sep_pos + 1,
39 end_pos - sep_pos - 1);
40
41 return (base::StringToInt(component1, render_process_id) &&
42 base::StringToInt(component2, main_render_frame_id));
43 }
44
45 bool ExtractOptions(const std::string& device_id,
46 bool* auto_throttling,
47 bool* mute_source) {
48 // Find the option part of the string and just do a naive string compare since
49 // there are no other options in the |device_id| to account for (at the time
50 // of this writing).
51 size_t option_pos = device_id.find(kOptionStart);
52 if (option_pos == std::string::npos) {
53 if (auto_throttling)
54 *auto_throttling = false;
55 if (mute_source)
56 *mute_source = false;
57 return true;
58 }
59
60 size_t option_pos_end;
61 while (option_pos < device_id.length()) {
62 option_pos_end = device_id.find(kOptionSeparator, option_pos + 1);
63 if (option_pos_end == std::string::npos)
64 option_pos_end = device_id.length();
65 const base::StringPiece component(device_id.data() + option_pos + 1,
66 option_pos_end - option_pos - 1);
67
68 if (auto_throttling && component.compare(kEnableThrottlingFlag) == 0)
69 *auto_throttling = true;
70 if (mute_source && component.compare(kMuteSourceFlag) == 0)
71 *mute_source = true;
72
73 option_pos = option_pos_end;
74 }
75 return true;
76 }
77
78 } // namespace
79
13 namespace content { 80 namespace content {
14 const char kWebContentsCaptureScheme[] = "web-contents-media-stream://";
15 static char kEnableThrottlingFlag[] = "?throttling=auto";
16 81
17 bool WebContentsMediaCaptureId::operator<( 82 bool WebContentsMediaCaptureId::operator<(
18 const WebContentsMediaCaptureId& other) const { 83 const WebContentsMediaCaptureId& other) const {
19 return std::tie(render_process_id, main_render_frame_id, 84 return std::tie(render_process_id, main_render_frame_id,
20 enable_auto_throttling) < 85 enable_auto_throttling, mute_source) <
21 std::tie(other.render_process_id, other.main_render_frame_id, 86 std::tie(other.render_process_id, other.main_render_frame_id,
22 other.enable_auto_throttling); 87 other.enable_auto_throttling, other.mute_source);
23 } 88 }
24 89
25 bool WebContentsMediaCaptureId::operator==( 90 bool WebContentsMediaCaptureId::operator==(
26 const WebContentsMediaCaptureId& other) const { 91 const WebContentsMediaCaptureId& other) const {
27 return std::tie(render_process_id, main_render_frame_id, 92 return std::tie(render_process_id, main_render_frame_id,
28 enable_auto_throttling) == 93 enable_auto_throttling, mute_source) ==
29 std::tie(other.render_process_id, other.main_render_frame_id, 94 std::tie(other.render_process_id, other.main_render_frame_id,
30 other.enable_auto_throttling); 95 other.enable_auto_throttling, other.mute_source);
31 } 96 }
32 97
33 bool WebContentsMediaCaptureId::is_null() const { 98 bool WebContentsMediaCaptureId::is_null() const {
34 return (render_process_id < 0) || (main_render_frame_id < 0); 99 return (render_process_id < 0) || (main_render_frame_id < 0);
35 } 100 }
36 101
37 std::string WebContentsMediaCaptureId::ToString() const { 102 std::string WebContentsMediaCaptureId::ToString() const {
38 std::string s = kWebContentsCaptureScheme; 103 std::string s = kWebContentsCaptureScheme;
39 s.append(base::Int64ToString(render_process_id)); 104 s.append(base::Int64ToString(render_process_id));
40 s.append(":"); 105 s.append(":");
41 s.append(base::Int64ToString(main_render_frame_id)); 106 s.append(base::Int64ToString(main_render_frame_id));
42 107
43 if (enable_auto_throttling) 108 char* connector = kOptionStart;
miu 2016/09/12 23:13:12 Just char (see prior comment).
qiangchen 2016/09/13 21:46:57 Done.
109 if (enable_auto_throttling) {
110 s.append(connector);
44 s.append(kEnableThrottlingFlag); 111 s.append(kEnableThrottlingFlag);
112 connector = kOptionSeparator;
113 }
114
115 if (mute_source) {
116 s.append(connector);
117 s.append(kMuteSourceFlag);
118 connector = kOptionSeparator;
119 }
45 120
46 return s; 121 return s;
47 } 122 }
48 123
49 // static 124 // static
50 WebContentsMediaCaptureId WebContentsMediaCaptureId::Parse( 125 bool WebContentsMediaCaptureId::TryParse(const std::string& str,
51 const std::string& str) { 126 WebContentsMediaCaptureId* output_id) {
52 int render_process_id; 127 int render_process_id;
53 int main_render_frame_id; 128 int main_render_frame_id;
54 if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) 129 if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id))
55 return WebContentsMediaCaptureId(); 130 return false;
56 131
57 return WebContentsMediaCaptureId(render_process_id, main_render_frame_id, 132 bool auto_throttling, mute_source;
58 IsAutoThrottlingOptionSet(str)); 133 if (!ExtractOptions(str, &auto_throttling, &mute_source))
134 return false;
135
136 if (output_id) {
137 output_id->render_process_id = render_process_id;
138 output_id->main_render_frame_id = main_render_frame_id;
139 output_id->enable_auto_throttling = auto_throttling;
140 output_id->mute_source = mute_source;
141 }
142
143 return true;
59 } 144 }
60 145
61 // static
62 bool WebContentsMediaCaptureId::IsWebContentsDeviceId(
63 const std::string& device_id) {
64 int ignored;
65 return ExtractTabCaptureTarget(device_id, &ignored, &ignored);
66 }
67
68 // static
69 bool WebContentsMediaCaptureId::ExtractTabCaptureTarget(
70 const std::string& device_id_param,
71 int* render_process_id,
72 int* main_render_frame_id) {
73 const std::string device_scheme = kWebContentsCaptureScheme;
74 if (!base::StartsWith(device_id_param, device_scheme,
75 base::CompareCase::SENSITIVE))
76 return false;
77
78 const std::string device_id = device_id_param.substr(device_scheme.size());
79
80 const size_t sep_pos = device_id.find(':');
81 if (sep_pos == std::string::npos)
82 return false;
83
84 const base::StringPiece component1(device_id.data(), sep_pos);
85 size_t end_pos = device_id.find('?');
86 if (end_pos == std::string::npos)
87 end_pos = device_id.length();
88 const base::StringPiece component2(device_id.data() + sep_pos + 1,
89 end_pos - sep_pos - 1);
90
91 return (base::StringToInt(component1, render_process_id) &&
92 base::StringToInt(component2, main_render_frame_id));
93 }
94
95 // static
96 bool WebContentsMediaCaptureId::IsAutoThrottlingOptionSet(
97 const std::string& device_id) {
98 if (!IsWebContentsDeviceId(device_id))
99 return false;
100
101 // Find the option part of the string and just do a naive string compare since
102 // there are no other options in the |device_id| to account for (at the time
103 // of this writing).
104 const size_t option_pos = device_id.find('?');
105 if (option_pos == std::string::npos)
106 return false;
107 const base::StringPiece component(device_id.data() + option_pos,
108 device_id.length() - option_pos);
109 return component.compare(kEnableThrottlingFlag) == 0;
110 }
111 146
112 } // namespace content 147 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698