ppapi_decryptor.h revision bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3
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 webkit {
23namespace ppapi {
24class ContentDecryptorDelegate;
25class PluginInstanceImpl;
26}
27}
28
29namespace content {
30
31// PpapiDecryptor implements media::Decryptor and forwards all calls to the
32// PluginInstance.
33// This class should always be created & destroyed on the main renderer thread.
34class PpapiDecryptor : public media::MediaKeys, public media::Decryptor {
35 public:
36  static scoped_ptr<PpapiDecryptor> Create(
37      // TODO(ddorwin): Remove after updating the delegate.
38      const std::string& key_system,
39      const scoped_refptr<webkit::ppapi::PluginInstanceImpl>& 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);
44
45  virtual ~PpapiDecryptor();
46
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;
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(
79      const scoped_refptr<webkit::ppapi::PluginInstanceImpl>& 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);
85
86  void ReportFailureToCallPlugin(const std::string& session_id);
87
88  void OnDecoderInitialized(StreamType stream_type, bool success);
89
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,
94                int system_code);
95  void KeyMessage(const std::string& session_id,
96                  const std::vector<uint8>& message,
97                  const std::string& default_url);
98
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::PluginInstanceImpl> plugin_instance_;
103
104  webkit::ppapi::ContentDecryptorDelegate* plugin_cdm_delegate_;
105
106  // Callbacks for firing key events.
107  media::KeyAddedCB key_added_cb_;
108  media::KeyErrorCB key_error_cb_;
109  media::KeyMessageCB key_message_cb_;
110
111  // Called to destroy the helper plugin when this class no longer needs it.
112  base::Closure destroy_plugin_cb_;
113
114  scoped_refptr<base::MessageLoopProxy> render_loop_proxy_;
115
116  DecoderInitCB audio_decoder_init_cb_;
117  DecoderInitCB video_decoder_init_cb_;
118  NewKeyCB new_audio_key_cb_;
119  NewKeyCB new_video_key_cb_;
120
121  base::WeakPtrFactory<PpapiDecryptor> weak_ptr_factory_;
122  base::WeakPtr<PpapiDecryptor> weak_this_;
123
124  DISALLOW_COPY_AND_ASSIGN(PpapiDecryptor);
125};
126
127}  // namespace content
128
129#endif  // CONTENT_RENDERER_MEDIA_CRYPTO_PPAPI_DECRYPTOR_H_
130