- add sources.
[platform/framework/web/crosswalk.git] / src / media / filters / decrypting_demuxer_stream.h
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 MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_
6 #define MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_
7
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/weak_ptr.h"
11 #include "media/base/audio_decoder_config.h"
12 #include "media/base/decryptor.h"
13 #include "media/base/demuxer_stream.h"
14 #include "media/base/pipeline_status.h"
15 #include "media/base/video_decoder_config.h"
16
17 namespace base {
18 class MessageLoopProxy;
19 }
20
21 namespace media {
22
23 class DecoderBuffer;
24
25 // Decryptor-based DemuxerStream implementation that converts a potentially
26 // encrypted demuxer stream to a clear demuxer stream.
27 // All public APIs and callbacks are trampolined to the |message_loop_| so
28 // that no locks are required for thread safety.
29 class MEDIA_EXPORT DecryptingDemuxerStream : public DemuxerStream {
30  public:
31   DecryptingDemuxerStream(
32       const scoped_refptr<base::MessageLoopProxy>& message_loop,
33       const SetDecryptorReadyCB& set_decryptor_ready_cb);
34   virtual ~DecryptingDemuxerStream();
35
36   void Initialize(DemuxerStream* stream,
37                   const PipelineStatusCB& status_cb);
38
39   // Cancels all pending operations and fires all pending callbacks.  Sets
40   // |this| to kUninitialized state if |this| hasn't been initialized, or to
41   // kIdle state otherwise.
42   void Reset(const base::Closure& closure);
43
44   // DemuxerStream implementation.
45   virtual void Read(const ReadCB& read_cb) OVERRIDE;
46   virtual AudioDecoderConfig audio_decoder_config() OVERRIDE;
47   virtual VideoDecoderConfig video_decoder_config() OVERRIDE;
48   virtual Type type() OVERRIDE;
49   virtual void EnableBitstreamConverter() OVERRIDE;
50
51  private:
52   // For a detailed state diagram please see this link: http://goo.gl/8jAok
53   // TODO(xhwang): Add a ASCII state diagram in this file after this class
54   // stabilizes.
55   // TODO(xhwang): Update this diagram for DecryptingDemuxerStream.
56   enum State {
57     kUninitialized = 0,
58     kDecryptorRequested,
59     kIdle,
60     kPendingDemuxerRead,
61     kPendingDecrypt,
62     kWaitingForKey,
63   };
64
65   // Callback for DecryptorHost::RequestDecryptor().
66   void SetDecryptor(Decryptor* decryptor);
67
68   // Callback for DemuxerStream::Read().
69   void DecryptBuffer(DemuxerStream::Status status,
70                      const scoped_refptr<DecoderBuffer>& buffer);
71
72   void DecryptPendingBuffer();
73
74   // Callback for Decryptor::Decrypt().
75   void DeliverBuffer(Decryptor::Status status,
76                      const scoped_refptr<DecoderBuffer>& decrypted_buffer);
77
78   // Callback for the |decryptor_| to notify this object that a new key has been
79   // added.
80   void OnKeyAdded();
81
82   // Resets decoder and calls |reset_cb_|.
83   void DoReset();
84
85   // Returns Decryptor::StreamType converted from |stream_type_|.
86   Decryptor::StreamType GetDecryptorStreamType() const;
87
88   // Creates and initializes either |audio_config_| or |video_config_| based on
89   // |demuxer_stream_|.
90   void InitializeDecoderConfig();
91
92   scoped_refptr<base::MessageLoopProxy> message_loop_;
93   base::WeakPtrFactory<DecryptingDemuxerStream> weak_factory_;
94   base::WeakPtr<DecryptingDemuxerStream> weak_this_;
95
96   State state_;
97
98   PipelineStatusCB init_cb_;
99   ReadCB read_cb_;
100   base::Closure reset_cb_;
101
102   // Pointer to the input demuxer stream that will feed us encrypted buffers.
103   DemuxerStream* demuxer_stream_;
104
105   AudioDecoderConfig audio_config_;
106   VideoDecoderConfig video_config_;
107
108   // Callback to request/cancel decryptor creation notification.
109   SetDecryptorReadyCB set_decryptor_ready_cb_;
110
111   Decryptor* decryptor_;
112
113   // The buffer returned by the demuxer that needs to be decrypted.
114   scoped_refptr<media::DecoderBuffer> pending_buffer_to_decrypt_;
115
116   // Indicates the situation where new key is added during pending decryption
117   // (in other words, this variable can only be set in state kPendingDecrypt).
118   // If this variable is true and kNoKey is returned then we need to try
119   // decrypting again in case the newly added key is the correct decryption key.
120   bool key_added_while_decrypt_pending_;
121
122   DISALLOW_COPY_AND_ASSIGN(DecryptingDemuxerStream);
123 };
124
125 }  // namespace media
126
127 #endif  // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_