| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "media/formats/webm/webm_content_encodings_client.h" | 5 #include "media/formats/webm/webm_content_encodings_client.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "media/formats/webm/webm_constants.h" | 9 #include "media/formats/webm/webm_constants.h" |
| 10 | 10 |
| 11 namespace media { | 11 namespace media { |
| 12 | 12 |
| 13 WebMContentEncodingsClient::WebMContentEncodingsClient(const LogCB& log_cb) | 13 WebMContentEncodingsClient::WebMContentEncodingsClient( |
| 14 : log_cb_(log_cb), | 14 const scoped_refptr<MediaLog>& media_log) |
| 15 : media_log_(media_log), |
| 15 content_encryption_encountered_(false), | 16 content_encryption_encountered_(false), |
| 16 content_encodings_ready_(false) { | 17 content_encodings_ready_(false) { |
| 17 } | 18 } |
| 18 | 19 |
| 19 WebMContentEncodingsClient::~WebMContentEncodingsClient() { | 20 WebMContentEncodingsClient::~WebMContentEncodingsClient() { |
| 20 STLDeleteElements(&content_encodings_); | 21 STLDeleteElements(&content_encodings_); |
| 21 } | 22 } |
| 22 | 23 |
| 23 const ContentEncodings& WebMContentEncodingsClient::content_encodings() const { | 24 const ContentEncodings& WebMContentEncodingsClient::content_encodings() const { |
| 24 DCHECK(content_encodings_ready_); | 25 DCHECK(content_encodings_ready_); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 37 if (id == kWebMIdContentEncoding) { | 38 if (id == kWebMIdContentEncoding) { |
| 38 DCHECK(!cur_content_encoding_.get()); | 39 DCHECK(!cur_content_encoding_.get()); |
| 39 DCHECK(!content_encryption_encountered_); | 40 DCHECK(!content_encryption_encountered_); |
| 40 cur_content_encoding_.reset(new ContentEncoding()); | 41 cur_content_encoding_.reset(new ContentEncoding()); |
| 41 return this; | 42 return this; |
| 42 } | 43 } |
| 43 | 44 |
| 44 if (id == kWebMIdContentEncryption) { | 45 if (id == kWebMIdContentEncryption) { |
| 45 DCHECK(cur_content_encoding_.get()); | 46 DCHECK(cur_content_encoding_.get()); |
| 46 if (content_encryption_encountered_) { | 47 if (content_encryption_encountered_) { |
| 47 MEDIA_LOG(ERROR, log_cb_) << "Unexpected multiple ContentEncryption."; | 48 MEDIA_LOG(ERROR, media_log_) << "Unexpected multiple ContentEncryption."; |
| 48 return NULL; | 49 return NULL; |
| 49 } | 50 } |
| 50 content_encryption_encountered_ = true; | 51 content_encryption_encountered_ = true; |
| 51 return this; | 52 return this; |
| 52 } | 53 } |
| 53 | 54 |
| 54 if (id == kWebMIdContentEncAESSettings) { | 55 if (id == kWebMIdContentEncAESSettings) { |
| 55 DCHECK(cur_content_encoding_.get()); | 56 DCHECK(cur_content_encoding_.get()); |
| 56 return this; | 57 return this; |
| 57 } | 58 } |
| 58 | 59 |
| 59 // This should not happen if WebMListParser is working properly. | 60 // This should not happen if WebMListParser is working properly. |
| 60 DCHECK(false); | 61 DCHECK(false); |
| 61 return NULL; | 62 return NULL; |
| 62 } | 63 } |
| 63 | 64 |
| 64 // Mandatory occurrence restriction is checked in this function. Multiple | 65 // Mandatory occurrence restriction is checked in this function. Multiple |
| 65 // occurrence restriction is checked in OnUInt and OnBinary. | 66 // occurrence restriction is checked in OnUInt and OnBinary. |
| 66 bool WebMContentEncodingsClient::OnListEnd(int id) { | 67 bool WebMContentEncodingsClient::OnListEnd(int id) { |
| 67 if (id == kWebMIdContentEncodings) { | 68 if (id == kWebMIdContentEncodings) { |
| 68 // ContentEncoding element is mandatory. Check this! | 69 // ContentEncoding element is mandatory. Check this! |
| 69 if (content_encodings_.empty()) { | 70 if (content_encodings_.empty()) { |
| 70 MEDIA_LOG(ERROR, log_cb_) << "Missing ContentEncoding."; | 71 MEDIA_LOG(ERROR, media_log_) << "Missing ContentEncoding."; |
| 71 return false; | 72 return false; |
| 72 } | 73 } |
| 73 content_encodings_ready_ = true; | 74 content_encodings_ready_ = true; |
| 74 return true; | 75 return true; |
| 75 } | 76 } |
| 76 | 77 |
| 77 if (id == kWebMIdContentEncoding) { | 78 if (id == kWebMIdContentEncoding) { |
| 78 DCHECK(cur_content_encoding_.get()); | 79 DCHECK(cur_content_encoding_.get()); |
| 79 | 80 |
| 80 // | 81 // |
| 81 // Specify default values to missing mandatory elements. | 82 // Specify default values to missing mandatory elements. |
| 82 // | 83 // |
| 83 | 84 |
| 84 if (cur_content_encoding_->order() == ContentEncoding::kOrderInvalid) { | 85 if (cur_content_encoding_->order() == ContentEncoding::kOrderInvalid) { |
| 85 // Default value of encoding order is 0, which should only be used on the | 86 // Default value of encoding order is 0, which should only be used on the |
| 86 // first ContentEncoding. | 87 // first ContentEncoding. |
| 87 if (!content_encodings_.empty()) { | 88 if (!content_encodings_.empty()) { |
| 88 MEDIA_LOG(ERROR, log_cb_) << "Missing ContentEncodingOrder."; | 89 MEDIA_LOG(ERROR, media_log_) << "Missing ContentEncodingOrder."; |
| 89 return false; | 90 return false; |
| 90 } | 91 } |
| 91 cur_content_encoding_->set_order(0); | 92 cur_content_encoding_->set_order(0); |
| 92 } | 93 } |
| 93 | 94 |
| 94 if (cur_content_encoding_->scope() == ContentEncoding::kScopeInvalid) | 95 if (cur_content_encoding_->scope() == ContentEncoding::kScopeInvalid) |
| 95 cur_content_encoding_->set_scope(ContentEncoding::kScopeAllFrameContents); | 96 cur_content_encoding_->set_scope(ContentEncoding::kScopeAllFrameContents); |
| 96 | 97 |
| 97 if (cur_content_encoding_->type() == ContentEncoding::kTypeInvalid) | 98 if (cur_content_encoding_->type() == ContentEncoding::kTypeInvalid) |
| 98 cur_content_encoding_->set_type(ContentEncoding::kTypeCompression); | 99 cur_content_encoding_->set_type(ContentEncoding::kTypeCompression); |
| 99 | 100 |
| 100 // Check for elements valid in spec but not supported for now. | 101 // Check for elements valid in spec but not supported for now. |
| 101 if (cur_content_encoding_->type() == ContentEncoding::kTypeCompression) { | 102 if (cur_content_encoding_->type() == ContentEncoding::kTypeCompression) { |
| 102 MEDIA_LOG(ERROR, log_cb_) << "ContentCompression not supported."; | 103 MEDIA_LOG(ERROR, media_log_) << "ContentCompression not supported."; |
| 103 return false; | 104 return false; |
| 104 } | 105 } |
| 105 | 106 |
| 106 // Enforce mandatory elements without default values. | 107 // Enforce mandatory elements without default values. |
| 107 DCHECK(cur_content_encoding_->type() == ContentEncoding::kTypeEncryption); | 108 DCHECK(cur_content_encoding_->type() == ContentEncoding::kTypeEncryption); |
| 108 if (!content_encryption_encountered_) { | 109 if (!content_encryption_encountered_) { |
| 109 MEDIA_LOG(ERROR, log_cb_) << "ContentEncodingType is encryption but" | 110 MEDIA_LOG(ERROR, media_log_) << "ContentEncodingType is encryption but" |
| 110 << " ContentEncryption is missing."; | 111 << " ContentEncryption is missing."; |
| 111 return false; | 112 return false; |
| 112 } | 113 } |
| 113 | 114 |
| 114 content_encodings_.push_back(cur_content_encoding_.release()); | 115 content_encodings_.push_back(cur_content_encoding_.release()); |
| 115 content_encryption_encountered_ = false; | 116 content_encryption_encountered_ = false; |
| 116 return true; | 117 return true; |
| 117 } | 118 } |
| 118 | 119 |
| 119 if (id == kWebMIdContentEncryption) { | 120 if (id == kWebMIdContentEncryption) { |
| 120 DCHECK(cur_content_encoding_.get()); | 121 DCHECK(cur_content_encoding_.get()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 139 return false; | 140 return false; |
| 140 } | 141 } |
| 141 | 142 |
| 142 // Multiple occurrence restriction and range are checked in this function. | 143 // Multiple occurrence restriction and range are checked in this function. |
| 143 // Mandatory occurrence restriction is checked in OnListEnd. | 144 // Mandatory occurrence restriction is checked in OnListEnd. |
| 144 bool WebMContentEncodingsClient::OnUInt(int id, int64 val) { | 145 bool WebMContentEncodingsClient::OnUInt(int id, int64 val) { |
| 145 DCHECK(cur_content_encoding_.get()); | 146 DCHECK(cur_content_encoding_.get()); |
| 146 | 147 |
| 147 if (id == kWebMIdContentEncodingOrder) { | 148 if (id == kWebMIdContentEncodingOrder) { |
| 148 if (cur_content_encoding_->order() != ContentEncoding::kOrderInvalid) { | 149 if (cur_content_encoding_->order() != ContentEncoding::kOrderInvalid) { |
| 149 MEDIA_LOG(ERROR, log_cb_) << "Unexpected multiple ContentEncodingOrder."; | 150 MEDIA_LOG(ERROR, media_log_) |
| 151 << "Unexpected multiple ContentEncodingOrder."; |
| 150 return false; | 152 return false; |
| 151 } | 153 } |
| 152 | 154 |
| 153 if (val != static_cast<int64>(content_encodings_.size())) { | 155 if (val != static_cast<int64>(content_encodings_.size())) { |
| 154 // According to the spec, encoding order starts with 0 and counts upwards. | 156 // According to the spec, encoding order starts with 0 and counts upwards. |
| 155 MEDIA_LOG(ERROR, log_cb_) << "Unexpected ContentEncodingOrder."; | 157 MEDIA_LOG(ERROR, media_log_) << "Unexpected ContentEncodingOrder."; |
| 156 return false; | 158 return false; |
| 157 } | 159 } |
| 158 | 160 |
| 159 cur_content_encoding_->set_order(val); | 161 cur_content_encoding_->set_order(val); |
| 160 return true; | 162 return true; |
| 161 } | 163 } |
| 162 | 164 |
| 163 if (id == kWebMIdContentEncodingScope) { | 165 if (id == kWebMIdContentEncodingScope) { |
| 164 if (cur_content_encoding_->scope() != ContentEncoding::kScopeInvalid) { | 166 if (cur_content_encoding_->scope() != ContentEncoding::kScopeInvalid) { |
| 165 MEDIA_LOG(ERROR, log_cb_) << "Unexpected multiple ContentEncodingScope."; | 167 MEDIA_LOG(ERROR, media_log_) |
| 168 << "Unexpected multiple ContentEncodingScope."; |
| 166 return false; | 169 return false; |
| 167 } | 170 } |
| 168 | 171 |
| 169 if (val == ContentEncoding::kScopeInvalid || | 172 if (val == ContentEncoding::kScopeInvalid || |
| 170 val > ContentEncoding::kScopeMax) { | 173 val > ContentEncoding::kScopeMax) { |
| 171 MEDIA_LOG(ERROR, log_cb_) << "Unexpected ContentEncodingScope."; | 174 MEDIA_LOG(ERROR, media_log_) << "Unexpected ContentEncodingScope."; |
| 172 return false; | 175 return false; |
| 173 } | 176 } |
| 174 | 177 |
| 175 if (val & ContentEncoding::kScopeNextContentEncodingData) { | 178 if (val & ContentEncoding::kScopeNextContentEncodingData) { |
| 176 MEDIA_LOG(ERROR, log_cb_) << "Encoded next ContentEncoding is not " | 179 MEDIA_LOG(ERROR, media_log_) << "Encoded next ContentEncoding is not " |
| 177 "supported."; | 180 "supported."; |
| 178 return false; | 181 return false; |
| 179 } | 182 } |
| 180 | 183 |
| 181 cur_content_encoding_->set_scope(static_cast<ContentEncoding::Scope>(val)); | 184 cur_content_encoding_->set_scope(static_cast<ContentEncoding::Scope>(val)); |
| 182 return true; | 185 return true; |
| 183 } | 186 } |
| 184 | 187 |
| 185 if (id == kWebMIdContentEncodingType) { | 188 if (id == kWebMIdContentEncodingType) { |
| 186 if (cur_content_encoding_->type() != ContentEncoding::kTypeInvalid) { | 189 if (cur_content_encoding_->type() != ContentEncoding::kTypeInvalid) { |
| 187 MEDIA_LOG(ERROR, log_cb_) << "Unexpected multiple ContentEncodingType."; | 190 MEDIA_LOG(ERROR, media_log_) |
| 191 << "Unexpected multiple ContentEncodingType."; |
| 188 return false; | 192 return false; |
| 189 } | 193 } |
| 190 | 194 |
| 191 if (val == ContentEncoding::kTypeCompression) { | 195 if (val == ContentEncoding::kTypeCompression) { |
| 192 MEDIA_LOG(ERROR, log_cb_) << "ContentCompression not supported."; | 196 MEDIA_LOG(ERROR, media_log_) << "ContentCompression not supported."; |
| 193 return false; | 197 return false; |
| 194 } | 198 } |
| 195 | 199 |
| 196 if (val != ContentEncoding::kTypeEncryption) { | 200 if (val != ContentEncoding::kTypeEncryption) { |
| 197 MEDIA_LOG(ERROR, log_cb_) << "Unexpected ContentEncodingType " << val | 201 MEDIA_LOG(ERROR, media_log_) << "Unexpected ContentEncodingType " << val |
| 198 << "."; | 202 << "."; |
| 199 return false; | 203 return false; |
| 200 } | 204 } |
| 201 | 205 |
| 202 cur_content_encoding_->set_type(static_cast<ContentEncoding::Type>(val)); | 206 cur_content_encoding_->set_type(static_cast<ContentEncoding::Type>(val)); |
| 203 return true; | 207 return true; |
| 204 } | 208 } |
| 205 | 209 |
| 206 if (id == kWebMIdContentEncAlgo) { | 210 if (id == kWebMIdContentEncAlgo) { |
| 207 if (cur_content_encoding_->encryption_algo() != | 211 if (cur_content_encoding_->encryption_algo() != |
| 208 ContentEncoding::kEncAlgoInvalid) { | 212 ContentEncoding::kEncAlgoInvalid) { |
| 209 MEDIA_LOG(ERROR, log_cb_) << "Unexpected multiple ContentEncAlgo."; | 213 MEDIA_LOG(ERROR, media_log_) << "Unexpected multiple ContentEncAlgo."; |
| 210 return false; | 214 return false; |
| 211 } | 215 } |
| 212 | 216 |
| 213 if (val < ContentEncoding::kEncAlgoNotEncrypted || | 217 if (val < ContentEncoding::kEncAlgoNotEncrypted || |
| 214 val > ContentEncoding::kEncAlgoAes) { | 218 val > ContentEncoding::kEncAlgoAes) { |
| 215 MEDIA_LOG(ERROR, log_cb_) << "Unexpected ContentEncAlgo " << val << "."; | 219 MEDIA_LOG(ERROR, media_log_) << "Unexpected ContentEncAlgo " << val |
| 220 << "."; |
| 216 return false; | 221 return false; |
| 217 } | 222 } |
| 218 | 223 |
| 219 cur_content_encoding_->set_encryption_algo( | 224 cur_content_encoding_->set_encryption_algo( |
| 220 static_cast<ContentEncoding::EncryptionAlgo>(val)); | 225 static_cast<ContentEncoding::EncryptionAlgo>(val)); |
| 221 return true; | 226 return true; |
| 222 } | 227 } |
| 223 | 228 |
| 224 if (id == kWebMIdAESSettingsCipherMode) { | 229 if (id == kWebMIdAESSettingsCipherMode) { |
| 225 if (cur_content_encoding_->cipher_mode() != | 230 if (cur_content_encoding_->cipher_mode() != |
| 226 ContentEncoding::kCipherModeInvalid) { | 231 ContentEncoding::kCipherModeInvalid) { |
| 227 MEDIA_LOG(ERROR, log_cb_) << "Unexpected multiple AESSettingsCipherMode."; | 232 MEDIA_LOG(ERROR, media_log_) |
| 233 << "Unexpected multiple AESSettingsCipherMode."; |
| 228 return false; | 234 return false; |
| 229 } | 235 } |
| 230 | 236 |
| 231 if (val != ContentEncoding::kCipherModeCtr) { | 237 if (val != ContentEncoding::kCipherModeCtr) { |
| 232 MEDIA_LOG(ERROR, log_cb_) << "Unexpected AESSettingsCipherMode " << val | 238 MEDIA_LOG(ERROR, media_log_) << "Unexpected AESSettingsCipherMode " << val |
| 233 << "."; | 239 << "."; |
| 234 return false; | 240 return false; |
| 235 } | 241 } |
| 236 | 242 |
| 237 cur_content_encoding_->set_cipher_mode( | 243 cur_content_encoding_->set_cipher_mode( |
| 238 static_cast<ContentEncoding::CipherMode>(val)); | 244 static_cast<ContentEncoding::CipherMode>(val)); |
| 239 return true; | 245 return true; |
| 240 } | 246 } |
| 241 | 247 |
| 242 // This should not happen if WebMListParser is working properly. | 248 // This should not happen if WebMListParser is working properly. |
| 243 DCHECK(false); | 249 DCHECK(false); |
| 244 return false; | 250 return false; |
| 245 } | 251 } |
| 246 | 252 |
| 247 // Multiple occurrence restriction is checked in this function. Mandatory | 253 // Multiple occurrence restriction is checked in this function. Mandatory |
| 248 // restriction is checked in OnListEnd. | 254 // restriction is checked in OnListEnd. |
| 249 bool WebMContentEncodingsClient::OnBinary(int id, const uint8* data, int size) { | 255 bool WebMContentEncodingsClient::OnBinary(int id, const uint8* data, int size) { |
| 250 DCHECK(cur_content_encoding_.get()); | 256 DCHECK(cur_content_encoding_.get()); |
| 251 DCHECK(data); | 257 DCHECK(data); |
| 252 DCHECK_GT(size, 0); | 258 DCHECK_GT(size, 0); |
| 253 | 259 |
| 254 if (id == kWebMIdContentEncKeyID) { | 260 if (id == kWebMIdContentEncKeyID) { |
| 255 if (!cur_content_encoding_->encryption_key_id().empty()) { | 261 if (!cur_content_encoding_->encryption_key_id().empty()) { |
| 256 MEDIA_LOG(ERROR, log_cb_) << "Unexpected multiple ContentEncKeyID"; | 262 MEDIA_LOG(ERROR, media_log_) << "Unexpected multiple ContentEncKeyID"; |
| 257 return false; | 263 return false; |
| 258 } | 264 } |
| 259 cur_content_encoding_->SetEncryptionKeyId(data, size); | 265 cur_content_encoding_->SetEncryptionKeyId(data, size); |
| 260 return true; | 266 return true; |
| 261 } | 267 } |
| 262 | 268 |
| 263 // This should not happen if WebMListParser is working properly. | 269 // This should not happen if WebMListParser is working properly. |
| 264 DCHECK(false); | 270 DCHECK(false); |
| 265 return false; | 271 return false; |
| 266 } | 272 } |
| 267 | 273 |
| 268 } // namespace media | 274 } // namespace media |
| OLD | NEW |