OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 "webkit/media/crypto/clear_key_cdm.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/logging.h" | |
11 #include "base/time.h" | |
12 #include "media/base/decoder_buffer.h" | |
13 | |
14 static const char kClearKeyCdmVersion[] = "0.1.0.0"; | |
15 | |
16 static scoped_refptr<media::DecoderBuffer> CopyDecoderBufferFrom( | |
17 const InputBuffer& input_buffer) { | |
18 scoped_refptr<media::DecoderBuffer> output_buffer = | |
19 media::DecoderBuffer::CopyFrom(input_buffer.data, input_buffer.data_size); | |
20 | |
21 std::vector<media::SubsampleEntry> subsamples; | |
22 for (int i = 0; i < input_buffer.num_subsamples; ++i) { | |
23 media::SubsampleEntry subsample; | |
24 subsample.clear_bytes = input_buffer.subsamples[i].clear_bytes; | |
25 subsample.cypher_bytes = input_buffer.subsamples[i].cypher_bytes; | |
26 subsamples.push_back(subsample); | |
27 } | |
28 | |
29 scoped_ptr<media::DecryptConfig> decrypt_config(new media::DecryptConfig( | |
30 std::string(reinterpret_cast<const char*>(input_buffer.key_id), | |
31 input_buffer.key_id_size), | |
32 std::string(reinterpret_cast<const char*>(input_buffer.iv), | |
33 input_buffer.key_id_size), | |
34 std::string(reinterpret_cast<const char*>(input_buffer.checksum), | |
35 input_buffer.checksum_size), | |
36 input_buffer.data_offset, | |
37 subsamples)); | |
38 | |
39 output_buffer->SetDecryptConfig(decrypt_config.Pass()); | |
40 output_buffer->SetTimestamp( | |
41 base::TimeDelta::FromMilliseconds(input_buffer.timestamp)); | |
42 output_buffer->SetDuration( | |
43 base::TimeDelta::FromMilliseconds(input_buffer.duration)); | |
44 | |
45 return output_buffer; | |
46 } | |
47 | |
48 template<typename Type> | |
49 class ScopedResetter { | |
50 public: | |
51 explicit ScopedResetter(Type* object) : object_(object) {} | |
52 ~ScopedResetter() { | |
53 object_->Reset(); | |
54 } | |
55 | |
56 private: | |
57 Type* object_; | |
ddorwin
2012/08/15 20:15:00
*const
xhwang
2012/08/16 02:32:40
Done.
| |
58 }; | |
59 | |
60 template<typename Type> | |
61 static Type* AllocateAndCopy(const Type* data, int size) { | |
62 COMPILE_ASSERT(sizeof(Type) == 1, type_size_is_not_one); | |
63 Type* copy = new Type[size]; | |
64 memcpy(copy, data, size); | |
65 return copy; | |
66 } | |
67 | |
68 ContentDecryptionModule* CdmCreateInstance() { | |
69 return new ClearKeyCdm(); | |
70 } | |
71 | |
72 const char* CdmGetVersion() { | |
73 return kClearKeyCdmVersion; | |
74 } | |
75 | |
76 ClearKeyCdm::Client::Client() : status_(kKeyError), key_message_length_(0) {} | |
77 | |
78 ClearKeyCdm::Client::~Client() {} | |
79 | |
80 void ClearKeyCdm::Client::Reset() { | |
81 status_ = kKeyError; | |
82 session_id_.clear(); | |
83 key_message_.reset(); | |
84 key_message_length_ = 0; | |
85 default_url_.clear(); | |
86 } | |
87 | |
88 void ClearKeyCdm::Client::KeyAdded(const std::string& key_system, | |
89 const std::string& session_id) { | |
90 status_ = kKeyAdded; | |
91 session_id_ = session_id; | |
92 } | |
93 | |
94 void ClearKeyCdm::Client::KeyError(const std::string& key_system, | |
95 const std::string& session_id, | |
96 media::Decryptor::KeyError error_code, | |
97 int system_code) { | |
98 status_ = kKeyError; | |
99 session_id_ = session_id; | |
100 } | |
101 | |
102 void ClearKeyCdm::Client::KeyMessage(const std::string& key_system, | |
103 const std::string& session_id, | |
104 scoped_array<uint8> message, | |
105 int message_length, | |
106 const std::string& default_url) { | |
107 status_ = kKeyMessage; | |
108 session_id_ = session_id; | |
109 key_message_ = message.Pass(); | |
110 key_message_length_ = message_length; | |
111 } | |
112 | |
113 void ClearKeyCdm::Client::NeedKey(const std::string& key_system, | |
114 const std::string& session_id, | |
115 scoped_array<uint8> init_data, | |
116 int init_data_length) { | |
117 NOTREACHED(); | |
118 } | |
119 | |
120 ClearKeyCdm::ClearKeyCdm() | |
121 : decryptor_(&client_), | |
ddorwin
2012/08/15 21:03:07
Re-using the client means we cannot parallelize an
xhwang
2012/08/16 02:32:40
Yes, we cannot parallelize these calls. Added comm
| |
122 decryption_status_(media::Decryptor::kError) { | |
123 } | |
124 | |
125 ClearKeyCdm::~ClearKeyCdm() {} | |
126 | |
127 void ClearKeyCdm::Reset() { | |
ddorwin
2012/08/15 20:15:00
This doesn't reset the whole class, so I think thi
xhwang
2012/08/16 02:32:40
Removed this method and made decryption_status_ an
| |
128 decryption_status_ = media::Decryptor::kError; | |
129 decrypted_buffer_ = NULL; | |
130 } | |
131 | |
132 CdmStatus ClearKeyCdm::GenerateKeyRequest(const uint8_t* init_data, | |
133 int init_data_size, | |
134 char** session_id, | |
135 int* session_id_size, | |
136 uint8_t** key_request, | |
137 int* key_request_size, | |
138 char** default_url, | |
139 int* default_url_size) { | |
140 ScopedResetter<Client> _(&client_); | |
Tom Finegan
2012/08/15 20:02:42
How about s/_(/resetter(/? :)
xhwang
2012/08/16 02:32:40
Replaced with auto_resetter.
| |
141 decryptor_.GenerateKeyRequest("", init_data, init_data_size); | |
Tom Finegan
2012/08/15 20:02:42
Don't we have a placeholder key system name we can
Tom Finegan
2012/08/15 20:22:24
Err- disregard this, meant to remove it.
| |
142 | |
143 if (client_.status() != Client::kKeyMessage) | |
144 return kCdmStatusErrorUnknown; | |
145 | |
146 *session_id_size = client_.session_id().size(); | |
147 *session_id = AllocateAndCopy(client_.session_id().data(), *session_id_size); | |
148 *key_request_size = client_.key_message_length(); | |
149 *key_request = AllocateAndCopy(client_.key_message(), *key_request_size); | |
150 *default_url_size = client_.default_url().size(); | |
151 *default_url = AllocateAndCopy(client_.default_url().data(), | |
152 *default_url_size); | |
153 | |
Tom Finegan
2012/08/15 20:02:42
nit: can probably remove this empty line
xhwang
2012/08/16 02:32:40
Done.
| |
154 return kCdmStatusSuccess; | |
155 } | |
156 | |
157 CdmStatus ClearKeyCdm::AddKey(const char* session_id, | |
158 int session_id_size, | |
159 const uint8_t* key, | |
160 int key_size) { | |
161 ScopedResetter<Client> _(&client_); | |
Tom Finegan
2012/08/15 20:02:42
Ditto on all of these. :)
xhwang
2012/08/16 02:32:40
Done.
| |
162 decryptor_.AddKey("", key, key_size, NULL, 0, | |
163 std::string(session_id, session_id_size)); | |
164 if (client_.status() != Client::kKeyAdded) | |
165 return kCdmStatusErrorUnknown; | |
166 | |
167 return kCdmStatusSuccess; | |
168 } | |
169 | |
170 CdmStatus ClearKeyCdm::CancelKeyRequest(const char* session_id, | |
171 int session_id_size) { | |
172 ScopedResetter<Client> _(&client_); | |
173 decryptor_.CancelKeyRequest("", std::string(session_id, session_id_size)); | |
174 | |
175 return kCdmStatusSuccess; | |
176 } | |
177 | |
178 CdmStatus ClearKeyCdm::Decrypt(const char* session_id, | |
179 int session_id_size, | |
180 const InputBuffer &encrypted_buffer, | |
181 OutputBuffer* decrypted_buffer) { | |
182 ScopedResetter<ClearKeyCdm> _(this); | |
183 scoped_refptr<media::DecoderBuffer> decoder_buffer = | |
184 CopyDecoderBufferFrom(encrypted_buffer); | |
185 | |
186 decryptor_.Decrypt(decoder_buffer, base::Bind(&ClearKeyCdm::OnBufferDecrpted, | |
187 base::Unretained(this))); | |
188 if (decryption_status_ == media::Decryptor::kError) | |
189 return kCdmStatusErrorUnknown; | |
190 | |
191 if (decryption_status_ == media::Decryptor::kNoKey) | |
192 return kCdmStatusErrorNoKey; | |
193 | |
194 DCHECK(decrypted_buffer_); | |
195 int data_size = decrypted_buffer_->GetDataSize(); | |
196 decrypted_buffer->data = AllocateAndCopy(decrypted_buffer_->GetData(), | |
197 data_size); | |
198 decrypted_buffer->data_size = data_size; | |
199 decrypted_buffer->timestamp = | |
200 decrypted_buffer_->GetTimestamp().InMilliseconds(); | |
201 decrypted_buffer->duration = | |
202 decrypted_buffer_->GetDuration().InMilliseconds(); | |
203 | |
204 return kCdmStatusSuccess; | |
205 } | |
206 | |
207 void ClearKeyCdm::OnBufferDecrpted( | |
Tom Finegan
2012/08/15 20:02:42
s/Decrpted/Decrypted/
xhwang
2012/08/16 02:32:40
This function is removed, but thanks for catching
| |
208 media::Decryptor::Status status, | |
209 const scoped_refptr<media::DecoderBuffer>& buffer) { | |
210 decryption_status_ = status; | |
211 decrypted_buffer_ = buffer; | |
212 } | |
OLD | NEW |