Home | History | Annotate | Download | only in filters
      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.
      4 
      5 #ifndef MEDIA_FILTERS_VIDEO_FRAME_STREAM_H_
      6 #define MEDIA_FILTERS_VIDEO_FRAME_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/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"
     19 
     20 namespace base {
     21 class MessageLoopProxy;
     22 }
     23 
     24 namespace media {
     25 
     26 class DecryptingDemuxerStream;
     27 class VideoDecoderSelector;
     28 
     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 {
     32  public:
     33   // Indicates completion of VideoFrameStream initialization.
     34   typedef base::Callback<void(bool success, bool has_alpha)> InitCB;
     35 
     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.
     42   };
     43 
     44   // Indicates completion of a VideoFrameStream read.
     45   typedef base::Callback<void(Status, const scoped_refptr<VideoFrame>&)> ReadCB;
     46 
     47   VideoFrameStream(const scoped_refptr<base::MessageLoopProxy>& message_loop,
     48                    ScopedVector<VideoDecoder> decoders,
     49                    const SetDecryptorReadyCB& set_decryptor_ready_cb);
     50   virtual ~VideoFrameStream();
     51 
     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);
     57 
     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);
     63 
     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);
     70 
     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
     75   // pending Stop().
     76   void Stop(const base::Closure& closure);
     77 
     78   // Returns true if the decoder currently has the ability to decode and return
     79   // a VideoFrame.
     80   bool CanReadWithoutStalling() const;
     81 
     82  private:
     83   enum State {
     84     STATE_UNINITIALIZED,
     85     STATE_INITIALIZING,
     86     STATE_NORMAL,  // Includes idle, pending decoder decode/reset/stop.
     87     STATE_FLUSHING_DECODER,
     88     STATE_PENDING_DEMUXER_READ,
     89     STATE_REINITIALIZING_DECODER,
     90     STATE_STOPPED,
     91     STATE_ERROR
     92   };
     93 
     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);
    100 
    101   // Satisfy pending |read_cb_| with |status| and |frame|.
    102   void SatisfyRead(Status status, const scoped_refptr<VideoFrame>& frame);
    103 
    104   // Abort pending |read_cb_|.
    105   void AbortRead();
    106 
    107   // Decodes |buffer| and returns the result via OnFrameReady().
    108   void Decode(const scoped_refptr<DecoderBuffer>& buffer);
    109 
    110   // Flushes the decoder with an EOS buffer to retrieve internally buffered
    111   // video frames.
    112   void FlushDecoder();
    113 
    114   // Callback for VideoDecoder::Decode().
    115   void OnFrameReady(int buffer_size,
    116                     const VideoDecoder::Status status,
    117                     const scoped_refptr<VideoFrame>& frame);
    118 
    119   // Reads a buffer from |stream_| and returns the result via OnBufferReady().
    120   void ReadFromDemuxerStream();
    121 
    122   // Callback for DemuxerStream::Read().
    123   void OnBufferReady(DemuxerStream::Status status,
    124                      const scoped_refptr<DecoderBuffer>& buffer);
    125 
    126   void ReinitializeDecoder();
    127 
    128   // Callback for VideoDecoder reinitialization.
    129   void OnDecoderReinitialized(PipelineStatus status);
    130 
    131   void ResetDecoder();
    132   void OnDecoderReset();
    133 
    134   void StopDecoder();
    135   void OnDecoderStopped();
    136 
    137   scoped_refptr<base::MessageLoopProxy> message_loop_;
    138   base::WeakPtrFactory<VideoFrameStream> weak_factory_;
    139   base::WeakPtr<VideoFrameStream> weak_this_;
    140 
    141   State state_;
    142 
    143   StatisticsCB statistics_cb_;
    144   InitCB init_cb_;
    145 
    146   ReadCB read_cb_;
    147   base::Closure reset_cb_;
    148   base::Closure stop_cb_;
    149 
    150   DemuxerStream* stream_;
    151 
    152   scoped_ptr<VideoDecoderSelector> decoder_selector_;
    153 
    154   // These two will be set by VideoDecoderSelector::SelectVideoDecoder().
    155   scoped_ptr<VideoDecoder> decoder_;
    156   scoped_ptr<DecryptingDemuxerStream> decrypting_demuxer_stream_;
    157 
    158   DISALLOW_COPY_AND_ASSIGN(VideoFrameStream);
    159 };
    160 
    161 }  // namespace media
    162 
    163 #endif  // MEDIA_FILTERS_VIDEO_FRAME_STREAM_H_
    164