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
3 // found in the LICENSE file.
4 
5 #ifndef CHROMECAST_MEDIA_BASE_DECRYPT_CONTEXT_IMPL_H_
6 #define CHROMECAST_MEDIA_BASE_DECRYPT_CONTEXT_IMPL_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "chromecast/public/media/cast_key_system.h"
16 #include "chromecast/public/media/decrypt_context.h"
17 
18 namespace chromecast {
19 namespace media {
20 
21 // Base class of a decryption context: a decryption context gathers all the
22 // information needed to decrypt frames with a given key id.
23 // Each CDM should implement this and add fields needed to fully describe a
24 // decryption context.
25 class DecryptContextImpl : public DecryptContext {
26  public:
27   using DecryptCB = base::OnceCallback<void(bool)>;
28 
29   // Type for the output buffer:
30   // |kSecure| means the decrypted data will be put in a secured buffer, where
31   // normal CPU can't access.
32   // |kClearAllowed| means the license allows the decrypted data to be accessed
33   // by normal CPU. Caller can decide where is the decrypted data.
34   // |kClearRequired| means the CDM or platform doesn't support secure buffer.
35   enum class OutputType { kSecure, kClearAllowed, kClearRequired };
36 
37   explicit DecryptContextImpl(CastKeySystem key_system);
38   ~DecryptContextImpl() override;
39 
40   // DecryptContext implementation:
41   CastKeySystem GetKeySystem() final;
42   bool Decrypt(CastDecoderBuffer* buffer,
43                uint8_t* opaque_handle,
44                size_t data_offset) final;
45 
46   // Similar as the above one. Decryption success or not will be returned in
47   // |decrypt_cb|. |output_or_handle| is a pointer to the normal memory, if
48   // |clear_output| is true. Otherwise, it's an opaque handle to the secure
49   // memory which is only accessible in TEE. |decrypt_cb| will be called on
50   // caller's thread.
51   virtual void DecryptAsync(CastDecoderBuffer* buffer,
52                             uint8_t* output_or_handle,
53                             size_t data_offset,
54                             bool clear_output,
55                             DecryptCB decrypt_cb);
56 
57   // Returns the type of output buffer.
58   virtual OutputType GetOutputType() const;
59 
60  private:
61   CastKeySystem key_system_;
62 
63   DISALLOW_COPY_AND_ASSIGN(DecryptContextImpl);
64 };
65 
66 }  // namespace media
67 }  // namespace chromecast
68 
69 #endif  // CHROMECAST_MEDIA_BASE_DECRYPT_CONTEXT_IMPL_H_
70