Skip to content

Instantly share code, notes, and snippets.

@tomthecarrot
Created October 6, 2021 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomthecarrot/b3c22696e7b27c7e13404696cc084818 to your computer and use it in GitHub Desktop.
Save tomthecarrot/b3c22696e7b27c7e13404696cc084818 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using NatSuite.Recorders;
using NatSuite.Recorders.Clocks;
public class NatcorderAndroidCrashExample : MonoBehaviour
{
private void Start()
{
//for ease of operation, generate video from the Start method
GenerateVideo();
}
//hookup to a button or call from Start()
public void GenerateVideo()
{
Debug.LogFormat("creating demo texture2d pixels");
Texture2D demoTexture = new Texture2D(VIDEO_WIDTH, VIDEO_HEIGHT);
Color32[] demoPixels = demoTexture.GetPixels32();
MP4Recorder natcorder = new MP4Recorder(VIDEO_WIDTH, VIDEO_HEIGHT, FRAMERATE,
AUDIO_FREQ, AUDIO_CHANNELS);
//run on a separate thread
Task encodeTask = Task.Run(new Func<Task>(async () => { await GenerateVideoTask(demoPixels, natcorder); } ));
}
private async Task GenerateVideoTask(Color32[] demoPixels, MP4Recorder natcorder)
{
Debug.LogFormat("creating natcorder mp4 recorder with audio");
int audioSamplesPerVideoFrame = AUDIO_FREQ / (int)FRAMERATE; //a clean 1470 at 30fps 44.1khz in this case
Debug.LogFormat("creating demo audio samples for all frames");
int totalFrames = (int)(LENGTH_SECONDS * FRAMERATE);
float[] demoAudioSamples = new float[audioSamplesPerVideoFrame * totalFrames];
FixedIntervalClock natclock = new FixedIntervalClock(FRAMERATE);
for(int i = 0; i < totalFrames; i++)
{
long timestamp = natclock.timestamp;
Debug.LogFormat("Committing frame {0} pixels and audio", i);
natcorder.CommitFrame(demoPixels, timestamp);
}
natcorder.CommitSamples(demoAudioSamples, 0); // audio starts at T=0
Debug.LogFormat("Finishing Natcorder...");
//this will trigger a hard crash
//on Android SDK
//Pkg.Desc=Android SDK Platform 11
//Pkg.UserSrc=false
//Platform.Version=11
//Platform.CodeName=
//Pkg.Revision=3
//AndroidVersion.ApiLevel=30
//Layoutlib.Api=15
//Layoutlib.Revision=1
//Platform.MinToolsRev=22
//Phone: Pixel 2 XL
//Android Vesion 11
//Android Build number PR1A.201005.004
//Android Studio Version 4.2.2
//Unity Version 2020.3.4f1
await natcorder.FinishWriting();
Debug.LogFormat("Natcorder Finished....but am I ever reached?");
}
private const int VIDEO_WIDTH = 720;
private const int VIDEO_HEIGHT = 1280;
private const float LENGTH_SECONDS = 15f;
private const float FRAMERATE = 30f;
private const int AUDIO_FREQ = 44100;
private const int AUDIO_CHANNELS = 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment