Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: media/base/android/media_codec_player.h

Issue 1128383003: Implementation of MediaCodecPlayer stage 1 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: AccessUnitQueue changed to list of chunks, threading cleanup for decoders Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ 5 #ifndef MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ 6 #define MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
7 7
8 #include "base/android/scoped_java_ref.h" 8 #include "base/android/scoped_java_ref.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
11 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
12 #include "base/time/default_tick_clock.h"
12 #include "media/base/android/demuxer_android.h" 13 #include "media/base/android/demuxer_android.h"
13 #include "media/base/android/media_player_android.h" 14 #include "media/base/android/media_player_android.h"
15 #include "media/base/demuxer_stream.h"
14 #include "media/base/media_export.h" 16 #include "media/base/media_export.h"
17 #include "media/base/time_delta_interpolator.h"
18 #include "ui/gfx/geometry/size.h"
15 #include "ui/gl/android/scoped_java_surface.h" 19 #include "ui/gl/android/scoped_java_surface.h"
16 20
21 // The MediaCodecPlayer class implements the media player by using Android's
22 // MediaCodec. It differs from MediaSourcePlayer in that it removes most
23 // processing away from the UI thread: it uses a dedicated Media thread to
24 // receive the data and to handle the commands.
25
26 // The player works as a state machine. Here are relationships between states:
27 //
28 // [ Paused ] ------------------------ (Any state)
29 // | | |
30 // | v v
31 // | <------------------[ WaitingForConfig ] [ Error ]
32 // |
33 // |
34 // | <----------------------------------------------
35 // v |
36 // [ Prefetching ] ------------------- |
37 // | | |
38 // | v |
39 // | <-----------------[ WaitingForSurface ] |
40 // v |
41 // [ Playing ] |
42 // | |
43 // | |
44 // v |
45 // [ Stopping ] --------------------------------> [ WaitingForSeek ]
46 //
47 //
48
49 /*
50 This is the diagram for the pause/resume workflow with states and actions.
51
52
53 ------------------------> [ Paused ]
54 | StopDone:
55 | ^ |
56 | | |
57 | Pause: | | Start: dec.Prefetch
58 | | |
59 | | |
60 | | |
61 | | v PrefetchDone,
62 | no surface:
63 | ------------------> [ Prefetching ] --------------> [ Waiting ]
64 | | [for surface]
65 | | | /
66 | | | /
67 | | StopDone w/ | /
68 | | pending start: | PrefetchDone: /
69 | | dec.Prefetch | dec.Start /
70 | | | / SetSurface:
71 | | | / dec.Start
72 | | | /
73 | | v /
74 | | /
75 | | [ Playing ] <----------/
76 | |
77 | | |
78 | | |
79 | | | Pause: dec.RequestToStop
80 | | |
81 | | |
82 | | v
83 | |
84 --------------------- --- [ Stopping ]
85
86 */
87
17 namespace media { 88 namespace media {
18 89
19 class BrowserCdm; 90 class BrowserCdm;
91 class MediaCodecAudioDecoder;
92 class MediaCodecVideoDecoder;
20 93
21 // Returns the task runner for the media thread 94 // Returns the task runner for the media thread
22 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); 95 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner();
23 96
24
25 // This class implements the media player using Android's MediaCodec.
26 // It differs from MediaSourcePlayer in that it removes most
27 // processing away from UI thread: it uses a dedicated Media thread
28 // to receive the data and to handle commands.
29 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid, 97 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid,
30 public DemuxerAndroidClient { 98 public DemuxerAndroidClient {
31 public: 99 public:
100
101 // Typedefs for the notification callbacks
102 typedef base::Callback<
103 void (base::TimeDelta duration, const gfx::Size& video_size)>
104 MetadataChangedCallback;
105
106 typedef base::Callback<
107 void (base::TimeDelta current_timestamp, base::TimeTicks current_ticks)>
108 TimeUpdateCallback;
109
32 // Constructs a player with the given ID and demuxer. |manager| must outlive 110 // Constructs a player with the given ID and demuxer. |manager| must outlive
33 // the lifetime of this object. 111 // the lifetime of this object.
34 MediaCodecPlayer(int player_id, 112 MediaCodecPlayer(int player_id,
35 MediaPlayerManager* manager, 113 base::WeakPtr<MediaPlayerManager> manager,
36 const RequestMediaResourcesCB& request_media_resources_cb, 114 const RequestMediaResourcesCB& request_media_resources_cb,
37 scoped_ptr<DemuxerAndroid> demuxer, 115 scoped_ptr<DemuxerAndroid> demuxer,
38 const GURL& frame_url); 116 const GURL& frame_url);
39 ~MediaCodecPlayer() override; 117 ~MediaCodecPlayer() override;
40 118
41 // MediaPlayerAndroid implementation. 119 // MediaPlayerAndroid implementation.
42 void DeleteOnCorrectThread() override; 120 void DeleteOnCorrectThread() override;
43 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; 121 void SetVideoSurface(gfx::ScopedJavaSurface surface) override;
44 void Start() override; 122 void Start() override;
45 void Pause(bool is_media_related_action) override; 123 void Pause(bool is_media_related_action) override;
(...skipping 12 matching lines...) Expand all
58 void SetCdm(BrowserCdm* cdm) override; 136 void SetCdm(BrowserCdm* cdm) override;
59 137
60 // DemuxerAndroidClient implementation. 138 // DemuxerAndroidClient implementation.
61 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; 139 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override;
62 void OnDemuxerDataAvailable(const DemuxerData& params) override; 140 void OnDemuxerDataAvailable(const DemuxerData& params) override;
63 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; 141 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override;
64 void OnDemuxerDurationChanged(base::TimeDelta duration) override; 142 void OnDemuxerDurationChanged(base::TimeDelta duration) override;
65 143
66 // Helper methods 144 // Helper methods
67 void Initialize(); 145 void Initialize();
68 void DestroySelf(); 146
147 base::WeakPtr<MediaCodecPlayer> weak_ptr() const { return weak_this_; }
148
149 protected:
150 // MediaPlayerAndroid implementation.
151 // This method caches the data and calls manager's OnMediaMetadataChanged().
152 void OnMediaMetadataChanged(
153 base::TimeDelta duration, const gfx::Size& video_size) override;
154
155 // This method cached the current time and calls manager's OnTimeUpdate().
156 void OnTimeUpdate(base::TimeDelta current_timestamp,
157 base::TimeTicks current_time_ticks) override;
69 158
70 private: 159 private:
160 // The state machine states.
161 enum PlayerState {
162 STATE_PAUSED,
163 STATE_WAITING_FOR_CONFIG,
164 STATE_PREFETCHING,
165 STATE_PLAYING,
166 STATE_STOPPING,
167 STATE_WAITING_FOR_SURFACE,
168 STATE_WAITING_FOR_SEEK,
169 STATE_ERROR,
170 };
171
172 // Cached valued for the manager, accessed on the UI thread.
173 struct MediaMetadata {
174 base::TimeDelta duration;
175 gfx::Size video_size;
176 // Have some defined aspect ratio by default.
177 MediaMetadata() : duration(), video_size(320, 240) {}
178 };
179
180 // Callbacks from decoders
181 void RequestDemuxerData(DemuxerStream::Type stream_type);
182 void OnPrefetchDone();
183 void OnStarvation();
184 void OnStopDone();
185 void OnError();
186 void OnTimeIntervalUpdate(base::TimeDelta now_playing,
187 base::TimeDelta last_buffered);
188
189 // Callbacks from video decoder
190 void OnVideoCodecCreated();
191 void OnVideoSizeChanged(const gfx::Size& size);
192
193 // Operations called from the state machine.
194 void SetState(PlayerState new_state);
195 void SetPendingSurface(gfx::ScopedJavaSurface surface);
196 bool HasPendingSurface();
197 void SetPendingStart(bool need_to_start);
198 bool HasPendingStart();
199 bool HasVideo();
200 bool HasAudio();
201 void SetDemuxerConfigs(const DemuxerConfigs& configs);
202 void StartPrefetchDecoders();
203 void StartPlaybackDecoders();
204 void StopDecoders();
205 void RequestToStopDecoders();
206 void ReleaseDecoderResources();
207
208 // Helper methods.
209 void CreateDecoders();
210 bool AudioFinished();
211 bool VideoFinished();
212 base::TimeDelta GetInterpolatedTime();
213
214 static const char* AsString(PlayerState state);
215
216 // Data.
217
71 // Object for posting tasks on UI thread. 218 // Object for posting tasks on UI thread.
72 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 219 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
73 220
221 // Major components: demuxer, audio and video decoders.
74 scoped_ptr<DemuxerAndroid> demuxer_; 222 scoped_ptr<DemuxerAndroid> demuxer_;
223 scoped_ptr<MediaCodecAudioDecoder> audio_decoder_;
224 scoped_ptr<MediaCodecVideoDecoder> video_decoder_;
225
226 // The state of the state machine.
227 PlayerState state_;
228
229 // Notification callbacks, they call MediaPlayerManager.
230 base::Closure request_resources_cb_;
231 TimeUpdateCallback time_update_cb_;
232 base::Closure completion_cb_;
233
234 // A callback that updates metadata cache and calls the manager.
235 MetadataChangedCallback metadata_changed_cb_;
236
237 // We call the base class' AttachListener() and DetachListener() methods on UI
238 // thread with these callbacks.
239 base::Closure attach_listener_cb_;
240 base::Closure detach_listener_cb_;
241
242 // Error callback might be posted internally
243 base::Closure error_cb_;
244
245 // Total duration reported by demuxer.
246 base::TimeDelta duration_;
247
248 // base::TickClock used by |interpolator_|.
249 base::DefaultTickClock default_tick_clock_;
250
251 // Tracks the most recent media time update and provides interpolated values
252 // as playback progresses.
253 TimeDeltaInterpolator interpolator_;
254
255 // Pendng data to be picked up by the upcoming state.
256 gfx::ScopedJavaSurface pending_surface_;
257 bool pending_start_;
258
259 // Configuration data for the manager, accessed on the UI thread.
260 MediaMetadata metadata_cache_;
261
262 // Cached current time, accessed on UI thread.
263 base::TimeDelta current_time_cache_;
75 264
76 base::WeakPtr<MediaCodecPlayer> weak_this_; 265 base::WeakPtr<MediaCodecPlayer> weak_this_;
77 // NOTE: Weak pointers must be invalidated before all other member variables. 266 // NOTE: Weak pointers must be invalidated before all other member variables.
78 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; 267 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_;
79 268
80 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); 269 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer);
81 }; 270 };
82 271
83 } // namespace media 272 } // namespace media
84 273
85 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ 274 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698