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 WEBKIT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
6 #define WEBKIT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "media/base/decryptor.h"
15 #include "media/base/media_keys.h"
16 #include "media/base/video_decoder_config.h"
19 class MessageLoopProxy
;
24 class ContentDecryptorDelegate
;
29 namespace webkit_media
{
31 // PpapiDecryptor implements media::Decryptor and forwards all calls to the
33 // This class should always be created & destroyed on the main renderer thread.
34 class PpapiDecryptor
: public media::MediaKeys
, public media::Decryptor
{
36 static scoped_ptr
<webkit_media::PpapiDecryptor
> Create(
37 // TODO(ddorwin): Remove after updating the delegate.
38 const std::string
& key_system
,
39 const scoped_refptr
<webkit::ppapi::PluginInstance
>& plugin_instance
,
40 const media::KeyAddedCB
& key_added_cb
,
41 const media::KeyErrorCB
& key_error_cb
,
42 const media::KeyMessageCB
& key_message_cb
,
43 const base::Closure
& destroy_plugin_cb
);
45 virtual ~PpapiDecryptor();
47 // media::MediaKeys implementation.
48 virtual bool GenerateKeyRequest(const std::string
& type
,
49 const uint8
* init_data
,
50 int init_data_length
) OVERRIDE
;
51 virtual void AddKey(const uint8
* key
, int key_length
,
52 const uint8
* init_data
, int init_data_length
,
53 const std::string
& session_id
) OVERRIDE
;
54 virtual void CancelKeyRequest(const std::string
& session_id
) OVERRIDE
;
55 virtual Decryptor
* GetDecryptor() OVERRIDE
;
57 // media::Decryptor implementation.
58 virtual void RegisterNewKeyCB(StreamType stream_type
,
59 const NewKeyCB
& key_added_cb
) OVERRIDE
;
60 virtual void Decrypt(StreamType stream_type
,
61 const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
62 const DecryptCB
& decrypt_cb
) OVERRIDE
;
63 virtual void CancelDecrypt(StreamType stream_type
) OVERRIDE
;
64 virtual void InitializeAudioDecoder(const media::AudioDecoderConfig
& config
,
65 const DecoderInitCB
& init_cb
) OVERRIDE
;
66 virtual void InitializeVideoDecoder(const media::VideoDecoderConfig
& config
,
67 const DecoderInitCB
& init_cb
) OVERRIDE
;
68 virtual void DecryptAndDecodeAudio(
69 const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
70 const AudioDecodeCB
& audio_decode_cb
) OVERRIDE
;
71 virtual void DecryptAndDecodeVideo(
72 const scoped_refptr
<media::DecoderBuffer
>& encrypted
,
73 const VideoDecodeCB
& video_decode_cb
) OVERRIDE
;
74 virtual void ResetDecoder(StreamType stream_type
) OVERRIDE
;
75 virtual void DeinitializeDecoder(StreamType stream_type
) OVERRIDE
;
79 const scoped_refptr
<webkit::ppapi::PluginInstance
>& plugin_instance
,
80 webkit::ppapi::ContentDecryptorDelegate
* plugin_cdm_delegate
,
81 const media::KeyAddedCB
& key_added_cb
,
82 const media::KeyErrorCB
& key_error_cb
,
83 const media::KeyMessageCB
& key_message_cb
,
84 const base::Closure
& destroy_plugin_cb
);
86 void ReportFailureToCallPlugin(const std::string
& session_id
);
88 void OnDecoderInitialized(StreamType stream_type
, bool success
);
90 // Callbacks for |plugin_cdm_delegate_| to fire key events.
91 void KeyAdded(const std::string
& session_id
);
92 void KeyError(const std::string
& session_id
,
93 media::MediaKeys::KeyError error_code
,
95 void KeyMessage(const std::string
& session_id
,
96 const std::vector
<uint8
>& message
,
97 const std::string
& default_url
);
99 // Hold a reference of the plugin instance to make sure the plugin outlives
100 // the |plugin_cdm_delegate_|. This is needed because |plugin_cdm_delegate_|
101 // is owned by the |plugin_instance_|.
102 scoped_refptr
<webkit::ppapi::PluginInstance
> plugin_instance_
;
104 webkit::ppapi::ContentDecryptorDelegate
* plugin_cdm_delegate_
;
106 // Callbacks for firing key events.
107 media::KeyAddedCB key_added_cb_
;
108 media::KeyErrorCB key_error_cb_
;
109 media::KeyMessageCB key_message_cb_
;
111 // Called to destroy the helper plugin when this class no longer needs it.
112 base::Closure destroy_plugin_cb_
;
114 scoped_refptr
<base::MessageLoopProxy
> render_loop_proxy_
;
116 DecoderInitCB audio_decoder_init_cb_
;
117 DecoderInitCB video_decoder_init_cb_
;
118 NewKeyCB new_audio_key_cb_
;
119 NewKeyCB new_video_key_cb_
;
121 base::WeakPtrFactory
<PpapiDecryptor
> weak_ptr_factory_
;
122 base::WeakPtr
<PpapiDecryptor
> weak_this_
;
124 DISALLOW_COPY_AND_ASSIGN(PpapiDecryptor
);
127 } // namespace webkit_media
129 #endif // WEBKIT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_