cdm_adapter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright 2015 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. #ifndef MEDIA_CDM_CDM_ADAPTER_H_
  5. #define MEDIA_CDM_CDM_ADAPTER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/compiler_specific.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/scoped_native_library.h"
  15. #include "base/threading/thread.h"
  16. #include "components/crash/core/common/crash_key.h"
  17. #include "media/base/audio_buffer.h"
  18. #include "media/base/callback_registry.h"
  19. #include "media/base/cdm_config.h"
  20. #include "media/base/cdm_context.h"
  21. #include "media/base/cdm_factory.h"
  22. #include "media/base/cdm_promise_adapter.h"
  23. #include "media/base/content_decryption_module.h"
  24. #include "media/base/decryptor.h"
  25. #include "media/base/media_export.h"
  26. #include "media/base/video_aspect_ratio.h"
  27. #include "media/cdm/api/content_decryption_module.h"
  28. #include "ui/gfx/geometry/size.h"
  29. namespace media {
  30. class AudioFramesImpl;
  31. class CdmAuxiliaryHelper;
  32. class CdmWrapper;
  33. class MEDIA_EXPORT CdmAdapter final : public ContentDecryptionModule,
  34. public CdmContext,
  35. public Decryptor,
  36. public cdm::Host_10,
  37. public cdm::Host_11 {
  38. public:
  39. using CreateCdmFunc = void* (*)(int cdm_interface_version,
  40. const char* key_system,
  41. uint32_t key_system_size,
  42. GetCdmHostFunc get_cdm_host_func,
  43. void* user_data);
  44. // Creates the CDM and initialize it using |cdm_config|.
  45. // |allocator| is to be used whenever the CDM needs memory and to create
  46. // VideoFrames. |file_io_provider| is to be used whenever the CDM needs access
  47. // to the file system. Callbacks will be used for events generated by the CDM.
  48. // |cdm_created_cb| will be called when the CDM is initialized.
  49. static void Create(
  50. const CdmConfig& cdm_config,
  51. CreateCdmFunc create_cdm_func,
  52. std::unique_ptr<CdmAuxiliaryHelper> helper,
  53. const SessionMessageCB& session_message_cb,
  54. const SessionClosedCB& session_closed_cb,
  55. const SessionKeysChangeCB& session_keys_change_cb,
  56. const SessionExpirationUpdateCB& session_expiration_update_cb,
  57. CdmCreatedCB cdm_created_cb);
  58. CdmAdapter(const CdmAdapter&) = delete;
  59. CdmAdapter& operator=(const CdmAdapter&) = delete;
  60. // Returns the version of the CDM interface that the created CDM uses. Must
  61. // only be called after the CDM is successfully initialized.
  62. int GetInterfaceVersion();
  63. // ContentDecryptionModule implementation.
  64. void SetServerCertificate(const std::vector<uint8_t>& certificate,
  65. std::unique_ptr<SimpleCdmPromise> promise) final;
  66. void GetStatusForPolicy(HdcpVersion min_hdcp_version,
  67. std::unique_ptr<KeyStatusCdmPromise> promise) final;
  68. void CreateSessionAndGenerateRequest(
  69. CdmSessionType session_type,
  70. EmeInitDataType init_data_type,
  71. const std::vector<uint8_t>& init_data,
  72. std::unique_ptr<NewSessionCdmPromise> promise) final;
  73. void LoadSession(CdmSessionType session_type,
  74. const std::string& session_id,
  75. std::unique_ptr<NewSessionCdmPromise> promise) final;
  76. void UpdateSession(const std::string& session_id,
  77. const std::vector<uint8_t>& response,
  78. std::unique_ptr<SimpleCdmPromise> promise) final;
  79. void CloseSession(const std::string& session_id,
  80. std::unique_ptr<SimpleCdmPromise> promise) final;
  81. void RemoveSession(const std::string& session_id,
  82. std::unique_ptr<SimpleCdmPromise> promise) final;
  83. CdmContext* GetCdmContext() final;
  84. // CdmContext implementation.
  85. std::unique_ptr<CallbackRegistration> RegisterEventCB(EventCB event_cb) final;
  86. Decryptor* GetDecryptor() final;
  87. absl::optional<base::UnguessableToken> GetCdmId() const final;
  88. // Decryptor implementation.
  89. void Decrypt(StreamType stream_type,
  90. scoped_refptr<DecoderBuffer> encrypted,
  91. DecryptCB decrypt_cb) final;
  92. void CancelDecrypt(StreamType stream_type) final;
  93. void InitializeAudioDecoder(const AudioDecoderConfig& config,
  94. DecoderInitCB init_cb) final;
  95. void InitializeVideoDecoder(const VideoDecoderConfig& config,
  96. DecoderInitCB init_cb) final;
  97. void DecryptAndDecodeAudio(scoped_refptr<DecoderBuffer> encrypted,
  98. AudioDecodeCB audio_decode_cb) final;
  99. void DecryptAndDecodeVideo(scoped_refptr<DecoderBuffer> encrypted,
  100. VideoDecodeCB video_decode_cb) final;
  101. void ResetDecoder(StreamType stream_type) final;
  102. void DeinitializeDecoder(StreamType stream_type) final;
  103. // Common cdm::Host_10 and cdm::Host_11 implementation.
  104. cdm::Buffer* Allocate(uint32_t capacity) override;
  105. void SetTimer(int64_t delay_ms, void* context) override;
  106. cdm::Time GetCurrentWallTime() override;
  107. void OnInitialized(bool success) override;
  108. void OnResolveKeyStatusPromise(uint32_t promise_id,
  109. cdm::KeyStatus key_status) override;
  110. void OnResolveNewSessionPromise(uint32_t promise_id,
  111. const char* session_id,
  112. uint32_t session_id_size) override;
  113. void OnResolvePromise(uint32_t promise_id) override;
  114. void OnRejectPromise(uint32_t promise_id,
  115. cdm::Exception exception,
  116. uint32_t system_code,
  117. const char* error_message,
  118. uint32_t error_message_size) override;
  119. void OnSessionMessage(const char* session_id,
  120. uint32_t session_id_size,
  121. cdm::MessageType message_type,
  122. const char* message,
  123. uint32_t message_size) override;
  124. void OnSessionKeysChange(const char* session_id,
  125. uint32_t session_id_size,
  126. bool has_additional_usable_key,
  127. const cdm::KeyInformation* keys_info,
  128. uint32_t keys_info_count) override;
  129. void OnExpirationChange(const char* session_id,
  130. uint32_t session_id_size,
  131. cdm::Time new_expiry_time) override;
  132. void OnSessionClosed(const char* session_id,
  133. uint32_t session_id_size) override;
  134. void SendPlatformChallenge(const char* service_id,
  135. uint32_t service_id_size,
  136. const char* challenge,
  137. uint32_t challenge_size) override;
  138. void EnableOutputProtection(uint32_t desired_protection_mask) override;
  139. void QueryOutputProtectionStatus() override;
  140. void OnDeferredInitializationDone(cdm::StreamType stream_type,
  141. cdm::Status decoder_status) override;
  142. cdm::FileIO* CreateFileIO(cdm::FileIOClient* client) override;
  143. void RequestStorageId(uint32_t version) override;
  144. private:
  145. CdmAdapter(const CdmConfig& cdm_config,
  146. CreateCdmFunc create_cdm_func,
  147. std::unique_ptr<CdmAuxiliaryHelper> helper,
  148. const SessionMessageCB& session_message_cb,
  149. const SessionClosedCB& session_closed_cb,
  150. const SessionKeysChangeCB& session_keys_change_cb,
  151. const SessionExpirationUpdateCB& session_expiration_update_cb);
  152. ~CdmAdapter() final;
  153. // Resolves the |promise| if the CDM is successfully initialized; rejects it
  154. // otherwise.
  155. void Initialize(std::unique_ptr<media::SimpleCdmPromise> promise);
  156. // Create an instance of the CDM for |key_system|.
  157. // Caller owns the returned pointer. Returns nullptr on error, e.g. does not
  158. // support |key_system|, does not support an supported interface, etc.
  159. CdmWrapper* CreateCdmInstance(const std::string& key_system);
  160. // Helper for SetTimer().
  161. void TimerExpired(void* context);
  162. // Converts audio data stored in |audio_frames| into individual audio
  163. // buffers in |result_frames|. Returns true upon success.
  164. bool AudioFramesDataToAudioFrames(
  165. std::unique_ptr<AudioFramesImpl> audio_frames,
  166. Decryptor::AudioFrames* result_frames);
  167. // Callbacks for Platform Verification.
  168. void OnChallengePlatformDone(bool success,
  169. const std::string& signed_data,
  170. const std::string& signed_data_signature,
  171. const std::string& platform_key_certificate);
  172. void OnStorageIdObtained(uint32_t version,
  173. const std::vector<uint8_t>& storage_id);
  174. // Callbacks for OutputProtection.
  175. void OnEnableOutputProtectionDone(bool success);
  176. void OnQueryOutputProtectionStatusDone(bool success,
  177. uint32_t link_mask,
  178. uint32_t protection_mask);
  179. // Helper methods to report output protection UMAs.
  180. void ReportOutputProtectionQuery();
  181. void ReportOutputProtectionQueryResult(uint32_t link_mask,
  182. uint32_t protection_mask);
  183. // Callback to report |file_size_bytes| of the file successfully read by
  184. // cdm::FileIO.
  185. void OnFileRead(int file_size_bytes);
  186. const CdmConfig cdm_config_;
  187. CreateCdmFunc create_cdm_func_;
  188. // Helper that provides additional functionality for the CDM.
  189. std::unique_ptr<CdmAuxiliaryHelper> helper_;
  190. // Callbacks for firing session events.
  191. SessionMessageCB session_message_cb_;
  192. SessionClosedCB session_closed_cb_;
  193. SessionKeysChangeCB session_keys_change_cb_;
  194. SessionExpirationUpdateCB session_expiration_update_cb_;
  195. // CDM origin and crash key to be used in crash reporting.
  196. const std::string cdm_origin_;
  197. crash_reporter::ScopedCrashKeyString scoped_crash_key_;
  198. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  199. scoped_refptr<AudioBufferMemoryPool> pool_;
  200. // Callback for Initialize().
  201. uint32_t init_promise_id_ = CdmPromiseAdapter::kInvalidPromiseId;
  202. // Callbacks for deferred initialization.
  203. DecoderInitCB audio_init_cb_;
  204. DecoderInitCB video_init_cb_;
  205. CallbackRegistry<EventCB::RunType> event_callbacks_;
  206. // Keep track of audio parameters.
  207. int audio_samples_per_second_ = 0;
  208. ChannelLayout audio_channel_layout_ = CHANNEL_LAYOUT_NONE;
  209. // Keep track of aspect ratio from the latest configuration.
  210. VideoAspectRatio aspect_ratio_;
  211. // Whether the current video config is encrypted.
  212. bool is_video_encrypted_ = false;
  213. // Tracks whether an output protection query and a positive query result (no
  214. // unprotected external link) have been reported to UMA.
  215. bool uma_for_output_protection_query_reported_ = false;
  216. bool uma_for_output_protection_positive_result_reported_ = false;
  217. // Tracks CDM file IO related states.
  218. int last_read_file_size_kb_ = 0;
  219. bool file_size_uma_reported_ = false;
  220. // Used to keep track of promises while the CDM is processing the request.
  221. CdmPromiseAdapter cdm_promise_adapter_;
  222. // Declare |cdm_| after other member variables to avoid the CDM accessing
  223. // deleted objects (e.g. |helper_|) during destruction.
  224. std::unique_ptr<CdmWrapper> cdm_;
  225. // NOTE: Weak pointers must be invalidated before all other member variables.
  226. base::WeakPtrFactory<CdmAdapter> weak_factory_{this};
  227. };
  228. } // namespace media
  229. #endif // MEDIA_CDM_CDM_ADAPTER_H_