OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/cdm/browser/widevine_drm_delegate_android.h" |
| 6 |
| 7 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR. |
| 8 |
| 9 namespace cdm { |
| 10 |
| 11 namespace { |
| 12 |
| 13 uint32 ReadUint32(const uint8_t* data) { |
| 14 uint32 value = 0; |
| 15 for (int i = 0; i < 4; ++i) |
| 16 value = (value << 8) | data[i]; |
| 17 return value; |
| 18 } |
| 19 |
| 20 uint64 ReadUint64(const uint8_t* data) { |
| 21 uint64 value = 0; |
| 22 for (int i = 0; i < 8; ++i) |
| 23 value = (value << 8) | data[i]; |
| 24 return value; |
| 25 } |
| 26 |
| 27 // The structure of an ISO CENC Protection System Specific Header (PSSH) box is |
| 28 // as follows. (See ISO/IEC FDIS 23001-7:2011(E).) |
| 29 // Note: ISO boxes use big-endian values. |
| 30 // |
| 31 // PSSH { |
| 32 // uint32 Size |
| 33 // uint32 Type |
| 34 // uint64 LargeSize # Field is only present if value(Size) == 1. |
| 35 // uint32 VersionAndFlags |
| 36 // uint8[16] SystemId |
| 37 // uint32 DataSize |
| 38 // uint8[DataSize] Data |
| 39 // } |
| 40 const int kBoxHeaderSize = 8; // Box's header contains Size and Type. |
| 41 const int kBoxLargeSizeSize = 8; |
| 42 const int kPsshVersionFlagSize = 4; |
| 43 const int kPsshSystemIdSize = 16; |
| 44 const int kPsshDataSizeSize = 4; |
| 45 const uint32 kTencType = 0x74656e63; |
| 46 const uint32 kPsshType = 0x70737368; |
| 47 |
| 48 const uint8 kWidevineUuid[16] = { |
| 49 0xED, 0xEF, 0x8B, 0xA9, 0x79, 0xD6, 0x4A, 0xCE, |
| 50 0xA3, 0xC8, 0x27, 0xDC, 0xD5, 0x1D, 0x21, 0xED }; |
| 51 |
| 52 // Tries to find a PSSH box with the Widevine UUID, parses the |
| 53 // "Data" of the box and put it in |pssh_data|. Returns true if such a box is |
| 54 // found and successfully parsed. Returns false otherwise. |
| 55 // Notes: |
| 56 // 1, If multiple PSSH boxes are found,the "Data" of the first matching PSSH box |
| 57 // will be set in |pssh_data|. |
| 58 // 2, Only PSSH and TENC boxes are allowed in |data|. TENC boxes are skipped. |
| 59 bool GetPsshData(const uint8* data, |
| 60 int data_size, |
| 61 std::vector<uint8>* pssh_data) { |
| 62 const uint8* cur = data; |
| 63 const uint8* data_end = data + data_size; |
| 64 int bytes_left = data_size; |
| 65 |
| 66 while (bytes_left > 0) { |
| 67 const uint8* box_head = cur; |
| 68 |
| 69 if (bytes_left < kBoxHeaderSize) |
| 70 return false; |
| 71 |
| 72 uint64_t box_size = ReadUint32(cur); |
| 73 uint32 type = ReadUint32(cur + 4); |
| 74 cur += kBoxHeaderSize; |
| 75 bytes_left -= kBoxHeaderSize; |
| 76 |
| 77 if (box_size == 1) { // LargeSize is present. |
| 78 if (bytes_left < kBoxLargeSizeSize) |
| 79 return false; |
| 80 |
| 81 box_size = ReadUint64(cur); |
| 82 cur += kBoxLargeSizeSize; |
| 83 bytes_left -= kBoxLargeSizeSize; |
| 84 } else if (box_size == 0) { |
| 85 box_size = bytes_left + kBoxHeaderSize; |
| 86 } |
| 87 |
| 88 const uint8* box_end = box_head + box_size; |
| 89 if (data_end < box_end) |
| 90 return false; |
| 91 |
| 92 if (type == kTencType) { |
| 93 // Skip 'tenc' box. |
| 94 cur = box_end; |
| 95 bytes_left = data_end - cur; |
| 96 continue; |
| 97 } else if (type != kPsshType) { |
| 98 return false; |
| 99 } |
| 100 |
| 101 const int kPsshBoxMinimumSize = |
| 102 kPsshVersionFlagSize + kPsshSystemIdSize + kPsshDataSizeSize; |
| 103 if (box_end < cur + kPsshBoxMinimumSize) |
| 104 return false; |
| 105 |
| 106 uint32 version_and_flags = ReadUint32(cur); |
| 107 cur += kPsshVersionFlagSize; |
| 108 bytes_left -= kPsshVersionFlagSize; |
| 109 if (version_and_flags != 0) |
| 110 return false; |
| 111 |
| 112 DCHECK_GE(bytes_left, kPsshSystemIdSize); |
| 113 if (!std::equal(kWidevineUuid, |
| 114 kWidevineUuid + sizeof(kWidevineUuid), cur)) { |
| 115 cur = box_end; |
| 116 bytes_left = data_end - cur; |
| 117 continue; |
| 118 } |
| 119 |
| 120 cur += kPsshSystemIdSize; |
| 121 bytes_left -= kPsshSystemIdSize; |
| 122 |
| 123 uint32 data_size = ReadUint32(cur); |
| 124 cur += kPsshDataSizeSize; |
| 125 bytes_left -= kPsshDataSizeSize; |
| 126 |
| 127 if (box_end < cur + data_size) |
| 128 return false; |
| 129 |
| 130 pssh_data->assign(cur, cur + data_size); |
| 131 return true; |
| 132 } |
| 133 |
| 134 return false; |
| 135 } |
| 136 |
| 137 } |
| 138 |
| 139 // static |
| 140 void WidevineDrmDelegateAndroid::Install() { |
| 141 media::MediaDrmBridge::RegisterDelegate( |
| 142 kWidevineKeySystem, new cdm::WidevineDrmDelegateAndroid); |
| 143 } |
| 144 |
| 145 WidevineDrmDelegateAndroid::WidevineDrmDelegateAndroid() { |
| 146 } |
| 147 |
| 148 WidevineDrmDelegateAndroid::~WidevineDrmDelegateAndroid() { |
| 149 } |
| 150 |
| 151 bool WidevineDrmDelegateAndroid::OnCreateSession( |
| 152 const media::EmeInitDataType init_data_type, |
| 153 const uint8* init_data, |
| 154 int init_data_length, |
| 155 std::vector<uint8>& init_data_out, |
| 156 std::vector<std::string>& optional_parameters_out) { |
| 157 // Widevine MediaDrm plugin only accepts the "data" part of the PSSH box as |
| 158 // the init data when using MP4 container. |
| 159 if (init_data_type == media::EmeInitDataType::CENC) { |
| 160 if (!GetPsshData(init_data, init_data_length, &init_data_out)) |
| 161 return false; |
| 162 } |
| 163 return true; |
| 164 } |
| 165 |
| 166 } // namespace cdm |
OLD | NEW |