1 // Copyright 2013 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 MEDIA_CDM_PPAPI_EXTERNAL_CLEAR_KEY_CLEAR_KEY_CDM_H_
6 #define MEDIA_CDM_PPAPI_EXTERNAL_CLEAR_KEY_CLEAR_KEY_CDM_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/synchronization/lock.h"
16 #include "media/cdm/aes_decryptor.h"
17 #include "media/cdm/ppapi/external_clear_key/clear_key_cdm_common.h"
19 // Enable this to use the fake decoder for testing.
20 // TODO(tomfinegan): Move fake audio decoder into a separate class.
22 #define CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER
26 class FileIOTestRunner;
27 class CdmVideoDecoder;
29 class FFmpegCdmAudioDecoder;
31 // Clear key implementation of the cdm::ContentDecryptionModule interface.
32 class ClearKeyCdm : public ClearKeyCdmInterface {
34 ClearKeyCdm(Host* host, const std::string& key_system);
35 virtual ~ClearKeyCdm();
37 // ContentDecryptionModule implementation.
38 virtual void CreateSession(
40 const char* type, uint32 type_size,
41 const uint8* init_data, uint32 init_data_size) OVERRIDE;
42 virtual void LoadSession(
44 const char* web_session_id, uint32_t web_session_id_length) OVERRIDE;
45 virtual void UpdateSession(
47 const uint8* response, uint32 response_size) OVERRIDE;
48 virtual void ReleaseSession(uint32 session_id) OVERRIDE;
49 virtual void TimerExpired(void* context) OVERRIDE;
50 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer,
51 cdm::DecryptedBlock* decrypted_block) OVERRIDE;
52 virtual cdm::Status InitializeAudioDecoder(
53 const cdm::AudioDecoderConfig& audio_decoder_config) OVERRIDE;
54 virtual cdm::Status InitializeVideoDecoder(
55 const cdm::VideoDecoderConfig& video_decoder_config) OVERRIDE;
56 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) OVERRIDE;
57 virtual void ResetDecoder(cdm::StreamType decoder_type) OVERRIDE;
58 virtual cdm::Status DecryptAndDecodeFrame(
59 const cdm::InputBuffer& encrypted_buffer,
60 cdm::VideoFrame* video_frame) OVERRIDE;
61 virtual cdm::Status DecryptAndDecodeSamples(
62 const cdm::InputBuffer& encrypted_buffer,
63 cdm::AudioFrames* audio_frames) OVERRIDE;
64 virtual void Destroy() OVERRIDE;
65 virtual void OnPlatformChallengeResponse(
66 const cdm::PlatformChallengeResponse& response) OVERRIDE;
67 virtual void OnQueryOutputProtectionStatus(
68 uint32_t link_mask, uint32_t output_protection_mask) OVERRIDE;
71 // Emulates a session stored for |session_id_for_emulated_loadsession_|. This
72 // is necessary since aes_decryptor.cc does not support storing sessions.
73 void LoadLoadableSession();
75 // ContentDecryptionModule callbacks.
76 void OnSessionCreated(uint32 session_id, const std::string& web_session_id);
77 void OnSessionMessage(uint32 session_id,
78 const std::vector<uint8>& message,
79 const std::string& destination_url);
80 void OnSessionReady(uint32 session_id);
81 void OnSessionClosed(uint32 session_id);
82 void OnSessionError(uint32 session_id,
83 MediaKeys::KeyError error_code,
86 // Prepares next heartbeat message and sets a timer for it.
87 void ScheduleNextHeartBeat();
89 // Decrypts the |encrypted_buffer| and puts the result in |decrypted_buffer|.
90 // Returns cdm::kSuccess if decryption succeeded. The decrypted result is
91 // put in |decrypted_buffer|. If |encrypted_buffer| is empty, the
92 // |decrypted_buffer| is set to an empty (EOS) buffer.
93 // Returns cdm::kNoKey if no decryption key was available. In this case
94 // |decrypted_buffer| should be ignored by the caller.
95 // Returns cdm::kDecryptError if any decryption error occurred. In this case
96 // |decrypted_buffer| should be ignored by the caller.
97 cdm::Status DecryptToMediaDecoderBuffer(
98 const cdm::InputBuffer& encrypted_buffer,
99 scoped_refptr<DecoderBuffer>* decrypted_buffer);
101 #if defined(CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER)
102 int64 CurrentTimeStampInMicroseconds() const;
104 // Generates fake video frames with |duration_in_microseconds|.
105 // Returns the number of samples generated in the |audio_frames|.
106 int GenerateFakeAudioFramesFromDuration(int64 duration_in_microseconds,
107 cdm::AudioFrames* audio_frames) const;
109 // Generates fake video frames given |input_timestamp|.
110 // Returns cdm::kSuccess if any audio frame is successfully generated.
111 cdm::Status GenerateFakeAudioFrames(int64 timestamp_in_microseconds,
112 cdm::AudioFrames* audio_frames);
113 #endif // CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER
115 void StartFileIOTest();
117 // Callback for CDM File IO test.
118 void OnFileIOTestComplete(bool success);
120 AesDecryptor decryptor_;
122 ClearKeyCdmHost* host_;
124 const std::string key_system_;
126 uint32 last_session_id_;
127 std::string next_heartbeat_message_;
129 // TODO(xhwang): Extract testing code from main implementation.
130 // See http://crbug.com/341751
131 uint32 session_id_for_emulated_loadsession_;
133 // Timer delay in milliseconds for the next host_->SetTimer() call.
134 int64 timer_delay_ms_;
136 // Indicates whether a heartbeat timer has been set to prevent multiple timers
138 bool heartbeat_timer_set_;
140 #if defined(CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER)
142 int bits_per_channel_;
143 int samples_per_second_;
144 int64 output_timestamp_base_in_microseconds_;
145 int total_samples_generated_;
146 #endif // CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER
148 #if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER)
149 scoped_ptr<FFmpegCdmAudioDecoder> audio_decoder_;
150 #endif // CLEAR_KEY_CDM_USE_FFMPEG_DECODER
152 scoped_ptr<CdmVideoDecoder> video_decoder_;
154 scoped_ptr<FileIOTestRunner> file_io_test_runner_;
156 DISALLOW_COPY_AND_ASSIGN(ClearKeyCdm);
161 #endif // MEDIA_CDM_PPAPI_EXTERNAL_CLEAR_KEY_CLEAR_KEY_CDM_H_