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 SetSessionEventCallbacks( 45 const media::SessionCreatedCB& session_created_cb, 46 const media::SessionMessageCB& session_message_cb, 47 const media::SessionReadyCB& session_ready_cb, 48 const media::SessionClosedCB& session_closed_cb, 49 const media::SessionErrorCB& session_error_cb); 50 51 // Provides access to PPP_ContentDecryptor_Private. 52 bool CreateSession(uint32 session_id, 53 const std::string& type, 54 const uint8* init_data, 55 int init_data_length); 56 bool UpdateSession(uint32 session_id, 57 const uint8* response, 58 int response_length); 59 bool ReleaseSession(uint32 session_id); 60 bool Decrypt(media::Decryptor::StreamType stream_type, 61 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer, 62 const media::Decryptor::DecryptCB& decrypt_cb); 63 bool CancelDecrypt(media::Decryptor::StreamType stream_type); 64 bool InitializeAudioDecoder( 65 const media::AudioDecoderConfig& decoder_config, 66 const media::Decryptor::DecoderInitCB& decoder_init_cb); 67 bool InitializeVideoDecoder( 68 const media::VideoDecoderConfig& decoder_config, 69 const media::Decryptor::DecoderInitCB& decoder_init_cb); 70 // TODO(tomfinegan): Add callback args for DeinitializeDecoder() and 71 // ResetDecoder() 72 bool DeinitializeDecoder(media::Decryptor::StreamType stream_type); 73 bool ResetDecoder(media::Decryptor::StreamType stream_type); 74 // Note: These methods can be used with unencrypted data. 75 bool DecryptAndDecodeAudio( 76 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer, 77 const media::Decryptor::AudioDecodeCB& audio_decode_cb); 78 bool DecryptAndDecodeVideo( 79 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer, 80 const media::Decryptor::VideoDecodeCB& video_decode_cb); 81 82 // PPB_ContentDecryptor_Private dispatching methods. 83 void OnSessionCreated(uint32 session_id, PP_Var web_session_id_var); 84 void OnSessionMessage(uint32 session_id, 85 PP_Var message, 86 PP_Var destination_url); 87 void OnSessionReady(uint32 session_id); 88 void OnSessionClosed(uint32 session_id); 89 void OnSessionError(uint32 session_id, 90 int32_t media_error, 91 int32_t system_code); 92 void DeliverBlock(PP_Resource decrypted_block, 93 const PP_DecryptedBlockInfo* block_info); 94 void DecoderInitializeDone(PP_DecryptorStreamType decoder_type, 95 uint32_t request_id, 96 PP_Bool success); 97 void DecoderDeinitializeDone(PP_DecryptorStreamType decoder_type, 98 uint32_t request_id); 99 void DecoderResetDone(PP_DecryptorStreamType decoder_type, 100 uint32_t request_id); 101 void DeliverFrame(PP_Resource decrypted_frame, 102 const PP_DecryptedFrameInfo* frame_info); 103 void DeliverSamples(PP_Resource audio_frames, 104 const PP_DecryptedSampleInfo* sample_info); 105 106 private: 107 // Cancels the pending decrypt-and-decode callback for |stream_type|. 108 void CancelDecode(media::Decryptor::StreamType stream_type); 109 110 // Fills |resource| with a PPB_Buffer_Impl and copies the data from 111 // |encrypted_buffer| into the buffer resource. This method reuses 112 // |audio_input_resource_| and |video_input_resource_| to reduce the latency 113 // in requesting new PPB_Buffer_Impl resources. The caller must make sure that 114 // |audio_input_resource_| or |video_input_resource_| is available before 115 // calling this method. 116 // 117 // An end of stream |encrypted_buffer| is represented as a null |resource|. 118 // 119 // Returns true upon success and false if any error happened. 120 bool MakeMediaBufferResource( 121 media::Decryptor::StreamType stream_type, 122 const scoped_refptr<media::DecoderBuffer>& encrypted_buffer, 123 scoped_refptr<PPB_Buffer_Impl>* resource); 124 125 void FreeBuffer(uint32_t buffer_id); 126 127 void SetBufferToFreeInTrackingInfo(PP_DecryptTrackingInfo* tracking_info); 128 129 // Deserializes audio data stored in |audio_frames| into individual audio 130 // buffers in |frames|. Returns true upon success. 131 bool DeserializeAudioFrames(PP_Resource audio_frames, 132 size_t data_size, 133 media::SampleFormat sample_format, 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 session events. 143 media::SessionCreatedCB session_created_cb_; 144 media::SessionMessageCB session_message_cb_; 145 media::SessionReadyCB session_ready_cb_; 146 media::SessionClosedCB session_closed_cb_; 147 media::SessionErrorCB session_error_cb_; 148 149 gfx::Size natural_size_; 150 151 // Request ID for tracking pending content decryption callbacks. 152 // Note that zero indicates an invalid request ID. 153 // TODO(xhwang): Add completion callbacks for Reset/Stop and remove the use 154 // of request IDs. 155 uint32_t next_decryption_request_id_; 156 157 uint32_t pending_audio_decrypt_request_id_; 158 media::Decryptor::DecryptCB pending_audio_decrypt_cb_; 159 160 uint32_t pending_video_decrypt_request_id_; 161 media::Decryptor::DecryptCB pending_video_decrypt_cb_; 162 163 uint32_t pending_audio_decoder_init_request_id_; 164 media::Decryptor::DecoderInitCB pending_audio_decoder_init_cb_; 165 166 uint32_t pending_video_decoder_init_request_id_; 167 media::Decryptor::DecoderInitCB pending_video_decoder_init_cb_; 168 169 uint32_t pending_audio_decode_request_id_; 170 media::Decryptor::AudioDecodeCB pending_audio_decode_cb_; 171 172 uint32_t pending_video_decode_request_id_; 173 media::Decryptor::VideoDecodeCB pending_video_decode_cb_; 174 175 // Cached audio and video input buffers. See MakeMediaBufferResource. 176 scoped_refptr<PPB_Buffer_Impl> audio_input_resource_; 177 scoped_refptr<PPB_Buffer_Impl> video_input_resource_; 178 179 std::queue<uint32_t> free_buffers_; 180 181 // Keep track of audio parameters. 182 int audio_samples_per_second_; 183 int audio_channel_count_; 184 185 base::WeakPtr<ContentDecryptorDelegate> weak_this_; 186 base::WeakPtrFactory<ContentDecryptorDelegate> weak_ptr_factory_; 187 188 DISALLOW_COPY_AND_ASSIGN(ContentDecryptorDelegate); 189 }; 190 191 } // namespace content 192 193 #endif // CONTENT_RENDERER_PEPPER_CONTENT_DECRYPTOR_DELEGATE_H_ 194