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"
21 class MessageLoopProxy
;
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. VideoRendererBase).
31 class MEDIA_EXPORT VideoFrameStream
{
33 // Indicates completion of VideoFrameStream initialization.
34 typedef base::Callback
<void(bool success
, bool has_alpha
)> InitCB
;
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(const scoped_refptr
<base::MessageLoopProxy
>& message_loop
,
48 ScopedVector
<VideoDecoder
> decoders
,
49 const SetDecryptorReadyCB
& set_decryptor_ready_cb
);
50 virtual ~VideoFrameStream();
52 // Initializes the VideoFrameStream and returns the initialization result
53 // through |init_cb|. Note that |init_cb| is always called asynchronously.
54 void Initialize(DemuxerStream
* stream
,
55 const StatisticsCB
& statistics_cb
,
56 const InitCB
& init_cb
);
58 // Reads a decoded VideoFrame and returns it via the |read_cb|. Note that
59 // |read_cb| is always called asynchronously. This method should only be
60 // called after initialization has succeeded and must not be called during
61 // any pending Reset() and/or Stop().
62 void Read(const ReadCB
& read_cb
);
64 // Resets the decoder, flushes all decoded frames and/or internal buffers,
65 // fires any existing pending read callback and calls |closure| on completion.
66 // Note that |closure| is always called asynchronously. This method should
67 // only be called after initialization has succeeded and must not be called
68 // during any pending Reset() and/or Stop().
69 void Reset(const base::Closure
& closure
);
71 // Stops the decoder, fires any existing pending read callback or reset
72 // callback and calls |closure| on completion. Note that |closure| is always
73 // called asynchronously. The VideoFrameStream cannot be used anymore after
74 // it is stopped. This method can be called at any time but not during another
76 void Stop(const base::Closure
& closure
);
78 // Returns true if the decoder currently has the ability to decode and return
80 bool CanReadWithoutStalling() const;
86 STATE_NORMAL
, // Includes idle, pending decoder decode/reset/stop.
87 STATE_FLUSHING_DECODER
,
88 STATE_PENDING_DEMUXER_READ
,
89 STATE_REINITIALIZING_DECODER
,
94 // Called when |decoder_selector| selected the |selected_decoder|.
95 // |decrypting_demuxer_stream| was also populated if a DecryptingDemuxerStream
96 // is created to help decrypt the encrypted stream.
97 void OnDecoderSelected(
98 scoped_ptr
<VideoDecoder
> selected_decoder
,
99 scoped_ptr
<DecryptingDemuxerStream
> decrypting_demuxer_stream
);
101 // Satisfy pending |read_cb_| with |status| and |frame|.
102 void SatisfyRead(Status status
, const scoped_refptr
<VideoFrame
>& frame
);
104 // Abort pending |read_cb_|.
107 // Decodes |buffer| and returns the result via OnFrameReady().
108 void Decode(const scoped_refptr
<DecoderBuffer
>& buffer
);
110 // Flushes the decoder with an EOS buffer to retrieve internally buffered
114 // Callback for VideoDecoder::Decode().
115 void OnFrameReady(int buffer_size
,
116 const VideoDecoder::Status status
,
117 const scoped_refptr
<VideoFrame
>& frame
);
119 // Reads a buffer from |stream_| and returns the result via OnBufferReady().
120 void ReadFromDemuxerStream();
122 // Callback for DemuxerStream::Read().
123 void OnBufferReady(DemuxerStream::Status status
,
124 const scoped_refptr
<DecoderBuffer
>& buffer
);
126 void ReinitializeDecoder();
128 // Callback for VideoDecoder reinitialization.
129 void OnDecoderReinitialized(PipelineStatus status
);
132 void OnDecoderReset();
135 void OnDecoderStopped();
137 scoped_refptr
<base::MessageLoopProxy
> message_loop_
;
138 base::WeakPtrFactory
<VideoFrameStream
> weak_factory_
;
142 StatisticsCB statistics_cb_
;
146 base::Closure reset_cb_
;
147 base::Closure stop_cb_
;
149 DemuxerStream
* stream_
;
151 scoped_ptr
<VideoDecoderSelector
> decoder_selector_
;
153 // These two will be set by VideoDecoderSelector::SelectVideoDecoder().
154 scoped_ptr
<VideoDecoder
> decoder_
;
155 scoped_ptr
<DecryptingDemuxerStream
> decrypting_demuxer_stream_
;
157 DISALLOW_COPY_AND_ASSIGN(VideoFrameStream
);
162 #endif // MEDIA_FILTERS_VIDEO_FRAME_STREAM_H_