Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / media / filters / decoder_stream.h
blobd7eba5d47a029f8e8ddb0a32c8ebf2b45b857810
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.
5 #ifndef MEDIA_FILTERS_DECODER_STREAM_H_
6 #define MEDIA_FILTERS_DECODER_STREAM_H_
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/base/timestamp_constants.h"
21 #include "media/filters/decoder_selector.h"
22 #include "media/filters/decoder_stream_traits.h"
24 namespace base {
25 class SingleThreadTaskRunner;
28 namespace media {
30 class DecryptingDemuxerStream;
32 // Wraps a DemuxerStream and a list of Decoders and provides decoded
33 // output to its client (e.g. Audio/VideoRendererImpl).
34 template<DemuxerStream::Type StreamType>
35 class MEDIA_EXPORT DecoderStream {
36 public:
37 typedef DecoderStreamTraits<StreamType> StreamTraits;
38 typedef typename StreamTraits::DecoderType Decoder;
39 typedef typename StreamTraits::OutputType Output;
40 typedef typename Decoder::Status DecoderStatus;
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.
49 // Indicates completion of a DecoderStream initialization.
50 typedef base::Callback<void(bool success)> InitCB;
52 // Indicates completion of a DecoderStream read.
53 typedef base::Callback<void(Status, const scoped_refptr<Output>&)> ReadCB;
55 DecoderStream(
56 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
57 ScopedVector<Decoder> decoders,
58 const scoped_refptr<MediaLog>& media_log);
59 virtual ~DecoderStream();
61 // Returns the string representation of the StreamType for logging purpose.
62 std::string GetStreamTypeString();
64 // Initializes the DecoderStream and returns the initialization result
65 // through |init_cb|. Note that |init_cb| is always called asynchronously.
66 void Initialize(DemuxerStream* stream,
67 const InitCB& init_cb,
68 const SetDecryptorReadyCB& set_decryptor_ready_cb,
69 const StatisticsCB& statistics_cb,
70 const base::Closure& waiting_for_decryption_key_cb);
72 // Reads a decoded Output and returns it via the |read_cb|. Note that
73 // |read_cb| is always called asynchronously. This method should only be
74 // called after initialization has succeeded and must not be called during
75 // pending Reset().
76 void Read(const ReadCB& read_cb);
78 // Resets the decoder, flushes all decoded outputs and/or internal buffers,
79 // fires any existing pending read callback and calls |closure| on completion.
80 // Note that |closure| is always called asynchronously. This method should
81 // only be called after initialization has succeeded and must not be called
82 // during pending Reset().
83 void Reset(const base::Closure& closure);
85 // Returns true if the decoder currently has the ability to decode and return
86 // an Output.
87 // TODO(rileya): Remove the need for this by refactoring Decoder queueing
88 // behavior.
89 bool CanReadWithoutStalling() const;
91 // Returns maximum concurrent decode requests for the current |decoder_|.
92 int GetMaxDecodeRequests() const;
94 // Returns true if one more decode request can be submitted to the decoder.
95 bool CanDecodeMore() const;
97 // Allows callers to register for notification of splice buffers from the
98 // demuxer. I.e., DecoderBuffer::splice_timestamp() is not kNoTimestamp().
100 // The observer will be notified of all buffers with a splice_timestamp() and
101 // the first buffer after which has a splice_timestamp() of kNoTimestamp().
102 typedef base::Callback<void(base::TimeDelta)> SpliceObserverCB;
103 void set_splice_observer(const SpliceObserverCB& splice_observer) {
104 splice_observer_cb_ = splice_observer;
107 // Allows callers to register for notification of config changes; this is
108 // called immediately after receiving the 'kConfigChanged' status from the
109 // DemuxerStream, before any action is taken to handle the config change.
110 typedef base::Closure ConfigChangeObserverCB;
111 void set_config_change_observer(
112 const ConfigChangeObserverCB& config_change_observer) {
113 config_change_observer_cb_ = config_change_observer;
116 private:
117 enum State {
118 STATE_UNINITIALIZED,
119 STATE_INITIALIZING,
120 STATE_NORMAL, // Includes idle, pending decoder decode/reset.
121 STATE_FLUSHING_DECODER,
122 STATE_PENDING_DEMUXER_READ,
123 STATE_REINITIALIZING_DECODER,
124 STATE_END_OF_STREAM, // End of stream reached; returns EOS on all reads.
125 STATE_ERROR
128 void SelectDecoder(const SetDecryptorReadyCB& set_decryptor_ready_cb);
130 // Called when |decoder_selector| selected the |selected_decoder|.
131 // |decrypting_demuxer_stream| was also populated if a DecryptingDemuxerStream
132 // is created to help decrypt the encrypted stream.
133 void OnDecoderSelected(
134 scoped_ptr<Decoder> selected_decoder,
135 scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream);
137 // Satisfy pending |read_cb_| with |status| and |output|.
138 void SatisfyRead(Status status,
139 const scoped_refptr<Output>& output);
141 // Decodes |buffer| and returns the result via OnDecodeOutputReady().
142 void Decode(const scoped_refptr<DecoderBuffer>& buffer);
144 // Flushes the decoder with an EOS buffer to retrieve internally buffered
145 // decoder output.
146 void FlushDecoder();
148 // Callback for Decoder::Decode().
149 void OnDecodeDone(int buffer_size, bool end_of_stream, DecoderStatus status);
151 // Output callback passed to Decoder::Initialize().
152 void OnDecodeOutputReady(const scoped_refptr<Output>& output);
154 // Reads a buffer from |stream_| and returns the result via OnBufferReady().
155 void ReadFromDemuxerStream();
157 // Callback for DemuxerStream::Read().
158 void OnBufferReady(DemuxerStream::Status status,
159 const scoped_refptr<DecoderBuffer>& buffer);
161 void ReinitializeDecoder();
163 // Callback for Decoder reinitialization.
164 void OnDecoderReinitialized(bool success);
166 void CompleteDecoderReinitialization(bool success);
168 void ResetDecoder();
169 void OnDecoderReset();
171 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
173 scoped_refptr<MediaLog> media_log_;
175 State state_;
177 StatisticsCB statistics_cb_;
178 InitCB init_cb_;
179 base::Closure waiting_for_decryption_key_cb_;
181 ReadCB read_cb_;
182 base::Closure reset_cb_;
184 DemuxerStream* stream_;
186 scoped_ptr<DecoderSelector<StreamType> > decoder_selector_;
188 scoped_ptr<Decoder> decoder_;
189 // TODO(watk): When falling back from H/W decoding to S/W decoding,
190 // destructing the GpuVideoDecoder too early results in black frames being
191 // displayed. |previous_decoder_| is used to keep it alive.
192 scoped_ptr<Decoder> previous_decoder_;
193 scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream_;
195 SpliceObserverCB splice_observer_cb_;
196 ConfigChangeObserverCB config_change_observer_cb_;
198 // If a splice_timestamp() has been seen, this is true until a
199 // splice_timestamp() of kNoTimestamp() is encountered.
200 bool active_splice_;
202 // An end-of-stream buffer has been sent for decoding, no more buffers should
203 // be sent for decoding until it completes.
204 // TODO(sandersd): Turn this into a State. http://crbug.com/408316
205 bool decoding_eos_;
207 // Decoded buffers that haven't been read yet. Used when the decoder supports
208 // parallel decoding.
209 std::list<scoped_refptr<Output> > ready_outputs_;
211 // Number of outstanding decode requests sent to the |decoder_|.
212 int pending_decode_requests_;
214 // NOTE: Weak pointers must be invalidated before all other member variables.
215 base::WeakPtrFactory<DecoderStream<StreamType> > weak_factory_;
218 template <>
219 bool DecoderStream<DemuxerStream::AUDIO>::CanReadWithoutStalling() const;
221 template <>
222 int DecoderStream<DemuxerStream::AUDIO>::GetMaxDecodeRequests() const;
224 typedef DecoderStream<DemuxerStream::VIDEO> VideoFrameStream;
225 typedef DecoderStream<DemuxerStream::AUDIO> AudioBufferStream;
227 } // namespace media
229 #endif // MEDIA_FILTERS_DECODER_STREAM_H_