Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 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_BASE_VIDEO_DECODER_H_
      6 #define MEDIA_BASE_VIDEO_DECODER_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "media/base/media_export.h"
     11 #include "media/base/pipeline_status.h"
     12 #include "ui/gfx/size.h"
     13 
     14 namespace media {
     15 
     16 class DecoderBuffer;
     17 class VideoDecoderConfig;
     18 class VideoFrame;
     19 
     20 class MEDIA_EXPORT VideoDecoder {
     21  public:
     22   // Status codes for decode operations on VideoDecoder.
     23   // TODO(rileya): Now that both AudioDecoder and VideoDecoder Status enums
     24   // match, break them into a decoder_status.h.
     25   enum Status {
     26     kOk,  // Everything went as planned.
     27     kAborted,  // Decode was aborted as a result of Reset() being called.
     28     kDecodeError,  // Decoding error happened.
     29     kDecryptError  // Decrypting error happened.
     30   };
     31 
     32   // Callback for VideoDecoder to return a decoded frame whenever it becomes
     33   // available. Only non-EOS frames should be returned via this callback.
     34   typedef base::Callback<void(const scoped_refptr<VideoFrame>&)> OutputCB;
     35 
     36   // Callback type for Decode(). Called after the decoder has completed decoding
     37   // corresponding DecoderBuffer, indicating that it's ready to accept another
     38   // buffer to decode.
     39   typedef base::Callback<void(Status status)> DecodeCB;
     40 
     41   VideoDecoder();
     42   virtual ~VideoDecoder();
     43 
     44   // Initializes a VideoDecoder with the given |config|, executing the
     45   // |status_cb| upon completion. |output_cb| is called for each output frame
     46   // decoded by Decode().
     47   //
     48   // Note:
     49   // 1) The VideoDecoder will be reinitialized if it was initialized before.
     50   //    Upon reinitialization, all internal buffered frames will be dropped.
     51   // 2) This method should not be called during pending decode, reset or stop.
     52   // 3) No VideoDecoder calls except for Stop() should be made before
     53   //    |status_cb| is executed.
     54   virtual void Initialize(const VideoDecoderConfig& config,
     55                           bool low_delay,
     56                           const PipelineStatusCB& status_cb,
     57                           const OutputCB& output_cb) = 0;
     58 
     59   // Requests a |buffer| to be decoded. The status of the decoder and decoded
     60   // frame are returned via the provided callback. Some decoders may allow
     61   // decoding multiple buffers in parallel. Callers should call
     62   // GetMaxDecodeRequests() to get number of buffers that may be decoded in
     63   // parallel. Decoder must call |decode_cb| in the same order in which Decode()
     64   // is called.
     65   //
     66   // Implementations guarantee that the callback will not be called from within
     67   // this method and that |decode_cb| will not be blocked on the following
     68   // Decode() calls (i.e. |decode_cb| will be called even Decode() is never
     69   // called again).
     70   //
     71   // After decoding is finished the decoder calls |output_cb| specified in
     72   // Initialize() for each decoded frame. |output_cb| may be called before or
     73   // after |decode_cb|.
     74   //
     75   // If |buffer| is an EOS buffer then the decoder must be flushed, i.e.
     76   // |output_cb| must be called for each frame pending in the queue and
     77   // |decode_cb| must be called after that.
     78   virtual void Decode(const scoped_refptr<DecoderBuffer>& buffer,
     79                       const DecodeCB& decode_cb) = 0;
     80 
     81   // Resets decoder state. All pending Decode() requests will be finished or
     82   // aborted before |closure| is called.
     83   // Note: No VideoDecoder calls should be made before |closure| is executed.
     84   virtual void Reset(const base::Closure& closure) = 0;
     85 
     86   // Stops decoder, fires any pending callbacks and sets the decoder to an
     87   // uninitialized state. A VideoDecoder cannot be re-initialized after it has
     88   // been stopped.
     89   // Note that if Initialize() is pending or has finished successfully, Stop()
     90   // must be called before destructing the decoder.
     91   virtual void Stop() = 0;
     92 
     93   // Returns true if the decoder needs bitstream conversion before decoding.
     94   virtual bool NeedsBitstreamConversion() const;
     95 
     96   // Returns true if the decoder currently has the ability to decode and return
     97   // a VideoFrame. Most implementations can allocate a new VideoFrame and hence
     98   // this will always return true. Override and return false for decoders that
     99   // use a fixed set of VideoFrames for decoding.
    100   virtual bool CanReadWithoutStalling() const;
    101 
    102   // Returns maximum number of parallel decode requests.
    103   virtual int GetMaxDecodeRequests() const;
    104 
    105  private:
    106   DISALLOW_COPY_AND_ASSIGN(VideoDecoder);
    107 };
    108 
    109 }  // namespace media
    110 
    111 #endif  // MEDIA_BASE_VIDEO_DECODER_H_
    112