/*
 * Copyright (c) 2010-2022 Belledonne Communications SARL.
 *
 * This file is part of mediastreamer2 
 * (see https://gitlab.linphone.org/BC/public/mediastreamer2).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.linphone.mediastream.video.capture;

import org.linphone.mediastream.Log;

import android.graphics.ImageFormat;
import android.hardware.Camera;
 
public class AndroidVideoApi8JniWrapper {

	static public int detectCamerasCount() {
		return AndroidVideoApi5JniWrapper.detectCamerasCount();
	}

	static public int detectCameras(int[] indexes, int[] frontFacing, int[] orientation) {
		return AndroidVideoApi5JniWrapper.detectCameras(indexes, frontFacing, orientation);
	}
	
	static public int[] selectNearestResolutionAvailable(int cameraId, int requestedW, int requestedH) {
		return AndroidVideoApi5JniWrapper.selectNearestResolutionAvailable(cameraId, requestedW, requestedH);
	}
	
	public static Object startRecording(int cameraId, int width, int height, int fps, int rotation, final long nativePtr) {
		Log.d("startRecording(" + cameraId + ", " + width + ", " + height + ", " + fps + ", " + rotation + ", " + nativePtr + ")");
		Camera camera = null;
		try{
			camera = Camera.open();

			AndroidVideoApi5JniWrapper.applyCameraParameters(camera, width, height, fps);

			int bufferSize = (width * height * ImageFormat.getBitsPerPixel(camera.getParameters().getPreviewFormat())) / 8;
			camera.addCallbackBuffer(new byte[bufferSize]);
			camera.addCallbackBuffer(new byte[bufferSize]);

			camera.setPreviewCallbackWithBuffer(new Camera.PreviewCallback() {
				public void onPreviewFrame(byte[] data, Camera camera) {
					if (AndroidVideoApi5JniWrapper.isRecording) {
						// forward image data to JNI
						AndroidVideoApi5JniWrapper.putImage(nativePtr, data);
						camera.addCallbackBuffer(data);
					}
				}
			});

			camera.startPreview();
			AndroidVideoApi5JniWrapper.isRecording = true;
		}catch(Throwable e){
			Log.e("AndroidVideoApi8JniWrapper.startRecording(): caught exception " + e);
		}
		Log.d("Returning camera object: " + camera);
		return camera; 
	} 
	
	public static void stopRecording(Object cam) {
		AndroidVideoApi5JniWrapper.isRecording = false;
		Log.d("stopRecording(" + cam + ")"); 
		Camera camera = (Camera) cam;
		 
		if (camera != null) {
			try{
				camera.setPreviewCallbackWithBuffer(null);
				camera.stopPreview();
				camera.release();
			}catch(Throwable e){
				Log.e("AndroidVideoApi8JniWrapper.stopRecording(): caught exception " + e);
			}
		} else {
			Log.i("Cannot stop recording ('camera' is null)");
		}
	} 
	
	public static void setPreviewDisplaySurface(Object cam, Object surf) {
		AndroidVideoApi5JniWrapper.setPreviewDisplaySurface(cam, surf);
	}
}
