OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef MEDIA_CRYPTO_AES_DECRYPTOR_H_ | 5 #ifndef MEDIA_CRYPTO_AES_DECRYPTOR_H_ |
6 #define MEDIA_CRYPTO_AES_DECRYPTOR_H_ | 6 #define MEDIA_CRYPTO_AES_DECRYPTOR_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "media/base/decryptor.h" | 16 #include "media/base/decryptor.h" |
17 #include "media/base/media_export.h" | 17 #include "media/base/media_export.h" |
18 | 18 |
19 namespace crypto { | 19 namespace crypto { |
20 class SymmetricKey; | 20 class SymmetricKey; |
21 } | 21 } |
22 | 22 |
23 namespace media { | 23 namespace media { |
24 | 24 |
25 class DecryptorClient; | |
26 | |
27 // Decrypts an AES encrypted buffer into an unencrypted buffer. The AES | 25 // Decrypts an AES encrypted buffer into an unencrypted buffer. The AES |
28 // encryption must be CTR with a key size of 128bits. | 26 // encryption must be CTR with a key size of 128bits. |
29 class MEDIA_EXPORT AesDecryptor : public Decryptor { | 27 class MEDIA_EXPORT AesDecryptor : public Decryptor { |
30 public: | 28 public: |
31 // The AesDecryptor does not take ownership of the |client|. The |client| | 29 AesDecryptor(const KeyAddedCB& key_added_cb, |
32 // must be valid throughout the lifetime of the AesDecryptor. | 30 const KeyErrorCB& key_error_cb, |
33 explicit AesDecryptor(DecryptorClient* client); | 31 const KeyMessageCB& key_message_cb, |
| 32 const NeedKeyCB& need_key_cb); |
34 virtual ~AesDecryptor(); | 33 virtual ~AesDecryptor(); |
35 | 34 |
36 // Decryptor implementation. | 35 // Decryptor implementation. |
37 virtual bool GenerateKeyRequest(const std::string& key_system, | 36 virtual bool GenerateKeyRequest(const std::string& key_system, |
38 const std::string& type, | 37 const std::string& type, |
39 const uint8* init_data, | 38 const uint8* init_data, |
40 int init_data_length) OVERRIDE; | 39 int init_data_length) OVERRIDE; |
41 virtual void AddKey(const std::string& key_system, | 40 virtual void AddKey(const std::string& key_system, |
42 const uint8* key, | 41 const uint8* key, |
43 int key_length, | 42 int key_length, |
44 const uint8* init_data, | 43 const uint8* init_data, |
45 int init_data_length, | 44 int init_data_length, |
46 const std::string& session_id) OVERRIDE; | 45 const std::string& session_id) OVERRIDE; |
47 virtual void CancelKeyRequest(const std::string& key_system, | 46 virtual void CancelKeyRequest(const std::string& key_system, |
48 const std::string& session_id) OVERRIDE; | 47 const std::string& session_id) OVERRIDE; |
49 virtual void RegisterKeyAddedCB(StreamType stream_type, | 48 virtual void RegisterNewKeyCB(StreamType stream_type, |
50 const KeyAddedCB& key_added_cb) OVERRIDE; | 49 const NewKeyCB& key_added_cb) OVERRIDE; |
51 virtual void Decrypt(StreamType stream_type, | 50 virtual void Decrypt(StreamType stream_type, |
52 const scoped_refptr<DecoderBuffer>& encrypted, | 51 const scoped_refptr<DecoderBuffer>& encrypted, |
53 const DecryptCB& decrypt_cb) OVERRIDE; | 52 const DecryptCB& decrypt_cb) OVERRIDE; |
54 virtual void CancelDecrypt(StreamType stream_type) OVERRIDE; | 53 virtual void CancelDecrypt(StreamType stream_type) OVERRIDE; |
55 virtual void InitializeAudioDecoder(scoped_ptr<AudioDecoderConfig> config, | 54 virtual void InitializeAudioDecoder(scoped_ptr<AudioDecoderConfig> config, |
56 const DecoderInitCB& init_cb) OVERRIDE; | 55 const DecoderInitCB& init_cb) OVERRIDE; |
57 virtual void InitializeVideoDecoder(scoped_ptr<VideoDecoderConfig> config, | 56 virtual void InitializeVideoDecoder(scoped_ptr<VideoDecoderConfig> config, |
58 const DecoderInitCB& init_cb) OVERRIDE; | 57 const DecoderInitCB& init_cb) OVERRIDE; |
59 virtual void DecryptAndDecodeAudio( | 58 virtual void DecryptAndDecodeAudio( |
60 const scoped_refptr<DecoderBuffer>& encrypted, | 59 const scoped_refptr<DecoderBuffer>& encrypted, |
(...skipping 28 matching lines...) Expand all Loading... |
89 DISALLOW_COPY_AND_ASSIGN(DecryptionKey); | 88 DISALLOW_COPY_AND_ASSIGN(DecryptionKey); |
90 }; | 89 }; |
91 | 90 |
92 // Sets |key| for |key_id|. The AesDecryptor takes the ownership of the |key|. | 91 // Sets |key| for |key_id|. The AesDecryptor takes the ownership of the |key|. |
93 void SetKey(const std::string& key_id, scoped_ptr<DecryptionKey> key); | 92 void SetKey(const std::string& key_id, scoped_ptr<DecryptionKey> key); |
94 | 93 |
95 // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns | 94 // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns |
96 // the key. Returns NULL if no key is associated with |key_id|. | 95 // the key. Returns NULL if no key is associated with |key_id|. |
97 DecryptionKey* GetKey(const std::string& key_id) const; | 96 DecryptionKey* GetKey(const std::string& key_id) const; |
98 | 97 |
| 98 // Callbacks for firing key events. |
| 99 KeyAddedCB key_added_cb_; |
| 100 KeyErrorCB key_error_cb_; |
| 101 KeyMessageCB key_message_cb_; |
| 102 NeedKeyCB need_key_cb_; |
| 103 |
99 // KeyMap owns the DecryptionKey* and must delete them when they are | 104 // KeyMap owns the DecryptionKey* and must delete them when they are |
100 // not needed any more. | 105 // not needed any more. |
101 typedef base::hash_map<std::string, DecryptionKey*> KeyMap; | 106 typedef base::hash_map<std::string, DecryptionKey*> KeyMap; |
102 | 107 |
103 // Since only Decrypt() is called off the renderer thread, we only need to | 108 // Since only Decrypt() is called off the renderer thread, we only need to |
104 // protect |key_map_|, the only member variable that is shared between | 109 // protect |key_map_|, the only member variable that is shared between |
105 // Decrypt() and other methods. | 110 // Decrypt() and other methods. |
106 KeyMap key_map_; // Protected by the |key_map_lock_|. | 111 KeyMap key_map_; // Protected by the |key_map_lock_|. |
107 mutable base::Lock key_map_lock_; // Protects the |key_map_|. | 112 mutable base::Lock key_map_lock_; // Protects the |key_map_|. |
108 | 113 |
109 // Make session ID unique per renderer by making it static. | 114 // Make session ID unique per renderer by making it static. |
110 // TODO(xhwang): Make session ID more strictly defined if needed: | 115 // TODO(xhwang): Make session ID more strictly defined if needed: |
111 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16739#c0 | 116 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16739#c0 |
112 static uint32 next_session_id_; | 117 static uint32 next_session_id_; |
113 | 118 |
114 DecryptorClient* const client_; | 119 NewKeyCB new_audio_key_cb_; |
115 | 120 NewKeyCB new_video_key_cb_; |
116 KeyAddedCB audio_key_added_cb_; | |
117 KeyAddedCB video_key_added_cb_; | |
118 | 121 |
119 DISALLOW_COPY_AND_ASSIGN(AesDecryptor); | 122 DISALLOW_COPY_AND_ASSIGN(AesDecryptor); |
120 }; | 123 }; |
121 | 124 |
122 } // namespace media | 125 } // namespace media |
123 | 126 |
124 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_ | 127 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_ |
OLD | NEW |