| 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 { |
| 23 | void onDone(CameraSession session); |
| 24 | void onFailure(FailureType failureType, String error); |
| 25 | } |
| 26 |
| 27 | // Events are fired on the camera thread. |
| 28 | interface Events { |
| 29 | void onCameraOpening(); |
| 30 | void onCameraError(CameraSession session, String error); |
| 31 | void onCameraDisconnected(CameraSession session); |
| 32 | void onCameraClosed(CameraSession session); |
| 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 | */ |
| 40 | void stop(); |
| 41 |
| 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 |
| 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 |