| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 package org.chromium.media; | 5 package org.chromium.media; |
| 6 | 6 |
| 7 import android.Manifest; | 7 import android.Manifest; |
| 8 import android.content.Context; | |
| 9 import android.content.pm.PackageManager; | 8 import android.content.pm.PackageManager; |
| 10 import android.os.Build; | 9 import android.os.Build; |
| 11 | 10 |
| 11 import org.chromium.base.ContextUtils; |
| 12 import org.chromium.base.Log; | 12 import org.chromium.base.Log; |
| 13 import org.chromium.base.annotations.CalledByNative; | 13 import org.chromium.base.annotations.CalledByNative; |
| 14 import org.chromium.base.annotations.JNINamespace; | 14 import org.chromium.base.annotations.JNINamespace; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * This class implements a factory of Android Video Capture objects for Chrome. | 17 * This class implements a factory of Android Video Capture objects for Chrome. |
| 18 * Cameras are identified by |id|. Video Capture objects allocated via | 18 * Cameras are identified by |id|. Video Capture objects allocated via |
| 19 * createVideoCapture() are explicitly owned by the caller. ChromiumCameraInfo | 19 * createVideoCapture() are explicitly owned by the caller. ChromiumCameraInfo |
| 20 * is an internal class with some static methods needed from the rest of the | 20 * is an internal class with some static methods needed from the rest of the |
| 21 * class to manipulate the |id|s of devices. | 21 * class to manipulate the |id|s of devices. |
| 22 **/ | 22 **/ |
| 23 @JNINamespace("media") | 23 @JNINamespace("media") |
| 24 @SuppressWarnings("deprecation") | 24 @SuppressWarnings("deprecation") |
| 25 class VideoCaptureFactory { | 25 class VideoCaptureFactory { |
| 26 // Internal class to encapsulate camera device id manipulations. | 26 // Internal class to encapsulate camera device id manipulations. |
| 27 static class ChromiumCameraInfo { | 27 static class ChromiumCameraInfo { |
| 28 private static int sNumberOfSystemCameras = -1; | 28 private static int sNumberOfSystemCameras = -1; |
| 29 private static final String TAG = "cr.media"; | 29 private static final String TAG = "cr.media"; |
| 30 | 30 |
| 31 private static int getNumberOfCameras(Context appContext) { | 31 private static int getNumberOfCameras() { |
| 32 if (sNumberOfSystemCameras == -1) { | 32 if (sNumberOfSystemCameras == -1) { |
| 33 // getNumberOfCameras() would not fail due to lack of permission
, but the | 33 // getNumberOfCameras() would not fail due to lack of permission
, but the |
| 34 // following operations on camera would. "No permission" isn't a
fatal | 34 // following operations on camera would. "No permission" isn't a
fatal |
| 35 // error in WebView, specially for those applications which have
no purpose | 35 // error in WebView, specially for those applications which have
no purpose |
| 36 // to use a camera, but "load page" requires it. So, output a wa
rning log | 36 // to use a camera, but "load page" requires it. So, output a wa
rning log |
| 37 // and carry on pretending the system has no camera(s). This op
timization | 37 // and carry on pretending the system has no camera(s). This op
timization |
| 38 // applies only to pre-M on Android because that is when runtime
permissions | 38 // applies only to pre-M on Android because that is when runtime
permissions |
| 39 // were introduced. | 39 // were introduced. |
| 40 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M | 40 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M |
| 41 && appContext.getPackageManager().checkPermission( | 41 && ContextUtils.getApplicationContext().getPackageManage
r().checkPermission( |
| 42 Manifest.permission.CAMERA, appContext.getPac
kageName()) | 42 Manifest.permission.CAMERA, |
| 43 ContextUtils.getApplicationContext().getPacka
geName()) |
| 43 != PackageManager.PERMISSION_GRANTED) { | 44 != PackageManager.PERMISSION_GRANTED) { |
| 44 sNumberOfSystemCameras = 0; | 45 sNumberOfSystemCameras = 0; |
| 45 Log.w(TAG, "Missing android.permission.CAMERA permission, " | 46 Log.w(TAG, "Missing android.permission.CAMERA permission, " |
| 46 + "no system camera available."); | 47 + "no system camera available."); |
| 47 } else { | 48 } else { |
| 48 if (isLReleaseOrLater()) { | 49 if (isLReleaseOrLater()) { |
| 49 sNumberOfSystemCameras = VideoCaptureCamera2.getNumberOf
Cameras(appContext); | 50 sNumberOfSystemCameras = VideoCaptureCamera2.getNumberOf
Cameras(); |
| 50 } else { | 51 } else { |
| 51 sNumberOfSystemCameras = VideoCaptureCamera.getNumberOfC
ameras(); | 52 sNumberOfSystemCameras = VideoCaptureCamera.getNumberOfC
ameras(); |
| 52 } | 53 } |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 return sNumberOfSystemCameras; | 56 return sNumberOfSystemCameras; |
| 56 } | 57 } |
| 57 } | 58 } |
| 58 | 59 |
| 59 private static boolean isLReleaseOrLater() { | 60 private static boolean isLReleaseOrLater() { |
| 60 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; | 61 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; |
| 61 } | 62 } |
| 62 | 63 |
| 63 @CalledByNative | 64 @CalledByNative |
| 64 static boolean isLegacyOrDeprecatedDevice(Context context, int id) { | 65 static boolean isLegacyOrDeprecatedDevice(int id) { |
| 65 return !isLReleaseOrLater() || VideoCaptureCamera2.isLegacyDevice(contex
t, id); | 66 return !isLReleaseOrLater() || VideoCaptureCamera2.isLegacyDevice(id); |
| 66 } | 67 } |
| 67 | 68 |
| 68 // Factory methods. | 69 // Factory methods. |
| 69 @CalledByNative | 70 @CalledByNative |
| 70 static VideoCapture createVideoCapture( | 71 static VideoCapture createVideoCapture(int id, long nativeVideoCaptureDevice
Android) { |
| 71 Context context, int id, long nativeVideoCaptureDeviceAndroid) { | 72 if (isLReleaseOrLater() && !VideoCaptureCamera2.isLegacyDevice(id)) { |
| 72 if (isLReleaseOrLater() && !VideoCaptureCamera2.isLegacyDevice(context,
id)) { | 73 return new VideoCaptureCamera2(id, nativeVideoCaptureDeviceAndroid); |
| 73 return new VideoCaptureCamera2(context, id, nativeVideoCaptureDevice
Android); | |
| 74 } | 74 } |
| 75 return new VideoCaptureCamera(context, id, nativeVideoCaptureDeviceAndro
id); | 75 return new VideoCaptureCamera(id, nativeVideoCaptureDeviceAndroid); |
| 76 } | 76 } |
| 77 | 77 |
| 78 @CalledByNative | 78 @CalledByNative |
| 79 static int getNumberOfCameras(Context appContext) { | 79 static int getNumberOfCameras() { |
| 80 return ChromiumCameraInfo.getNumberOfCameras(appContext); | 80 return ChromiumCameraInfo.getNumberOfCameras(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 @CalledByNative | 83 @CalledByNative |
| 84 static int getCaptureApiType(int id, Context appContext) { | 84 static int getCaptureApiType(int id) { |
| 85 if (isLReleaseOrLater()) { | 85 if (isLReleaseOrLater()) { |
| 86 return VideoCaptureCamera2.getCaptureApiType(id, appContext); | 86 return VideoCaptureCamera2.getCaptureApiType(id); |
| 87 } | 87 } |
| 88 return VideoCaptureCamera.getCaptureApiType(id); | 88 return VideoCaptureCamera.getCaptureApiType(id); |
| 89 } | 89 } |
| 90 | 90 |
| 91 @CalledByNative | 91 @CalledByNative |
| 92 static String getDeviceName(int id, Context appContext) { | 92 static String getDeviceName(int id) { |
| 93 if (isLReleaseOrLater() && !VideoCaptureCamera2.isLegacyDevice(appContex
t, id)) { | 93 if (isLReleaseOrLater() && !VideoCaptureCamera2.isLegacyDevice(id)) { |
| 94 return VideoCaptureCamera2.getName(id, appContext); | 94 return VideoCaptureCamera2.getName(id); |
| 95 } | 95 } |
| 96 return VideoCaptureCamera.getName(id); | 96 return VideoCaptureCamera.getName(id); |
| 97 } | 97 } |
| 98 | 98 |
| 99 @CalledByNative | 99 @CalledByNative |
| 100 static VideoCaptureFormat[] getDeviceSupportedFormats(Context appContext, in
t id) { | 100 static VideoCaptureFormat[] getDeviceSupportedFormats(int id) { |
| 101 if (isLReleaseOrLater() && !VideoCaptureCamera2.isLegacyDevice(appContex
t, id)) { | 101 if (isLReleaseOrLater() && !VideoCaptureCamera2.isLegacyDevice(id)) { |
| 102 return VideoCaptureCamera2.getDeviceSupportedFormats(appContext, id)
; | 102 return VideoCaptureCamera2.getDeviceSupportedFormats(id); |
| 103 } | 103 } |
| 104 return VideoCaptureCamera.getDeviceSupportedFormats(id); | 104 return VideoCaptureCamera.getDeviceSupportedFormats(id); |
| 105 } | 105 } |
| 106 | 106 |
| 107 @CalledByNative | 107 @CalledByNative |
| 108 static int getCaptureFormatWidth(VideoCaptureFormat format) { | 108 static int getCaptureFormatWidth(VideoCaptureFormat format) { |
| 109 return format.getWidth(); | 109 return format.getWidth(); |
| 110 } | 110 } |
| 111 | 111 |
| 112 @CalledByNative | 112 @CalledByNative |
| 113 static int getCaptureFormatHeight(VideoCaptureFormat format) { | 113 static int getCaptureFormatHeight(VideoCaptureFormat format) { |
| 114 return format.getHeight(); | 114 return format.getHeight(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 @CalledByNative | 117 @CalledByNative |
| 118 static int getCaptureFormatFramerate(VideoCaptureFormat format) { | 118 static int getCaptureFormatFramerate(VideoCaptureFormat format) { |
| 119 return format.getFramerate(); | 119 return format.getFramerate(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 @CalledByNative | 122 @CalledByNative |
| 123 static int getCaptureFormatPixelFormat(VideoCaptureFormat format) { | 123 static int getCaptureFormatPixelFormat(VideoCaptureFormat format) { |
| 124 return format.getPixelFormat(); | 124 return format.getPixelFormat(); |
| 125 } | 125 } |
| 126 } | 126 } |
| OLD | NEW |