Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

NatCorder - Video Recording API

Discussion in 'Assets and Asset Store' started by Lanre, Nov 18, 2017.

  1. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    Can you share the full logs from Xcode in a .txt attachment? I'm not sure why the recorder could be failing on the iPhone 6; I haven't had any similar issue. Does the MP4Recorder work?
     
  2. TomasArguello

    TomasArguello

    Joined:
    Jan 25, 2021
    Posts:
    6
    Hi,
    I am having performance issues using NatCorder for a project using Oculus Quest. I do this when a player enters a room:
    Code (CSharp):
    1. clock = new RealtimeClock();
    2. videoCapture = new MP4Recorder(640, 480, 72, 48000, 2);
    3. cameraCapture = new CameraInput(videoCapture, clock, observerCamera);
    observerCamera is a specific camera in the scene. And when the player is finished I do this:
    Code (CSharp):
    1. cameraCapture.Dispose();
    2. var path = videoCapture.FinishWriting();
    It records video and its smooth for the first few seconds but then it starts to lag badly to the point I can't even exit from the game. This is in the Editor done via Oculus Link. I haven't tried it out yet on the Quest by itself. Is there something I am missing? I also tried to run it on a background thread but I get a message saying that it must be initialized on the main thread. Please help.
    EDIT: I decided to delete the 48000 and 2 from the MP4Recorder initialization and it records very smoothly! I don't really know why tho... And now no audio is recorded. Do I need to manually add audioListener or something of the sort?
     
    Last edited: Feb 2, 2021
  3. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    If you are on Windows, this happens when you don't commit audio frames. I see that you aren't creating an AudioInput, and aren't manually committing audio frames.
     
  4. TomasArguello

    TomasArguello

    Joined:
    Jan 25, 2021
    Posts:
    6
    @Lanre How do I commit audio frames? This is what I've done and the resulting video doesn't have any audio.
    Here is what I initialize.
    Code (CSharp):
    1. public Camera observerCamera;
    2. MP4Recorder videoCapture;
    3. AudioInput audioCapture;
    4. CameraInput cameraCapture;
    5. IClock clock;
    6.  
    7. public AudioListener observerListener;
    Here is the code that starts recording video.
    Code (CSharp):
    1. public void StartRecording()
    2.     {
    3.         clock = new RealtimeClock();
    4.         videoCapture = new MP4Recorder(1280, 720, 72);
    5.         audioCapture = new AudioInput(videoCapture, clock, observerListener);
    6.         cameraCapture = new CameraInput(videoCapture, clock, observerCamera);
    7.     }
    Then I make sure to dispose of the audio as well.
    Code (CSharp):
    1. public void StopRecording()
    2.     {
    3.         audioCapture.Dispose();
    4.         cameraCapture.Dispose();
    5.         var path = videoCapture.FinishWriting();
    6.         Debug.Log("Finished writing the video!");
    7.     }
    Am I missing anything?
     
  5. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    You don't specify an audio format. Trying to commit audio frames without specifying a format can result in a hard crash on some platforms. You need to pass the sample rate and channel count to the recorder's constructor (you need to get these values from Unity's audio settings).
     
  6. TomasArguello

    TomasArguello

    Joined:
    Jan 25, 2021
    Posts:
    6
    It works now, thanks for your help!
     
    Lanre likes this.
  7. JacekCi

    JacekCi

    Joined:
    Feb 23, 2018
    Posts:
    6
    Please find in the attachment logs from XCode and advise.

    When I tried MP4Recorder, it's working on iPhone 6. If we can't fix it for HEVCRecorder, is there a way to detect that HEVCRecorder is not supported so only then we would default to MP4Recorder?
     

    Attached Files:

    Last edited: Feb 3, 2021
  8. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    It looks like recording is failing immediately. I recommend switching to the MP4Recorder on this device. There is likely some incompatibility issue at play.
     
  9. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    Hello,

    Does NAT CORDER and NAT MIC etc work on PC or MAC desktop computers?

    If not, does anybody know of a way to do the same things NAT corder and NAT mic does for desktop?

    Thanks
     
  10. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
  11. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    I bought nat corder and nat mic.

    I hear you have a new upgraded asset, can you please tell me the name of it so i can buy it?
    Thanks
     
  12. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    NatMic (along with NatCam) was replaced with NatDevice. NatDevice allows you to use the hardware camera and microphone, and is specifically built around high performance, low-latency streaming. I recommend checking out the online docs to get a good sense of what it offers.
     
  13. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    Hi Lanre!
    I hope you are doing well!

    Looks like we have a similar issue to the one mentioned here:

    I tried to access the GitHub repo link to the open issue but the repo seems down.

    We are having an issue when recording with Natcorder. The app crashes and we get the following log:

    upload_2021-2-6_17-17-38.png

    upload_2021-2-6_17-16-56.png

    Should I buy NatDevice to fix that issue?

    Here are the phone specs where we get that issue:

    Samsung Galaxy A51 Android smartphone. Announced Dec 2019. Features 6.5″ display, Exynos 9611 chipset, 4000 mAh battery, 256 GB storage, 8 GB RAM, Corning Gorilla Glass 3.

    Thank you
     
    Last edited: Feb 6, 2021
  14. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    I believe this has been fixed in the latest version of NatShare. NatShare is no longer distributed on the Asset Store or as a unitypackage; it now uses the Unity Package Manager. So simply follow the instructions in the repo I linked.

    EDIT: None of these errors have anything to do with NatCorder or NatDevice; they are solely from (what looks like an older version of) NatShare.
     
  15. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    Hi Lanre!

    Thanks a lot for the quick response!

    Upgrading the NatShare version to the latest version using the GitHub repo solved that issue! I think having a big bold warning in the Asset Description to use the Github repo as package to get the latest version would be very useful!

    But now we have another issue, it does not crash the mobile app now but we get the following errors:
    upload_2021-2-8_15-0-4.png

    I have also attached the full log file to help you troubleshoot.

    Here are the phone specs where we get that issue (Same phone):

    Samsung Galaxy A51 Android smartphone. Announced Dec 2019. Features 6.5″ display, Exynos 9611 chipset, 4000 mAh battery, 256 GB storage, 8 GB RAM, Corning Gorilla Glass 3.

    Thanks :)
     

    Attached Files:

  16. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    You are not committing any audio, even though you initialize the recorder to expect audio:
    Code (CSharp):
    1. Info MPEG4Writer Received total/0-length (0/0) buffers and encoded 0 frames. - Audio
     
  17. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    Hi Lanre,

    That's was definitely the error! A checkbox in the inspector that should not have been checked, was checked!

    Thank you! Very much appreciated the support seriously!
     
    Lanre likes this.
  18. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    Hey Lanre,

    I'm trying to add Audio to the video but it doesn't work.

    I built the ReplayCam Example scene within NatSuite subfolder and I'm not getting any audio there too.

    I did saved the scene with Record Microphone ON and I don't get any audio when the video is played back on my phone.

    I'm using Unity 2020.1.6f1

    Natcorder: Version 1.7.3 - August 06, 2020

    upload_2021-2-8_17-22-45.png

    upload_2021-2-8_17-23-9.png

    And this is our app's code which is very similar to the demo scene:


    Code (CSharp):
    1.         [ShowInInspector, ReadOnly, PropertyOrder(999), TabGroup("Advanced/Extra", "Debug")]
    2.         private IMediaRecorder recorder;
    3.         [ShowInInspector, ReadOnly, PropertyOrder(999), TabGroup("Advanced/Extra", "Debug")]
    4.         private CameraInput cameraInput;
    5.         [ShowInInspector, ReadOnly, PropertyOrder(999), TabGroup("Advanced/Extra", "Debug")]
    6.         private string lastRecordedVideoPath;
    7.         [ShowInInspector, ReadOnly, PropertyOrder(999), TabGroup("Advanced/Extra", "Debug")]
    8.         private AudioInput audioInput;
    9.         [ShowInInspector, ReadOnly, PropertyOrder(999), TabGroup("Advanced/Extra", "Debug")]
    10.         private AudioSource microphoneSource;
    11.  
    12.         /*
    13.             ========================================================================================
    14.             CALLBACKS
    15.             ========================================================================================
    16.         */
    17.  
    18.         protected new void Start() {
    19.             base.Start();
    20.             StartCoroutine("AddMicrophoneSource");
    21.         }
    22.  
    23.         private void OnDestroy() {
    24.             StopMicrophone();
    25.         }
    26.  
    27.         /*
    28.             ========================================================================================
    29.             FUNCTIONS
    30.             ========================================================================================
    31.         */
    32.  
    33.         public void StartRecording() {
    34.             var frameRate = 30;
    35.             var sampleRate = recordMicrophone ? AudioSettings.outputSampleRate : 0;
    36.             var channelCount = recordMicrophone ? (int)AudioSettings.speakerMode : 0;
    37.             var clock = new RealtimeClock();
    38.             recorder = new MP4Recorder(videoWidth, videoHeight, frameRate, sampleRate, channelCount);
    39.  
    40.             // Create recording inputs
    41.             cameraInput = new CameraInput(recorder, clock, Camera.main);
    42.  
    43.             audioInput = recordMicrophone ? new AudioInput(recorder, clock, microphoneSource, true) : null;
    44.             // Unmute microphone
    45.             microphoneSource.mute = audioInput == null;
    46.         }
    47.  
    48.         public async void StopRecording() {
    49.             TriggerOutputEvent1();
    50.  
    51.             // Mute microphone
    52.             microphoneSource.mute = true;
    53.  
    54.             // Stop recording
    55.             audioInput?.Dispose();        
    56.             cameraInput.Dispose();        
    57.             string path = await recorder.FinishWriting();
    58.             lastRecordedVideoPath = path;
    59.             TriggerOutputEvent2(path);
    60.  
    61.             // Playback recording
    62.             Debug.Log($"Saved ARpresentation recording to: {path}");        
    63.         }
    64.  
    65.         private IEnumerator AddMicrophoneSource() {
    66.             microphoneSource = gameObject.AddComponent<AudioSource>();
    67.             microphoneSource.mute = false;
    68.             microphoneSource.loop = true;
    69.             microphoneSource.bypassEffects = false;
    70.             microphoneSource.bypassListenerEffects = false;
    71.             microphoneSource.clip = Microphone.Start(null, true, 10, AudioSettings.outputSampleRate);
    72.             yield return new WaitUntil(() => Microphone.GetPosition(null) > 0);
    73.             microphoneSource.Play();
    74.         }
    75.  
    76.         private void StopMicrophone() {
    77.             microphoneSource.Stop();
    78.             Microphone.End(null);
    79.         }
    We try building the demo scene on mobile or with the Editor on windows 10 and no luck.

    We get the same result when building our app and in the Editor on windows 10.

    EDIT: I also tried with a simple AudioListener and passing it to the AudioInput constructor instead of using a AudioSource but it didn't work.

    Maybe it's something simple we forgot to do...
     
    Last edited: Feb 9, 2021
  19. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    I forgot to attach the log file of the example scene
     

    Attached Files:

  20. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    I also didn't changed the default Audio settings on the Project but here they are :

    upload_2021-2-8_19-18-26.png
     
  21. JacekCi

    JacekCi

    Joined:
    Feb 23, 2018
    Posts:
    6
    We are also seeing issues on other Android devices (crash in case of Android on Honor 8A). Since exception is not thrown at the moment when we create new HEVCRecorder, can you recommend the way to detect if HEVCRecorder is supported?

    I see "Invalid video recorder state: 3" and then exception when trying to write the video which is already too late.

    I would like to get an option to check if HEVCRecorder is supported before we start recording.
     
  22. CBMUN

    CBMUN

    Joined:
    Nov 8, 2013
    Posts:
    2
    Hello. I save the video files using mp4 recorder and audioInput in natcorder.

    I implemented the function both IOS and Android and proceeded with the video shooting.
    However, howling (aka audio feedback) occurs on some smartphones when recording videos on Android.
    I wonder if there is any solution to the above situation.

    Test conducted through code progression and analogy

    1) Press the record button to start recording MP4 format.
    2) Audio listeners listen to background music and microphone clips.
    3) The audio listener's sound is transmitted through the speaker.
    4) Sound feedback and Larsen effect occur on certain devices.
    5) To solve this phenomenon, I tried to apply audio mixer and filter, but the sound feedback phenomenon did not improve.

    Below is the code.

    Code (CSharp):
    1.  
    2. IEnumerator TakeRecodingAndSave()
    3.     {
    4.         recorder = new MP4Recorder(mainCam.pixelWidth, mainCam.pixelHeight, 30f, AudioSettings.outputSampleRate, (int)AudioSettings.speakerMode);
    5.         clock = new RealtimeClock();
    6.         cameraInput = new CameraInput(recorder, clock, mainCam);
    7.         audioInput = new AudioInput(recorder, clock,gameObject.GetComponent<AudioListener>());
    8.  
    9.         // media?.CommitSamples(sampleBuffer, clock.timestamp);
    10.  
    11.         Debug.Log(GetComponent<AudioListener>().gameObject.name);
    12.         long sec = clock.timestamp;
    13.         UIMgr.instance.img_RecordTime.gameObject.SetActive(true);
    14.         // Microphone recording
    15.         microphoneSource.mute = audioInput == null;
    16.         microphoneSource.loop = true;
    17.         microphoneSource.bypassEffects =
    18.         microphoneSource.bypassListenerEffects = false;
    19.  
    20.         if (rec)
    21.             microphoneSource.clip = Microphone.Start(null, true, 10, AudioSettings.outputSampleRate);
    22.         yield return new WaitUntil(() => Microphone.GetPosition(null) > 0);
    23.         microphoneSource.Play();
    24.         while (rec)
    25.         {
    26.             if (clock.timestamp >= sec + (1 * (1e+9)))
    27.             {
    28.                 sec = clock.timestamp;
    29.                 UIMgr.instance.txt_RecordTime.text = (string.Format("{00:00}:{01:00}", (int)(sec / (6e+10)), (int)((sec / 1e+9)) % 60));
    30.             }
    31.             yield return new WaitForFixedUpdate();
    32.         }
    33.         yield return new WaitForEndOfFrame();
    34.         UIMgr.instance.txt_RecordTime.text = "00:00";
    35.         UIMgr.instance.img_RecordTime.gameObject.SetActive(false);
    36.        
    37.         UIMgr.instance.MessageObject.MSG_Call(eMESSAGE_TYPE.SAVE);
    38.     }
    39.  
    40.  
     
  23. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    Check that your app has microphone permissions. A simple test for this is to simply playback the microphone clip in an empty scene. You should hear audio feedback. If you hear nothing, then that means the microphone isn't actually streaming.
     
    danUnity likes this.
  24. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    On Android, an exception will be thrown immediately you create the recorder. On iOS, HEVC should always be supported, which is why I'm not sure about the error that pops up on some devices.
     
  25. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    You should not playback the microphone in the scene because you will get audio feedback. Instead, record the audio source itself and when you create the `AudioInput`, set the `mute` flag to `true`. This will prevent feedback. See how the ReplayCam example does it.
     
  26. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    The permission was definitely the issue! Thanks a lot :)
     
    Lanre likes this.
  27. TomasArguello

    TomasArguello

    Joined:
    Jan 25, 2021
    Posts:
    6
    Hey Lanre, I am working on a project for the Oculus Quest. Currently I have a camera that records the scene and saves the video but the only problem is that the frame rate is consistently around 35-40 frames per second. The project needs it at 72 frames per second. Is there a way to increase the frame rate to at least higher than 60 frames per second?
     
  28. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    Is your app rendering with OpenGL ES3 or Vulkan? And what is your recording resolution?
     
  29. SbrenmanMSG

    SbrenmanMSG

    Joined:
    Jun 18, 2020
    Posts:
    1

    Hi, what is your workaround?

    Thanks
     
  30. TomasArguello

    TomasArguello

    Joined:
    Jan 25, 2021
    Posts:
    6
    So I was using a different camera to record the session from a different point of view instead of the camera of the OVR rig. When I removed the different camera and started recording from the camera of the OVR rig, the frame rate went up to about 55 frames per second. The resolution used was 640 x 360. I am currently seeing what is being used to render the app.
    EDIT: It seems I am using OpenGL ES3 for the project.
     
    Last edited: Feb 10, 2021
  31. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    OpenGL ES3 will give you much worse recording performance. You can read about it here. I'm working on a free package that will provide recording primitives with much better performance on Android when rendering with OpenGL ES3. This has been a pain point for devs using NatCorder for recording in their AR games, given that ARCore does not support Vulkan.
     
  32. TomasArguello

    TomasArguello

    Joined:
    Jan 25, 2021
    Posts:
    6
    So I tried using Vulkan but the application keeps crashing when it starts recording video instead. Do I need to lower the resolution? Are there any reasons why it would crash?
     
  33. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    Get the logs from logcat and attach them in a .txt attachment. The logs will contain any reason for a crash.
     
  34. Klamore74

    Klamore74

    Joined:
    Jun 17, 2013
    Posts:
    103
    I just buy the plugin and try it on an Android device with the example scene "WebCam".

    The Autorotation doesn't work, but for now, is not a problem for now.

    I'm worry about the quality of recording: there are a lot of tearing like that:



    This is the entire video: http://www.theeyefeel.com/tmp/record.mp4

    How to remove it?
     
  35. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    Tearing is likely coming from the WebCamTexture. NatCorder does not support autorotation. Do you get tearing when you record the ReplayCam example? And what device and OS version are you testing on?
     
  36. Batataglins

    Batataglins

    Joined:
    May 6, 2018
    Posts:
    15
    Hello.

    I'm working in a project that was already implemented with your API. Now we want to add a logo at our videos, like tiktok does.

    It's possible do something like that? And how?

    Thanks
     
  37. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    Yes. You can use a second game camera that renders a UI canvas with your watermark. Disable the camera in the scene, but pass it to the CameraInput when you start recording.
     
  38. Batataglins

    Batataglins

    Joined:
    May 6, 2018
    Posts:
    15
    I'm already doing this. Just to clearify, using a CameraInput, it will also record what is on the screen?

    Code (CSharp):
    1. var frameRate = 60;
    2.         var sampleRate = AudioSettings.outputSampleRate;
    3.         var channelCount = (int)AudioSettings.speakerMode;
    4.         var clock = new RealtimeClock();
    5.         recorder = new MP4Recorder(Screen.width, Screen.height, frameRate, sampleRate, channelCount);
    6.         // Create recording inputs
    7.         cameraInput = new CameraInput(recorder, clock, _Camera);
    8.         audioInput = new AudioInput(recorder, clock, microphoneSource, true);
    9.         // Unmute microphone
    10.         microphoneSource.mute = audioInput == null;
     
  39. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    I don't know what you `_Camera` variable is. Is it an array of cameras or a single camera? CameraInput records game cameras, not the screen, so you have to give it all the cameras you want recorded. I'm suggesting recording your 'normal' game camera along with a second, disabled game camera that only sees your watermark.

    Also, don't record at screen resolution. A high res screen will kill performance, and recording with an odd resolution (odd number width or height) will crash on some devices.
     
    Batataglins likes this.
  40. Batataglins

    Batataglins

    Joined:
    May 6, 2018
    Posts:
    15
    My bad, it's my main camera (and only one) with culling mask set to 'everyting'. So it should be recording the entire UI, right?
     
  41. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    Create a second camera along with a UI canvas which is set to 'Screen Space - Camera' render mode (and of course set the disabled camera you just created). Disable the second camera in the scene (so that you don't see the watermark on screen). Then when you create the camera input, pass in your main camera and this disabled camera.
     
  42. Klamore74

    Klamore74

    Joined:
    Jun 17, 2013
    Posts:
    103
    Just like I wrote : "try it on an Android device with the example scene "WebCam".
     
  43. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    I don't get tearing which is why I'm asking you. And "an Android device" is generic, I'm trying to know what specific device and OS version to see if I can find any relevant information on it. Finally, testing ReplayCam will allow you to isolate it to WebCamTexture. Help me help you.
     
  44. backwheelbates

    backwheelbates

    Joined:
    Jan 14, 2014
    Posts:
    232
    Hi @Lanre ,

    Just looking for some advice. I have a coroutine that encodes a specific number of frames, its based on your CameraInput.cs script.

    Although since I know ahead of time, how many frames I'm capturing, I attempted to put the finish writing task at the bottom of this coroutine by calling this task function.

    Code (CSharp):
    1. public async Task StopRecording()
    2. {
    3. // write out video file
    4. var path = await cameraRecorderInput.recorder.FinishWriting();
    5. }
    Unfortunately it consistently crashes. Do you have any advice how I can write the file, from within the initial coroutine?

    Thanks for any advice!
     
  45. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,976
    Can you share the code for the coroutine? This likely happens because your coroutine is inadvertently calling a method on the recorder after `FinishWriting`. This will happen if you use AsyncGPUReadback for example, because the callback will get invoked a few frames after you actually call that function.
     
  46. backwheelbates

    backwheelbates

    Joined:
    Jan 14, 2014
    Posts:
    232
    EDIT:

    I think I found the issue. Non-divisible-by-two resolutions were crashing it. So far this seems to have fixed it, I'll do more testing and report back if its still crashing on ios builds.

    Using this to check the resolution
    Code (CSharp):
    1.         public static int closestInteger(int a, int b)
    2.         {
    3.             int c1 = a - (a % b);
    4.             int c2 = (a + b) - (a % b);
    5.             if (a - c1 > c2 - a)
    6.             {
    7.                 return c2;
    8.             }
    9.             else
    10.             {
    11.                 return c1;
    12.             }
    13.         }
    Fixing the resolution
    Code (CSharp):
    1.             var width = (int)captureResolution.x;
    2.             width = closestInteger(width, 2);
    3.             var height = (int)captureResolution.y;
    4.             height = closestInteger(height, 2);


    Thanks for the super quick reply @Lanre !

    Yes, you are correct, Im using the AsyncGPUReadback.Request as in your CameraInput. I guess I could track the async request progress and wait for this before starting the FinishWriting. Unless you have a better approach.

    Code (CSharp):
    1.                 if (SystemInfo.supportsAsyncGPUReadback)
    2.                     AsyncGPUReadback.Request(frameBuffer, 0, request => {
    3.                         if (pixelBuffer != null) {
    4.                             request.GetData<byte>().CopyTo(pixelBuffer);
    5.                             recorder.CommitFrame(pixelBuffer, timestamp);
    6.                         }
    7.                     });

    Here is a larger view of the code, Im trying with and without async, and its crashing either way.
    Code (CSharp):
    1. //
    2.             bool allow_async = false;// SystemInfo.supportsAsyncGPUReadback && false;
    3.  
    4.             // build up recorder data
    5.             cameraRecorderInput = new CameraRecorderInput();
    6.             cameraRecorderInput.recorder = new MP4Recorder(width, height, framerate, captureBitrate, 0);
    7.             cameraRecorderInput.cameras = new Camera[1] { mainCamera };
    8.             cameraRecorderInput.clock = new FixedIntervalClock(30);
    9.             cameraRecorderInput.frameDescriptor = new RenderTextureDescriptor(width, height, RenderTextureFormat.ARGB32, 24);
    10.             cameraRecorderInput.readbackBuffer = allow_async ? null : new Texture2D(width, height, TextureFormat.RGBA32, false, false);
    11.             cameraRecorderInput.attachment = cameraRecorderInput.cameras[0].gameObject.AddComponent<CameraInputAttachment>();
    12.             cameraRecorderInput.pixelBuffer = new byte[width * height * 4];
    13.             cameraRecorderInput.frameDescriptor.sRGB = true;
    14.  
    15.             //
    16.             var endOfFrame = new WaitForEndOfFrame();
    17.  
    18.             // record
    19.             float altTime = start;
    20.             while (altTime <= end)
    21.             {
    22.                 if (PlayerObserver.Instance != null)
    23.                 {
    24.                     // track captured frames
    25.                     cameraRecorderInput.capture_queue++;
    26.                     var tickTime = ((1 / playbackFPS) * speed);
    27.  
    28.                     // update animation
    29.                     PlayerObserver.Instance.setAltTimeManually(altTime);
    30.  
    31.                     // wait for end of frame
    32.                     yield return endOfFrame;
    33.  
    34.                     // Render camera
    35.                     var frameBuffer = RenderTexture.GetTemporary(cameraRecorderInput.frameDescriptor);
    36.                     for (var i = 0; i < cameraRecorderInput.cameras.Length; i++)
    37.                     {
    38.                         var prevTarget = cameraRecorderInput.cameras[i].targetTexture;
    39.                         cameraRecorderInput.cameras[i].targetTexture = frameBuffer;
    40.                         cameraRecorderInput.cameras[i].Render();
    41.                         cameraRecorderInput.cameras[i].targetTexture = prevTarget;
    42.                     }
    43.                     // Readback and commit
    44.                     var timestamp = cameraRecorderInput.clock.timestamp;
    45.                     if (allow_async)
    46.                         AsyncGPUReadback.Request(frameBuffer, 0, request =>
    47.                         {
    48.                             if (cameraRecorderInput.pixelBuffer != null)
    49.                             {
    50.                                 request.GetData<byte>().CopyTo(cameraRecorderInput.pixelBuffer);
    51.                                 cameraRecorderInput.recorder.CommitFrame(cameraRecorderInput.pixelBuffer, timestamp);
    52.                                 cameraRecorderInput.capture_queue--;
    53.                             }
    54.                         });
    55.                     else
    56.                     {
    57.                         var prevActive = RenderTexture.active;
    58.                         RenderTexture.active = frameBuffer;
    59.                         cameraRecorderInput.readbackBuffer.ReadPixels(new Rect(0, 0, frameBuffer.width, frameBuffer.height), 0, 0, false);
    60.                         cameraRecorderInput.readbackBuffer.GetRawTextureData<byte>().CopyTo(cameraRecorderInput.pixelBuffer);
    61.                         cameraRecorderInput.recorder.CommitFrame(cameraRecorderInput.pixelBuffer, timestamp);
    62.                         cameraRecorderInput.capture_queue--;
    63.                         RenderTexture.active = prevActive;
    64.                     }
    65.                     RenderTexture.ReleaseTemporary(frameBuffer);
    66.  
    67.                     // tick time
    68.                     altTime += ((1 / playbackFPS) * speed);
    69.                 }
    70.             }
    71.  
    72.             // finish recording
    73.             while (cameraRecorderInput.capture_queue != 0)
    74.             {
    75.                 // because frames are captured async, wait till the async operations are done
    76.                 yield return endOfFrame;
    77.             }
    78.  
    79.             //
    80.             StopRecording();
    Here is the StopRecording function as well.
    Code (CSharp):
    1. public async Task StopRecording()
    2. {
    3. // write out video file
    4. var path = await cameraRecorderInput.recorder.FinishWriting();
    5. }

    Thanks again for your help!!
     
    Last edited: Feb 19, 2021
    ROBYER1 and Lanre like this.
  47. mskalash

    mskalash

    Joined:
    Oct 24, 2016
    Posts:
    5
    Hello, I have a problem. The first time you launch the video, the application crashes. After reboot everything is fine.
    Telephone: Xiaomi POCOPHONE F1

     
  48. Klamore74

    Klamore74

    Joined:
    Jun 17, 2013
    Posts:
    103
    For who is facing the same problem, can be resolved force frame rate and vsync:
    Application.targetFrameRate = framerate;
    QualitySettings.vSyncCount = 1;
     
    Lanre likes this.
  49. Klamore74

    Klamore74

    Joined:
    Jun 17, 2013
    Posts:
    103
    Hi,

    I have a simple task: Record a video with audio from camera and microphone at specific resolution.

    I bought also NatDevice for use advance features, but I didn't find an example that combine both camera and microphone with synch.

    The example ReplayCam procude horrible video with audio out of sync of about 1/2 seconds.

    Thanks.
     
  50. danUnity

    danUnity

    Joined:
    Apr 28, 2015
    Posts:
    229
    Hi,

    I'm having performance issues with Natcorder on Android, the fps goes from around 30 fps to around 20 fps when recording. I'm testing on a Samsung S7 Edge.

    This is an AR foundation app so I can't use Multithreading on Android and I can't use Vulkan so I'm stuck with OpenGLES3 unless I'm mistaken about those... Is there any other way to improve performance?