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. 4 5 #ifndef CONTENT_RENDERER_PEPPER_CONTENT_DECRYPTOR_DELEGATE_H_ 6 #define CONTENT_RENDERER_PEPPER_CONTENT_DECRYPTOR_DELEGATE_H_ 7 8 #include <queue> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/compiler_specific.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/weak_ptr.h" 15 #include "media/base/decryptor.h" 16 #include "media/base/media_keys.h" 17 #include "media/base/sample_format.h" 18 #include "ppapi/c/private/pp_content_decryptor.h" 19 #include "ppapi/c/private/ppp_content_decryptor_private.h" 20 #include "ui/gfx/size.h" 21 22 namespace media { 23 class AudioDecoderConfig; 24 class DecoderBuffer; 25 class VideoDecoderConfig; 26 } 27 28 namespace content { 29 30 class PPB_Buffer_Impl; 31 32 class ContentDecryptorDelegate { 33 public: 34 // ContentDecryptorDelegate does not take ownership of 35 // |plugin_decryption_interface|. Therefore |plugin_decryption_interface| 36 // must outlive this object. 37 ContentDecryptorDelegate( 38 PP_Instance pp_instance, 39 const PPP_ContentDecryptor_Private* plugin_decryption_interface); 40 ~ContentDecryptorDelegate(); 41 42 void Initialize(const std::string& key_system); 43 44 void SetKeyEventCallbacks(const media::KeyAddedCB& key_added_cb, 45 const media::KeyErrorCB& key_error_cb, 46 const media::KeyMessageCB& key_message_cb); 47 48 // Provides access to PPP_ContentDecryptor_Private. 49 bool GenerateKeyRequest(const std::string& type, 50 const uint8* init_data, 51 int init_data_length); 52 bool AddKey(const std::string& session_id, 53 const uint8* key, 54 int key_length, 55 const uint8* init_data, 56 int init_data_length); 57 bool CancelKeyRequest(const std::string& session_id); 58 bool Decrypt(media::Decryptor::StreamType stream_type, 59 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer, 60 const media::Decryptor::DecryptCB& decrypt_cb); 61 bool CancelDecrypt(media::Decryptor::StreamType stream_type); 62 bool InitializeAudioDecoder( 63 const media::AudioDecoderConfig& decoder_config, 64 const media::Decryptor::DecoderInitCB& decoder_init_cb); 65 bool InitializeVideoDecoder( 66 const media::VideoDecoderConfig& decoder_config, 67 const media::Decryptor::DecoderInitCB& decoder_init_cb); 68 // TODO(tomfinegan): Add callback args for DeinitializeDecoder() and 69 // ResetDecoder() 70 bool DeinitializeDecoder(media::Decryptor::StreamType stream_type); 71 bool ResetDecoder(media::Decryptor::StreamType stream_type); 72 // Note: These methods can be used with unencrypted data. 73 bool DecryptAndDecodeAudio( 74 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer, 75 const media::Decryptor::AudioDecodeCB& audio_decode_cb); 76 bool DecryptAndDecodeVideo( 77 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer, 78 const media::Decryptor::VideoDecodeCB& video_decode_cb); 79 80 // PPB_ContentDecryptor_Private dispatching methods. 81 // TODO(ddorwin): Remove this method. 82 void NeedKey(PP_Var key_system, PP_Var session_id, PP_Var init_data); 83 // TODO(ddorwin): Remove key_system_var parameter from these methods. 84 void KeyAdded(PP_Var key_system, PP_Var session_id); 85 void KeyMessage(PP_Var key_system, 86 PP_Var session_id, 87 PP_Var message, 88 PP_Var default_url); 89 void KeyError(PP_Var key_system, 90 PP_Var session_id, 91 int32_t media_error, 92 int32_t system_code); 93 void DeliverBlock(PP_Resource decrypted_block, 94 const PP_DecryptedBlockInfo* block_info); 95 void DecoderInitializeDone(PP_DecryptorStreamType decoder_type, 96 uint32_t request_id, 97 PP_Bool success); 98 void DecoderDeinitializeDone(PP_DecryptorStreamType decoder_type, 99 uint32_t request_id); 100 void DecoderResetDone(PP_DecryptorStreamType decoder_type, 101 uint32_t request_id); 102 void DeliverFrame(PP_Resource decrypted_frame, 103 const PP_DecryptedFrameInfo* frame_info); 104 void DeliverSamples(PP_Resource audio_frames, 105 const PP_DecryptedBlockInfo* block_info); 106 107 private: 108 // Cancels the pending decrypt-and-decode callback for |stream_type|. 109 void CancelDecode(media::Decryptor::StreamType stream_type); 110 111 // Fills |resource| with a PPB_Buffer_Impl and copies the data from 112 // |encrypted_buffer| into the buffer resource. This method reuses 113 // |audio_input_resource_| and |video_input_resource_| to reduce the latency 114 // in requesting new PPB_Buffer_Impl resources. The caller must make sure that 115 // |audio_input_resource_| or |video_input_resource_| is available before 116 // calling this method. 117 // 118 // An end of stream |encrypted_buffer| is represented as a null |resource|. 119 // 120 // Returns true upon success and false if any error happened. 121 bool MakeMediaBufferResource( 122 media::Decryptor::StreamType stream_type, 123 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer, 124 scoped_refptr<PPB_Buffer_Impl>* resource); 125 126 void FreeBuffer(uint32_t buffer_id); 127 128 void SetBufferToFreeInTrackingInfo(PP_DecryptTrackingInfo* tracking_info); 129 130 // Deserializes audio data stored in |audio_frames| into individual audio 131 // buffers in |frames|. Returns true upon success. 132 bool DeserializeAudioFrames(PP_Resource audio_frames, 133 size_t data_size, 134 media::Decryptor::AudioBuffers* frames); 135 136 const PP_Instance pp_instance_; 137 const PPP_ContentDecryptor_Private* const plugin_decryption_interface_; 138 139 // TODO(ddorwin): Remove after updating the Pepper API to not use key system. 140 std::string key_system_; 141 142 // Callbacks for firing key events. 143 media::KeyAddedCB key_added_cb_; 144 media::KeyErrorCB key_error_cb_; 145 media::KeyMessageCB key_message_cb_; 146 147 gfx::Size natural_size_; 148 149 // Request ID for tracking pending content decryption callbacks. 150 // Note that zero indicates an invalid request ID. 151 // TODO(xhwang): Add completion callbacks for Reset/Stop and remove the use 152 // of request IDs. 153 uint32_t next_decryption_request_id_; 154 155 uint32_t pending_audio_decrypt_request_id_; 156 media::Decryptor::DecryptCB pending_audio_decrypt_cb_; 157 158 uint32_t pending_video_decrypt_request_id_; 159 media::Decryptor::DecryptCB pending_video_decrypt_cb_; 160 161 uint32_t pending_audio_decoder_init_request_id_; 162 media::Decryptor::DecoderInitCB pending_audio_decoder_init_cb_; 163 164 uint32_t pending_video_decoder_init_request_id_; 165 media::Decryptor::DecoderInitCB pending_video_decoder_init_cb_; 166 167 uint32_t pending_audio_decode_request_id_; 168 media::Decryptor::AudioDecodeCB pending_audio_decode_cb_; 169 170 uint32_t pending_video_decode_request_id_; 171 media::Decryptor::VideoDecodeCB pending_video_decode_cb_; 172 173 // Cached audio and video input buffers. See MakeMediaBufferResource. 174 scoped_refptr<PPB_Buffer_Impl> audio_input_resource_; 175 scoped_refptr<PPB_Buffer_Impl> video_input_resource_; 176 177 std::queue<uint32_t> free_buffers_; 178 179 base::WeakPtrFactory<ContentDecryptorDelegate> weak_ptr_factory_; 180 base::WeakPtr<ContentDecryptorDelegate> weak_this_; 181 182 // Keep track of audio parameters. 183 media::SampleFormat audio_sample_format_; 184 int audio_samples_per_second_; 185 int audio_channel_count_; 186 int audio_bytes_per_frame_; 187 188 DISALLOW_COPY_AND_ASSIGN(ContentDecryptorDelegate); 189 }; 190 191 } // namespace content 192 193 #endif // CONTENT_RENDERER_PEPPER_CONTENT_DECRYPTOR_DELEGATE_H_ 194