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

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: Addressed some comments Created 5 years, 7 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 /*
27 State machine diagram.
28
29
30 --------------------------> Paused
31 | StopDone:
32 | ^ |
33 | | |
34 | Pause: | | Start: dec.Prefetch
35 | | |
36 | | |
37 | | |
38 | | v PrefetchDone,
39 | no surface:
40 | ------------------> Prefetching --------------> Waiting
41 | | for surface
42 | | | /
43 | | | /
44 | | StopDone w/ | /
45 | | pending start: | PrefetchDone: /
46 | | dec.Prefetch | dec.Start /
47 | | | / SetSurface:
48 | | | / dec.Start
49 | | | /
50 | | v /
51 | | /
52 | | Playing <----------/
53 | |
54 | | |
55 | | |
56 | | | Pause: dec.RequestToStop
57 | | |
58 | | |
59 | | v
60 | |
61 -------------------------- Stopping
62
63 */
64
17 namespace media { 65 namespace media {
18 66
19 class BrowserCdm; 67 class BrowserCdm;
68 class MediaCodecAudioDecoder;
69 class MediaCodecVideoDecoder;
20 70
21 // Returns the task runner for the media thread 71 // Returns the task runner for the media thread
22 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); 72 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner();
23 73
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, 74 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid,
30 public DemuxerAndroidClient { 75 public DemuxerAndroidClient {
31 public: 76 public:
77
78 // Typedefs for the notification callbacks
79 typedef base::Callback<
80 void (base::TimeDelta duration, const gfx::Size& video_size)>
81 MetadataChangedCallback;
82
83 typedef base::Callback<
84 void (base::TimeDelta current_timestamp, base::TimeTicks current_ticks)>
85 TimeUpdateCallback;
86
32 // Constructs a player with the given ID and demuxer. |manager| must outlive 87 // Constructs a player with the given ID and demuxer. |manager| must outlive
33 // the lifetime of this object. 88 // the lifetime of this object.
34 MediaCodecPlayer(int player_id, 89 MediaCodecPlayer(int player_id,
35 MediaPlayerManager* manager, 90 base::WeakPtr<MediaPlayerManager> manager,
36 const RequestMediaResourcesCB& request_media_resources_cb, 91 const RequestMediaResourcesCB& request_media_resources_cb,
37 scoped_ptr<DemuxerAndroid> demuxer, 92 scoped_ptr<DemuxerAndroid> demuxer,
38 const GURL& frame_url); 93 const GURL& frame_url);
39 ~MediaCodecPlayer() override; 94 ~MediaCodecPlayer() override;
40 95
41 // MediaPlayerAndroid implementation. 96 // MediaPlayerAndroid implementation.
42 void DeleteOnCorrectThread() override; 97 void DeleteOnCorrectThread() override;
43 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; 98 void SetVideoSurface(gfx::ScopedJavaSurface surface) override;
44 void Start() override; 99 void Start() override;
45 void Pause(bool is_media_related_action) override; 100 void Pause(bool is_media_related_action) override;
(...skipping 12 matching lines...) Expand all
58 void SetCdm(BrowserCdm* cdm) override; 113 void SetCdm(BrowserCdm* cdm) override;
59 114
60 // DemuxerAndroidClient implementation. 115 // DemuxerAndroidClient implementation.
61 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; 116 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override;
62 void OnDemuxerDataAvailable(const DemuxerData& params) override; 117 void OnDemuxerDataAvailable(const DemuxerData& params) override;
63 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; 118 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override;
64 void OnDemuxerDurationChanged(base::TimeDelta duration) override; 119 void OnDemuxerDurationChanged(base::TimeDelta duration) override;
65 120
66 // Helper methods 121 // Helper methods
67 void Initialize(); 122 void Initialize();
68 void DestroySelf(); 123
124 base::WeakPtr<MediaCodecPlayer> weak_ptr() const { return weak_this_; }
125
126 protected:
127 // MediaPlayerAndroid implementation.
128 // This method caches the data and calls manager's OnMediaMetadataChanged().
129 void OnMediaMetadataChanged(
130 base::TimeDelta duration, const gfx::Size& video_size) override;
69 131
70 private: 132 private:
133 // The state machine states.
134 enum PlayerState {
135 STATE_PAUSED,
136 STATE_WAITING_FOR_CONFIG,
137 STATE_PREFETCHING,
138 STATE_PLAYING,
139 STATE_STOPPING,
140 STATE_WAITING_FOR_SURFACE,
141 STATE_WAITING_FOR_SEEK,
142 STATE_ERROR,
143 };
144
145 // Cached valued for the manager, accessed on the UI thread.
146 struct MediaMetadata {
147 base::TimeDelta duration;
148 gfx::Size video_size;
149 // Have some defined aspect ratio by default.
150 MediaMetadata() : duration(), video_size(320, 240) {}
151 };
152
153 // Callbacks from decoders
154 void RequestDemuxerData(DemuxerStream::Type stream_type);
155 void OnPrefetchDone();
156 void OnStarvation();
157 void OnStopDone();
158 void OnError();
159 void OnTimeIntervalUpdate(base::TimeDelta now_playing,
160 base::TimeDelta last_buffered);
161
162 // Operations called from the state machine.
163 void SetState(PlayerState new_state);
164 void SetPendingSurface(gfx::ScopedJavaSurface surface);
165 bool HasPendingSurface();
166 void SetPendingStart(bool need_to_start);
167 bool HasPendingStart();
168 bool HasVideo();
169 bool HasAudio();
170 void SetDemuxerConfigs(const DemuxerConfigs& configs);
171 void StartPrefetchDecoders();
172 void StartPlaybackDecoders();
173 void StopDecoders();
174 void RequestToStopDecoders();
175 void ReleaseDecoderResources();
176
177 // Helper methods.
178 void CreateDecoders();
179 bool AudioFinished();
180 bool VideoFinished();
181
182 static const char* AsString(PlayerState state);
183
184 // Data.
185
71 // Object for posting tasks on UI thread. 186 // Object for posting tasks on UI thread.
72 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; 187 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_;
73 188
189 // Major components: demuxer, audio and video decoders.
74 scoped_ptr<DemuxerAndroid> demuxer_; 190 scoped_ptr<DemuxerAndroid> demuxer_;
191 scoped_ptr<MediaCodecAudioDecoder> audio_decoder_;
192 scoped_ptr<MediaCodecVideoDecoder> video_decoder_;
193
194 // The state of the state machine.
195 PlayerState state_;
196
197 // Notification callbacks, they call MediaPlayerManager.
198 base::Closure request_resources_cb_;
199 TimeUpdateCallback time_update_cb_;
200 base::Closure completion_cb_;
201
202 // A callback that updates metadata cache and calls the manager.
203 MetadataChangedCallback metadata_changed_cb_;
204
205 // We call the base class' AttachListener() and DetachListener() methods on UI
206 // thread with these callbacks.
207 base::Closure attach_listener_cb_;
208 base::Closure detach_listener_cb_;
209
210 // Error callback might be posted internally
211 base::Closure error_cb_;
212
213 // Total duration reported by demuxer.
214 base::TimeDelta duration_;
215
216 // base::TickClock used by |interpolator_|.
217 base::DefaultTickClock default_tick_clock_;
218
219 // Tracks the most recent media time update and provides interpolated values
220 // as playback progresses.
221 TimeDeltaInterpolator interpolator_;
222
223 // |interpolator_| will be accessed from the UI and decoder threads
224 base::Lock interpolator_lock_;
225
226 // Pendng data to be picked up by the upcoming state.
227 gfx::ScopedJavaSurface pending_surface_;
228 bool pending_start_;
229
230 // Configuration data for the manager, accessed on the UI thread.
231 MediaMetadata metadata_cache_;
75 232
76 base::WeakPtr<MediaCodecPlayer> weak_this_; 233 base::WeakPtr<MediaCodecPlayer> weak_this_;
77 // NOTE: Weak pointers must be invalidated before all other member variables. 234 // NOTE: Weak pointers must be invalidated before all other member variables.
78 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; 235 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_;
79 236
80 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); 237 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer);
81 }; 238 };
82 239
83 } // namespace media 240 } // namespace media
84 241
85 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ 242 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698