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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
// Copyright 2020 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/cast_streaming/browser/cast_message_port_impl.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/values.h"
#include "components/cast_streaming/browser/cast_message_port_converter.h"
#include "components/cast_streaming/common/message_serialization.h"
#include "third_party/openscreen/src/platform/base/error.h"
namespace cast_streaming {
namespace {
const char kKeyMediaSessionId[] = "mediaSessionId";
const char kKeyPlaybackRate[] = "playbackRate";
const char kKeyPlayerState[] = "playerState";
const char kKeyCurrentTime[] = "currentTime";
const char kKeySupportedMediaCommands[] = "supportedMediaCommands";
const char kKeyDisableStreamGrouping[] = "disableStreamGrouping";
const char kKeyMedia[] = "media";
const char kKeyContentId[] = "contentId";
const char kKeyStreamType[] = "streamType";
const char kKeyContentType[] = "contentType";
const char kValuePlaying[] = "PLAYING";
const char kValueLive[] = "LIVE";
const char kValueVideoWebm[] = "video/webm";
base::Value::Dict GetMediaCurrentStatusValue() {
base::Value::Dict media;
media.Set(kKeyContentId, "");
media.Set(kKeyStreamType, kValueLive);
media.Set(kKeyContentType, kValueVideoWebm);
base::Value::Dict media_current_status;
media_current_status.Set(kKeyMediaSessionId, 0);
media_current_status.Set(kKeyPlaybackRate, 1.0);
media_current_status.Set(kKeyPlayerState, kValuePlaying);
media_current_status.Set(kKeyCurrentTime, 0);
media_current_status.Set(kKeySupportedMediaCommands, 0);
media_current_status.Set(kKeyDisableStreamGrouping, true);
media_current_status.Set(kKeyMedia, std::move(media));
return media_current_status;
}
// Implementation of CastMessagePortConverter using CastMessagePortImpl.
class CastMessagePortConverterImpl : public CastMessagePortConverter {
public:
CastMessagePortConverterImpl(
ReceiverSession::MessagePortProvider message_port_provider,
base::OnceClosure on_close)
: message_port_(std::move(message_port_provider).Run()),
on_close_(std::move(on_close)) {}
~CastMessagePortConverterImpl() override = default;
openscreen::cast::MessagePort& GetMessagePort() override {
if (!openscreen_port_) {
DCHECK(message_port_);
openscreen_port_ = std::make_unique<CastMessagePortImpl>(
std::move(message_port_), std::move(on_close_));
}
return *openscreen_port_;
}
private:
std::unique_ptr<cast_api_bindings::MessagePort> message_port_;
base::OnceClosure on_close_;
std::unique_ptr<openscreen::cast::MessagePort> openscreen_port_;
};
} // namespace
std::unique_ptr<CastMessagePortConverter> CastMessagePortConverter::Create(
ReceiverSession::MessagePortProvider message_port_provider,
base::OnceClosure on_close) {
return std::make_unique<CastMessagePortConverterImpl>(
std::move(message_port_provider), std::move(on_close));
}
CastMessagePortImpl::CastMessagePortImpl(
std::unique_ptr<cast_api_bindings::MessagePort> message_port,
base::OnceClosure on_close)
: message_port_(std::move(message_port)), on_close_(std::move(on_close)) {
DVLOG(1) << __func__;
message_port_->SetReceiver(this);
// Initialize the connection with the Cast Streaming Sender.
PostMessage(kValueSystemSenderId, kSystemNamespace, kInitialConnectMessage);
}
CastMessagePortImpl::~CastMessagePortImpl() = default;
void CastMessagePortImpl::MaybeClose() {
if (message_port_) {
message_port_.reset();
}
if (client_) {
client_->OnError(
openscreen::Error(openscreen::Error::Code::kCastV2CastSocketError));
}
if (on_close_) {
// |this| might be deleted as part of |on_close_| being run. Do not add any
// code after running the closure.
std::move(on_close_).Run();
}
}
void CastMessagePortImpl::SetClient(
openscreen::cast::MessagePort::Client& client) {
DVLOG(2) << __func__;
client_ = &client;
}
void CastMessagePortImpl::ResetClient() {
client_ = nullptr;
MaybeClose();
}
void CastMessagePortImpl::SendInjectResponse(const std::string& sender_id,
const std::string& message) {
std::optional<base::Value::Dict> value = base::JSONReader::ReadDict(message);
if (!value) {
LOG(ERROR) << "Malformed message from sender " << sender_id
<< ": non-dictionary json payload: " << message;
return;
}
const std::string* type = value->FindString(kKeyType);
if (!type) {
LOG(ERROR) << "Malformed message from sender " << sender_id
<< ": no message type: " << message;
return;
}
if (*type != kValueWrapped) {
LOG(ERROR) << "Malformed message from sender " << sender_id
<< ": unknown message type: " << *type;
return;
}
std::optional<int> request_id = value->FindInt(kKeyRequestId);
if (!request_id) {
LOG(ERROR) << "Malformed message from sender " << sender_id
<< ": no request id: " << message;
return;
}
// Build the response message.
base::Value::Dict response_value;
response_value.Set(kKeyType, kValueError);
response_value.Set(kKeyRequestId, request_id.value());
response_value.Set(kKeyData, kValueInjectNotSupportedError);
response_value.Set(kKeyCode, kValueWrappedError);
std::string json_message;
CHECK(base::JSONWriter::Write(response_value, &json_message));
PostMessage(sender_id, kInjectNamespace, json_message);
}
void CastMessagePortImpl::HandleMediaMessage(const std::string& sender_id,
const std::string& message) {
std::optional<base::Value::Dict> value = base::JSONReader::ReadDict(message);
if (!value) {
LOG(ERROR) << "Malformed message from sender " << sender_id
<< ": non-dictionary json payload: " << message;
return;
}
const std::string* type = value->FindString(kKeyType);
if (!type) {
LOG(ERROR) << "Malformed message from sender " << sender_id
<< ": no message type: " << message;
return;
}
if (*type == kValueMediaPlay || *type == kValueMediaPause) {
// Not supported. Just ignore.
return;
}
if (*type != kValueMediaGetStatus) {
LOG(ERROR) << "Malformed message from sender " << sender_id
<< ": unknown message type: " << *type;
return;
}
std::optional<int> request_id = value->FindInt(kKeyRequestId);
if (!request_id) {
LOG(ERROR) << "Malformed message from sender " << sender_id
<< ": no request id: " << message;
return;
}
base::Value::List message_status_list;
message_status_list.Append(GetMediaCurrentStatusValue());
base::Value::Dict response_value;
response_value.Set(kKeyRequestId, request_id.value());
response_value.Set(kKeyType, kValueMediaStatus);
response_value.Set(kKeyStatus, std::move(message_status_list));
std::string json_message;
CHECK(base::JSONWriter::Write(response_value, &json_message));
PostMessage(sender_id, kMediaNamespace, json_message);
}
void CastMessagePortImpl::PostMessage(const std::string& sender_id,
const std::string& message_namespace,
const std::string& message) {
DVLOG(3) << __func__;
if (!message_port_) {
return;
}
DVLOG(3) << "Received Open Screen message. SenderId: " << sender_id
<< ". Namespace: " << message_namespace << ". Message: " << message;
message_port_->PostMessage(
SerializeCastMessage(sender_id, message_namespace, message));
}
bool CastMessagePortImpl::OnMessage(
std::string_view message,
std::vector<std::unique_ptr<cast_api_bindings::MessagePort>> ports) {
DVLOG(3) << __func__;
// If |client_| was cleared, |message_port_| should have been reset.
DCHECK(client_);
if (!ports.empty()) {
// We should never receive any ports for Cast Streaming.
LOG(ERROR) << "Received ports on Cast Streaming MessagePort.";
return false;
}
std::string sender_id;
std::string message_namespace;
std::string str_message;
if (!DeserializeCastMessage(message, &sender_id, &message_namespace,
&str_message)) {
LOG(ERROR) << "Received bad message.";
client_->OnError(
openscreen::Error(openscreen::Error::Code::kCastV2InvalidMessage));
return true;
}
DVLOG(3) << "Received Cast message. SenderId: " << sender_id
<< ". Namespace: " << message_namespace
<< ". Message: " << str_message;
// TODO(b/156118960): Have Open Screen handle message namespaces.
if (message_namespace == kMirroringNamespace ||
message_namespace == kRemotingNamespace) {
client_->OnMessage(sender_id, message_namespace, str_message);
} else if (message_namespace == kInjectNamespace) {
SendInjectResponse(sender_id, str_message);
} else if (message_namespace == kMediaNamespace) {
HandleMediaMessage(sender_id, str_message);
} else if (message_namespace != kSystemNamespace) {
// System messages are ignored, log messages from unknown namespaces.
DVLOG(2) << "Unknown message from " << sender_id
<< ", namespace=" << message_namespace
<< ", message=" << str_message;
}
return true;
}
void CastMessagePortImpl::OnPipeError() {
DVLOG(3) << __func__;
MaybeClose();
}
} // namespace cast_streaming
|