Home | History | Annotate | Download | only in android
      1 // Copyright 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_BASE_ANDROID_MEDIA_DECODER_JOB_H_
      6 #define MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/memory/weak_ptr.h"
     10 #include "base/time/time.h"
     11 #include "media/base/android/demuxer_stream_player_params.h"
     12 #include "media/base/android/media_codec_bridge.h"
     13 #include "ui/gl/android/scoped_java_surface.h"
     14 
     15 namespace base {
     16 class SingleThreadTaskRunner;
     17 }
     18 
     19 namespace media {
     20 
     21 class MediaDrmBridge;
     22 
     23 // Class for managing all the decoding tasks. Each decoding task will be posted
     24 // onto the same thread. The thread will be stopped once Stop() is called.
     25 // Data is stored in 2 chunks. When new data arrives, it is always stored in
     26 // an inactive chunk. And when the current active chunk becomes empty, a new
     27 // data request will be sent to the renderer.
     28 class MediaDecoderJob {
     29  public:
     30   struct Deleter {
     31     inline void operator()(MediaDecoderJob* ptr) const { ptr->Release(); }
     32   };
     33 
     34   // Callback when a decoder job finishes its work. Args: whether decode
     35   // finished successfully, current presentation time, max presentation time.
     36   // If the current presentation time is equal to kNoTimestamp(), the decoder
     37   // job skipped rendering of the decoded output and the callback target should
     38   // ignore the timestamps provided.
     39   typedef base::Callback<void(MediaCodecStatus, base::TimeDelta,
     40                               base::TimeDelta)> DecoderCallback;
     41   // Callback when a decoder job finishes releasing the output buffer.
     42   // Args: current presentation time, max presentation time.
     43   // If the current presentation time is equal to kNoTimestamp(), the callback
     44   // target should ignore the timestamps provided.
     45   typedef base::Callback<void(base::TimeDelta, base::TimeDelta)>
     46       ReleaseOutputCompletionCallback;
     47 
     48   virtual ~MediaDecoderJob();
     49 
     50   // Called by MediaSourcePlayer when more data for this object has arrived.
     51   void OnDataReceived(const DemuxerData& data);
     52 
     53   // Prefetch so we know the decoder job has data when we call Decode().
     54   // |prefetch_cb| - Run when prefetching has completed.
     55   void Prefetch(const base::Closure& prefetch_cb);
     56 
     57   // Called by MediaSourcePlayer to decode some data.
     58   // |callback| - Run when decode operation has completed.
     59   //
     60   // Returns true if the next decode was started and |callback| will be
     61   // called when the decode operation is complete.
     62   // Returns false if |media_codec_bridge_| cannot be created; |callback| is
     63   // ignored and will not be called.
     64   bool Decode(base::TimeTicks start_time_ticks,
     65               base::TimeDelta start_presentation_timestamp,
     66               const DecoderCallback& callback);
     67 
     68   // Called to stop the last Decode() early.
     69   // If the decoder is in the process of decoding the next frame, then
     70   // this method will just allow the decode to complete as normal. If
     71   // this object is waiting for a data request to complete, then this method
     72   // will wait for the data to arrive and then call the |callback|
     73   // passed to Decode() with a status of MEDIA_CODEC_STOPPED. This ensures that
     74   // the |callback| passed to Decode() is always called and the status
     75   // reflects whether data was actually decoded or the decode terminated early.
     76   void StopDecode();
     77 
     78   // Flushes the decoder and abandons all the data that is being decoded.
     79   virtual void Flush();
     80 
     81   // Enters prerolling state. The job must not currently be decoding.
     82   void BeginPrerolling(base::TimeDelta preroll_timestamp);
     83 
     84   // Releases all the decoder resources as the current tab is going background.
     85   virtual void ReleaseDecoderResources();
     86 
     87   // Sets the demuxer configs.
     88   virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0;
     89 
     90   // Returns whether the decoder has finished decoding all the data.
     91   bool OutputEOSReached() const;
     92 
     93   // Returns true if the audio/video stream is available, implemented by child
     94   // classes.
     95   virtual bool HasStream() const = 0;
     96 
     97   void SetDrmBridge(MediaDrmBridge* drm_bridge);
     98 
     99   bool is_decoding() const { return !decode_cb_.is_null(); }
    100 
    101   bool is_content_encrypted() const { return is_content_encrypted_; }
    102 
    103   bool prerolling() const { return prerolling_; }
    104 
    105  protected:
    106   // Creates a new MediaDecoderJob instance.
    107   // |decoder_task_runner| - Thread on which the decoder task will run.
    108   // |request_data_cb| - Callback to request more data for the decoder.
    109   // |config_changed_cb| - Callback to inform the caller that
    110   // demuxer config has changed.
    111   MediaDecoderJob(
    112       const scoped_refptr<base::SingleThreadTaskRunner>& decoder_task_runner,
    113       const base::Closure& request_data_cb,
    114       const base::Closure& config_changed_cb);
    115 
    116   // Release the output buffer at index |output_buffer_index| and render it if
    117   // |render_output| is true. Upon completion, |callback| will be called.
    118   virtual void ReleaseOutputBuffer(
    119       int output_buffer_index,
    120       size_t size,
    121       bool render_output,
    122       base::TimeDelta current_presentation_timestamp,
    123       const ReleaseOutputCompletionCallback& callback) = 0;
    124 
    125   // Returns true if the "time to render" needs to be computed for frames in
    126   // this decoder job.
    127   virtual bool ComputeTimeToRender() const = 0;
    128 
    129   // Gets MediaCrypto object from |drm_bridge_|.
    130   base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto();
    131 
    132   // Releases the |media_codec_bridge_|.
    133   void ReleaseMediaCodecBridge();
    134 
    135   MediaDrmBridge* drm_bridge() { return drm_bridge_; }
    136 
    137   void set_is_content_encrypted(bool is_content_encrypted) {
    138     is_content_encrypted_ = is_content_encrypted;
    139   }
    140 
    141   bool need_to_reconfig_decoder_job_;
    142 
    143   scoped_ptr<MediaCodecBridge> media_codec_bridge_;
    144 
    145  private:
    146   friend class MediaSourcePlayerTest;
    147 
    148   // Causes this instance to be deleted on the thread it is bound to.
    149   void Release();
    150 
    151   // Queues an access unit into |media_codec_bridge_|'s input buffer.
    152   MediaCodecStatus QueueInputBuffer(const AccessUnit& unit);
    153 
    154   // Returns true if this object has data to decode.
    155   bool HasData() const;
    156 
    157   // Initiates a request for more data.
    158   // |done_cb| is called when more data is available in |received_data_|.
    159   void RequestData(const base::Closure& done_cb);
    160 
    161   // Posts a task to start decoding the current access unit in |received_data_|.
    162   void DecodeCurrentAccessUnit(
    163       base::TimeTicks start_time_ticks,
    164       base::TimeDelta start_presentation_timestamp);
    165 
    166   // Helper function to decode data on |decoder_task_runner_|. |unit| contains
    167   // the data to be decoded. |start_time_ticks| and
    168   // |start_presentation_timestamp| represent the system time and the
    169   // presentation timestamp when the first frame is rendered. We use these
    170   // information to estimate when the current frame should be rendered.
    171   // If |needs_flush| is true, codec needs to be flushed at the beginning of
    172   // this call.
    173   // It is possible that |stop_decode_pending_| or |release_resources_pending_|
    174   // becomes true while DecodeInternal() is called. However, they should have
    175   // no impact on DecodeInternal(). They will be handled after DecoderInternal()
    176   // finishes and OnDecodeCompleted() is posted on the UI thread.
    177   void DecodeInternal(const AccessUnit& unit,
    178                       base::TimeTicks start_time_ticks,
    179                       base::TimeDelta start_presentation_timestamp,
    180                       bool needs_flush,
    181                       const DecoderCallback& callback);
    182 
    183   // Called on the UI thread to indicate that one decode cycle has completed.
    184   // Completes any pending job destruction or any pending decode stop. If
    185   // destruction was not pending, passes its arguments to |decode_cb_|.
    186   void OnDecodeCompleted(MediaCodecStatus status,
    187                          base::TimeDelta current_presentation_timestamp,
    188                          base::TimeDelta max_presentation_timestamp);
    189 
    190   // Helper function to get the current access unit that is being decoded.
    191   const AccessUnit& CurrentAccessUnit() const;
    192 
    193   // Helper function to get the current data chunk index that is being decoded.
    194   size_t CurrentReceivedDataChunkIndex() const;
    195 
    196   // Check whether a chunk has no remaining access units to decode. If
    197   // |is_active_chunk| is true, this function returns whether decoder has
    198   // consumed all data in |received_data_[current_demuxer_data_index_]|.
    199   // Otherwise, it returns whether decoder has consumed all data in the inactive
    200   // chunk.
    201   bool NoAccessUnitsRemainingInChunk(bool is_active_chunk) const;
    202 
    203   // Requests new data for the current chunk if it runs out of data.
    204   void RequestCurrentChunkIfEmpty();
    205 
    206   // Initializes |received_data_| and |access_unit_index_|.
    207   void InitializeReceivedData();
    208 
    209   // Called when the decoder is completely drained and is ready to be released.
    210   void OnDecoderDrained();
    211 
    212   // Creates |media_codec_bridge_| for decoding purpose. Returns true if it is
    213   // created, or false otherwise.
    214   bool CreateMediaCodecBridge();
    215 
    216   // Called when an access unit is consumed by the decoder. |is_config_change|
    217   // indicates whether the current access unit is a config change. If it is
    218   // true, the next access unit is guarateed to be an I-frame.
    219   virtual void CurrentDataConsumed(bool is_config_change) {}
    220 
    221   // Implemented by the child class to create |media_codec_bridge_| for a
    222   // particular stream. Returns true if it is created, or false otherwise.
    223   virtual bool CreateMediaCodecBridgeInternal() = 0;
    224 
    225   // Returns true if the |configs| doesn't match the current demuxer configs
    226   // the decoder job has.
    227   virtual bool AreDemuxerConfigsChanged(
    228       const DemuxerConfigs& configs) const = 0;
    229 
    230   // Returns true if |media_codec_bridge_| needs to be reconfigured for the
    231   // new DemuxerConfigs, or false otherwise.
    232   virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& configs) const;
    233 
    234   // Update the output format from the decoder, returns true if the output
    235   // format changes, or false otherwise.
    236   virtual bool UpdateOutputFormat();
    237 
    238   // Return the index to |received_data_| that is not currently being decoded.
    239   size_t inactive_demuxer_data_index() const {
    240     return 1 - current_demuxer_data_index_;
    241   }
    242 
    243   // The UI message loop where callbacks should be dispatched.
    244   scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
    245 
    246   // The task runner that decoder job runs on.
    247   scoped_refptr<base::SingleThreadTaskRunner> decoder_task_runner_;
    248 
    249   // Whether the decoder needs to be flushed.
    250   bool needs_flush_;
    251 
    252   // Whether input EOS is encountered.
    253   // TODO(wolenetz/qinmin): Protect with a lock. See http://crbug.com/320043.
    254   bool input_eos_encountered_;
    255 
    256   // Whether output EOS is encountered.
    257   bool output_eos_encountered_;
    258 
    259   // Tracks whether DecodeInternal() should skip decoding if the first access
    260   // unit is EOS or empty, and report |MEDIA_CODEC_OUTPUT_END_OF_STREAM|. This
    261   // is to work around some decoders that could crash otherwise. See
    262   // http://b/11696552.
    263   bool skip_eos_enqueue_;
    264 
    265   // The timestamp the decoder needs to preroll to. If an access unit's
    266   // timestamp is smaller than |preroll_timestamp_|, don't render it.
    267   // TODO(qinmin): Comparing access unit's timestamp with |preroll_timestamp_|
    268   // is not very accurate.
    269   base::TimeDelta preroll_timestamp_;
    270 
    271   // Indicates prerolling state. If true, this job has not yet decoded output
    272   // that it will render, since the most recent of job construction or
    273   // BeginPrerolling(). If false, |preroll_timestamp_| has been reached.
    274   // TODO(qinmin): Comparing access unit's timestamp with |preroll_timestamp_|
    275   // is not very accurate.
    276   bool prerolling_;
    277 
    278   // Callback used to request more data.
    279   base::Closure request_data_cb_;
    280 
    281   // Callback to notify the caller config has changed.
    282   base::Closure config_changed_cb_;
    283 
    284   // Callback to run when new data has been received.
    285   base::Closure data_received_cb_;
    286 
    287   // Callback to run when the current Decode() operation completes.
    288   DecoderCallback decode_cb_;
    289 
    290   // Data received over IPC from last RequestData() operation.
    291   // We keep 2 chunks at the same time to reduce the IPC latency between chunks.
    292   // If data inside the current chunk are all decoded, we will request a new
    293   // chunk from the demuxer and swap the current chunk with the other one.
    294   // New data will always be stored in the other chunk since the current
    295   // one may be still in use.
    296   DemuxerData received_data_[2];
    297 
    298   // Index to the current data chunk that is being decoded.
    299   size_t current_demuxer_data_index_;
    300 
    301   // Index to the access unit inside each data chunk that is being decoded.
    302   size_t access_unit_index_[2];
    303 
    304   // The index of input buffer that can be used by QueueInputBuffer().
    305   // If the index is uninitialized or invalid, it must be -1.
    306   int input_buf_index_;
    307 
    308   // Indicates whether content is encrypted.
    309   bool is_content_encrypted_;
    310 
    311   // Indicates the decoder job should stop after decoding the current access
    312   // unit.
    313   bool stop_decode_pending_;
    314 
    315   // Indicates that this object should be destroyed once the current
    316   // Decode() has completed. This gets set when Release() gets called
    317   // while there is a decode in progress.
    318   bool destroy_pending_;
    319 
    320   // Indicates whether the decoder is in the middle of requesting new data.
    321   bool is_requesting_demuxer_data_;
    322 
    323   // Indicates whether the incoming data should be ignored.
    324   bool is_incoming_data_invalid_;
    325 
    326   // Indicates that |media_codec_bridge_| should be released once the current
    327   // Decode() has completed. This gets set when ReleaseDecoderResources() gets
    328   // called while there is a decode in progress.
    329   bool release_resources_pending_;
    330 
    331   // Pointer to a DRM object that will be used for encrypted streams.
    332   MediaDrmBridge* drm_bridge_;
    333 
    334   // Indicates whether |media_codec_bridge_| is in the middle of being drained
    335   // due to a config change.
    336   bool drain_decoder_;
    337 
    338   // This access unit is passed to the decoder during config changes to drain
    339   // the decoder.
    340   AccessUnit eos_unit_;
    341 
    342   DISALLOW_IMPLICIT_CONSTRUCTORS(MediaDecoderJob);
    343 };
    344 
    345 }  // namespace media
    346 
    347 #endif  // MEDIA_BASE_ANDROID_MEDIA_DECODER_JOB_H_
    348