• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #ifndef CONTENT_BROWSER_ANDROID_CONTENT_VIDEO_VIEW_H_
6 #define CONTENT_BROWSER_ANDROID_CONTENT_VIDEO_VIEW_H_
7 
8 #include <jni.h>
9 
10 #include "base/android/jni_helper.h"
11 #include "base/android/scoped_java_ref.h"
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/timer/timer.h"
16 #include "ui/gfx/native_widget_types.h"
17 
18 namespace content {
19 
20 class BrowserMediaPlayerManager;
21 class PowerSaveBlocker;
22 
23 // Native mirror of ContentVideoView.java. This class is responsible for
24 // creating the Java video view and pass all the player status change to
25 // it. It accepts media control from Java class, and forwards it to
26 // MediaPlayerManagerImpl.
27 class ContentVideoView {
28  public:
29   // Construct a ContentVideoView object. The |manager| will handle all the
30   // playback controls from the Java class.
31   ContentVideoView(
32       const base::android::ScopedJavaLocalRef<jobject>& context,
33       const base::android::ScopedJavaLocalRef<jobject>& client,
34       BrowserMediaPlayerManager* manager);
35 
36   ~ContentVideoView();
37 
38   // To open another video on existing ContentVideoView.
39   void OpenVideo();
40 
41   static bool RegisterContentVideoView(JNIEnv* env);
42   static void KeepScreenOn(bool screen_on);
43 
44   // Return true if there is existing ContentVideoView object.
45   static bool HasContentVideoView();
46 
47   // Getter method called by the Java class to get the media information.
48   int GetVideoWidth(JNIEnv*, jobject obj) const;
49   int GetVideoHeight(JNIEnv*, jobject obj) const;
50   int GetDurationInMilliSeconds(JNIEnv*, jobject obj) const;
51   int GetCurrentPosition(JNIEnv*, jobject obj) const;
52   bool IsPlaying(JNIEnv*, jobject obj);
53   void UpdateMediaMetadata(JNIEnv*, jobject obj);
54 
55   // Called when the Java fullscreen view is destroyed. If
56   // |release_media_player| is true, |manager_| needs to release the player
57   // as we are quitting the app.
58   void ExitFullscreen(JNIEnv*, jobject, jboolean release_media_player);
59 
60   // Media control method called by the Java class.
61   void SeekTo(JNIEnv*, jobject obj, jint msec);
62   void Play(JNIEnv*, jobject obj);
63   void Pause(JNIEnv*, jobject obj);
64 
65   // Called by the Java class to pass the surface object to the player.
66   void SetSurface(JNIEnv*, jobject obj, jobject surface);
67 
68   // Method called by |manager_| to inform the Java class about player status
69   // change.
70   void UpdateMediaMetadata();
71   void OnMediaPlayerError(int errorType);
72   void OnVideoSizeChanged(int width, int height);
73   void OnBufferingUpdate(int percent);
74   void OnPlaybackComplete();
75   void OnExitFullscreen();
76 
77   // Return the corresponing ContentVideoView Java object if any.
78   base::android::ScopedJavaLocalRef<jobject> GetJavaObject(JNIEnv* env);
79 
80  private:
81   // Destroy the |j_content_video_view_|. If |native_view_destroyed| is true,
82   // no further calls to the native object is allowed.
83   void DestroyContentVideoView(bool native_view_destroyed);
84 
85   // Returns the associated NativeView
86   gfx::NativeView GetNativeView();
87 
88   void CreatePowerSaveBlocker();
89 
90   // Object that manages the fullscreen media player. It is responsible for
91   // handling all the playback controls.
92   BrowserMediaPlayerManager* manager_;
93 
94   // PowerSaveBlock to keep screen on for fullscreen video.
95   // There is already blocker when inline video started, and it requires the
96   // ContentView's container displayed to take effect; but in WebView, apps
97   // could use another container to hold ContentVideoView, and the blocker in
98   // ContentView's container can not keep screen on; so we need another blocker
99   // here, it is no harm, just an additonal blocker.
100   scoped_ptr<PowerSaveBlocker> power_save_blocker_;
101 
102   // Weak reference of corresponding Java object.
103   JavaObjectWeakGlobalRef j_content_video_view_;
104 
105   DISALLOW_COPY_AND_ASSIGN(ContentVideoView);
106 };
107 
108 } // namespace content
109 
110 #endif  // CONTENT_BROWSER_ANDROID_CONTENT_VIDEO_VIEW_H_
111