OLD | NEW |
| (Empty) |
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 "chrome/browser/media/cast_remoting_connector_messaging.h" | |
6 | |
7 #include <stdio.h> | |
8 | |
9 #include <limits> | |
10 | |
11 #include "base/strings/string_number_conversions.h" | |
12 | |
13 const char CastRemotingConnectorMessaging::kMessageFieldSeparator = ':'; | |
14 const char CastRemotingConnectorMessaging::kStartRemotingMessageFormat[] = | |
15 "START_CAST_REMOTING:session=%x"; | |
16 const char CastRemotingConnectorMessaging::kStartStreamsMessageFormat[] = | |
17 "START_CAST_REMOTING_STREAMS:session=%x:audio=%c:video=%c"; | |
18 const char | |
19 CastRemotingConnectorMessaging::kStartedStreamsMessageFormatPartial[] = | |
20 "STARTED_CAST_REMOTING_STREAMS:session=%x"; | |
21 const char | |
22 CastRemotingConnectorMessaging::kStartedStreamsMessageAudioIdSpecifier[] = | |
23 ":audio_stream_id="; | |
24 const char | |
25 CastRemotingConnectorMessaging::kStartedStreamsMessageVideoIdSpecifier[] = | |
26 ":video_stream_id="; | |
27 const char CastRemotingConnectorMessaging::kStopRemotingMessageFormat[] = | |
28 "STOP_CAST_REMOTING:session=%x"; | |
29 const char CastRemotingConnectorMessaging::kStoppedMessageFormat[] = | |
30 "STOPPED_CAST_REMOTING:session=%x"; | |
31 const char CastRemotingConnectorMessaging::kFailedMessageFormat[] = | |
32 "FAILED_CAST_REMOTING:session=%x"; | |
33 | |
34 // static | |
35 bool CastRemotingConnectorMessaging::IsMessageForSession( | |
36 const std::string& message, const char* format, | |
37 unsigned int expected_session_id) { | |
38 unsigned int session_id; | |
39 if (sscanf(message.c_str(), format, &session_id) == 1) | |
40 return session_id == expected_session_id; | |
41 return false; | |
42 } | |
43 | |
44 // static | |
45 int32_t CastRemotingConnectorMessaging::GetStreamIdFromStartedMessage( | |
46 base::StringPiece message, base::StringPiece specifier) { | |
47 auto start = message.find(specifier); | |
48 if (start == std::string::npos) | |
49 return -1; | |
50 start += specifier.size(); | |
51 if (start + 1 >= message.size()) | |
52 return -1; // Must be at least one hex digit following the specifier. | |
53 const auto length = message.find(kMessageFieldSeparator, start) - start; | |
54 int parsed_value; | |
55 if (!base::HexStringToInt(message.substr(start, length), &parsed_value) || | |
56 parsed_value < 0 || | |
57 parsed_value > std::numeric_limits<int32_t>::max()) { | |
58 return -1; // Non-hex digits, or outside valid range. | |
59 } | |
60 return static_cast<int32_t>(parsed_value); | |
61 } | |
OLD | NEW |