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.
5 #ifndef CHROMECAST_MEDIA_CDM_BROWSER_CDM_CAST_H_
6 #define CHROMECAST_MEDIA_CDM_BROWSER_CDM_CAST_H_
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/threading/thread_checker.h"
17 #include "media/base/browser_cdm.h"
18 #include "media/cdm/json_web_key.h"
21 class MessageLoopProxy
;
25 class PlayerTrackerImpl
;
28 namespace chromecast
{
32 // BrowserCdmCast is an extension of BrowserCdm that provides common
33 // functionality across CDM implementations.
34 // All these additional functions are synchronous so:
35 // - either both the CDM and the media pipeline must be running on the same
37 // - or BrowserCdmCast implementations must use some locks.
39 class BrowserCdmCast
: public ::media::BrowserCdm
{
42 ~BrowserCdmCast() override
;
45 const ::media::SessionMessageCB
& session_message_cb
,
46 const ::media::SessionClosedCB
& session_closed_cb
,
47 const ::media::LegacySessionErrorCB
& legacy_session_error_cb
,
48 const ::media::SessionKeysChangeCB
& session_keys_change_cb
,
49 const ::media::SessionExpirationUpdateCB
& session_expiration_update_cb
);
51 // PlayerTracker implementation.
52 int RegisterPlayer(const base::Closure
& new_key_cb
,
53 const base::Closure
& cdm_unset_cb
) override
;
54 void UnregisterPlayer(int registration_id
) override
;
56 // ::media::BrowserCdm implementation:
57 void LoadSession(::media::MediaKeys::SessionType session_type
,
58 const std::string
& session_id
,
59 scoped_ptr
<::media::NewSessionCdmPromise
> promise
) override
;
60 ::media::CdmContext
* GetCdmContext() override
;
62 // Returns the decryption context needed to decrypt frames encrypted with
64 // Returns null if |key_id| is not available.
65 virtual scoped_refptr
<DecryptContext
> GetDecryptContext(
66 const std::string
& key_id
) const = 0;
69 void OnSessionMessage(const std::string
& session_id
,
70 const std::vector
<uint8
>& message
,
71 const GURL
& destination_url
);
72 void OnSessionClosed(const std::string
& session_id
);
73 void OnSessionKeysChange(const std::string
& session_id
,
74 const ::media::KeyIdAndKeyPairs
& keys
);
77 friend class BrowserCdmCastUi
;
79 // Helper methods for forwarding calls to methods that take raw pointers.
80 void SetServerCertificateHelper(
81 const std::vector
<uint8
>& certificate_data
,
82 scoped_ptr
<::media::SimpleCdmPromise
> promise
);
83 void CreateSessionAndGenerateRequestHelper(
84 ::media::MediaKeys::SessionType session_type
,
85 ::media::EmeInitDataType init_data_type
,
86 const std::vector
<uint8
>& init_data
,
87 scoped_ptr
<::media::NewSessionCdmPromise
> promise
);
88 void UpdateSessionHelper(const std::string
& session_id
,
89 const std::vector
<uint8
>& response
,
90 scoped_ptr
<::media::SimpleCdmPromise
> promise
);
92 ::media::SessionMessageCB session_message_cb_
;
93 ::media::SessionClosedCB session_closed_cb_
;
94 ::media::LegacySessionErrorCB legacy_session_error_cb_
;
95 ::media::SessionKeysChangeCB session_keys_change_cb_
;
96 ::media::SessionExpirationUpdateCB session_expiration_update_cb_
;
98 scoped_ptr
<::media::PlayerTrackerImpl
> player_tracker_impl_
;
100 base::ThreadChecker thread_checker_
;
102 DISALLOW_COPY_AND_ASSIGN(BrowserCdmCast
);
105 // BrowserCdm implementation that lives on the UI thread and forwards all calls
106 // to a BrowserCdmCast instance on the CMA thread. This is used to simplify the
107 // UI-CMA threading interaction.
108 class BrowserCdmCastUi
: public ::media::BrowserCdm
{
111 scoped_ptr
<BrowserCdmCast
> browser_cdm_cast
,
112 const scoped_refptr
<base::MessageLoopProxy
>& cdm_loop
);
113 ~BrowserCdmCastUi() override
;
115 // PlayerTracker implementation:
116 int RegisterPlayer(const base::Closure
& new_key_cb
,
117 const base::Closure
& cdm_unset_cb
) override
;
118 void UnregisterPlayer(int registration_id
) override
;
120 BrowserCdmCast
* browser_cdm_cast() const;
123 // ::media::MediaKeys implementation:
124 void SetServerCertificate(
125 const uint8
* certificate_data
,
126 int certificate_data_length
,
127 scoped_ptr
<::media::SimpleCdmPromise
> promise
) override
;
128 void CreateSessionAndGenerateRequest(
129 ::media::MediaKeys::SessionType session_type
,
130 ::media::EmeInitDataType init_data_type
,
131 const uint8
* init_data
,
132 int init_data_length
,
133 scoped_ptr
<::media::NewSessionCdmPromise
> promise
) override
;
134 void LoadSession(::media::MediaKeys::SessionType session_type
,
135 const std::string
& session_id
,
136 scoped_ptr
<::media::NewSessionCdmPromise
> promise
) override
;
137 void UpdateSession(const std::string
& session_id
,
138 const uint8
* response
,
140 scoped_ptr
<::media::SimpleCdmPromise
> promise
) override
;
141 void CloseSession(const std::string
& session_id
,
142 scoped_ptr
<::media::SimpleCdmPromise
> promise
) override
;
143 void RemoveSession(const std::string
& session_id
,
144 scoped_ptr
<::media::SimpleCdmPromise
> promise
) override
;
145 ::media::CdmContext
* GetCdmContext() override
;
147 scoped_ptr
<BrowserCdmCast
> browser_cdm_cast_
;
148 scoped_refptr
<base::MessageLoopProxy
> cdm_loop_
;
150 base::ThreadChecker thread_checker_
;
152 DISALLOW_COPY_AND_ASSIGN(BrowserCdmCastUi
);
156 } // namespace chromecast
158 #endif // CHROMECAST_MEDIA_CDM_BROWSER_CDM_CAST_H_