OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "content/browser/android/content_video_view.h" | 5 #include "content/browser/android/content_video_view.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_helper.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "content/browser/android/media_player_manager_android.h" | 10 #include "content/browser/android/media_player_manager_android.h" |
11 #include "content/common/android/surface_texture_peer.h" | 11 #include "content/common/android/surface_texture_peer.h" |
12 #include "content/public/common/content_switches.h" | 12 #include "content/public/common/content_switches.h" |
13 #include "jni/ContentVideoView_jni.h" | 13 #include "jni/ContentVideoView_jni.h" |
14 | 14 |
15 using base::android::AttachCurrentThread; | 15 using base::android::AttachCurrentThread; |
16 using base::android::CheckException; | 16 using base::android::CheckException; |
17 using base::android::ScopedJavaGlobalRef; | 17 using base::android::ScopedJavaGlobalRef; |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 // WeakRef Reference to the Java object. | |
joth
2013/04/12 17:56:24
Add "there can only be one content video view at a
michaelbai
2013/04/18 18:21:59
Done.
| |
22 static JavaObjectWeakGlobalRef* g_content_video_view_ = NULL; | |
joth
2013/04/12 17:56:24
no trailing _ on globals
michaelbai
2013/04/18 18:21:59
Done.
| |
23 | |
24 static ScopedJavaLocalRef<jobject> GetContentVideoView(JNIEnv* env) { | |
joth
2013/04/12 17:56:24
instead of static, put these two into anon namesp
michaelbai
2013/04/18 18:21:59
Done.
| |
25 if (g_content_video_view_) | |
26 return g_content_video_view_->get(env); | |
27 else | |
28 return ScopedJavaLocalRef<jobject>(); | |
joth
2013/04/12 17:56:24
I think this whole file will be easier to read if
michaelbai
2013/04/18 18:21:59
Done.
| |
29 } | |
30 | |
31 static jobject GetSingletonJavaContentVideoView(JNIEnv*env, jclass) { | |
32 return GetContentVideoView(env).obj(); | |
joth
2013/04/12 17:56:24
I think you need to use Release() here, otherwise
michaelbai
2013/04/18 18:21:59
Done.
| |
33 } | |
34 | |
21 bool ContentVideoView::RegisterContentVideoView(JNIEnv* env) { | 35 bool ContentVideoView::RegisterContentVideoView(JNIEnv* env) { |
22 return RegisterNativesImpl(env); | 36 return RegisterNativesImpl(env); |
23 } | 37 } |
24 | 38 |
25 ContentVideoView::ContentVideoView(MediaPlayerManagerAndroid* manager) | 39 ContentVideoView::ContentVideoView(MediaPlayerManagerAndroid* manager) |
26 : manager_(manager) { | 40 : manager_(manager) { |
27 } | 41 } |
28 | 42 |
29 ContentVideoView::~ContentVideoView() { | 43 ContentVideoView::~ContentVideoView() { |
30 DestroyContentVideoView(); | 44 DestroyContentVideoView(); |
31 } | 45 } |
32 | 46 |
33 void ContentVideoView::CreateContentVideoView() { | 47 void ContentVideoView::CreateContentVideoView( |
34 if (j_content_video_view_.is_null()) { | 48 ScopedJavaLocalRef<jobject> context, |
35 JNIEnv* env = AttachCurrentThread(); | 49 ScopedJavaLocalRef<jobject> content_video_view_client) { |
36 j_content_video_view_.Reset(Java_ContentVideoView_createContentVideoView( | 50 JNIEnv *env = AttachCurrentThread(); |
37 env, reinterpret_cast<jint>(this))); | 51 ScopedJavaLocalRef<jobject> content_video_view = GetContentVideoView(env); |
52 if (content_video_view.is_null()) { | |
53 g_content_video_view_ = new JavaObjectWeakGlobalRef(env, | |
joth
2013/04/12 17:56:24
g_content_video_view_ might already hold an instan
| |
54 Java_ContentVideoView_createContentVideoView(env, context.obj(), | |
55 reinterpret_cast<int>(this), | |
56 content_video_view_client.obj()).obj()); | |
38 } else { | 57 } else { |
39 // Just ask video view to reopen the video. | 58 Java_ContentVideoView_openVideo(env, content_video_view.obj()); |
joth
2013/04/12 17:56:24
in the else branch we ignore the content_video_vie
| |
40 Java_ContentVideoView_openVideo(AttachCurrentThread(), | |
41 j_content_video_view_.obj()); | |
42 } | 59 } |
43 } | 60 } |
44 | 61 |
45 void ContentVideoView::DestroyContentVideoView() { | 62 void ContentVideoView::DestroyContentVideoView() { |
46 if (!j_content_video_view_.is_null()) { | 63 JNIEnv *env = AttachCurrentThread(); |
47 Java_ContentVideoView_destroyContentVideoView(AttachCurrentThread()); | 64 ScopedJavaLocalRef<jobject> content_video_view = GetContentVideoView(env); |
48 j_content_video_view_.Reset(); | 65 if (!content_video_view.is_null()) { |
66 Java_ContentVideoView_destroyContentVideoView(env, | |
67 content_video_view.obj()); | |
68 g_content_video_view_->reset(); | |
49 } | 69 } |
50 } | 70 } |
51 | 71 |
52 void ContentVideoView::OnMediaPlayerError(int error_type) { | 72 void ContentVideoView::OnMediaPlayerError(int error_type) { |
53 if (!j_content_video_view_.is_null()) { | 73 JNIEnv *env = AttachCurrentThread(); |
54 Java_ContentVideoView_onMediaPlayerError(AttachCurrentThread(), | 74 ScopedJavaLocalRef<jobject> content_video_view = GetContentVideoView(env); |
55 j_content_video_view_.obj(), | 75 if (!content_video_view.is_null()) { |
56 error_type); | 76 Java_ContentVideoView_onMediaPlayerError(env, content_video_view.obj(), |
77 error_type); | |
57 } | 78 } |
58 } | 79 } |
59 | 80 |
60 void ContentVideoView::OnVideoSizeChanged(int width, int height) { | 81 void ContentVideoView::OnVideoSizeChanged(int width, int height) { |
61 if (!j_content_video_view_.is_null()) { | 82 JNIEnv *env = AttachCurrentThread(); |
62 Java_ContentVideoView_onVideoSizeChanged(AttachCurrentThread(), | 83 ScopedJavaLocalRef<jobject> content_video_view = GetContentVideoView(env); |
63 j_content_video_view_.obj(), | 84 if (!content_video_view.is_null()) { |
64 width, | 85 Java_ContentVideoView_onVideoSizeChanged(env, content_video_view.obj(), |
65 height); | 86 width, height); |
66 } | 87 } |
67 } | 88 } |
68 | 89 |
69 void ContentVideoView::OnBufferingUpdate(int percent) { | 90 void ContentVideoView::OnBufferingUpdate(int percent) { |
70 if (!j_content_video_view_.is_null()) { | 91 JNIEnv *env = AttachCurrentThread(); |
71 Java_ContentVideoView_onBufferingUpdate(AttachCurrentThread(), | 92 ScopedJavaLocalRef<jobject> content_video_view = GetContentVideoView(env); |
72 j_content_video_view_.obj(), | 93 if (!content_video_view.is_null()) { |
73 percent); | 94 Java_ContentVideoView_onBufferingUpdate(env, content_video_view.obj(), |
95 percent); | |
74 } | 96 } |
75 } | 97 } |
76 | 98 |
77 void ContentVideoView::OnPlaybackComplete() { | 99 void ContentVideoView::OnPlaybackComplete() { |
78 if (!j_content_video_view_.is_null()) { | 100 JNIEnv *env = AttachCurrentThread(); |
79 Java_ContentVideoView_onPlaybackComplete(AttachCurrentThread(), | 101 ScopedJavaLocalRef<jobject> content_video_view = GetContentVideoView(env); |
80 j_content_video_view_.obj()); | 102 if (!content_video_view.is_null()) |
81 } | 103 Java_ContentVideoView_onPlaybackComplete(env, content_video_view.obj()); |
82 } | 104 } |
83 | 105 |
84 void ContentVideoView::UpdateMediaMetadata() { | 106 void ContentVideoView::UpdateMediaMetadata() { |
85 if (!j_content_video_view_.is_null()) | 107 JNIEnv *env = AttachCurrentThread(); |
86 UpdateMediaMetadata(AttachCurrentThread(), j_content_video_view_.obj()); | 108 ScopedJavaLocalRef<jobject> content_video_view = GetContentVideoView(env); |
109 if (!content_video_view.is_null()) | |
110 UpdateMediaMetadata(env, content_video_view.obj()); | |
87 } | 111 } |
88 | 112 |
89 int ContentVideoView::GetVideoWidth(JNIEnv*, jobject obj) const { | 113 int ContentVideoView::GetVideoWidth(JNIEnv*, jobject obj) const { |
90 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer(); | 114 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer(); |
91 return player ? player->GetVideoWidth() : 0; | 115 return player ? player->GetVideoWidth() : 0; |
92 } | 116 } |
93 | 117 |
94 int ContentVideoView::GetVideoHeight(JNIEnv*, jobject obj) const { | 118 int ContentVideoView::GetVideoHeight(JNIEnv*, jobject obj) const { |
95 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer(); | 119 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer(); |
96 return player ? player->GetVideoHeight() : 0; | 120 return player ? player->GetVideoHeight() : 0; |
(...skipping 22 matching lines...) Expand all Loading... | |
119 manager_->FullscreenPlayerPlay(); | 143 manager_->FullscreenPlayerPlay(); |
120 } | 144 } |
121 | 145 |
122 void ContentVideoView::Pause(JNIEnv*, jobject obj) { | 146 void ContentVideoView::Pause(JNIEnv*, jobject obj) { |
123 manager_->FullscreenPlayerPause(); | 147 manager_->FullscreenPlayerPause(); |
124 } | 148 } |
125 | 149 |
126 void ContentVideoView::ExitFullscreen( | 150 void ContentVideoView::ExitFullscreen( |
127 JNIEnv*, jobject, jboolean release_media_player) { | 151 JNIEnv*, jobject, jboolean release_media_player) { |
128 manager_->ExitFullscreen(release_media_player); | 152 manager_->ExitFullscreen(release_media_player); |
129 j_content_video_view_.Reset(); | 153 g_content_video_view_->reset(); |
130 } | 154 } |
131 | 155 |
132 void ContentVideoView::SetSurface(JNIEnv* env, jobject obj, | 156 void ContentVideoView::SetSurface(JNIEnv* env, jobject obj, |
133 jobject surface) { | 157 jobject surface) { |
134 manager_->SetVideoSurface(surface); | 158 manager_->SetVideoSurface(surface); |
135 } | 159 } |
136 | 160 |
137 void ContentVideoView::UpdateMediaMetadata(JNIEnv* env, jobject obj) { | 161 void ContentVideoView::UpdateMediaMetadata(JNIEnv* env, jobject obj) { |
138 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer(); | 162 media::MediaPlayerBridge* player = manager_->GetFullscreenPlayer(); |
139 if (player && player->prepared()) | 163 if (player && player->prepared()) |
140 Java_ContentVideoView_updateMediaMetadata( | 164 Java_ContentVideoView_updateMediaMetadata( |
141 env, obj, player->GetVideoWidth(), player->GetVideoHeight(), | 165 env, obj, player->GetVideoWidth(), player->GetVideoHeight(), |
142 player->GetDuration().InMilliseconds(), player->can_pause(), | 166 player->GetDuration().InMilliseconds(), player->can_pause(), |
143 player->can_seek_forward(), player->can_seek_backward()); | 167 player->can_seek_forward(), player->can_seek_backward()); |
144 } | 168 } |
145 | 169 |
146 } // namespace content | 170 } // namespace content |
OLD | NEW |