Update To 11.40.268.0
[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 SingleThreadTaskRunner;
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 |task_runner_| 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::SingleThreadTaskRunner>& task_runner,
33       const SetDecryptorReadyCB& set_decryptor_ready_cb);
34
35   // Cancels all pending operations immediately and fires all pending callbacks.
36   ~DecryptingDemuxerStream() override;
37
38   void Initialize(DemuxerStream* stream,
39                   const PipelineStatusCB& status_cb);
40
41   // Cancels all pending operations and fires all pending callbacks. If in
42   // kPendingDemuxerRead or kPendingDecrypt state, waits for the pending
43   // operation to finish before satisfying |closure|. Sets the state to
44   // kUninitialized if |this| hasn't been initialized, or to kIdle otherwise.
45   void Reset(const base::Closure& closure);
46
47   // DemuxerStream implementation.
48   void Read(const ReadCB& read_cb) override;
49   AudioDecoderConfig audio_decoder_config() override;
50   VideoDecoderConfig video_decoder_config() override;
51   Type type() override;
52   void EnableBitstreamConverter() override;
53   bool SupportsConfigChanges() override;
54   VideoRotation video_rotation() override;
55
56  private:
57   // For a detailed state diagram please see this link: http://goo.gl/8jAok
58   // TODO(xhwang): Add a ASCII state diagram in this file after this class
59   // stabilizes.
60   // TODO(xhwang): Update this diagram for DecryptingDemuxerStream.
61   enum State {
62     kUninitialized = 0,
63     kDecryptorRequested,
64     kIdle,
65     kPendingDemuxerRead,
66     kPendingDecrypt,
67     kWaitingForKey
68   };
69
70   // Callback for DecryptorHost::RequestDecryptor(). |decryptor_attached_cb| is
71   // called when the decryptor has been completely attached to the pipeline.
72   void SetDecryptor(Decryptor* decryptor,
73                     const DecryptorAttachedCB& decryptor_attached_cb);
74
75   // Callback for DemuxerStream::Read().
76   void DecryptBuffer(DemuxerStream::Status status,
77                      const scoped_refptr<DecoderBuffer>& buffer);
78
79   void DecryptPendingBuffer();
80
81   // Callback for Decryptor::Decrypt().
82   void DeliverBuffer(Decryptor::Status status,
83                      const scoped_refptr<DecoderBuffer>& decrypted_buffer);
84
85   // Callback for the |decryptor_| to notify this object that a new key has been
86   // added.
87   void OnKeyAdded();
88
89   // Resets decoder and calls |reset_cb_|.
90   void DoReset();
91
92   // Returns Decryptor::StreamType converted from |stream_type_|.
93   Decryptor::StreamType GetDecryptorStreamType() const;
94
95   // Creates and initializes either |audio_config_| or |video_config_| based on
96   // |demuxer_stream_|.
97   void InitializeDecoderConfig();
98
99   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
100
101   State state_;
102
103   PipelineStatusCB init_cb_;
104   ReadCB read_cb_;
105   base::Closure reset_cb_;
106
107   // Pointer to the input demuxer stream that will feed us encrypted buffers.
108   DemuxerStream* demuxer_stream_;
109
110   AudioDecoderConfig audio_config_;
111   VideoDecoderConfig video_config_;
112
113   // Callback to request/cancel decryptor creation notification.
114   SetDecryptorReadyCB set_decryptor_ready_cb_;
115
116   Decryptor* decryptor_;
117
118   // The buffer returned by the demuxer that needs to be decrypted.
119   scoped_refptr<media::DecoderBuffer> pending_buffer_to_decrypt_;
120
121   // Indicates the situation where new key is added during pending decryption
122   // (in other words, this variable can only be set in state kPendingDecrypt).
123   // If this variable is true and kNoKey is returned then we need to try
124   // decrypting again in case the newly added key is the correct decryption key.
125   bool key_added_while_decrypt_pending_;
126
127   // NOTE: Weak pointers must be invalidated before all other member variables.
128   base::WeakPtrFactory<DecryptingDemuxerStream> weak_factory_;
129   base::WeakPtr<DecryptingDemuxerStream> weak_this_;
130
131   DISALLOW_COPY_AND_ASSIGN(DecryptingDemuxerStream);
132 };
133
134 }  // namespace media
135
136 #endif  // MEDIA_FILTERS_DECRYPTING_DEMUXER_STREAM_H_