Removed unused VideoCaptureCapability parameters.
[chromium-blink-merge.git] / media / cdm / ppapi / clear_key_cdm.h
blob57029059a22815a3687ad88730582b8c0dd5653b
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_CLEAR_KEY_CDM_H_
6 #define MEDIA_CDM_PPAPI_CLEAR_KEY_CDM_H_
8 #include <string>
9 #include <vector>
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/api/content_decryption_module.h"
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
25 namespace media {
26 class CdmVideoDecoder;
27 class DecoderBuffer;
28 class FFmpegCdmAudioDecoder;
30 // Clear key implementation of the cdm::ContentDecryptionModule interface.
31 class ClearKeyCdm : public cdm::ContentDecryptionModule_2 {
32 public:
33 explicit ClearKeyCdm(cdm::Host* host);
34 virtual ~ClearKeyCdm();
36 // ContentDecryptionModule implementation.
37 virtual cdm::Status GenerateKeyRequest(
38 const char* type, uint32_t type_size,
39 const uint8_t* init_data, uint32_t init_data_size) OVERRIDE;
40 virtual cdm::Status AddKey(const char* session_id,
41 uint32_t session_id_size,
42 const uint8_t* key,
43 uint32_t key_size,
44 const uint8_t* key_id,
45 uint32_t key_id_size) OVERRIDE;
46 virtual cdm::Status CancelKeyRequest(const char* session_id,
47 uint32_t 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;
64 virtual void OnPlatformChallengeResponse(
65 const cdm::PlatformChallengeResponse& response) OVERRIDE;
66 virtual void OnQueryOutputProtectionStatus(
67 uint32_t link_mask, uint32_t output_protection_mask) OVERRIDE;
69 private:
70 // TODO(xhwang): After we removed DecryptorClient. We probably can also remove
71 // this Client class as well. Investigate this possibility.
72 class Client {
73 public:
74 enum Status {
75 kKeyAdded,
76 kKeyError,
77 kKeyMessage
80 Client();
81 virtual ~Client();
83 Status status() { return status_; }
84 const std::string& session_id() { return session_id_; }
85 const std::vector<uint8>& key_message() { return key_message_; }
86 const std::string& default_url() { return default_url_; }
88 // Resets the Client to a clean state.
89 void Reset();
91 void KeyAdded(const std::string& session_id);
92 void KeyError(const std::string& session_id,
93 MediaKeys::KeyError error_code,
94 int system_code);
95 void KeyMessage(const std::string& session_id,
96 const std::vector<uint8>& message,
97 const std::string& default_url);
99 private:
100 Status status_;
101 std::string session_id_;
102 std::vector<uint8> key_message_;
103 std::string default_url_;
106 // Prepares next heartbeat message and sets a timer for it.
107 void ScheduleNextHeartBeat();
109 // Decrypts the |encrypted_buffer| and puts the result in |decrypted_buffer|.
110 // Returns cdm::kSuccess if decryption succeeded. The decrypted result is
111 // put in |decrypted_buffer|. If |encrypted_buffer| is empty, the
112 // |decrypted_buffer| is set to an empty (EOS) buffer.
113 // Returns cdm::kNoKey if no decryption key was available. In this case
114 // |decrypted_buffer| should be ignored by the caller.
115 // Returns cdm::kDecryptError if any decryption error occurred. In this case
116 // |decrypted_buffer| should be ignored by the caller.
117 cdm::Status DecryptToMediaDecoderBuffer(
118 const cdm::InputBuffer& encrypted_buffer,
119 scoped_refptr<DecoderBuffer>* decrypted_buffer);
121 #if defined(CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER)
122 int64 CurrentTimeStampInMicroseconds() const;
124 // Generates fake video frames with |duration_in_microseconds|.
125 // Returns the number of samples generated in the |audio_frames|.
126 int GenerateFakeAudioFramesFromDuration(int64 duration_in_microseconds,
127 cdm::AudioFrames* audio_frames) const;
129 // Generates fake video frames given |input_timestamp|.
130 // Returns cdm::kSuccess if any audio frame is successfully generated.
131 cdm::Status GenerateFakeAudioFrames(int64 timestamp_in_microseconds,
132 cdm::AudioFrames* audio_frames);
133 #endif // CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER
135 Client client_;
136 AesDecryptor decryptor_;
138 // Protects the |client_| from being accessed by the |decryptor_|
139 // simultaneously.
140 base::Lock client_lock_;
142 cdm::Host* host_;
144 std::string heartbeat_session_id_;
145 std::string next_heartbeat_message_;
147 // Timer delay in milliseconds for the next host_->SetTimer() call.
148 int64 timer_delay_ms_;
150 // Indicates whether a timer has been set to prevent multiple timers from
151 // running.
152 bool timer_set_;
154 #if defined(CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER)
155 int channel_count_;
156 int bits_per_channel_;
157 int samples_per_second_;
158 int64 output_timestamp_base_in_microseconds_;
159 int total_samples_generated_;
160 #endif // CLEAR_KEY_CDM_USE_FAKE_AUDIO_DECODER
162 #if defined(CLEAR_KEY_CDM_USE_FFMPEG_DECODER)
163 scoped_ptr<FFmpegCdmAudioDecoder> audio_decoder_;
164 #endif // CLEAR_KEY_CDM_USE_FFMPEG_DECODER
166 scoped_ptr<CdmVideoDecoder> video_decoder_;
168 DISALLOW_COPY_AND_ASSIGN(ClearKeyCdm);
171 } // namespace media
173 #endif // MEDIA_CDM_PPAPI_CLEAR_KEY_CDM_H_