Disable view source for Developer Tools.
[chromium-blink-merge.git] / media / filters / video_frame_stream.h
blobf0f91939704021bfadc300b8b83515a1d9311747
1 // Copyright (c) 2013 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_VIDEO_FRAME_STREAM_H_
6 #define MEDIA_FILTERS_VIDEO_FRAME_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/decryptor.h"
15 #include "media/base/demuxer_stream.h"
16 #include "media/base/media_export.h"
17 #include "media/base/pipeline_status.h"
18 #include "media/base/video_decoder.h"
20 namespace base {
21 class SingleThreadTaskRunner;
24 namespace media {
26 class DecryptingDemuxerStream;
27 class VideoDecoderSelector;
29 // Wraps a DemuxerStream and a list of VideoDecoders and provides decoded
30 // VideoFrames to its client (e.g. VideoRendererImpl).
31 class MEDIA_EXPORT VideoFrameStream {
32 public:
33 // Indicates completion of VideoFrameStream initialization.
34 typedef base::Callback<void(bool success, bool has_alpha)> InitCB;
36 enum Status {
37 OK, // Everything went as planned.
38 ABORTED, // Read aborted due to Reset() during pending read.
39 DEMUXER_READ_ABORTED, // Demuxer returned aborted read.
40 DECODE_ERROR, // Decoder returned decode error.
41 DECRYPT_ERROR // Decoder returned decrypt error.
44 // Indicates completion of a VideoFrameStream read.
45 typedef base::Callback<void(Status, const scoped_refptr<VideoFrame>&)> ReadCB;
47 VideoFrameStream(
48 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
49 ScopedVector<VideoDecoder> decoders,
50 const SetDecryptorReadyCB& set_decryptor_ready_cb);
51 virtual ~VideoFrameStream();
53 // Initializes the VideoFrameStream and returns the initialization result
54 // through |init_cb|. Note that |init_cb| is always called asynchronously.
55 void Initialize(DemuxerStream* stream,
56 const StatisticsCB& statistics_cb,
57 const InitCB& init_cb);
59 // Reads a decoded VideoFrame and returns it via the |read_cb|. Note that
60 // |read_cb| is always called asynchronously. This method should only be
61 // called after initialization has succeeded and must not be called during
62 // any pending Reset() and/or Stop().
63 void Read(const ReadCB& read_cb);
65 // Resets the decoder, flushes all decoded frames and/or internal buffers,
66 // fires any existing pending read callback and calls |closure| on completion.
67 // Note that |closure| is always called asynchronously. This method should
68 // only be called after initialization has succeeded and must not be called
69 // during any pending Reset() and/or Stop().
70 void Reset(const base::Closure& closure);
72 // Stops the decoder, fires any existing pending read callback or reset
73 // callback and calls |closure| on completion. Note that |closure| is always
74 // called asynchronously. The VideoFrameStream cannot be used anymore after
75 // it is stopped. This method can be called at any time but not during another
76 // pending Stop().
77 void Stop(const base::Closure& closure);
79 // Returns true if the decoder currently has the ability to decode and return
80 // a VideoFrame.
81 bool CanReadWithoutStalling() const;
83 private:
84 enum State {
85 STATE_UNINITIALIZED,
86 STATE_INITIALIZING,
87 STATE_NORMAL, // Includes idle, pending decoder decode/reset/stop.
88 STATE_FLUSHING_DECODER,
89 STATE_PENDING_DEMUXER_READ,
90 STATE_REINITIALIZING_DECODER,
91 STATE_STOPPED,
92 STATE_ERROR
95 // Called when |decoder_selector| selected the |selected_decoder|.
96 // |decrypting_demuxer_stream| was also populated if a DecryptingDemuxerStream
97 // is created to help decrypt the encrypted stream.
98 void OnDecoderSelected(
99 scoped_ptr<VideoDecoder> selected_decoder,
100 scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream);
102 // Satisfy pending |read_cb_| with |status| and |frame|.
103 void SatisfyRead(Status status, const scoped_refptr<VideoFrame>& frame);
105 // Abort pending |read_cb_|.
106 void AbortRead();
108 // Decodes |buffer| and returns the result via OnFrameReady().
109 void Decode(const scoped_refptr<DecoderBuffer>& buffer);
111 // Flushes the decoder with an EOS buffer to retrieve internally buffered
112 // video frames.
113 void FlushDecoder();
115 // Callback for VideoDecoder::Decode().
116 void OnFrameReady(int buffer_size,
117 const VideoDecoder::Status status,
118 const scoped_refptr<VideoFrame>& frame);
120 // Reads a buffer from |stream_| and returns the result via OnBufferReady().
121 void ReadFromDemuxerStream();
123 // Callback for DemuxerStream::Read().
124 void OnBufferReady(DemuxerStream::Status status,
125 const scoped_refptr<DecoderBuffer>& buffer);
127 void ReinitializeDecoder();
129 // Callback for VideoDecoder reinitialization.
130 void OnDecoderReinitialized(PipelineStatus status);
132 void ResetDecoder();
133 void OnDecoderReset();
135 void StopDecoder();
136 void OnDecoderStopped();
138 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
139 base::WeakPtrFactory<VideoFrameStream> weak_factory_;
141 State state_;
143 StatisticsCB statistics_cb_;
144 InitCB init_cb_;
146 ReadCB read_cb_;
147 base::Closure reset_cb_;
148 base::Closure stop_cb_;
150 DemuxerStream* stream_;
152 scoped_ptr<VideoDecoderSelector> decoder_selector_;
154 // These two will be set by VideoDecoderSelector::SelectVideoDecoder().
155 scoped_ptr<VideoDecoder> decoder_;
156 scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream_;
158 DISALLOW_COPY_AND_ASSIGN(VideoFrameStream);
161 } // namespace media
163 #endif // MEDIA_FILTERS_VIDEO_FRAME_STREAM_H_