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