Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / crypto / ppapi_decryptor.h
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.
4
5 #ifndef CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
6 #define CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
7
8 #include <string>
9 #include <vector>
10
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"
17
18 namespace base {
19 class MessageLoopProxy;
20 }
21
22 namespace content {
23 class ContentDecryptorDelegate;
24 class PepperPluginInstanceImpl;
25
26 // PpapiDecryptor implements media::MediaKeys and media::Decryptor and forwards
27 // all calls to the PluginInstance.
28 // This class should always be created & destroyed on the main renderer thread.
29 class PpapiDecryptor : public media::MediaKeys, public media::Decryptor {
30  public:
31   static scoped_ptr<PpapiDecryptor> Create(
32       // TODO(ddorwin): Remove after updating the delegate.
33       const std::string& key_system,
34       const scoped_refptr<PepperPluginInstanceImpl>& plugin_instance,
35       const media::SessionCreatedCB& session_created_cb,
36       const media::SessionMessageCB& session_message_cb,
37       const media::SessionReadyCB& session_ready_cb,
38       const media::SessionClosedCB& session_closed_cb,
39       const media::SessionErrorCB& session_error_cb,
40       const base::Closure& destroy_plugin_cb);
41
42   virtual ~PpapiDecryptor();
43
44   // media::MediaKeys implementation.
45   virtual bool CreateSession(uint32 session_id,
46                              const std::string& content_type,
47                              const uint8* init_data,
48                              int init_data_length) OVERRIDE;
49   virtual void LoadSession(uint32 session_id,
50                            const std::string& web_session_id) OVERRIDE;
51   virtual void UpdateSession(uint32 session_id,
52                              const uint8* response,
53                              int response_length) OVERRIDE;
54   virtual void ReleaseSession(uint32 session_id) OVERRIDE;
55   virtual Decryptor* GetDecryptor() OVERRIDE;
56
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;
76
77  private:
78   PpapiDecryptor(const std::string& key_system,
79                  const scoped_refptr<PepperPluginInstanceImpl>& plugin_instance,
80                  ContentDecryptorDelegate* plugin_cdm_delegate,
81                  const media::SessionCreatedCB& session_created_cb,
82                  const media::SessionMessageCB& session_message_cb,
83                  const media::SessionReadyCB& session_ready_cb,
84                  const media::SessionClosedCB& session_closed_cb,
85                  const media::SessionErrorCB& session_error_cb,
86                  const base::Closure& destroy_plugin_cb);
87
88   void ReportFailureToCallPlugin(uint32 session_id);
89
90   void OnDecoderInitialized(StreamType stream_type, bool success);
91
92   // Callbacks for |plugin_cdm_delegate_| to fire session events.
93   void OnSessionCreated(uint32 session_id, const std::string& web_session_id);
94   void OnSessionMessage(uint32 session_id,
95                         const std::vector<uint8>& message,
96                         const std::string& destination_url);
97   void OnSessionReady(uint32 session_id);
98   void OnSessionClosed(uint32 session_id);
99   void OnSessionError(uint32 session_id,
100                       media::MediaKeys::KeyError error_code,
101                       int system_code);
102
103   // Callback to notify that a fatal error happened in |plugin_cdm_delegate_|.
104   // The error is terminal and |plugin_cdm_delegate_| should not be used after
105   // this call.
106   void OnFatalPluginError();
107
108   base::WeakPtr<PpapiDecryptor> weak_this_;
109
110   // Hold a reference of the plugin instance to make sure the plugin outlives
111   // the |plugin_cdm_delegate_|. This is needed because |plugin_cdm_delegate_|
112   // is owned by the |plugin_instance_|.
113   scoped_refptr<PepperPluginInstanceImpl> plugin_instance_;
114
115   ContentDecryptorDelegate* plugin_cdm_delegate_;
116
117   // Callbacks for firing session events.
118   media::SessionCreatedCB session_created_cb_;
119   media::SessionMessageCB session_message_cb_;
120   media::SessionReadyCB session_ready_cb_;
121   media::SessionClosedCB session_closed_cb_;
122   media::SessionErrorCB session_error_cb_;
123
124   // Called to destroy the helper plugin when this class no longer needs it.
125   base::Closure destroy_plugin_cb_;
126
127   scoped_refptr<base::MessageLoopProxy> render_loop_proxy_;
128
129   DecoderInitCB audio_decoder_init_cb_;
130   DecoderInitCB video_decoder_init_cb_;
131   NewKeyCB new_audio_key_cb_;
132   NewKeyCB new_video_key_cb_;
133
134   base::WeakPtrFactory<PpapiDecryptor> weak_ptr_factory_;
135
136   DISALLOW_COPY_AND_ASSIGN(PpapiDecryptor);
137 };
138
139 }  // namespace content
140
141 #endif  // CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_