ppapi_decryptor.h revision f2477e01787aa58f445919b809d89e252beef54f
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
18namespace base {
19class MessageLoopProxy;
20}
21
22namespace content {
23class ContentDecryptorDelegate;
24class PepperPluginInstanceImpl;
25
26// PpapiDecryptor implements media::Decryptor and forwards all calls to the
27// PluginInstance.
28// This class should always be created & destroyed on the main renderer thread.
29class 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::KeyAddedCB& key_added_cb,
36      const media::KeyErrorCB& key_error_cb,
37      const media::KeyMessageCB& key_message_cb,
38      const media::SetSessionIdCB& set_session_id_cb,
39      const base::Closure& destroy_plugin_cb);
40
41  virtual ~PpapiDecryptor();
42
43  // media::MediaKeys implementation.
44  virtual bool GenerateKeyRequest(uint32 reference_id,
45                                  const std::string& type,
46                                  const uint8* init_data,
47                                  int init_data_length) OVERRIDE;
48  virtual void AddKey(uint32 reference_id,
49                      const uint8* key,
50                      int key_length,
51                      const uint8* init_data,
52                      int init_data_length) OVERRIDE;
53  virtual void CancelKeyRequest(uint32 reference_id) OVERRIDE;
54  virtual Decryptor* GetDecryptor() OVERRIDE;
55
56  // media::Decryptor implementation.
57  virtual void RegisterNewKeyCB(StreamType stream_type,
58                                const NewKeyCB& key_added_cb) OVERRIDE;
59  virtual void Decrypt(StreamType stream_type,
60                       const scoped_refptr<media::DecoderBuffer>& encrypted,
61                       const DecryptCB& decrypt_cb) OVERRIDE;
62  virtual void CancelDecrypt(StreamType stream_type) OVERRIDE;
63  virtual void InitializeAudioDecoder(const media::AudioDecoderConfig& config,
64                                      const DecoderInitCB& init_cb) OVERRIDE;
65  virtual void InitializeVideoDecoder(const media::VideoDecoderConfig& config,
66                                      const DecoderInitCB& init_cb) OVERRIDE;
67  virtual void DecryptAndDecodeAudio(
68      const scoped_refptr<media::DecoderBuffer>& encrypted,
69      const AudioDecodeCB& audio_decode_cb) OVERRIDE;
70  virtual void DecryptAndDecodeVideo(
71      const scoped_refptr<media::DecoderBuffer>& encrypted,
72      const VideoDecodeCB& video_decode_cb) OVERRIDE;
73  virtual void ResetDecoder(StreamType stream_type) OVERRIDE;
74  virtual void DeinitializeDecoder(StreamType stream_type) OVERRIDE;
75
76 private:
77  PpapiDecryptor(const scoped_refptr<PepperPluginInstanceImpl>& plugin_instance,
78                 ContentDecryptorDelegate* plugin_cdm_delegate,
79                 const media::KeyAddedCB& key_added_cb,
80                 const media::KeyErrorCB& key_error_cb,
81                 const media::KeyMessageCB& key_message_cb,
82                 const media::SetSessionIdCB& set_session_id_cb,
83                 const base::Closure& destroy_plugin_cb);
84
85  void ReportFailureToCallPlugin(uint32 reference_id);
86
87  void OnDecoderInitialized(StreamType stream_type, bool success);
88
89  // Callbacks for |plugin_cdm_delegate_| to fire key events.
90  void KeyAdded(uint32 reference_id);
91  void KeyError(uint32 reference_id,
92                media::MediaKeys::KeyError error_code,
93                int system_code);
94  void KeyMessage(uint32 reference_id,
95                  const std::vector<uint8>& message,
96                  const std::string& default_url);
97  void SetSessionId(uint32 reference_id, const std::string& session_id);
98
99  base::WeakPtr<PpapiDecryptor> weak_this_;
100
101  // Hold a reference of the plugin instance to make sure the plugin outlives
102  // the |plugin_cdm_delegate_|. This is needed because |plugin_cdm_delegate_|
103  // is owned by the |plugin_instance_|.
104  scoped_refptr<PepperPluginInstanceImpl> plugin_instance_;
105
106  ContentDecryptorDelegate* plugin_cdm_delegate_;
107
108  // Callbacks for firing key events.
109  media::KeyAddedCB key_added_cb_;
110  media::KeyErrorCB key_error_cb_;
111  media::KeyMessageCB key_message_cb_;
112  media::SetSessionIdCB set_session_id_cb_;
113
114  // Called to destroy the helper plugin when this class no longer needs it.
115  base::Closure destroy_plugin_cb_;
116
117  scoped_refptr<base::MessageLoopProxy> render_loop_proxy_;
118
119  DecoderInitCB audio_decoder_init_cb_;
120  DecoderInitCB video_decoder_init_cb_;
121  NewKeyCB new_audio_key_cb_;
122  NewKeyCB new_video_key_cb_;
123
124  base::WeakPtrFactory<PpapiDecryptor> weak_ptr_factory_;
125
126  DISALLOW_COPY_AND_ASSIGN(PpapiDecryptor);
127};
128
129}  // namespace content
130
131#endif  // CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
132