Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NatCorder - Video Recording API

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

  1. infosekr

    infosekr

    Joined:
    Jul 12, 2013
    Posts:
    46
    Hi there,
    I'm able to record and playback video fine on my Pixel device. I'm getting people to test on a wider range of devices now. I'm getting a crash on Note 8 here:

    0 libc.so 0x0000007eca2a2e50 <system symbols missing>
    1 libart.so 0x0000007e44e19424 <system symbols missing>
    2 libbase.so 0x0000007ec7b25cd4 <system symbols missing>
    3 libart.so 0x0000007e44c99348 <system symbols missing>
    4 libart.so 0x0000007e44ce5218 <system symbols missing>
    5 libNatCorder.so 0x0000007d94be61d8 <system symbols missing>
    6 libil2cpp.so 0x0000007e257945d8 Bridge_FrameSize_m085AE9D1EB78FBFA20DE33D36866C18195F1EC2D
    7 libil2cpp.so 0x0000007e25794cac NativeRecorder_get_frameSize_m6A2EEB68481BA1116A448E01DCC86FB0E52BCD52
    8 libil2cpp.so 0x0000007e25792590 CameraInput__ctor_m4BEA8A9CD3DC78C65B73FAF449D39D0437B87BDF
    9 libil2cpp.so 0x0000007e2607e13c VideoRecordingManager_StartRecording_m58E83FC05E4F5CC37C94C8EBA8F2BA26D6C423D0
    10 libil2cpp.so 0x0000007e257fff04 UIManager_RecordVideoClicked_mF0B44EFAAB81C704218980407CEE3CE29BF658DC

    Is it possible to test for device support before recording so that I can block all devices that may crash when recording?
    Any idea what may be causing this crash?
     
  2. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    There isn't a device blacklist, especially because you will practically never run into a crash specific to a single device. NatCorder is built on the official Android API's, and all Android devices must meet the Android Compatibility Definition.
    For this crash, send me the full, unfiltered logs from logcat in a .txt attachment.
     
  3. infosekr

    infosekr

    Joined:
    Jul 12, 2013
    Posts:
    46
    I've attached a crash log from a Samsung Note 8 that I used via the Samsung Test Lab.

    Someone is also reporting a crash on a A6+. From the crash I listed in my previous post you can see that the crash occurs on Line 19 of NativeRecorder.cs ("recorder.FrameSize(out var width, out var height);"). Beyond that the call is going into your .dll.

    Do you have a device list that you've tested this library against? Is this a known problem that you are addressing?
    Do you have any work arounds, should I reduce the framesize? Should it be a fixed size like 1280x720, right now i'm using screensize/2.

    Thanks.

    EDIT: I added another log file (log3.txt) from a Note 9 which has a different exception message:
    "
    ERROR|05-06 06:13:38.563|28154|28538||ACodec|--------- beginning of crash
    ERROR|05-06 06:13:38.564|28154|28551||AndroidRuntime|FATAL EXCEPTION: MP4Recorder Video Encoding Thread
    ERROR|05-06 06:13:38.564|28154|28551||AndroidRuntime|java.lang.IllegalStateException
    ERROR|05-06 06:13:38.564|28154|28551||AndroidRuntime| at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
    ERROR|05-06 06:13:38.564|28154|28551||AndroidRuntime| at api.natsuite.natcorder.MP4Recorder$6.run(MP4Recorder.java:192)
    ERROR|05-06 06:13:38.564|28154|28551||AndroidRuntime| at java.lang.Thread.run(Thread.java:764)
    ERROR|05-06 06:13:38.564|28154|28551||AndroidRuntime| at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:2698)
    "
     

    Attached Files:

    • log.txt
      File size:
      915.4 KB
      Views:
      447
    • log3.txt
      File size:
      264.3 KB
      Views:
      359
    Last edited: May 5, 2020
  4. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    The crashes in log.txt and log3.txt both occur because your video resolution contains an odd number. I can't stress this enough: don't record at screen resolution or some multiple thereof. Record at a fixed resolution like 1280x720 or 1920x1080.
     
  5. infosekr

    infosekr

    Joined:
    Jul 12, 2013
    Posts:
    46
    Sorry, I didn't notice that Screen.width and Screen.height were not returning the full device screen size (looks like a safe screen size), so I was getting odd dimensions. Recording at a fixed resolution is not an option for Android devices with so many varied aspect ratios. The result is squished videos and letter-boxing. I've changed it to use a height of 720 and a width that is appropriate for the aspect ratio, and ensuring the width is divisible by 2. So far that seems to work on all the devices I've tested or people have tested on.

    float aspectRatio = (float)Screen.width / Screen.height;
    int videoHeight = 720;
    int videoWidth = ((int)(aspectRatio * m_videoHeight) >> 1) << 1;
     
  6. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    CameraInput will never be squished. It re-renders the game camera with the specified recorder resolution, which is effectively the same as rendering to a new screen so Unity does all necessary resizing. The vast majority of the internet runs on a few standard resolutions: 1280x720, 1920x1080, and 4K. When you share any of these recorded videos anywhere, they get transcoded to one of these resolutions. So you might as well fix your code.
     
  7. infosekr

    infosekr

    Joined:
    Jul 12, 2013
    Posts:
    46
    It looks like the squishing was due to the aspect ratio of the playback render texture I was using. So I guess the only issue is the letterboxing, which as you point out will be added by the site they post to anyway. Thanks for your help, I'll fix my code.
     
    Lanre likes this.
  8. timbokoppers

    timbokoppers

    Joined:
    Nov 14, 2016
    Posts:
    69
    How to achieve to chop off the letterboxing when the phone's aspect ratio is > 16:9?
     
  9. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Depends on what you are displaying on screen. If it is a UI panel, use an aspect ratio fitter and set it to envelope the parent. Then make sure your canvas is set to scale with screen size.
     
  10. timbokoppers

    timbokoppers

    Joined:
    Nov 14, 2016
    Posts:
    69
    Hard to explain maybe, but my screen has a ratio of 19.5:9, portrait mode, but for example I only want to record the middle part of the screen in 16:9 ratio (720x1280).
    It's not UI I want to record.
     
  11. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Oh you want letterboxing? This isn't trivial as far as I know, because Unity/CameraInput will just rescale the camera viewport to match the recording aspect (16:9), completely subverting any letterboxing. You'll likely have to use a custom camera input implementation that handles this.

    EDIT: Such a camera input could offer better performance, as it doesn't require re-rendering the camera view. We can collaborate on this.
     
    Last edited: May 8, 2020
    timbokoppers likes this.
  12. unity_btvB7DdIEov8zg

    unity_btvB7DdIEov8zg

    Joined:
    Aug 29, 2019
    Posts:
    2
    Hi, I am interested in this for my ARKit App. Some concerns:
    1) Need an Image Capture feature that shows preview, and on 'Save' stores in device's Photos album.
    2) Ability to customize recordings GUI ( add timer, custom buttons for image and video capture ) will be excluded from final video capture. Thanks
     
  13. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    NatCorder doesn't have any UI flow for what to do after recording (unlike Everyplay). It simply records videos to the app's private documents directory. You'll have to build such a UI yourself.
    You can exclude certain items from the recording by using a secondary camera.

    Check out the docs to get a full picture of how NatCorder works.
     
  14. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Hi all,

    I'd love to know how you use NatCorder.

    I'm working on online docs and I'd like to showcase some use cases. Feel free to PM me your app names/logos, company names/logos, screenshots, videos, and so on. I'd very much appreciate it!
     
  15. Flamacore

    Flamacore

    Joined:
    Dec 17, 2013
    Posts:
    137
    Hey there,

    So I've been using the NatCorder for my AR Foundation app (Android for now) last year. Now it needed to be updated to 2019.3+ and unfortunately, NatCorder lost compatibility somehow. Read through a few posts here but none of them seems like my issue.

    This code (which is from the examples), causes a startup crash with no message whatsoever. Not with Unity tag under logcat anyway and it gets way too fast to be able to track what's causing the crash without a proper tag. Basically, the app opens, gets past the unity logo (I don't have the splash screen with plus but yeah) and then a black screen for 1-2 seconds folllowed by a crash. Which is strange cause I don't even start the recording at start (obviously).

    Only one time, I had a GLSL vertex limit error which is normally caused by trying to access compute shader when unsupported but that shouldn't be even remotely possible for NatCorder should it?

    Using ONLY OpenGLES3
    No external plugins except for a native sharing plugin
    Camera, mic and storage permissions are allowed

    Building without this specific code in the scene works fine as expected. Tested every possible way. Also a note; ReplayCam is still in the scene (also from the examples) and it just builds fine with it.

    Double, triple, quadruple checked everything. Got any idea or leads that you can guide me to? :)

    Code Block:
    Code (CSharp):
    1.  
    2. /*
    3. *   NatCorder
    4. *   Copyright (c) 2019 Yusuf Olokoba
    5. */
    6.  
    7. namespace NatCorder.Examples
    8. {
    9.  
    10.    using System.Collections;
    11.    using UnityEngine.Events;
    12.    using UnityEngine.EventSystems;
    13.    using UnityEngine.UI;
    14.    using UnityEngine;
    15.  
    16.    [RequireComponent (typeof (EventTrigger))]
    17.    public class RecordButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    18.    {
    19.  
    20.       public Image button, countdown;
    21.       public UnityEvent onTouchDown, onTouchUp;
    22.       private bool pressed;
    23.       private const float MaxRecordingTime = 10f; // seconds
    24.  
    25.       private void Start ()
    26.       {
    27.          Reset ();
    28.       }
    29.  
    30.       private void Reset ()
    31.       {
    32.          // Reset fill amounts
    33.          if (button)
    34.             button.fillAmount = 1.0f;
    35.          if (countdown)
    36.             countdown.fillAmount = 0.0f;
    37.       }
    38.  
    39.       void IPointerDownHandler.OnPointerDown (PointerEventData eventData)
    40.       {
    41.          // Start counting
    42.          if (!pressed)
    43.          {
    44.             StartCoroutine (Countdown ());
    45.             return;
    46.          }
    47.          if(pressed)
    48.          {
    49.             pressed = false;
    50.          }
    51.       }
    52.  
    53.       void IPointerUpHandler.OnPointerUp (PointerEventData eventData)
    54.       {
    55.          // Reset pressed
    56.          //pressed = false;
    57.       }
    58.  
    59.       private IEnumerator Countdown ()
    60.       {
    61.          pressed = true;
    62.          // First wait a short time to make sure it's not a tap
    63.          yield return new WaitForSeconds (0.2f);
    64.          if (!pressed)
    65.             yield break;
    66.          // Start recording
    67.          onTouchDown?.Invoke ();
    68.          // Animate the countdown
    69.          float startTime = Time.time, ratio = 0f;
    70.          while (pressed && (ratio = (Time.time - startTime) / MaxRecordingTime) < 1.0f)
    71.          {
    72.             countdown.fillAmount = ratio;
    73.             button.fillAmount = 1f - ratio;
    74.             yield return null;
    75.          }
    76.          // Reset
    77.          Reset ();
    78.          // Stop recording
    79.          onTouchUp?.Invoke ();
    80.       }
    81.    }
    82. }
    83.  
     
  16. Flamacore

    Flamacore

    Joined:
    Dec 17, 2013
    Posts:
    137
    Ok, you can ignore this. Crazily strange enough, writing a different script with a different approach to starting/stopping recording with a coroutine for filling a circle fixed the issue. Can't imagine why :)
     
    Lanre likes this.
  17. unity_btvB7DdIEov8zg

    unity_btvB7DdIEov8zg

    Joined:
    Aug 29, 2019
    Posts:
    2
    Thanks a lot for your prompt response. Need more clarification on ability to save snap as in a photo like JPG instead of a video. My app needs feature for both for photo and video. Thanks again.
     
  18. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    NatCorder doesn't take screenshots. Unity already has functionality for this.
     
  19. Busymonkey

    Busymonkey

    Joined:
    Mar 7, 2019
    Posts:
    7
    Hi @Lanre, how can I make it possible to record from a muted audio source?

    I'm looking for a way to record the player's voice without playing it back to them, creating an annoying echo effect. Right now we're feeding Unity's Microphone into an audio source and trying to record that without replaying it for the player. Simply muting the audio source returns a silent .wav file.
     
  20. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Use the AudioInput script, and specify the `mute` flag as `true`. See the source for how it is implemented.
     
    Busymonkey likes this.
  21. Conor_Tradiebot

    Conor_Tradiebot

    Joined:
    Mar 2, 2020
    Posts:
    3
    Hi I have both NatCorder and NatDevice and I am having a build error saying that I have two instances of plugins with the same name.

    In my assets folder I have a “NatSuit” (NatCorder) folder and a “NatDevice” folder which both have a “plugin” folder and both have an “Android” folder.

    The files that have the same name are Natcorder and NatRender (could be NatRendor)

    If any one has had this issue do they have any tips on how to fix it?

    (sorry for the unsure-ness about the file name, it’s a question for the work I have to do tomorrow and I’m not at work)
     
  22. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    I responded to your email. Delete the NatRender.aar copy in NatDevice.
     
  23. Conor_Tradiebot

    Conor_Tradiebot

    Joined:
    Mar 2, 2020
    Posts:
    3
    Thank you for the idea! I’ll see how it goes tomorrow and let you know. I think I may have tried that but I may have deleted the one in the other folder.
     
  24. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Which one you delete shouldn't actually matter.
     
  25. manixx0011

    manixx0011

    Joined:
    May 13, 2020
    Posts:
    1
    there are different ways to record screens on iPhone such as with third party app and inbuilt features. A few days ago I was also finding solution of it and i got it in an article How you can record screen on your Device. Check it there is a detailed explanation.
     
  26. unitydevstudio

    unitydevstudio

    Joined:
    Jun 24, 2017
    Posts:
    24
    Hi, @Lanre
    I used your plugin(1.6.0) for my ARFoundation project.
    It worked well on Unity2018.
    When I upgrade unity version to 2019.3, I got inverted color in recorded video on iOS 13.
    So I updated your plugin to latest 1.7.2, but it crashes on iOS by memory issue.
    I have also checked in unity editor, memory usage increased 3~4G for a few seconds when I start the recording.
    I tested on Unity2019.2.17f, 2019.3.3f1, 2019.3.5f1, 2019.3.13f1.
    On all version, it happened.

    I removed 1.7.2 and reimported 1.6.0. it doesnt increase memory while recording on Unity Editor and iOS.
    Just getting inverted color.

    What is the difference betweeen 1.6.0 and 1.7.2?
    On iOS, app crashes immediately when I start recording with 1.7.2.
     
  27. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    What is your recording resolution? And what is your app's CPU and GPU frame time? Chances are that the encoder can't keep up with how much data you're trying to push. You can look through the changelog.
     
  28. unitydevstudio

    unitydevstudio

    Joined:
    Jun 24, 2017
    Posts:
    24
    I used (Screen.width/2, Screen.height/2) for recordng resolution.
    On my iphone X it's 1218x563 so under the 1920 x 720 you recommend.
    how can I get my app's CPU and GPU frame time?
     
  29. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    In Xcode, the Frame tab has information on this. If your app already has a high CPU or GPU frame time, then there's going to be a growing backlog of frames waiting to be encoded which would explain memory exploding.

    EDIT: Also, do not record at dynamic resolutions computed from the screen resolution. For instance, this resolution would cause a crash on most Android devices as it has an odd number for its height. Use a fixed resolution.
     
  30. ugurmalki

    ugurmalki

    Joined:
    Dec 12, 2019
    Posts:
    3
    Hi Eric,
    How you capture whole screen.Can you explain more about "I have a RawImage setup to scale with the entire size of the screen to output the camera's texture." I tried couple of thinks like create a new RenderTexture and set it to rawImage but couldn't manage it.
     
    Lanre likes this.
  31. Venzel

    Venzel

    Joined:
    May 7, 2018
    Posts:
    19
    Hi @Lanre!
    There is strange thing in iOS project with AR + NatCorder + NatDevice.
    After start of video recording the screen stops any updates, BUT buttons works, so I can stop recording (after that screen starts update). Moreover, on finish recording I have video file with great quality, with AR objects in proper palaces.

    Sometimes there is error in log. But last 5 last attempts were without it. And with no changes.
    Code (CSharp):
    1. samples[476:90219] [Technique] World tracking performance is being affected by resource constraints [1]
    2. samples[476:90219] [Technique] VIO error callback: 34237.661568, 1, Frame processing rate has fallen below pre-set threshold
    Logs are common I guess:
    Code (CSharp):
    1. 2020-05-14 16:18:18.072188+0300 samples[517:97419] Metal GPU Frame Capture Enabled
    2. 2020-05-14 16:18:29.049487+0300 samples[517:97419] NatCorder: Prepared MP4Recorder video encoder at resolution 1080x1920@30.000000Hz with average bitrate 4800000 and keyframe interval 3s
    3. 2020-05-14 16:18:29.050332+0300 samples[517:97419] NatCorder: Prepared MP4Recorder audio encoder for 1 channels at 48000Hz
    4. 2020-05-14 16:18:29.746395+0300 samples[517:97419] NatDevice: AudioDevice Built-In Microphone started recording
    5. 2020-05-14 16:18:33.723234+0300 samples[517:97419] NatDevice: AudioDevice Built-In Microphone stopped recording
    6. 2020-05-14 16:18:35.037133+0300 samples[517:97419] NatCorder: MP4Recorder finishing
    7. Record saved in /var/mobile/Containers/Data/Application/CDCF761D-917D-4D60-849E-9598B85AB444/Documents/recording_2020_05_14_16_18_29_040.mp4
    NatCorder 1.7.1, NatDevice 1.0.1, iOS 13, iPhone 8
    Unity 2018.4.21, AR Foundation on board.
     
  32. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    The screen freezing is a known issue with Unity 2018. Unity fixed it in 2019.2, so I recommend updating. If world tracking performance is taking a hit, you might want to reduce your recording resolution to say 1280x720.
     
  33. Venzel

    Venzel

    Joined:
    May 7, 2018
    Posts:
    19
    @Lanre
    Thank you for response!
     
    Lanre likes this.
  34. unitydevstudio

    unitydevstudio

    Joined:
    Jun 24, 2017
    Posts:
    24
    Hi, Lanre
    When I use Natcorder 1.6.0, my app works well for video recording regardless of which unity version I use.
    This means my app has no high CPU or GPU frame time.
    The problem is just only getting inverted color.
    But with other versions 1.6.6, 1.7.2, it crashes due to memory issue.
    How can I resolve this problem?

    And for the screen resolution problem, yes. I have some problems on android as you said.
    But when I try with 1280x720, I got stretched video for 1280x720.
    As I told u, my app is AR project use ARFoundation.
    So the screen to be recorded is mobile device resolution.
    How can I get correct this problem too?

    I can't get the correct aspect ratio of the recorded video when I record 1280x720.
     
  35. Busymonkey

    Busymonkey

    Joined:
    Mar 7, 2019
    Posts:
    7
    I can't believe I missed that... Thanks!

    If I want to cancel a recording, how would I go about that? Would I just set the CameraInput, AudioInput, and MP4Recorder to null? Would I have to Dispose the inputs beforehand?

    Thanks in advance.
     
    Last edited: May 14, 2020
  36. AugmentedSpaceAgency

    AugmentedSpaceAgency

    Joined:
    May 11, 2017
    Posts:
    12
    Did anyone experimented with recording with Natcorder using URP and camera stacking?

    We render a 3D UI on a different camera and want to record only the main camera, but the 3D UI is still being recorded. It seems that URP implementation of camera stacking is outputting the combined cameras.
     
    Milena_ham and Lanre like this.
  37. unity_45y2YK0_r_f0eQ

    unity_45y2YK0_r_f0eQ

    Joined:
    Apr 12, 2019
    Posts:
    4
    Hi, we're experiencing a crash on our iPhone 5S when finishing a video recording (iOS 12.4.6). We're using NatCorder with NatDevice for microphone input. The crash does not happen when we're not including the microphone in the video, so our best guess is it has something to do with NatDevice. It crashes the moment we stop the recording, not when starting the recording. We're forced to use Unity Cloud Build currently (no access to our build Mac), so sadly we don't have any more info currently. We're hoping that this sounds familiar or hopefully you can reproduce this issue yourself? We hope to hear from you soon.
     
  38. laminhtruong

    laminhtruong

    Joined:
    Feb 10, 2015
    Posts:
    3
    Hi, we got the issue about microphone recorder. The audio is not sync from 2nd record, we have tried "ReplayCam" sample on newest version 1.7.2 but this issue still occurs. Can you let me know what happen with it and how to fix this issue

    Note: this issue occurs on iOS and Android
     
  39. ugurmalki

    ugurmalki

    Joined:
    Dec 12, 2019
    Posts:
    3
    I can capture my screen with sound but my problem is some peak noises comes sizzle as you can see in my sample video. 0:04 - 0:08 - 0:27 thru 0:30
    my code is simple took from your ReplayCam and changed little bit Width, Height and frameRate comes from my source video.

    https://imgur.com/a/6fYx5rW



    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using NatSuite.Recorders;
    4. using NatSuite.Recorders.Clocks;
    5. using NatSuite.Recorders.Inputs;
    6. public class renderScn : MonoBehaviour
    7. {
    8.      private int videoWidth;
    9.     private int videoHeight;
    10.     private float frameRate;
    11.  
    12.     private IMediaRecorder recorder;
    13.     private CameraInput cameraInput;
    14.     private AudioInput audioInput;
    15.  
    16. public void StartRecording()
    17.     {
    18.         Debug.Log("Start Rec");
    19.         var sampleRate = AudioSettings.outputSampleRate;
    20.         var channelCount = (int)AudioSettings.speakerMode;
    21.         var clock = new RealtimeClock();
    22.         recorder = new MP4Recorder(videoWidth, videoHeight, frameRate, sampleRate, channelCount);
    23.         cameraInput = new CameraInput(recorder, clock, Camera.main);
    24.         audioInput = new AudioInput(recorder, clock, audioPlayer, false);
    25.     }
    26.     public async void StopRecording()
    27.     {
    28.         audioInput?.Dispose();
    29.         cameraInput.Dispose();
    30.         var path = await recorder.FinishWriting();
    31.         Debug.Log($"Saved recording to: {path}");
    32.         var prefix = Application.platform == RuntimePlatform.WindowsPlayer ? "file://" : "";
    33.         Handheld.PlayFullScreenMovie($"{prefix}{path}");
    34.     }
    35. }
     
  40. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    What version of Unity do you use? There is a memory leak bug in Unity 2020.1 which has been fixed in 2020.2. As for your frame time, please share a screenshot of the frame time in Xcode. That is the only way to know for sure.
    If this is the case then make sure that the width and height are not odd numbers.
     
  41. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    There is no such concept as cancelling recording. You simply stop recording, then decide what to do with the recorded video. Simply setting things to `null` will cause a resource leak.
     
  42. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    I got the logs, but they show that the main thread (thread 0) crashed. This isn't consistent with audio being the cause of the crash (NatDevice will report sample buffers on a background thread), so I'll need more info. Can you make sure that your app is built and run in development mode? Send me the logs from that because the current logs don't show much info apart from the fact that the crash happens on your app's main thread.
     
  43. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    This happens because of Unity's Microphone API, which will record audio to a circular buffer. I recommend using NatDevice instead.
     
  44. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    NatCorder doesn't modify the committed audio in any way, so you can be sure that any glitching in the audio must be coming from the audio source. Try reducing the audio source volume in the Editor.
     
  45. ugurmalki

    ugurmalki

    Joined:
    Dec 12, 2019
    Posts:
    3
    Yep it worked.

    float audioVolume = currentVideoObj.muted ? 0 : currentVideoObj.volume;
    video.SetDirectAudioVolume(0, audioVolume - 0.5f);
    video.audioOutputMode = VideoAudioOutputMode.AudioSource;
    video.controlledAudioTrackCount = 1;
    video.EnableAudioTrack(0, true);

    video.SetTargetAudioSource(0, audioPlayer);
    audioPlayer.volume = audioVolume - 0.5f;
    screenImage.texture = video.targetTexture;
    video.url = currentVideoObj.url;
    video.Prepare();
     
    Lanre likes this.
  46. unitydevstudio

    unitydevstudio

    Joined:
    Jun 24, 2017
    Posts:
    24
    Hi, @Lanre
    I don't use Unity2020
    I have tested memory crash on iOS build from Unity2019.2.17f, 2019.3.3f1, 2019.3.5f1, 2019.3.13f1 with your Natcorder plugin 1.6.6 and 1.7.2.
    For all above unity versions, Natcorder 1.6.0 has no memory issue. the problem is only getting inverted color for recorded video.
    The below link has Xcode screenshots of 1.6.0 version - no memory crash and 1.7.2 version - memory crash.
    -1.6.0_No crash, getting inverted color
    https://www.dropbox.com/sh/ywpmeiuatn89z33/AABLkmYN5XRjvldSlANvfV64a?dl=0
    -1.7.2_Crash due to memory issue
    https://www.dropbox.com/sh/ffmn8qeqzv6scl2/AAC0Yvq0WvUlyBEh7FZ7gFaMa?dl=0


    Okay, I will try.
     
  47. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    Run your app with OpenGL ES3 instead of Metal and let me know if this memory behaviour still occurs. Also can you share your recording code?
     
  48. unitydevstudio

    unitydevstudio

    Joined:
    Jun 24, 2017
    Posts:
    24
    It's AR app uses ARFoundation. so OpenGL ES3 doesn't work with ARKit. it's also deprecated on 2019.3
    My project uses auto graphic api.

    And this is my code for the recording - 1.7.2 version
    Code (CSharp):
    1.    public void StartRecordVideoClicked()
    2.     {
    3.         var clock = new RealtimeClock();
    4.         videoRecorder = new MP4Recorder(Screen.width / 2, Screen.height / 2, 24);
    5.         cameraInput = new CameraInput(videoRecorder, clock, Camera.main);
    6.     }
    7.  
    8.     public async void StopRecordVideoClicked()
    9.     {
    10.         cameraInput.Dispose();
    11.         recordedVideoPath = await videoRecorder.FinishWriting();
    12.     }
     
    Last edited: May 25, 2020
  49. Venzel

    Venzel

    Joined:
    May 7, 2018
    Posts:
    19
    Hi @Lanre!

    I have crashes on finish recording (StopRecording() method bellow) on iphone.
    Can you help me?

    Crashes on the following models:
    iphone 8, ios 13.5;
    iphone 7, ios 13.4.1;
    iphone xs max, ios 13.3.1;

    No crashes on:
    iphone 8 plus, ios 13.4.1;
    iphone 8, ios 13.3.1 (Yes, there are two iphones 8. One crashes, another doesn't)

    I use:
    NatCorder 1.7.1, NatDevice 1.0.1, Unity 2019.3.13, AR Foundation.

    Recording pipeline example:
    StartRecording() -> PauseRecording() -> ResumeRecording() -> PauseRecording() -> StopRecording()

    Code:
    Code (CSharp):
    1. public void StartRecording()
    2.     {
    3.         var query = new MediaDeviceQuery(MediaDeviceQuery.Criteria.AudioDevice);
    4.         device = query.currentDevice as AudioDevice;
    5.         clock = new RealtimeClock();
    6.         recorder = new MP4Recorder(720, 1280, 30, device.sampleRate, device.channelCount, 4800000);
    7.         cameraInput = new CameraInput(recorder, clock, Camera.main);
    8.         device.StartRunning((sampleBuffer, timestamp) => recorder.CommitSamples(sampleBuffer, clock.timestamp));
    9.     }
    10.  
    11.     public void PauseRecording()
    12.     {
    13.         clock.Paused = true;
    14.         cameraInput.Dispose();
    15.         device.StopRunning();
    16.     }
    17.  
    18.     public void ResumeRecording()
    19.     {
    20.         clock.Paused = false;
    21.         cameraInput = new CameraInput(recorder, clock, Camera.main);
    22.         device.StartRunning((sampleBuffer, timestamp) => recorder.CommitSamples(sampleBuffer, clock.timestamp));
    23.     }
    24.  
    25.     public async void StopRecording()
    26.     {
    27.         var path = await recorder.FinishWriting();
    28.         curentRecordingTime = 0f;
    29.         isUpdatingRecordingTime = false;
    30.         OnFileSaved(path);
    31.     }
    Logs from Firebase Crashlytics (if necessary, I can add info about others threads):
    Code (CSharp):
    1. Crashed: NatDevice AudioDevice Thread
    2. 0  libobjc.A.dylib                0x1913e7b28 objc_msgSend + 8
    3. 1  UnityFramework                 0x1047d9ecc -[NCMP4Recorder commitSamples:sampleCount:timestamp:] + 135 (NCMP4Recorder.m:135)
    4. 2  UnityFramework                 0x104881050 stack_allocator::deallocate(void*) (.cold.2) + 4361687120
    5. 3  UnityFramework                 0x104880d7c stack_allocator::deallocate(void*) (.cold.2) + 4361686396
    6. 4  UnityFramework                 0x1049d62f0 stack_allocator::deallocate(void*) (.cold.2) + 4363084528
    7. 5  UnityFramework                 0x104880a14 stack_allocator::deallocate(void*) (.cold.2) + 4361685524
    8. 6  UnityFramework                 0x103825d04 ReversePInvokeWrapper_NativeAudioDevice_OnSampleBuffer_mDA38F2767021984F83B275BC9DD903BBA68856A4 + 23092 (Assembly-CSharp2.cpp:23092)
    9. 7  UnityFramework                 0x1047df184 __NDAudioDeviceStartRunning_block_invoke + 77 (Bridge.m:77)
    10. 8  UnityFramework                 0x1047de8b0 -[NDAudioDevice captureOutput:didOutputSampleBuffer:fromConnection:] + 125 (NDAudioDevice.m:125)
    11. 9  AVFoundation                   0x19b918c0c -[AVCaptureAudioDataOutput _handleRemoteQueueOperation:] + 236
    12. 10 AVFoundation                   0x19b918aec __47-[AVCaptureAudioDataOutput _updateRemoteQueue:]_block_invoke + 88
    13. 11 CoreMedia                      0x197f4ed88 __FigRemoteOperationReceiverCreateMessageReceiver_block_invoke + 280
    14. 12 CoreMedia                      0x197f6f53c __rqReceiverSetSource_block_invoke + 248
    15. 13 libdispatch.dylib              0x19138f524 _dispatch_client_callout + 16
    16. 14 libdispatch.dylib              0x191337e98 _dispatch_continuation_pop$VARIANT$mp + 412
    17. 15 libdispatch.dylib              0x191348430 _dispatch_source_invoke$VARIANT$mp + 1276
    18. 16 libdispatch.dylib              0x19133b770 _dispatch_lane_serial_drain$VARIANT$mp + 300
    19. 17 libdispatch.dylib              0x19133c294 _dispatch_lane_invoke$VARIANT$mp + 416
    20. 18 libdispatch.dylib              0x19134578c _dispatch_workloop_worker_thread + 588
    21. 19 libsystem_pthread.dylib        0x1913e0b74 _pthread_wqthread + 272
    22. 20 libsystem_pthread.dylib        0x1913e3740 start_wqthread + 8

    Code (CSharp):
    1. com.apple.main-thread
    2. 0  libsystem_kernel.dylib         0x19149d198 mach_msg_trap + 8
    3. 1  libsystem_kernel.dylib         0x19149c60c mach_msg + 72
    4. 2  CoreFoundation                 0x1916473b4 __CFRunLoopServiceMachPort + 148
    5. 3  CoreFoundation                 0x1916423e8 __CFRunLoopRun + 1160
    6. 4  CoreFoundation                 0x191641c34 CFRunLoopRunSpecific + 424
    7. 5  GraphicsServices               0x19b78b38c GSEventRunModal + 160
    8. 6  UIKitCore                      0x19577422c UIApplicationMain + 1932
    9. 7  UnityFramework                 0x10381a724 -[UnityFramework runUIApplicationMainWithArgc:argv:] + 96 (main.mm:96)
    10. 8  PopcornAR                      0x102997e1c main + 28 (main.mm:28)
    11. 9  libdyld.dylib                  0x1914c9800 start + 4
     
  50. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,960
    I asked to use OpenGL ES3 as a way to disable Unity's asynchronous GPU readbacks. What you can do instead is open CameraInput.cs and in the `OnFrame` method, change this line:
    Code (CSharp):
    1. if (SystemInfo.supportsAsyncGPUReadback)
    to this:
    Code (CSharp):
    1. if (false && SystemInfo.supportsAsyncGPUReadback)
    When you do this, you should see how your memory behaves without Unity's async readbacks allocating memory.