Chromium Code Reviews| 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 "media/remoting/rpc/rpc.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "base/values.h" | |
| 12 #include "media/base/encryption_scheme.h" | |
| 13 | |
| 14 namespace media { | |
| 15 namespace remoting { | |
| 16 | |
| 17 const int kInvalidHandle = -1; | |
| 18 const int kReceiverHandle = 0; | |
| 19 | |
| 20 void SerializeEncryptionScheme( | |
| 21 pb::EncryptionScheme* message, | |
| 22 const ::media::EncryptionScheme& encryption_scheme) { | |
| 23 message->set_mode( | |
| 24 static_cast<pb::EncryptionScheme::CipherMode>(encryption_scheme.mode())); | |
| 25 message->set_encrypt_blocks(encryption_scheme.pattern().encrypt_blocks()); | |
| 26 message->set_skip_blocks(encryption_scheme.pattern().skip_blocks()); | |
| 27 } | |
| 28 | |
| 29 void DeserializeEncryptionScheme(const pb::EncryptionScheme& message, | |
| 30 ::media::EncryptionScheme* encryption_scheme) { | |
| 31 ::media::EncryptionScheme::CipherMode mode = | |
| 32 ::media::EncryptionScheme::CIPHER_MODE_UNENCRYPTED; | |
| 33 ::media::EncryptionScheme::Pattern pattern; | |
| 34 uint32_t encrypt_blocks = 0; | |
| 35 uint32_t skip_blocks = 0; | |
| 36 | |
| 37 if (message.has_mode()) | |
| 38 mode = static_cast<::media::EncryptionScheme::CipherMode>(message.mode()); | |
|
miu
2016/09/13 05:40:57
The static_cast between enum types worries me: The
| |
| 39 if (message.has_encrypt_blocks()) | |
|
miu
2016/09/13 05:40:57
Do we need "has" guards here? The default value wi
erickung1
2016/09/15 02:13:33
Done.
| |
| 40 encrypt_blocks = message.encrypt_blocks(); | |
| 41 if (message.has_skip_blocks()) | |
| 42 skip_blocks = message.skip_blocks(); | |
| 43 DCHECK(encrypt_blocks >= 0 && skip_blocks >= 0); | |
|
miu
2016/09/13 05:40:57
This will crash the process on bad external input.
erickung1
2016/09/15 02:13:33
Done. Actually I just realized that both are unit3
| |
| 44 | |
| 45 pattern = ::media::EncryptionScheme::Pattern(encrypt_blocks, skip_blocks); | |
| 46 *encryption_scheme = ::media::EncryptionScheme(mode, pattern); | |
| 47 } | |
| 48 | |
| 49 void SerializeAudioConfig(const ::media::AudioDecoderConfig& audio_config, | |
| 50 pb::AudioDecoderConfig* audio_message) { | |
| 51 if (!audio_config.IsValidConfig()) | |
|
miu
2016/09/13 05:40:57
Since this is the sender generating an RPC message
erickung1
2016/09/15 02:13:33
Done.
| |
| 52 return; | |
| 53 | |
| 54 audio_message->set_codec( | |
| 55 static_cast<pb::AudioDecoderConfig_Codec>(audio_config.codec())); | |
| 56 audio_message->set_sample_format( | |
| 57 static_cast<pb::AudioDecoderConfig_SampleFormat>( | |
| 58 audio_config.sample_format())); | |
| 59 audio_message->set_channel_layout( | |
| 60 static_cast<pb::AudioDecoderConfig_ChannelLayout>( | |
| 61 audio_config.channel_layout())); | |
| 62 audio_message->set_samples_per_second(audio_config.samples_per_second()); | |
| 63 audio_message->set_seek_preroll_usec( | |
| 64 audio_config.seek_preroll().InMicroseconds()); | |
| 65 audio_message->set_codec_delay(audio_config.codec_delay()); | |
| 66 | |
| 67 if (!audio_config.extra_data().empty()) { | |
| 68 audio_message->set_extra_data(audio_config.extra_data().data(), | |
| 69 audio_config.extra_data().size()); | |
| 70 } | |
| 71 | |
| 72 if (audio_config.is_encrypted()) { | |
| 73 pb::EncryptionScheme* encryption_scheme_message = | |
| 74 audio_message->mutable_encryption_scheme(); | |
| 75 SerializeEncryptionScheme(encryption_scheme_message, | |
| 76 audio_config.encryption_scheme()); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 bool DeserializeAudioConfig(const pb::AudioDecoderConfig& audio_message, | |
| 81 ::media::AudioDecoderConfig* audio_config) { | |
| 82 DCHECK(audio_config); | |
| 83 | |
| 84 ::media::AudioCodec codec = ::media::kUnknownAudioCodec; | |
| 85 ::media::SampleFormat sample_format = ::media::kUnknownSampleFormat; | |
| 86 ::media::ChannelLayout channel_layout = ::media::CHANNEL_LAYOUT_NONE; | |
| 87 int samples_per_second = 0; | |
| 88 base::TimeDelta seek_preroll = base::TimeDelta(); | |
| 89 int codec_delay = 0; | |
| 90 ::media::EncryptionScheme encryption_scheme; | |
| 91 std::vector<uint8_t> extra_data; | |
| 92 | |
| 93 codec = static_cast<::media::AudioCodec>(audio_message.codec()); | |
| 94 sample_format = | |
| 95 static_cast<::media::SampleFormat>(audio_message.sample_format()); | |
| 96 channel_layout = | |
| 97 static_cast<::media::ChannelLayout>(audio_message.channel_layout()); | |
| 98 samples_per_second = audio_message.samples_per_second(); | |
| 99 seek_preroll = | |
| 100 base::TimeDelta::FromMicroseconds(audio_message.seek_preroll_usec()); | |
| 101 codec_delay = audio_message.codec_delay(); | |
| 102 | |
| 103 if (audio_message.has_extra_data()) { | |
| 104 extra_data.assign(audio_message.extra_data().begin(), | |
| 105 audio_message.extra_data().end()); | |
| 106 } | |
| 107 | |
| 108 const pb::EncryptionScheme encryption_scheme_message = | |
| 109 audio_message.encryption_scheme(); | |
| 110 DeserializeEncryptionScheme(encryption_scheme_message, &encryption_scheme); | |
| 111 | |
| 112 audio_config->Initialize(codec, sample_format, channel_layout, | |
|
miu
2016/09/13 05:40:58
Please see my comments from last week about simpli
erickung1
2016/09/15 02:13:33
Done.
| |
| 113 samples_per_second, extra_data, encryption_scheme, | |
| 114 seek_preroll, codec_delay); | |
| 115 return true; | |
|
miu
2016/09/13 05:40:58
Should be: return audio_config->IsValidConfig();
erickung1
2016/09/15 02:13:33
Done.
| |
| 116 } | |
| 117 | |
| 118 bool DeserializeVideoConfig(const pb::VideoDecoderConfig& video_message, | |
| 119 ::media::VideoDecoderConfig* video_config) { | |
| 120 ::media::VideoCodec codec = ::media::kUnknownVideoCodec; | |
| 121 ::media::VideoCodecProfile profile = ::media::VIDEO_CODEC_PROFILE_UNKNOWN; | |
| 122 ::media::VideoPixelFormat format = ::media::PIXEL_FORMAT_UNKNOWN; | |
| 123 ::media::ColorSpace color_space = ::media::COLOR_SPACE_UNSPECIFIED; | |
| 124 ::media::EncryptionScheme encryption_scheme; | |
| 125 std::vector<uint8_t> extra_data; | |
| 126 | |
| 127 codec = static_cast<::media::VideoCodec>(video_message.codec()); | |
| 128 profile = static_cast<::media::VideoCodecProfile>(video_message.profile()); | |
| 129 format = static_cast<::media::VideoPixelFormat>(video_message.format()); | |
| 130 color_space = static_cast<::media::ColorSpace>(video_message.color_space()); | |
| 131 | |
| 132 if (video_message.has_extra_data()) { | |
| 133 extra_data.assign(video_message.extra_data().begin(), | |
| 134 video_message.extra_data().end()); | |
| 135 } | |
| 136 | |
| 137 gfx::Size coded_size(video_message.coded_size().width(), | |
| 138 video_message.coded_size().height()); | |
| 139 gfx::Rect visible_rect(video_message.visible_rect().x(), | |
| 140 video_message.visible_rect().y(), | |
| 141 video_message.visible_rect().width(), | |
| 142 video_message.visible_rect().height()); | |
| 143 gfx::Size natural_size(video_message.natural_size().width(), | |
| 144 video_message.natural_size().height()); | |
| 145 | |
| 146 const pb::EncryptionScheme& encryption_scheme_message = | |
| 147 video_message.encryption_scheme(); | |
| 148 DeserializeEncryptionScheme(encryption_scheme_message, &encryption_scheme); | |
| 149 | |
| 150 video_config->Initialize(codec, profile, format, color_space, coded_size, | |
| 151 visible_rect, natural_size, extra_data, | |
| 152 encryption_scheme); | |
| 153 return true; | |
|
miu
2016/09/13 05:40:57
Should be: return video_config->IsValidConfig();
erickung1
2016/09/15 02:13:33
Done.
| |
| 154 } | |
| 155 | |
| 156 bool DeserializeCdmPromiseResult(const pb::CdmPromise& promise_message, | |
| 157 CdmPromiseResult* result) { | |
| 158 DCHECK(promise_message.has_success()); | |
| 159 bool success = promise_message.success(); | |
| 160 if (success) { | |
| 161 *result = CdmPromiseResult::SuccessResult(); | |
| 162 return true; | |
| 163 } | |
| 164 | |
| 165 ::media::MediaKeys::Exception exception = ::media::MediaKeys::UNKNOWN_ERROR; | |
| 166 uint32_t system_code = 0; | |
| 167 std::string error_message; | |
| 168 | |
| 169 exception = | |
| 170 static_cast<::media::MediaKeys::Exception>(promise_message.exception()); | |
| 171 system_code = promise_message.system_code(); | |
| 172 error_message = promise_message.error_message(); | |
| 173 *result = CdmPromiseResult(exception, system_code, error_message); | |
| 174 return true; | |
| 175 } | |
| 176 | |
| 177 void SerializeVideoConfig(const ::media::VideoDecoderConfig& video_config, | |
| 178 pb::VideoDecoderConfig* video_message) { | |
| 179 if (!video_config.IsValidConfig()) | |
|
miu
2016/09/13 05:40:57
ditto here: Should be DCHECK(...);
erickung1
2016/09/15 02:13:33
Done.
| |
| 180 return; | |
| 181 | |
| 182 video_message->set_codec( | |
| 183 static_cast<pb::VideoDecoderConfig_Codec>(video_config.codec())); | |
| 184 video_message->set_profile( | |
| 185 static_cast<pb::VideoDecoderConfig_Profile>(video_config.profile())); | |
| 186 video_message->set_format( | |
| 187 static_cast<pb::VideoDecoderConfig_Format>(video_config.format())); | |
| 188 video_message->set_color_space(static_cast<pb::VideoDecoderConfig_ColorSpace>( | |
| 189 video_config.color_space())); | |
| 190 | |
| 191 pb::Size* coded_size_message = video_message->mutable_coded_size(); | |
| 192 coded_size_message->set_width(video_config.coded_size().width()); | |
| 193 coded_size_message->set_height(video_config.coded_size().height()); | |
| 194 | |
| 195 pb::Rect* visible_rect_message = video_message->mutable_visible_rect(); | |
| 196 visible_rect_message->set_x(video_config.visible_rect().x()); | |
| 197 visible_rect_message->set_y(video_config.visible_rect().y()); | |
| 198 visible_rect_message->set_width(video_config.visible_rect().width()); | |
| 199 visible_rect_message->set_height(video_config.visible_rect().height()); | |
| 200 | |
| 201 pb::Size* natural_size_message = video_message->mutable_natural_size(); | |
| 202 natural_size_message->set_width(video_config.natural_size().width()); | |
| 203 natural_size_message->set_height(video_config.natural_size().height()); | |
| 204 | |
| 205 if (!video_config.extra_data().empty()) { | |
| 206 video_message->set_extra_data(video_config.extra_data().data(), | |
| 207 video_config.extra_data().size()); | |
| 208 } | |
| 209 | |
| 210 if (video_config.is_encrypted()) { | |
| 211 pb::EncryptionScheme* encryption_scheme_message = | |
| 212 video_message->mutable_encryption_scheme(); | |
| 213 SerializeEncryptionScheme(encryption_scheme_message, | |
| 214 video_config.encryption_scheme()); | |
| 215 } | |
| 216 } | |
| 217 | |
| 218 void SerializeCdmConfig(const ::media::CdmConfig& cdm_config, | |
| 219 pb::CdmInitialize* message) { | |
| 220 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 221 | |
| 222 message->set_allow_distinctive_identifier( | |
| 223 cdm_config.allow_distinctive_identifier); | |
| 224 message->set_allow_persistent_state(cdm_config.allow_persistent_state); | |
| 225 message->set_use_hw_secure_codecs(cdm_config.use_hw_secure_codecs); | |
| 226 } | |
| 227 | |
| 228 void SerializeCdmPromiseResult(pb::CdmPromise* promise_message, | |
| 229 const CdmPromiseResult& result) { | |
| 230 promise_message->set_success(result.success()); | |
| 231 if (!result.success()) { | |
| 232 promise_message->set_exception( | |
| 233 static_cast<pb::MediaKeysException>(result.exception())); | |
| 234 promise_message->set_system_code(result.system_code()); | |
| 235 promise_message->set_error_message(result.error_message()); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 void SerializeCdmKeyInformation( | |
| 240 const ::media::CdmKeysInfo& keys_information, | |
| 241 pb::CdmClientOnSessionKeysChange* key_change_message) { | |
| 242 for (const auto& info : keys_information) { | |
| 243 pb::CdmKeyInformation* key = key_change_message->add_key_information(); | |
| 244 key->set_key_id(info->key_id.data(), info->key_id.size()); | |
| 245 key->set_status( | |
| 246 static_cast<pb::CdmKeyInformation::KeyStatus>(info->status)); | |
| 247 key->set_system_code(info->system_code); | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 bool DeserializeCdmKeyInformation( | |
| 252 const pb::CdmClientOnSessionKeysChange keychange_message, | |
| 253 CdmKeysInfo* key_information) { | |
| 254 DCHECK(key_information); | |
| 255 for (int i = 0; i < keychange_message.key_information_size(); ++i) { | |
| 256 const pb::CdmKeyInformation key_info_msg = | |
| 257 keychange_message.key_information(i); | |
| 258 | |
| 259 std::unique_ptr<::media::CdmKeyInformation> key( | |
| 260 new ::media::CdmKeyInformation( | |
| 261 key_info_msg.key_id(), | |
| 262 static_cast<::media::CdmKeyInformation::KeyStatus>( | |
| 263 key_info_msg.status()), | |
| 264 key_info_msg.system_code())); | |
| 265 key_information->push_back(std::move(key)); | |
| 266 } | |
| 267 return true; | |
| 268 } | |
| 269 | |
| 270 bool CdmPromiseFromMessage(const pb::RpcMessage& message, | |
| 271 CdmPromiseResult* result, | |
| 272 int* cdm_id, | |
| 273 std::string* session_id) { | |
| 274 DCHECK(message.has_cdm_promise_rpc()); | |
|
miu
2016/09/13 05:40:57
ditto: Return false instead.
erickung1
2016/09/15 02:13:33
Done.
| |
| 275 const auto& promise_message = message.cdm_promise_rpc(); | |
| 276 | |
| 277 DCHECK(DeserializeCdmPromiseResult(promise_message, result)); | |
|
miu
2016/09/13 05:40:57
This function won't be called in release builds.
erickung1
2016/09/15 02:13:33
Done.
| |
| 278 | |
| 279 if (cdm_id) | |
| 280 *cdm_id = promise_message.cdm_id(); | |
| 281 if (session_id) | |
| 282 *session_id = promise_message.session_id(); | |
| 283 | |
| 284 return true; | |
| 285 } | |
| 286 | |
| 287 void CdmPromiseToMessage(pb::CdmPromise* promise_message, | |
| 288 const CdmPromiseResult& result, | |
| 289 const std::string& session_id) { | |
| 290 SerializeCdmPromiseResult(promise_message, result); | |
| 291 if (!session_id.empty()) | |
|
miu
2016/09/13 05:40:57
Should this be a DCHECK(). Or, is it valid to some
erickung1
2016/09/15 02:13:33
Done.
| |
| 292 promise_message->set_session_id(session_id); | |
| 293 } | |
| 294 | |
| 295 void CdmPromiseWithCdmIdToMessage(pb::CdmPromise* promise_message, | |
| 296 const CdmPromiseResult& result, | |
| 297 int cdm_id) { | |
| 298 SerializeCdmPromiseResult(promise_message, result); | |
| 299 promise_message->set_cdm_id(cdm_id); | |
| 300 } | |
| 301 | |
| 302 //============================================================================== | |
| 303 CdmPromiseResult::CdmPromiseResult() | |
| 304 : CdmPromiseResult(::media::MediaKeys::UNKNOWN_ERROR, 0, "") {} | |
| 305 | |
| 306 CdmPromiseResult::CdmPromiseResult(::media::MediaKeys::Exception exception, | |
| 307 uint32_t system_code, | |
| 308 std::string error_message) | |
| 309 : success_(false), | |
| 310 exception_(exception), | |
| 311 system_code_(system_code), | |
| 312 error_message_(error_message) {} | |
| 313 | |
| 314 CdmPromiseResult::CdmPromiseResult(const CdmPromiseResult& other) = default; | |
| 315 | |
| 316 CdmPromiseResult::~CdmPromiseResult() = default; | |
| 317 | |
| 318 CdmPromiseResult CdmPromiseResult::SuccessResult() { | |
| 319 CdmPromiseResult result(static_cast<::media::MediaKeys::Exception>(0), 0, ""); | |
| 320 result.success_ = true; | |
| 321 return result; | |
| 322 } | |
| 323 | |
| 324 } // namespace remoting | |
| 325 } // namespace media | |
| OLD | NEW |