Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / media / crypto / ppapi / clear_key_cdm.h
blob35f01879ef25d87cba8a3242aadb6407c18850ba
1 // Copyright (c) 2012 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 WEBKIT_MEDIA_CRYPTO_PPAPI_CLEAR_KEY_CDM_H_
6 #define WEBKIT_MEDIA_CRYPTO_PPAPI_CLEAR_KEY_CDM_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "media/crypto/aes_decryptor.h"
16 #include "webkit/media/crypto/ppapi/cdm/content_decryption_module.h"
18 // Enable this to use the fake decoder for testing.
19 // TODO(tomfinegan): Move fake audio decoder into a separate class.
20 #if 0
21 #define CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER
22 #endif
24 namespace media {
25 class DecoderBuffer;
28 namespace webkit_media {
30 class CdmVideoDecoder;
31 class FFmpegCdmAudioDecoder;
33 // Clear key implementation of the cdm::ContentDecryptionModule interface.
34 class ClearKeyCdm : public cdm::ContentDecryptionModule {
35 public:
36 explicit ClearKeyCdm(cdm::Host* host);
37 virtual ~ClearKeyCdm();
39 // ContentDecryptionModule implementation.
40 virtual cdm::Status GenerateKeyRequest(
41 const char* type, int type_size,
42 const uint8_t* init_data, int init_data_size) OVERRIDE;
43 virtual cdm::Status AddKey(const char* session_id, int session_id_size,
44 const uint8_t* key, int key_size,
45 const uint8_t* key_id, int key_id_size) OVERRIDE;
46 virtual cdm::Status CancelKeyRequest(const char* session_id,
47 int session_id_size) OVERRIDE;
48 virtual void TimerExpired(void* context) OVERRIDE;
49 virtual cdm::Status Decrypt(const cdm::InputBuffer& encrypted_buffer,
50 cdm::DecryptedBlock* decrypted_block) OVERRIDE;
51 virtual cdm::Status InitializeAudioDecoder(
52 const cdm::AudioDecoderConfig& audio_decoder_config) OVERRIDE;
53 virtual cdm::Status InitializeVideoDecoder(
54 const cdm::VideoDecoderConfig& video_decoder_config) OVERRIDE;
55 virtual void DeinitializeDecoder(cdm::StreamType decoder_type) OVERRIDE;
56 virtual void ResetDecoder(cdm::StreamType decoder_type) OVERRIDE;
57 virtual cdm::Status DecryptAndDecodeFrame(
58 const cdm::InputBuffer& encrypted_buffer,
59 cdm::VideoFrame* video_frame) OVERRIDE;
60 virtual cdm::Status DecryptAndDecodeSamples(
61 const cdm::InputBuffer& encrypted_buffer,
62 cdm::AudioFrames* audio_frames) OVERRIDE;
63 virtual void Destroy() OVERRIDE;
65 private:
66 // TODO(xhwang): After we removed DecryptorClient. We probably can also remove
67 // this Client class as well. Investigate this possibility.
68 class Client {
69 public:
70 enum Status {
71 kKeyAdded,
72 kKeyError,
73 kKeyMessage,
74 kNeedKey
77 Client();
78 virtual ~Client();
80 Status status() { return status_; }
81 const std::string& session_id() { return session_id_; }
82 const std::string& key_message() { return key_message_; }
83 const std::string& default_url() { return default_url_; }
85 // Resets the Client to a clean state.
86 void Reset();
88 void KeyAdded(const std::string& key_system, const std::string& session_id);
89 void KeyError(const std::string& key_system,
90 const std::string& session_id,
91 media::Decryptor::KeyError error_code,
92 int system_code);
93 void KeyMessage(const std::string& key_system,
94 const std::string& session_id,
95 const std::string& message,
96 const std::string& default_url);
97 void NeedKey(const std::string& key_system,
98 const std::string& session_id,
99 const std::string& type,
100 scoped_ptr<uint8[]> init_data, int init_data_length);
102 private:
103 Status status_;
104 std::string session_id_;
105 std::string key_message_;
106 std::string default_url_;
109 // Prepares next heartbeat message and sets a timer for it.
110 void ScheduleNextHeartBeat();
112 // Decrypts the |encrypted_buffer| and puts the result in |decrypted_buffer|.
113 // Returns cdm::kSuccess if decryption succeeded. The decrypted result is
114 // put in |decrypted_buffer|. If |encrypted_buffer| is empty, the
115 // |decrypted_buffer| is set to an empty (EOS) buffer.
116 // Returns cdm::kNoKey if no decryption key was available. In this case
117 // |decrypted_buffer| should be ignored by the caller.
118 // Returns cdm::kDecryptError if any decryption error occurred. In this case
119 // |decrypted_buffer| should be ignored by the caller.
120 cdm::Status DecryptToMediaDecoderBuffer(
121 const cdm::InputBuffer& encrypted_buffer,
122 scoped_refptr<media::DecoderBuffer>* decrypted_buffer);
124 #if defined(CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER)
125 int64 CurrentTimeStampInMicroseconds() const;
127 // Generates fake video frames with |duration_in_microseconds|.
128 // Returns the number of samples generated in the |audio_frames|.
129 int GenerateFakeAudioFramesFromDuration(int64 duration_in_microseconds,
130 cdm::AudioFrames* audio_frames) const;
132 // Generates fake video frames given |input_timestamp|.
133 // Returns cdm::kSuccess if any audio frame is successfully generated.
134 cdm::Status GenerateFakeAudioFrames(int64 timestamp_in_microseconds,
135 cdm::AudioFrames* audio_frames);
136 #endif // CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER
138 Client client_;
139 media::AesDecryptor decryptor_;
141 // Protects the |client_| from being accessed by the |decryptor_|
142 // simultaneously.
143 base::Lock client_lock_;
145 cdm::Host* host_;
147 std::string heartbeat_session_id_;
148 std::string next_heartbeat_message_;
150 // Timer delay in milliseconds for the next host_->SetTimer() call.
151 int64 timer_delay_ms_;
153 // Indicates whether a timer has been set to prevent multiple timers from
154 // running.
155 bool timer_set_;
157 #if defined(CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER)
158 int channel_count_;
159 int bits_per_channel_;
160 int samples_per_second_;
161 int64 output_timestamp_base_in_microseconds_;
162 int total_samples_generated_;
163 #endif // CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER
165 #if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER)
166 scoped_ptr<FFmpegCdmAudioDecoder> audio_decoder_;
167 #endif // CLEAR_KEY_CDM_USE_FFMPEG_DECODER
169 scoped_ptr<CdmVideoDecoder> video_decoder_;
171 DISALLOW_COPY_AND_ASSIGN(ClearKeyCdm);
174 } // namespace webkit_media
176 #endif // WEBKIT_MEDIA_CRYPTO_PPAPI_CLEAR_KEY_CDM_H_