Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / media / filters / decoder_stream.h
1 // Copyright 2014 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_DECODER_STREAM_H_
6 #define MEDIA_FILTERS_DECODER_STREAM_H_
7
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "media/base/audio_decoder.h"
15 #include "media/base/decryptor.h"
16 #include "media/base/demuxer_stream.h"
17 #include "media/base/media_export.h"
18 #include "media/base/media_log.h"
19 #include "media/base/pipeline_status.h"
20 #include "media/filters/decoder_selector.h"
21 #include "media/filters/decoder_stream_traits.h"
22
23 namespace base {
24 class SingleThreadTaskRunner;
25 }
26
27 namespace media {
28
29 class DecryptingDemuxerStream;
30
31 // Wraps a DemuxerStream and a list of Decoders and provides decoded
32 // output to its client (e.g. Audio/VideoRendererImpl).
33 template<DemuxerStream::Type StreamType>
34 class MEDIA_EXPORT DecoderStream {
35  public:
36   typedef DecoderStreamTraits<StreamType> StreamTraits;
37   typedef typename StreamTraits::DecoderType Decoder;
38   typedef typename StreamTraits::OutputType Output;
39   typedef typename StreamTraits::StreamInitCB InitCB;
40   typedef typename Decoder::Status DecoderStatus;
41
42   enum Status {
43     OK,  // Everything went as planned.
44     ABORTED,  // Read aborted due to Reset() during pending read.
45     DEMUXER_READ_ABORTED,  // Demuxer returned aborted read.
46     DECODE_ERROR,  // Decoder returned decode error.
47     DECRYPT_ERROR  // Decoder returned decrypt error.
48   };
49
50   // Indicates completion of a DecoderStream read.
51   typedef base::Callback<void(Status, const scoped_refptr<Output>&)> ReadCB;
52
53   DecoderStream(
54       const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
55       ScopedVector<Decoder> decoders,
56       const SetDecryptorReadyCB& set_decryptor_ready_cb,
57       const scoped_refptr<MediaLog>& media_log);
58   virtual ~DecoderStream();
59
60   // Initializes the DecoderStream and returns the initialization result
61   // through |init_cb|. Note that |init_cb| is always called asynchronously.
62   void Initialize(DemuxerStream* stream,
63                   bool low_delay,
64                   const StatisticsCB& statistics_cb,
65                   const InitCB& init_cb);
66
67   // Reads a decoded Output and returns it via the |read_cb|. Note that
68   // |read_cb| is always called asynchronously. This method should only be
69   // called after initialization has succeeded and must not be called during
70   // pending Reset().
71   void Read(const ReadCB& read_cb);
72
73   // Resets the decoder, flushes all decoded outputs and/or internal buffers,
74   // fires any existing pending read callback and calls |closure| on completion.
75   // Note that |closure| is always called asynchronously. This method should
76   // only be called after initialization has succeeded and must not be called
77   // during pending Reset().
78   void Reset(const base::Closure& closure);
79
80   // Returns true if the decoder currently has the ability to decode and return
81   // an Output.
82   // TODO(rileya): Remove the need for this by refactoring Decoder queueing
83   // behavior.
84   bool CanReadWithoutStalling() const;
85
86   // Returns maximum concurrent decode requests for the current |decoder_|.
87   int GetMaxDecodeRequests() const;
88
89   // Returns true if one more decode request can be submitted to the decoder.
90   bool CanDecodeMore() const;
91
92   // Allows callers to register for notification of splice buffers from the
93   // demuxer.  I.e., DecoderBuffer::splice_timestamp() is not kNoTimestamp().
94   //
95   // The observer will be notified of all buffers with a splice_timestamp() and
96   // the first buffer after which has a splice_timestamp() of kNoTimestamp().
97   typedef base::Callback<void(base::TimeDelta)> SpliceObserverCB;
98   void set_splice_observer(const SpliceObserverCB& splice_observer) {
99     splice_observer_cb_ = splice_observer;
100   }
101
102   // Allows callers to register for notification of config changes; this is
103   // called immediately after recieving the 'kConfigChanged' status from the
104   // DemuxerStream, before any action is taken to handle the config change.
105   typedef base::Closure ConfigChangeObserverCB;
106   void set_config_change_observer(
107       const ConfigChangeObserverCB& config_change_observer) {
108     config_change_observer_cb_ = config_change_observer;
109   }
110
111  private:
112   enum State {
113     STATE_UNINITIALIZED,
114     STATE_INITIALIZING,
115     STATE_NORMAL,  // Includes idle, pending decoder decode/reset.
116     STATE_FLUSHING_DECODER,
117     STATE_PENDING_DEMUXER_READ,
118     STATE_REINITIALIZING_DECODER,
119     STATE_END_OF_STREAM,  // End of stream reached; returns EOS on all reads.
120     STATE_ERROR
121   };
122
123   // Called when |decoder_selector| selected the |selected_decoder|.
124   // |decrypting_demuxer_stream| was also populated if a DecryptingDemuxerStream
125   // is created to help decrypt the encrypted stream.
126   void OnDecoderSelected(
127       scoped_ptr<Decoder> selected_decoder,
128       scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream);
129
130   // Satisfy pending |read_cb_| with |status| and |output|.
131   void SatisfyRead(Status status,
132                    const scoped_refptr<Output>& output);
133
134   // Decodes |buffer| and returns the result via OnDecodeOutputReady().
135   void Decode(const scoped_refptr<DecoderBuffer>& buffer);
136
137   // Flushes the decoder with an EOS buffer to retrieve internally buffered
138   // decoder output.
139   void FlushDecoder();
140
141   // Callback for Decoder::Decode().
142   void OnDecodeDone(int buffer_size, bool end_of_stream, DecoderStatus status);
143
144   // Output callback passed to Decoder::Initialize().
145   void OnDecodeOutputReady(const scoped_refptr<Output>& output);
146
147   // Reads a buffer from |stream_| and returns the result via OnBufferReady().
148   void ReadFromDemuxerStream();
149
150   // Callback for DemuxerStream::Read().
151   void OnBufferReady(DemuxerStream::Status status,
152                      const scoped_refptr<DecoderBuffer>& buffer);
153
154   void ReinitializeDecoder();
155
156   // Callback for Decoder reinitialization.
157   void OnDecoderReinitialized(PipelineStatus status);
158
159   void ResetDecoder();
160   void OnDecoderReset();
161
162   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
163
164   scoped_refptr<MediaLog> media_log_;
165
166   State state_;
167
168   StatisticsCB statistics_cb_;
169   InitCB init_cb_;
170
171   ReadCB read_cb_;
172   base::Closure reset_cb_;
173
174   DemuxerStream* stream_;
175   bool low_delay_;
176
177   scoped_ptr<DecoderSelector<StreamType> > decoder_selector_;
178
179   // These two will be set by DecoderSelector::SelectDecoder().
180   scoped_ptr<Decoder> decoder_;
181   scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream_;
182
183   SpliceObserverCB splice_observer_cb_;
184   ConfigChangeObserverCB config_change_observer_cb_;
185
186   // If a splice_timestamp() has been seen, this is true until a
187   // splice_timestamp() of kNoTimestamp() is encountered.
188   bool active_splice_;
189
190   // An end-of-stream buffer has been sent for decoding, no more buffers should
191   // be sent for decoding until it completes.
192   // TODO(sandersd): Turn this into a State. http://crbug.com/408316
193   bool decoding_eos_;
194
195   // Decoded buffers that haven't been read yet. Used when the decoder supports
196   // parallel decoding.
197   std::list<scoped_refptr<Output> > ready_outputs_;
198
199   // Number of outstanding decode requests sent to the |decoder_|.
200   int pending_decode_requests_;
201
202   // NOTE: Weak pointers must be invalidated before all other member variables.
203   base::WeakPtrFactory<DecoderStream<StreamType> > weak_factory_;
204 };
205
206 template <>
207 bool DecoderStream<DemuxerStream::AUDIO>::CanReadWithoutStalling() const;
208
209 template <>
210 int DecoderStream<DemuxerStream::AUDIO>::GetMaxDecodeRequests() const;
211
212 typedef DecoderStream<DemuxerStream::VIDEO> VideoFrameStream;
213 typedef DecoderStream<DemuxerStream::AUDIO> AudioBufferStream;
214
215 }  // namespace media
216
217 #endif  // MEDIA_FILTERS_DECODER_STREAM_H_