OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef WEBKIT_RENDERER_MEDIA_CRYPTO_PPAPI_CLEAR_KEY_CDM_H_ | |
6 #define WEBKIT_RENDERER_MEDIA_CRYPTO_PPAPI_CLEAR_KEY_CDM_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
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/crypto/aes_decryptor.h" | |
17 #include "webkit/renderer/media/crypto/ppapi/cdm/content_decryption_module.h" | |
18 | |
19 // Enable this to use the fake decoder for testing. | |
20 // TODO(tomfinegan): Move fake audio decoder into a separate class. | |
21 #if 0 | |
22 #define CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER | |
23 #endif | |
24 | |
25 namespace media { | |
26 class DecoderBuffer; | |
27 } | |
28 | |
29 namespace webkit_media { | |
30 | |
31 class CdmVideoDecoder; | |
32 class FFmpegCdmAudioDecoder; | |
33 | |
34 // Clear key implementation of the cdm::ContentDecryptionModule interface. | |
35 class ClearKeyCdm : public cdm::ContentDecryptionModule { | |
36 public: | |
37 explicit ClearKeyCdm(cdm::Host* host); | |
38 virtual ~ClearKeyCdm(); | |
39 | |
40 // ContentDecryptionModule implementation. | |
41 virtual cdm::Status GenerateKeyRequest( | |
42 const char* type, int type_size, | |
43 const uint8_t* init_data, int init_data_size) OVERRIDE; | |
44 virtual cdm::Status AddKey(const char* session_id, int session_id_size, | |
45 const uint8_t* key, int key_size, | |
46 const uint8_t* key_id, int key_id_size) OVERRIDE; | |
47 virtual cdm::Status CancelKeyRequest(const char* session_id, | |
48 int session_id_size) 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 | |
66 private: | |
67 // TODO(xhwang): After we removed DecryptorClient. We probably can also remove | |
68 // this Client class as well. Investigate this possibility. | |
69 class Client { | |
70 public: | |
71 enum Status { | |
72 kKeyAdded, | |
73 kKeyError, | |
74 kKeyMessage | |
75 }; | |
76 | |
77 Client(); | |
78 virtual ~Client(); | |
79 | |
80 Status status() { return status_; } | |
81 const std::string& session_id() { return session_id_; } | |
82 const std::vector<uint8>& key_message() { return key_message_; } | |
83 const std::string& default_url() { return default_url_; } | |
84 | |
85 // Resets the Client to a clean state. | |
86 void Reset(); | |
87 | |
88 void KeyAdded(const std::string& session_id); | |
89 void KeyError(const std::string& session_id, | |
90 media::MediaKeys::KeyError error_code, | |
91 int system_code); | |
92 void KeyMessage(const std::string& session_id, | |
93 const std::vector<uint8>& message, | |
94 const std::string& default_url); | |
95 | |
96 private: | |
97 Status status_; | |
98 std::string session_id_; | |
99 std::vector<uint8> key_message_; | |
100 std::string default_url_; | |
101 }; | |
102 | |
103 // Prepares next heartbeat message and sets a timer for it. | |
104 void ScheduleNextHeartBeat(); | |
105 | |
106 // Decrypts the |encrypted_buffer| and puts the result in |decrypted_buffer|. | |
107 // Returns cdm::kSuccess if decryption succeeded. The decrypted result is | |
108 // put in |decrypted_buffer|. If |encrypted_buffer| is empty, the | |
109 // |decrypted_buffer| is set to an empty (EOS) buffer. | |
110 // Returns cdm::kNoKey if no decryption key was available. In this case | |
111 // |decrypted_buffer| should be ignored by the caller. | |
112 // Returns cdm::kDecryptError if any decryption error occurred. In this case | |
113 // |decrypted_buffer| should be ignored by the caller. | |
114 cdm::Status DecryptToMediaDecoderBuffer( | |
115 const cdm::InputBuffer& encrypted_buffer, | |
116 scoped_refptr<media::DecoderBuffer>* decrypted_buffer); | |
117 | |
118 #if defined(CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER) | |
119 int64 CurrentTimeStampInMicroseconds() const; | |
120 | |
121 // Generates fake video frames with |duration_in_microseconds|. | |
122 // Returns the number of samples generated in the |audio_frames|. | |
123 int GenerateFakeAudioFramesFromDuration(int64 duration_in_microseconds, | |
124 cdm::AudioFrames* audio_frames) const; | |
125 | |
126 // Generates fake video frames given |input_timestamp|. | |
127 // Returns cdm::kSuccess if any audio frame is successfully generated. | |
128 cdm::Status GenerateFakeAudioFrames(int64 timestamp_in_microseconds, | |
129 cdm::AudioFrames* audio_frames); | |
130 #endif // CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER | |
131 | |
132 Client client_; | |
133 media::AesDecryptor decryptor_; | |
134 | |
135 // Protects the |client_| from being accessed by the |decryptor_| | |
136 // simultaneously. | |
137 base::Lock client_lock_; | |
138 | |
139 cdm::Host* host_; | |
140 | |
141 std::string heartbeat_session_id_; | |
142 std::string next_heartbeat_message_; | |
143 | |
144 // Timer delay in milliseconds for the next host_->SetTimer() call. | |
145 int64 timer_delay_ms_; | |
146 | |
147 // Indicates whether a timer has been set to prevent multiple timers from | |
148 // running. | |
149 bool timer_set_; | |
150 | |
151 #if defined(CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER) | |
152 int channel_count_; | |
153 int bits_per_channel_; | |
154 int samples_per_second_; | |
155 int64 output_timestamp_base_in_microseconds_; | |
156 int total_samples_generated_; | |
157 #endif // CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER | |
158 | |
159 #if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER) | |
160 scoped_ptr<FFmpegCdmAudioDecoder> audio_decoder_; | |
161 #endif // CLEAR_KEY_CDM_USE_FFMPEG_DECODER | |
162 | |
163 scoped_ptr<CdmVideoDecoder> video_decoder_; | |
164 | |
165 DISALLOW_COPY_AND_ASSIGN(ClearKeyCdm); | |
166 }; | |
167 | |
168 } // namespace webkit_media | |
169 | |
170 #endif // WEBKIT_RENDERER_MEDIA_CRYPTO_PPAPI_CLEAR_KEY_CDM_H_ | |
OLD | NEW |