1 /* 2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 package org.webrtc; 12 13 import android.content.Context; 14 import android.graphics.Matrix; 15 import android.view.WindowManager; 16 import android.view.Surface; 17 18 interface CameraSession { 19 enum FailureType { ERROR, DISCONNECTED } 20 21 // Callbacks are fired on the camera thread. 22 interface CreateSessionCallback { onDone(CameraSession session)23 void onDone(CameraSession session); onFailure(FailureType failureType, String error)24 void onFailure(FailureType failureType, String error); 25 } 26 27 // Events are fired on the camera thread. 28 interface Events { onCameraOpening()29 void onCameraOpening(); onCameraError(CameraSession session, String error)30 void onCameraError(CameraSession session, String error); onCameraDisconnected(CameraSession session)31 void onCameraDisconnected(CameraSession session); onCameraClosed(CameraSession session)32 void onCameraClosed(CameraSession session); onFrameCaptured(CameraSession session, VideoFrame frame)33 void onFrameCaptured(CameraSession session, VideoFrame frame); 34 } 35 36 /** 37 * Stops the capture. Waits until no more calls to capture observer will be made. 38 * If waitCameraStop is true, also waits for the camera to stop. 39 */ stop()40 void stop(); 41 getDeviceOrientation(Context context)42 static int getDeviceOrientation(Context context) { 43 final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 44 switch (wm.getDefaultDisplay().getRotation()) { 45 case Surface.ROTATION_90: 46 return 90; 47 case Surface.ROTATION_180: 48 return 180; 49 case Surface.ROTATION_270: 50 return 270; 51 case Surface.ROTATION_0: 52 default: 53 return 0; 54 } 55 } 56 createTextureBufferWithModifiedTransformMatrix( TextureBufferImpl buffer, boolean mirror, int rotation)57 static VideoFrame.TextureBuffer createTextureBufferWithModifiedTransformMatrix( 58 TextureBufferImpl buffer, boolean mirror, int rotation) { 59 final Matrix transformMatrix = new Matrix(); 60 // Perform mirror and rotation around (0.5, 0.5) since that is the center of the texture. 61 transformMatrix.preTranslate(/* dx= */ 0.5f, /* dy= */ 0.5f); 62 if (mirror) { 63 transformMatrix.preScale(/* sx= */ -1f, /* sy= */ 1f); 64 } 65 transformMatrix.preRotate(rotation); 66 transformMatrix.preTranslate(/* dx= */ -0.5f, /* dy= */ -0.5f); 67 68 // The width and height are not affected by rotation since Camera2Session has set them to the 69 // value they should be after undoing the rotation. 70 return buffer.applyTransformMatrix(transformMatrix, buffer.getWidth(), buffer.getHeight()); 71 } 72 } 73