OLD | NEW |
---|---|
(Empty) | |
1 #include "media/base/android/media_codec_player.h" | |
2 | |
3 #include "base/bind.h" | |
4 #include "base/lazy_instance.h" | |
5 #include "base/logging.h" | |
6 | |
7 #define POST_TO_MEDIA_THREAD(closure) \ | |
xhwang
2015/05/06 17:42:44
This doesn't always "post". How about POST_TO_MEDI
Tima Vaisburd
2015/05/08 07:30:54
I tried to make it better. Please see the comments
| |
8 do { \ | |
9 if (!GetMediaTaskRunner()->BelongsToCurrentThread()) { \ | |
10 GetMediaTaskRunner()->PostTask(FROM_HERE, closure); \ | |
11 return; \ | |
12 } \ | |
13 } while(0) | |
14 | |
15 | |
16 namespace media { | |
17 | |
18 class MediaThread : public base::Thread { | |
19 public: | |
20 MediaThread() : base::Thread("BrowserMediaThread") { | |
21 Start(); | |
22 } | |
23 }; | |
24 | |
25 // Create media thread | |
26 base::LazyInstance<MediaThread>::Leaky | |
27 g_media_thread = LAZY_INSTANCE_INITIALIZER; | |
28 | |
29 | |
30 scoped_refptr<base::SingleThreadTaskRunner> GetMediaTaskRunner() { | |
31 return g_media_thread.Pointer()->task_runner(); | |
32 } | |
33 | |
34 // MediaCodecPlayer implementation. | |
35 | |
36 MediaCodecPlayer::MediaCodecPlayer( | |
37 int player_id, | |
38 MediaPlayerManager* manager, | |
39 const RequestMediaResourcesCB& request_media_resources_cb, | |
40 scoped_ptr<DemuxerAndroid> demuxer, | |
41 const GURL& frame_url) | |
42 : MediaPlayerAndroid(player_id, | |
43 manager, | |
44 request_media_resources_cb, | |
45 frame_url), | |
46 ui_task_runner_(base::MessageLoopProxy::current()), | |
47 demuxer_(demuxer.Pass()), | |
48 weak_factory_(this) { | |
49 // UI thread | |
50 DVLOG(1) << "MediaCodecPlayer::MediaCodecPlayer: player_id:" << player_id; | |
51 | |
52 weak_this_ = weak_factory_.GetWeakPtr(); | |
53 | |
54 // Finish initializaton on Media thread | |
55 GetMediaTaskRunner()->PostTask( | |
56 FROM_HERE, base::Bind(&MediaCodecPlayer::Initialize, weak_this_)); | |
57 } | |
58 | |
59 MediaCodecPlayer::~MediaCodecPlayer() | |
60 { | |
61 // Media thread | |
62 DVLOG(1) << "MediaCodecPlayer::~MediaCodecPlayer"; | |
63 } | |
64 | |
65 void MediaCodecPlayer::Initialize() { | |
66 // Media thread | |
67 DVLOG(1) << __FUNCTION__; | |
68 | |
69 demuxer_->Initialize(this); | |
70 } | |
71 | |
72 // MediaPlayerAndroid implementation. | |
73 | |
74 void MediaCodecPlayer::DeleteOnCorrectThread() { | |
75 DVLOG(1) << __FUNCTION__; | |
76 | |
77 if (ui_task_runner_->BelongsToCurrentThread()) { | |
78 DetachListener(); | |
79 DestroyListener(); | |
80 } | |
81 | |
82 if (GetMediaTaskRunner()->BelongsToCurrentThread()) | |
83 delete this; | |
84 else | |
85 GetMediaTaskRunner()->DeleteSoon(FROM_HERE, this); | |
86 } | |
87 | |
88 void MediaCodecPlayer::SetVideoSurface(gfx::ScopedJavaSurface surface) { | |
xhwang
2015/05/06 17:42:44
Use NOTIMPLEMENTED() in these functions.
Tima Vaisburd
2015/05/08 07:30:54
Done.
| |
89 POST_TO_MEDIA_THREAD( | |
xhwang
2015/05/06 17:42:44
I don't find this macro very readable, maybe becau
Tima Vaisburd
2015/05/08 07:30:54
Yes. I pulled the base::Bind into the macro, and u
| |
90 base::Bind(&MediaCodecPlayer::SetVideoSurface, weak_this_, | |
91 base::Passed(&surface))); | |
92 | |
93 // Media thread | |
94 DVLOG(1) << __FUNCTION__; | |
95 } | |
96 | |
97 void MediaCodecPlayer::Start() { | |
98 if (ui_task_runner_->BelongsToCurrentThread()) | |
99 AttachListener(NULL); | |
xhwang
2015/05/06 17:42:44
Seems to worth a comment.
Tima Vaisburd
2015/05/08 07:30:54
A closer look at the AttachListener/DetachListener
| |
100 | |
101 POST_TO_MEDIA_THREAD(base::Bind(&MediaCodecPlayer::Start, weak_this_)); | |
102 | |
103 // Media thread | |
104 DVLOG(1) << __FUNCTION__; | |
105 } | |
106 | |
107 void MediaCodecPlayer::Pause(bool is_media_related_action) { | |
108 POST_TO_MEDIA_THREAD( | |
109 base::Bind(&MediaCodecPlayer::Pause, weak_this_, | |
110 is_media_related_action)); | |
111 | |
112 // Media thread | |
113 DVLOG(1) << __FUNCTION__; | |
114 } | |
115 | |
116 void MediaCodecPlayer::SeekTo(base::TimeDelta timestamp) { | |
117 POST_TO_MEDIA_THREAD( | |
118 base::Bind(&MediaCodecPlayer::SeekTo, weak_this_, timestamp)); | |
119 | |
120 // Media thread | |
121 DVLOG(1) << __FUNCTION__ << " " << timestamp; | |
122 } | |
123 | |
124 void MediaCodecPlayer::Release() { | |
125 POST_TO_MEDIA_THREAD(base::Bind(&MediaCodecPlayer::Release, weak_this_)); | |
126 | |
127 // Media thread | |
128 DVLOG(1) << __FUNCTION__; | |
129 } | |
130 | |
131 void MediaCodecPlayer::SetVolume(double volume) { | |
132 POST_TO_MEDIA_THREAD( | |
133 base::Bind(&MediaCodecPlayer::SetVolume, weak_this_, volume)); | |
134 | |
135 // Media thread | |
136 DVLOG(1) << __FUNCTION__ << " " << volume; | |
137 } | |
138 | |
139 int MediaCodecPlayer::GetVideoWidth() { | |
140 // UI thread | |
xhwang
2015/05/06 17:42:44
Here and below, DCHECK it's called on the correct
Tima Vaisburd
2015/05/08 07:30:54
Done.
| |
141 return 320; | |
142 } | |
143 | |
144 int MediaCodecPlayer::GetVideoHeight() { | |
145 // UI thread | |
146 return 240; | |
147 } | |
148 | |
149 base::TimeDelta MediaCodecPlayer::GetCurrentTime() { | |
150 // UI thread, Media thread | |
151 return base::TimeDelta(); | |
152 } | |
153 | |
154 base::TimeDelta MediaCodecPlayer::GetDuration() { | |
155 // UI thread | |
156 return base::TimeDelta(); | |
157 } | |
158 | |
159 bool MediaCodecPlayer::IsPlaying() { | |
160 // UI thread | |
161 return false; | |
162 } | |
163 | |
164 bool MediaCodecPlayer::CanPause() { | |
165 // UI thread | |
166 return false; | |
167 } | |
168 | |
169 bool MediaCodecPlayer::CanSeekForward() { | |
170 // UI thread | |
171 return false; | |
172 } | |
173 | |
174 bool MediaCodecPlayer::CanSeekBackward() { | |
175 // UI thread | |
176 return false; | |
177 } | |
178 | |
179 bool MediaCodecPlayer::IsPlayerReady() { | |
180 // UI thread | |
181 DVLOG(1) << __FUNCTION__; | |
182 return true; | |
183 } | |
184 | |
185 void MediaCodecPlayer::SetCdm(BrowserCdm* cdm) { | |
186 DVLOG(1) << __FUNCTION__; | |
187 } | |
188 | |
189 // Callbacks from Demuxer. | |
190 | |
191 void MediaCodecPlayer::OnDemuxerConfigsAvailable( | |
192 const DemuxerConfigs& configs) { | |
193 // Media thread | |
194 DVLOG(1) << __FUNCTION__; | |
195 } | |
196 | |
197 void MediaCodecPlayer::OnDemuxerDataAvailable(const DemuxerData& data) { | |
198 // Media thread | |
199 } | |
200 | |
201 void MediaCodecPlayer::OnDemuxerSeekDone( | |
202 base::TimeDelta actual_browser_seek_time) { | |
203 // Media thread | |
204 DVLOG(1) << __FUNCTION__ << " actual_time:" << actual_browser_seek_time; | |
205 } | |
206 | |
207 void MediaCodecPlayer::OnDemuxerDurationChanged( | |
208 base::TimeDelta duration) { | |
209 // Media thread | |
210 DVLOG(1) << __FUNCTION__ << " duration:" << duration; | |
211 } | |
212 | |
213 } // namespace media | |
OLD | NEW |