Shaka Player Embedded
clearkey_implementation.h
Go to the documentation of this file.
1 // Copyright 2017 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_EME_CLEARKEY_FACTORY_H_
16 #define SHAKA_EMBEDDED_EME_CLEARKEY_FACTORY_H_
17 
18 #include <list>
19 #include <mutex>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
26 #include "src/util/decryptor.h"
27 
28 #define AES_BLOCK_SIZE 16u
29 
30 namespace shaka {
31 
32 namespace media {
33 class DecoderIntegration;
34 class DecoderDecryptIntegration;
35 } // namespace media
36 
37 namespace eme {
38 
39 class ClearKeyImplementation final : public Implementation {
40  public:
42  ~ClearKeyImplementation() override;
43 
44  bool GetExpiration(const std::string& session_id,
45  int64_t* expiration) const override;
46 
47  bool GetKeyStatuses(const std::string& session_id,
48  std::vector<KeyStatusInfo>* statuses) const override;
49 
50  void SetServerCertificate(EmePromise promise, Data cert) override;
51 
52  void CreateSessionAndGenerateRequest(
53  EmePromise promise,
54  std::function<void(const std::string&)> set_session_id,
55  MediaKeySessionType session_type, MediaKeyInitDataType init_data_type,
56  Data data) override;
57 
58  void Load(const std::string& session_id, EmePromise promise) override;
59 
60  void Update(const std::string& session_id, EmePromise promise,
61  Data data) override;
62 
63  void Close(const std::string& session_id, EmePromise promise) override;
64 
65  void Remove(const std::string& session_id, EmePromise promise) override;
66 
67  DecryptStatus Decrypt(const FrameEncryptionInfo* info, const uint8_t* data,
68  size_t data_size, uint8_t* dest) const override;
69 
70  private:
71  struct Session {
72  struct Key {
73  Key(std::vector<uint8_t> key_id, std::vector<uint8_t> key);
74  ~Key();
75 
76  // TODO: Consider storing keys in OpenSSL key objects instead.
77  std::vector<uint8_t> key_id;
78  std::vector<uint8_t> key; // This contains the raw AES key.
79  };
80 
81  Session();
82  ~Session();
83 
84  Session(Session&&);
85  Session(const Session&) = delete;
86  Session& operator=(Session&&);
87  Session& operator=(const Session&) = delete;
88 
89  std::list<Key> keys;
90  bool callable = false;
91  };
92 
93  friend class ClearKeyImplementationTest;
94  friend class media::DecoderIntegration;
95  friend class media::DecoderDecryptIntegration;
96 
97  DecryptStatus DecryptBlock(const FrameEncryptionInfo* info,
98  const uint8_t* data, size_t data_size,
99  size_t block_offset, uint8_t* dest,
100  util::Decryptor* decryptor) const;
101 
102  void LoadKeyForTesting(std::vector<uint8_t> key_id, std::vector<uint8_t> key);
103 
104  mutable std::mutex mutex_;
105  std::unordered_map<std::string, Session> sessions_;
106  ImplementationHelper* helper_;
107  uint32_t cur_session_id_;
108 };
109 
110 } // namespace eme
111 } // namespace shaka
112 
113 #endif // SHAKA_EMBEDDED_EME_CLEARKEY_FACTORY_H_
const char * dest
Definition: media_utils.cc:31