OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_CODEC_DECODER_H_ |
| 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
| 7 |
| 8 #include "base/android/scoped_java_ref.h" |
| 9 #include "base/callback.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 14 #include "base/synchronization/lock.h" |
| 15 #include "base/threading/thread.h" |
| 16 #include "base/time/time.h" |
| 17 #include "media/base/android/access_unit_queue.h" |
| 18 #include "media/base/android/demuxer_stream_player_params.h" |
| 19 |
| 20 namespace media { |
| 21 |
| 22 class MediaCodecBridge; |
| 23 |
| 24 // The decoder for MediaCodecPlayer. |
| 25 // This class accepts the incoming data into AccessUnitQueue |
| 26 // and works with MediaCodecBridge for decoding and rendering |
| 27 // the frames. The MediaCodecPlayer has two decoder objects: |
| 28 // audio and video. |
| 29 // |
| 30 // The decoder works on two threads. The data from demuxer comes |
| 31 // on Media thread. The commands from MediaCodecPlayer, such as |
| 32 // Prefetch, Start, RequestToStop also come on the Media thread. |
| 33 // However, the operations with MediaCodec buffers and rendering |
| 34 // happen on a separate thread called Decoder thread. |
| 35 // This class creates, starts and stops it as necessary. |
| 36 class MediaCodecDecoder { |
| 37 public: |
| 38 // The result of MediaCodec configuration, used by MediaCodecPlayer. |
| 39 enum ConfigStatus { |
| 40 CONFIG_FAILURE = 0, |
| 41 CONFIG_OK, |
| 42 CONFIG_KEY_FRAME_REQUIRED, |
| 43 }; |
| 44 |
| 45 // The decoder reports current playback time to the MediaCodecPlayer. |
| 46 typedef base::Callback<void(base::TimeDelta, base::TimeDelta)> |
| 47 SetTimeCallback; |
| 48 |
| 49 MediaCodecDecoder( |
| 50 const scoped_refptr<base::SingleThreadTaskRunner>& media_task_runner, |
| 51 const base::Closure& request_data_cb, |
| 52 const base::Closure& starvation_cb, |
| 53 const base::Closure& stop_done_cb, |
| 54 const base::Closure& error_cb, |
| 55 const char* decoder_thread_name); |
| 56 virtual ~MediaCodecDecoder(); |
| 57 |
| 58 virtual const char* class_name() const { return "Decoder"; } |
| 59 |
| 60 // MediaCodecDecoder exists through the whole lifetime of the player |
| 61 // to support dynamic addition and removal of the streams. |
| 62 // This method returns true if the current stream (audio or video) |
| 63 // is currently active. |
| 64 virtual bool HasStream() const = 0; |
| 65 |
| 66 // Stores configuration for the use of upcoming Configure() |
| 67 virtual void SetDemuxerConfigs(const DemuxerConfigs& configs) = 0; |
| 68 |
| 69 // Stops decoder thread, releases the MediaCodecBridge and other resources. |
| 70 virtual void ReleaseDecoderResources(); |
| 71 |
| 72 // Flushes the MediaCodec and resets the AccessUnitQueue. |
| 73 // Decoder thread should not be running. |
| 74 virtual void Flush(); |
| 75 |
| 76 // Releases MediaCodecBridge. |
| 77 void ReleaseMediaCodec(); |
| 78 |
| 79 // Returns corresponding conditions. |
| 80 bool IsPrefetchingOrPlaying() const; |
| 81 bool IsStopped() const; |
| 82 bool IsCompleted() const; |
| 83 |
| 84 base::android::ScopedJavaLocalRef<jobject> GetMediaCrypto(); |
| 85 |
| 86 // Starts prefetching: accumulates enough data in AccessUnitQueue. |
| 87 // Decoder thread is not running. |
| 88 void Prefetch(const base::Closure& prefetch_done_cb); |
| 89 |
| 90 // Configures MediaCodec. |
| 91 ConfigStatus Configure(); |
| 92 |
| 93 // Starts the decoder thread and resumes the playback. |
| 94 bool Start(base::TimeDelta current_time); |
| 95 |
| 96 // Stops the playback process synchronously. |
| 97 // This method stops the decoder thread synchronously, |
| 98 // and then releases all MediaCodec buffers. |
| 99 void SyncStop(); |
| 100 |
| 101 // Requests to stop the playback and returns. |
| 102 // Decoder will stop asynchronously after all the dequeued output buffers |
| 103 // are rendered. |
| 104 void RequestToStop(); |
| 105 |
| 106 // Notification posted when asynchronous stop is done |
| 107 // or the playback is completed. |
| 108 void OnLastFrameRendered(bool completed); |
| 109 |
| 110 // Puts the incoming data into AccessUnitQueue. |
| 111 void OnDemuxerDataAvailable(const DemuxerData& data); |
| 112 |
| 113 protected: |
| 114 // Returns true if the new DemuxerConfigs requires MediaCodec |
| 115 // reconfiguration. |
| 116 virtual bool IsCodecReconfigureNeeded(const DemuxerConfigs& curr, |
| 117 const DemuxerConfigs& next) const = 0; |
| 118 |
| 119 // Does the part of MediaCodecBridge configuration that is specific |
| 120 // to audio or video. |
| 121 virtual ConfigStatus ConfigureInternal() = 0; |
| 122 |
| 123 // Associates PTS with device time so we can calculate delays. |
| 124 // We use delays for video decoder only. |
| 125 virtual void SynchronizePTSWithTime(base::TimeDelta current_time) {} |
| 126 |
| 127 // Processes the change of the output format, varies by stream. |
| 128 virtual void OnOutputFormatChanged() = 0; |
| 129 |
| 130 // Renders the decoded frame and releases output buffer, or posts |
| 131 // a delayed task to do it at a later time, |
| 132 virtual void Render(int buffer_index, size_t size, bool render_output, |
| 133 base::TimeDelta pts, bool eos_encountered) = 0; |
| 134 |
| 135 // Returns the number of delayed task (we might have them for video). |
| 136 virtual int NumDelayedRenderTasks() const { return 0; } |
| 137 |
| 138 // Releases output buffers that are dequeued and not released yet |
| 139 // because their rendering is delayed (video). |
| 140 virtual void ReleaseDelayedBuffers() {} |
| 141 |
| 142 // Helper methods. |
| 143 |
| 144 // Notifies the decoder if the frame is the last one. |
| 145 void CheckLastFrame(bool eos_encountered, bool has_delayed_tasks); |
| 146 |
| 147 // Protected data. |
| 148 |
| 149 // Object for posting tasks on Media thread. |
| 150 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_; |
| 151 |
| 152 // Controls Android MediaCodec |
| 153 scoped_ptr<MediaCodecBridge> media_codec_bridge_; |
| 154 |
| 155 // We call MediaCodecBridge on this thread for both |
| 156 // input and output buffers. |
| 157 base::Thread decoder_thread_; |
| 158 |
| 159 // The queue of access units. |
| 160 AccessUnitQueue au_queue_; |
| 161 |
| 162 private: |
| 163 enum DecoderState { |
| 164 STOPPED = 0, |
| 165 PREFETCHING, |
| 166 RUNNING, |
| 167 STOPPING |
| 168 }; |
| 169 |
| 170 // Prefetching callback that is posted to Media thread |
| 171 // in the PREFETCHING state. |
| 172 void PrefetchNextChunk(); |
| 173 |
| 174 // The callback to do actual playback. Posted to Decoder thread |
| 175 // in the RUNNING mode |
| 176 void ProcessNextFrame(); |
| 177 |
| 178 // Helper method for ProcessNextFrame. |
| 179 // Pushes one input buffer to the MediaCodec if the codec can accept it. |
| 180 // Returns false if there was MediaCodec error. |
| 181 bool EnqueueInputBuffer(); |
| 182 |
| 183 // Helper method for ProcessNextFrame. |
| 184 // Pulls all currently available output frames and renders them. |
| 185 // Returns false if there was MediaCodec error. |
| 186 bool DepleteOutputBufferQueue(bool* eos_encountered); |
| 187 |
| 188 DecoderState GetState() const; |
| 189 void SetState(DecoderState state); |
| 190 |
| 191 // Private Data. |
| 192 |
| 193 // Callback used to request more data. |
| 194 base::Closure request_data_cb_; |
| 195 |
| 196 // These notifications are called on corresponding conditions. |
| 197 base::Closure prefetch_done_cb_; |
| 198 base::Closure starvation_cb_; |
| 199 base::Closure stop_done_cb_; |
| 200 base::Closure error_cb_; |
| 201 |
| 202 DecoderState state_; |
| 203 mutable base::Lock state_lock_; |
| 204 |
| 205 // Flag is set when the EOS is enqueued into MediaCodec. |
| 206 bool eos_enqueued_; |
| 207 |
| 208 // Flag is set when the EOS is received in MediaCodec output. |
| 209 bool completed_; |
| 210 |
| 211 // Flag to ensure we post last frame notification once. |
| 212 bool last_frame_posted_; |
| 213 |
| 214 base::WeakPtr<MediaCodecDecoder> weak_this_; |
| 215 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 216 base::WeakPtrFactory<MediaCodecDecoder> weak_factory_; |
| 217 |
| 218 DISALLOW_COPY_AND_ASSIGN(MediaCodecDecoder); |
| 219 }; |
| 220 |
| 221 } // namespace media |
| 222 |
| 223 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_DECODER_H_ |
OLD | NEW |