@bezlepkin/nativescript-ar
Version:
NativeScript Augmented Reality plugin. ARKit on iOS and (with the help of Sceneform) ARCore on Android.
153 lines (152 loc) • 5.55 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideoRecorder = void 0;
const core_1 = require("@nativescript/core");
const DEFAULT_FRAMERATE = 30;
const DEFAULT_BITRATE = 10000000;
const FALLBACK_QUALITY_LEVELS = [
android.media.CamcorderProfile.QUALITY_HIGH,
android.media.CamcorderProfile.QUALITY_2160P,
android.media.CamcorderProfile.QUALITY_1080P,
android.media.CamcorderProfile.QUALITY_720P,
android.media.CamcorderProfile.QUALITY_480P
];
class VideoRecorder {
constructor() {
this.recordingVideoFlag = false;
this.bitRate = DEFAULT_BITRATE;
this.frameRate = DEFAULT_FRAMERATE;
this.recordingVideoFlag = false;
}
static fromFragment(fragment) {
const videoRecorder = new VideoRecorder();
videoRecorder.setSceneView(fragment.getArSceneView());
return videoRecorder;
}
setSceneView(sceneView) {
this.sceneView = sceneView;
}
getVideoPath() {
return this.videoPath;
}
setBitRate(bitRate) {
this.bitRate = bitRate;
}
setFrameRate(frameRate) {
this.frameRate = frameRate;
}
onToggleRecord() {
if (this.recordingVideoFlag) {
this.stopRecordingVideo();
}
else {
this.startRecordingVideo();
}
return this.recordingVideoFlag;
}
startRecordingVideo() {
if (this.recordingVideoFlag) {
throw "already recording";
}
if (this.mediaRecorder == null) {
this.mediaRecorder = new android.media.MediaRecorder();
}
this.videoPath = null;
try {
this.buildFilename();
this.setUpMediaRecorder();
}
catch (e) {
console.error(e);
throw "Exception setting up recorder";
}
this.encoderSurface = this.mediaRecorder.getSurface();
this.sceneView.startMirroringToSurface(this.encoderSurface, 0, 0, this.videoSize.getWidth(), this.videoSize.getHeight());
this.recordingVideoFlag = true;
}
buildFilename() {
if (this.videoDirectory == null) {
this.videoDirectory =
new java.io.File(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES)
+ "/ARVideos");
}
if (this.videoBaseName == null) {
this.videoBaseName = "Sample";
}
this.videoPath =
new java.io.File(this.videoDirectory, this.videoBaseName + java.lang.Long.toHexString(java.lang.System.currentTimeMillis()) + ".mp4");
const dir = this.videoPath.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
}
stopRecordingVideo() {
if (!this.recordingVideoFlag) {
throw "not recording";
}
this.recordingVideoFlag = false;
if (this.encoderSurface != null) {
this.sceneView.stopMirroringToSurface(this.encoderSurface);
this.encoderSurface = null;
}
this.mediaRecorder.stop();
this.mediaRecorder.reset();
}
setUpMediaRecorder() {
this.mediaRecorder.setVideoSource(android.media.MediaRecorder.VideoSource.SURFACE);
this.mediaRecorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.MPEG_4);
this.mediaRecorder.setOutputFile(this.videoPath.getAbsolutePath());
this.mediaRecorder.setVideoEncodingBitRate(this.bitRate);
this.mediaRecorder.setVideoFrameRate(this.frameRate);
this.mediaRecorder.setVideoSize(this.videoSize.getWidth(), this.videoSize.getHeight());
this.mediaRecorder.setVideoEncoder(this.videoCodec);
this.mediaRecorder.prepare();
try {
this.mediaRecorder.start();
}
catch (e) {
console.error("Exception starting capture: " + e.getMessage());
}
}
setVideoSize(width, height) {
this.videoSize = new android.util.Size(width, height);
}
setVideoQualityAuto() {
const orientation = core_1.Application.android.context.getResources().getConfiguration().orientation;
this.setVideoQuality(android.media.CamcorderProfile.QUALITY_2160P, orientation);
}
setVideoQuality(quality, orientation) {
let profile = null;
if (android.media.CamcorderProfile.hasProfile(quality)) {
profile = android.media.CamcorderProfile.get(quality);
}
if (profile == null) {
for (let level of FALLBACK_QUALITY_LEVELS) {
if (android.media.CamcorderProfile.hasProfile(level)) {
profile = android.media.CamcorderProfile.get(level);
console.log(">> profile found: " + profile);
break;
}
}
}
if (profile == null) {
return;
}
if (orientation === android.content.res.Configuration.ORIENTATION_LANDSCAPE) {
this.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight);
}
else {
this.setVideoSize(profile.videoFrameHeight, profile.videoFrameWidth);
}
this.setVideoCodec(profile.videoCodec);
this.setBitRate(profile.videoBitRate);
this.setFrameRate(profile.videoFrameRate);
}
setVideoCodec(videoCodec) {
this.videoCodec = videoCodec;
}
isRecording() {
return this.recordingVideoFlag;
}
}
exports.VideoRecorder = VideoRecorder;