OLD | NEW |
---|---|
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" | |
15 #include "ui/gl/android/scoped_java_surface.h" | 18 #include "ui/gl/android/scoped_java_surface.h" |
16 | 19 |
17 namespace media { | 20 namespace media { |
18 | 21 |
19 class BrowserCdm; | 22 class BrowserCdm; |
23 class MediaCodecAudioDecoder; | |
24 class MediaCodecVideoDecoder; | |
20 | 25 |
21 // Returns the task runner for the media thread | 26 // Returns the task runner for the media thread |
22 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); | 27 MEDIA_EXPORT scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner(); |
23 | 28 |
24 | 29 |
25 // This class implements the media player using Android's MediaCodec. | 30 // This class implements the media player using Android's MediaCodec. |
26 // It differs from MediaSourcePlayer in that it removes most | 31 // It differs from MediaSourcePlayer in that it removes most |
27 // processing away from UI thread: it uses a dedicated Media thread | 32 // processing away from UI thread: it uses a dedicated Media thread |
28 // to receive the data and to handle commands. | 33 // to receive the data and to handle commands. |
29 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid, | 34 class MEDIA_EXPORT MediaCodecPlayer : public MediaPlayerAndroid, |
30 public DemuxerAndroidClient { | 35 public DemuxerAndroidClient { |
31 public: | 36 public: |
37 | |
38 // Typedefs for the notification callbacks | |
39 typedef base::Callback< | |
40 void (base::TimeDelta duration, int width, int height, bool success)> | |
41 MetadataChangedCallback; | |
42 | |
43 typedef base::Callback< | |
44 void (base::TimeDelta current_timestamp, base::TimeTicks current_ticks)> | |
45 TimeUpdateCallback; | |
46 | |
32 // Constructs a player with the given ID and demuxer. |manager| must outlive | 47 // Constructs a player with the given ID and demuxer. |manager| must outlive |
33 // the lifetime of this object. | 48 // the lifetime of this object. |
34 MediaCodecPlayer(int player_id, | 49 MediaCodecPlayer(int player_id, |
35 MediaPlayerManager* manager, | 50 base::WeakPtr<MediaPlayerManager> manager, |
36 const RequestMediaResourcesCB& request_media_resources_cb, | 51 const RequestMediaResourcesCB& request_media_resources_cb, |
37 scoped_ptr<DemuxerAndroid> demuxer, | 52 scoped_ptr<DemuxerAndroid> demuxer, |
38 const GURL& frame_url); | 53 const GURL& frame_url); |
39 ~MediaCodecPlayer() override; | 54 ~MediaCodecPlayer() override; |
40 | 55 |
41 // MediaPlayerAndroid implementation. | 56 // MediaPlayerAndroid implementation. |
wolenetz
2015/05/19 23:05:23
Please comment in the CL description the known gap
Tima Vaisburd
2015/05/22 23:37:52
None of this is implemented in this CL. I delibera
| |
42 void DeleteOnCorrectThread() override; | 57 void DeleteOnCorrectThread() override; |
43 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; | 58 void SetVideoSurface(gfx::ScopedJavaSurface surface) override; |
44 void Start() override; | 59 void Start() override; |
45 void Pause(bool is_media_related_action) override; | 60 void Pause(bool is_media_related_action) override; |
46 void SeekTo(base::TimeDelta timestamp) override; | 61 void SeekTo(base::TimeDelta timestamp) override; |
47 void Release() override; | 62 void Release() override; |
48 void SetVolume(double volume) override; | 63 void SetVolume(double volume) override; |
49 int GetVideoWidth() override; | 64 int GetVideoWidth() override; |
50 int GetVideoHeight() override; | 65 int GetVideoHeight() override; |
51 base::TimeDelta GetCurrentTime() override; | 66 base::TimeDelta GetCurrentTime() override; |
52 base::TimeDelta GetDuration() override; | 67 base::TimeDelta GetDuration() override; |
53 bool IsPlaying() override; | 68 bool IsPlaying() override; |
54 bool CanPause() override; | 69 bool CanPause() override; |
55 bool CanSeekForward() override; | 70 bool CanSeekForward() override; |
56 bool CanSeekBackward() override; | 71 bool CanSeekBackward() override; |
57 bool IsPlayerReady() override; | 72 bool IsPlayerReady() override; |
58 void SetCdm(BrowserCdm* cdm) override; | 73 void SetCdm(BrowserCdm* cdm) override; |
59 | 74 |
60 // DemuxerAndroidClient implementation. | 75 // DemuxerAndroidClient implementation. |
61 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; | 76 void OnDemuxerConfigsAvailable(const DemuxerConfigs& params) override; |
62 void OnDemuxerDataAvailable(const DemuxerData& params) override; | 77 void OnDemuxerDataAvailable(const DemuxerData& params) override; |
63 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; | 78 void OnDemuxerSeekDone(base::TimeDelta actual_browser_seek_time) override; |
64 void OnDemuxerDurationChanged(base::TimeDelta duration) override; | 79 void OnDemuxerDurationChanged(base::TimeDelta duration) override; |
65 | 80 |
66 // Helper methods | 81 // Helper methods |
67 void Initialize(); | 82 void Initialize(); |
68 void DestroySelf(); | 83 |
84 base::WeakPtr<MediaCodecPlayer> weak_ptr() const { return weak_this_; } | |
69 | 85 |
70 private: | 86 private: |
87 // The state machine states. | |
88 enum PlayerState { | |
89 STATE_PAUSED, | |
90 STATE_PREFETCHING, | |
91 STATE_PLAYING, | |
92 STATE_STOPPING, | |
93 STATE_WAITING_FOR_CONFIG, | |
94 STATE_WAITING_FOR_SURFACE, | |
95 STATE_WAITING_FOR_SEEK, | |
96 STATE_ERROR, | |
97 }; | |
98 | |
99 // Callbacks from decoders | |
100 void RequestDemuxerData(DemuxerStream::Type stream_type); | |
101 void OnPrefetchDone(); | |
102 void OnStarvation(); | |
103 void OnStopDone(); | |
104 void OnError(); | |
105 void OnTimeIntervalUpdate(base::TimeDelta now_playing, | |
106 base::TimeDelta last_buffered); | |
107 | |
108 // Operations called from the state machine. | |
109 void SetState(PlayerState new_state); | |
110 void SetPendingSurface(gfx::ScopedJavaSurface surface); | |
111 bool HasPendingSurface(); | |
112 void SetPendingStart(bool need_to_start); | |
113 bool HasPendingStart(); | |
114 bool HasVideo(); | |
115 bool HasAudio(); | |
116 void SetDemuxerConfigs(const DemuxerConfigs& configs); | |
117 void StartPrefetchDecoders(); | |
118 void StartPlaybackDecoders(); | |
119 void StopDecoders(); | |
120 void RequestToStopDecoders(); | |
121 void ReleaseDecoderResources(); | |
122 | |
123 // Helper methods. | |
124 void CreateDecoders(); | |
125 bool AudioFinished(); | |
126 bool VideoFinished(); | |
127 | |
128 static const char* AsString(PlayerState state); | |
129 | |
130 // Data. | |
131 | |
71 // Object for posting tasks on UI thread. | 132 // Object for posting tasks on UI thread. |
72 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 133 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
73 | 134 |
135 // Major components: demuxer, audio and video decoders. | |
74 scoped_ptr<DemuxerAndroid> demuxer_; | 136 scoped_ptr<DemuxerAndroid> demuxer_; |
137 scoped_ptr<MediaCodecAudioDecoder> audio_decoder_; | |
138 scoped_ptr<MediaCodecVideoDecoder> video_decoder_; | |
139 | |
140 // The state of the state machine. | |
141 PlayerState state_; | |
142 | |
143 // Notification callbacks, they call MediaPlayerManager. | |
144 base::Closure request_resources_cb_; | |
145 MetadataChangedCallback metadata_changed_cb_; | |
146 TimeUpdateCallback time_update_cb_; | |
147 base::Closure completion_cb_; | |
148 | |
149 // We call the MediaPlayerListener(request and release audio focus) | |
150 // on UI thread with these callbacks. | |
151 base::Closure attach_listener_cb_; | |
wolenetz
2015/05/19 23:05:23
What are the analogous methods in MediaSourcePlaye
Tima Vaisburd
2015/05/22 22:48:55
AttachListener() and DetachListener(), I changed t
| |
152 base::Closure detach_listener_cb_; | |
153 | |
154 // Error callback might be posted internally | |
155 base::Closure error_cb_; | |
156 | |
157 // Total duration reported by demuxer. | |
158 base::TimeDelta duration_; | |
159 | |
160 // base::TickClock used by |interpolator_|. | |
161 base::DefaultTickClock default_tick_clock_; | |
162 | |
163 // Tracks the most recent media time update and provides interpolated values | |
164 // as playback progresses. | |
165 TimeDeltaInterpolator interpolator_; | |
166 | |
167 // |interpolator_| will be accessed from the UI and decoder threads | |
168 base::Lock interpolator_lock_; | |
169 | |
170 // Pendng data to be picked up by the upcoming state. | |
171 gfx::ScopedJavaSurface pending_surface_; | |
172 bool pending_start_; | |
75 | 173 |
76 base::WeakPtr<MediaCodecPlayer> weak_this_; | 174 base::WeakPtr<MediaCodecPlayer> weak_this_; |
77 // NOTE: Weak pointers must be invalidated before all other member variables. | 175 // NOTE: Weak pointers must be invalidated before all other member variables. |
78 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; | 176 base::WeakPtrFactory<MediaCodecPlayer> weak_factory_; |
79 | 177 |
80 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); | 178 DISALLOW_COPY_AND_ASSIGN(MediaCodecPlayer); |
81 }; | 179 }; |
82 | 180 |
83 } // namespace media | 181 } // namespace media |
84 | 182 |
85 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ | 183 #endif // MEDIA_BASE_ANDROID_MEDIA_CODEC_PLAYER_H_ |
OLD | NEW |