Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    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,970
    Can you share your full logs? Also, what is your app's frame time (CPU and GPU) in Xcode? As for the audio device issue, I recommend upgrading to NatDevice. NatMic has been deprecated.
     
  2. Lordmin

    Lordmin

    Joined:
    Mar 9, 2017
    Posts:
    62
    In the latest version of Natcorder and Android environments, the gallery does not automatically refresh after shooting a video. Is there a way?

    (In the internal folder it saves immediately, but it is updated very late in the Android gallery.)
     
  3. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Videos are not supposed to show up in the gallery. Instead, you have to copy them there. You can do so with a native sharing plugin like NatShare.
     
  4. Lordmin

    Lordmin

    Joined:
    Mar 9, 2017
    Posts:
    62
    When recording Natcorder while '3D Object Player' is playing on iOS, '3D Object Player' pauses and Natcorder is not recorded either.

    Is there any conjecture cause?

    '3D Object Player is an external plug-in that plays the Unity renderer as an animation.'

    The AOS recording function works very well.
     
  5. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    What is "3D Object Player", and what is "AOS"? Can you share logs or any other relevant information?
     
  6. Mamoon178

    Mamoon178

    Joined:
    Jul 26, 2017
    Posts:
    36
    any possibility record video with audio in webGL?
     
  7. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    You cannot record Unity audio as Unity does not support the OnAudioFilterRead function on WebGL. The documentation has more details.
     
  8. vzheng

    vzheng

    Joined:
    Dec 4, 2012
    Posts:
    45
  9. vzheng

    vzheng

    Joined:
    Dec 4, 2012
    Posts:
    45
    So,why not take a picture?
     
  10. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    I already bought NATCORDER and NAT-MIC, screen recording works on Android, but when i try it on IOS it uses over 2G of RAM on iPhone, runs out of memory and crashes, so i want to upgrade to the latest integrated version, so i have to pay for that?
     
  11. eomerb

    eomerb

    Joined:
    Mar 6, 2020
    Posts:
    31
    Hi,
    I write a video editor app with unity UI.
    I play videos and music and record it from main camera
    the video is ok but I have some problems with audio
    to record videos sound I set my videos sound to an audioSource (as)
    Code (CSharp):
    1. vp.audioOutputMode = VideoAudioOutputMode.AudioSource;
    2. vp.controlledAudioTrackCount = 1;
    3. vp.EnableAudioTrack(0, true);
    4. vp.SetTargetAudioSource(0, as);
    and I also played and audio clip with the same audio source
    when I checked the recorded video there is no sound
    I trried to add a second audioInput and seperate the audiosources 1 for video and 1 for audioclip but it didnt work
    when I checked from the editor the videos audio is sent to audio source but when I mute audiosource there is still audio of video
    I also tried audioListener to audioInput:
    Code (CSharp):
    1. audioInput = new AudioInput(recorder, clock, Camera.main.GetComponent<AudioListener>());
    but I got no sound again there isnt any full example about audioListener

    the video and music I tried to record play ok I got no problem in there so the problem is in natcorder
    its either I add 2 audiosource or the audioListener
    pls help
     
  12. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
  13. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    If you are getting memory increasing rapidly on iOS, this is a Unity bug in AsyncGPUReadback. I believe it affects Unity 2020.1 and some 2018 builds. What version of Unity are you using?
     
  14. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    First off, you can't record more than one AudioSource with an AudioInput. Can you share your full recording code? Are you properly initializing the recorder to expect audio?
     
  15. eomerb

    eomerb

    Joined:
    Mar 6, 2020
    Posts:
    31
    can there be 2 audioInput?

    Code (CSharp):
    1.  public void StartRecording()
    2. {
    3.         var sampleRate = AudioSettings.outputSampleRate;
    4.         var channelCount = (int)AudioSettings.speakerMode;
    5.         var clock = new RealtimeClock();
    6.         recorder = new MP4Recorder(width, height,RenderVideoFrameRate, sampleRate, channelCount);
    7.         cameraInput = new CameraInput(recorder, clock, Camera.main);
    8.         audioInput = new AudioInput(recorder, clock, playerScript.audio, false);
    9.         audioInputForVideo = new AudioInput(recorder, clock, playerScript.videoAudio, false);
    10.     }
    11.     public async void StopRecording()
    12.     {
    13.         audioInput?.Dispose();
    14.         audioInputForVideo.Dispose();
    15.         cameraInput.Dispose();
    16.         var path = await recorder.FinishWriting();
    17.     }
    this my 2 audioInput try. also I tried
    Code (CSharp):
    1. audioInput = new AudioInput(recorder, clock, Camera.main.GetComponent<AudioListener>());
    and comment out audioInputForVideo for 1 audioInput
    playerScript is the class I play the video
     
  16. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    Im using Unity 2020.1 This only happens on IOS, on Android there is no problem it works. IOS runs out of memory, gives memory warning and then crashes.

    So which version of Unity works? 2020.2???
     
    Last edited: Aug 28, 2020
  17. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    You cannot use more than one CameraInput or AudioInput.
     
  18. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    The latest report I had of this issue was on Unity 2020.1.1, and apparently it was fixed in 2020.1.2f1.
     
  19. eomerb

    eomerb

    Joined:
    Mar 6, 2020
    Posts:
    31
    ok I understand that NOW.
    I also wrote that I tried 1 audioInput with a audioListener in it
    I am writing the code again
    Code (CSharp):
    1. public void StartRecording()
    2. {
    3.         var sampleRate = AudioSettings.outputSampleRate;
    4.         var channelCount = (int)AudioSettings.speakerMode;
    5.         var clock = new RealtimeClock();
    6.         recorder = new MP4Recorder(width, height,RenderVideoFrameRate, sampleRate, channelCount);
    7.         cameraInput = new CameraInput(recorder, clock, Camera.main);
    8.         audioInput = new AudioInput(recorder, clock, Camera.main.GetComponent<AudioListener>());
    9.     }
    10.     public async void StopRecording()
    11.     {
    12.         audioInput?.Dispose();
    13.         cameraInput.Dispose();
    14.         var path = await recorder.FinishWriting();
    15.     }
    this doesnt work either
     
  20. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Can you share a link to a recorded video? And can you share the logs from recording in a .txt attachment?
     
  21. eomerb

    eomerb

    Joined:
    Mar 6, 2020
    Posts:
    31
    where are the logs
    can u write an email address dont want to share here
     
  22. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    The logs depend on what platform you are running on. On iOS, Xcode will show the logs in a lower right box. On Android, you have to use the logcat tool which is bundled with the Android SDK. On macOS and Windows, there should be a player log file wherever your player files are. And in the Editor, go to the `Console` and in the menu at the top, click on 'Editor Log'.

    Email me.
     
  23. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    Did you actually install Unity 2020.1.2 and try if it works???
     
  24. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    I don't use Unity 2020. The developer who reported this issue tested it and confirmed that it was fixed in 2020.1.2.
     
  25. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    870
    Yes i tried it myself and it does work in 2020.1.2
     
    Lanre likes this.
  26. monda

    monda

    Joined:
    May 14, 2015
    Posts:
    35
    Hi, I'm using both Natcorder and Natdevice on my app, it works all great on iOs and Android but it crashes in several beta testers devices, for example "Redmi note 7", unfortunately I don't have access to the debug logs :(

    App is build with Unity 2020.1.3 using the latest version of both Natcorder and Natdevice, do you have any idea why this is happening only on Redmi devices?
     
  27. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Hi there. Sorry to hear this is happening. Without more information, I have no clue what could be going on. Does this only happen on Redmi devices? Do you have a Redmi device that you can test and get logs from?
     
  28. monda

    monda

    Joined:
    May 14, 2015
    Posts:
    35
    Hi, one Beta tester managed to send me 2 screenshots of the LOGCAT, hope it helps, I've one week away form publishing the app :(

    This is what's happening on a Redmi note 7 but Other developers (Android) reported the same thing on Motorola G, LG V40 ThinQ, Samsung Galaxy S9.

    https://www.dropbox.com/s/7kdzgmpiuwg2xng/01.jpg?dl=0

    https://www.dropbox.com/s/e6uod2kytp8c9ut/02.jpg?dl=0
     
  29. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    These errors have no mention of either NatCorder or NatDevice (you would expect to see `libNatCorder.so` and `libNatDevice.so` respectively). The call stack does have `libunity.so`, so it seems like it is coming from Unity. I recommend filing a bug report with Unity.
     
  30. Lordmin

    Lordmin

    Joined:
    Mar 9, 2017
    Posts:
    62


    3D Object Player is an external SDK and is a player that plays renderer objects.

    AOS stands for Android OS.

    When running on iOS, recording does not work and the following error log occurs.

    If there is a reason for guessing, please let me know.



    ----------------------------------Xcode Error Log List------------------------------------------------------------------
    2020-08-25 15:19:11.496689+0900 DnsoftARF[961:184137] [avas] AVAudioSession.mm:997:-[AVAudioSession setActive:withOptions:error:]: Deactivating an audio session that has running I/O. All I/O should be stopped or paused prior to deactivating the audio session.

    2020-08-25 15:19:11.819454+0900 DnsoftARF[961:184320] [0] OnUnityRender: dropped 3 frames

    2020-08-25 15:19:11.819911+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.493 sec

    2020-08-25 15:19:11.823672+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.460 sec

    2020-08-25 15:19:11.826048+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.427 sec

    2020-08-25 15:19:11.835499+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.393 sec

    2020-08-25 15:19:11.844927+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.360 sec

    2020-08-25 15:19:11.848604+0900 DnsoftARF[961:184381] [Technique] World tracking performance is being affected by resource constraints [1]

    2020-08-25 15:19:11.848818+0900 DnsoftARF[961:184381] [Technique] VIO error callback: 46132.967306, 1, Frame processing rate has fallen below pre-set threshold

    2020-08-25 15:19:11.848960+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.327 sec

    2020-08-25 15:19:11.853607+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.293 sec

    2020-08-25 15:19:11.858120+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.260 sec

    2020-08-25 15:19:11.866690+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.227 sec

    2020-08-25 15:19:11.871224+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.193 sec

    2020-08-25 15:19:11.883389+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.160 sec

    2020-08-25 15:19:11.891015+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.127 sec

    2020-08-25 15:19:11.900754+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.093 sec

    2020-08-25 15:19:11.903910+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.060 sec

    2020-08-25 15:19:11.906767+0900 DnsoftARF[961:185046] [0] video thread: dropping frame late by -0.027 sec

    2020-08-25 15:19:11.946367+0900 DnsoftARF[961:184381] [Technique] VIO error callback: 46133.067295, 1, Frame processing rate has fallen below pre-set threshold

    2020-08-25 15:19:12.047131+0900 DnsoftARF[961:184381] [Technique] VIO error callback: 46133.167284, 1, Frame processing rate has fallen below pre-set threshold

    2020-08-25 15:19:12.147027+0900 DnsoftARF[961:184381] [Technique] VIO error callback: 46133.267274, 1, Frame processing rate has fallen below pre-set threshold

    2020-08-25 15:19:16.628699+0900 DnsoftARF[961:184137] NatCorder: MP4 recorder finishing

    Saved recording to: /var/mobile/Containers/Data/Application/D215F419-F484-4FD8-9E76-B216550AF555/Documents/recording_2020_08_25_15_19_10_871.mp4

    NatCorder.Examples.ReplayCam:OnReplay(String)

    System.Action`1:Invoke(T)

    NatCorder.Internal.<>c__DisplayClass16_0:<OnRecording>b__0()

    System.Action:Invoke()

    NatCorder.Internal.DispatcherAttachment:Update()
     
  31. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    The logs indicate that NatCorder finishes recording. Also, your video plugin seems to be dropping a lot of frames. You should reach out to the plugin's support for that.
     
  32. RSMAPPLI

    RSMAPPLI

    Joined:
    Jul 12, 2020
    Posts:
    6
    in the API documentation I noticed these considerations.

    On iOS and macOS, we highly recommend using Metal. On Android, we highly recommend using Vulkan. On Windows

    However, for applications that use Vuforia they must use OpenGL ES2 to avoid compatibility problems. In the API description in the store it clearly said it was compatible with Vuforia.

    My question is how to get better performance in applications that use vuforia in view of these considerations?

    Thank you.
     
  33. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    NatCorder is compatible with Vuforia. The performance discussion centers around how recording happens: NatCorder expects the client to commit RGBA8888 pixel buffers resident in system memory; but when recording from a game camera, this frame buffer lives on the GPU. As such, the CameraInput script issues a readback to get the pixel buffer in system memory. As the docs explain, there are synchronous readbacks (which stall both the CPU and GPU) and asynchronous readbacks (which don't stall, but instead have latency on the order of a few frames). Vulkan supports async readbacks, OpenGL ES3 also supports them (through PBO's), but OpenGL ES2 does not. Unity exposes this functionality through the AsyncGPUReadback API, but they have chosen not to support it on OpenGL ES (3). And finally to make matters worse, Android itself has poor support for Vulkan so some its most fundamental graphics constructs (like SurfaceTexture) can only be used with OpenGL ES and not Vulkan. This is the reason why ARCore specifically doesn't support Vulkan and likely why Vuforia doesn't either (but I'm surprised, Vuforia doesn't support GLES3?).

    All this to say that if your Android app must use OpenGL ES, like if you are building an AR app, then you will not be able to benefit from the better performance afforded by async GPU readbacks. Depending on how heavy your app's rendering is, and how powerful the device running your app is, you might not notice a difference, so it all depends.
     
  34. RSMAPPLI

    RSMAPPLI

    Joined:
    Jul 12, 2020
    Posts:
    6
    Tests of my app and also in research I did showed that Vuforia in its latest version presents the image of the camera completely black when using GLES3 or Vulkan on some Android devices and the solution found, by me and by several developers who mentioned the same problem on the forums, it was using GLES2. I cannot say with certainty that this would be the only way out but it seems to be the most used.
     
    Lanre likes this.
  35. RSMAPPLI

    RSMAPPLI

    Joined:
    Jul 12, 2020
    Posts:
    6
    Testing with Vuforia and a Video Player in AR, I noticed that several times the video player starts without image when it is executed with the recording of the active screen (the player's screen is black, reproducing only the sound). This does not happen without recording the screen or starting recording after the player's video has already started. Would you know if you can avoid this?
     
  36. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    I'm not sure what could be causing this, beyond the device being oversubscribed. I recommend checking the logs for more clues.
     
  37. ualogic

    ualogic

    Joined:
    Oct 15, 2018
    Posts:
    25
    Hi, I am using NatCorder with an audiolistener to record all the scene sounds, but there are system sounds that I don't want it to record. Like start and stop recording beeps. Is there any way to record one pack of sounds, but not the other? Except, of-course, using single audiosourses.

    Also if I set VideoPlayer component to output sound directly it is not being recorded (by audiolistener recorder), so I am setting it to use audiosourse. Is this the way to go, or there is anything else I can do?

    Thanks.
     
  38. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    There is no way to record some but not other AudioSources. You would have to manually mix the audio from the audio sources yourself, which is not trivial.
    NatCorder does not automatically record anything; everything that makes it to the final recording has to be sent to the recorder; this is what the CameraInput and AudioInput scripts help you do. When you set the VideoPlayer to play directly to the speaker, that audio is not being sent to the recorder. So set the output to an AudioSource, then record either from that AudioSource, or from the AudioListener in the scene which picks up the audio.
     
  39. SweatyChair

    SweatyChair

    Joined:
    Feb 15, 2016
    Posts:
    140
    I cannot update to the latest version because asset store said 2019.3 required, while I'm still in 2018.4.

    I can see the doc saying just require 2018.3, is that a mistake or 2019.3 is really required?
     
  40. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    A few notes on this:
    • Technically speaking, NatCorder will work with Unity 2018.3+. NatCorder only needs two things which are in 2018.3 and newer: the stable AsyncGPUReadback API; and C# 7.
    • Less technically speaking, you should really only use 2019.2+. The reason here is that there have been many reports of a weird bug that has affected 2018.3 and 2018.4 LTS where offscreen rendering on iOS Metal (how CameraInput records game cameras) causes the screen to freeze. Unity 2019.2 fixes this issue.
    • The reason why the Asset Store mentions 2019.3 is because that was the version of Unity used to submit the package to the Asset Store. While not required, I always very (very) strongly recommend developers use the latest versions of both NatCorder and Unity. Unity now has the 2019 LTS.
    If the Asset Store prevents you from importing to 2018.4, you can download the package with 2019, then copy the NatSuite folder into your 2018 project.
     
  41. monda

    monda

    Joined:
    May 14, 2015
    Posts:
    35
    Hi, after several tests on the actual device (I had to buy a Xiaomi Redmi note 7 to test it myself) I've found that the problem was the resolution, which was calculated by taking the Screen.Height and Screen.Weight then divided by 4 (I've changed to divide by 2 and works).

    Now works on that device but I have the exact same problem with other devices and for obvious reasons I cannot change the Recording resolution for each device...I've tried to manually force 1280x720 but the it crashes on all devices (And obviously doesn't keep the device aspect ratio).

    My question is: What is the best way to Record with Natcorder and keep a decent resolution for all devices (and keep the device's aspect ratio)?
    Thanks!
     
  42. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Sounds like you're running into a problem where either the height or width is an odd number.
    This sounds like something totally different. Recording at 1280x720 will never cause a crash on any device. Are you recording with your custom code? If so can you share it?
    I don't recommend trying to keep the device's aspect ratio, unless you are working in AR which requires this. Use a standard resolution like 1280x720 or 1920x1080.
     
  43. monda

    monda

    Joined:
    May 14, 2015
    Posts:
    35
    Yeah..it's an AR app...

    I've also tried to record using the device's actual resolution and it works but obviously on low spec devices with big screen it's a problem.

    This is the code:

    Code (CSharp):
    1.  void StartRecording()
    2.         {
    3. //Set Resolution
    4. videoWidth = Screen.Width/2;
    5. videoHeight = Screen.Height/2;
    6.  
    7.             // Get a microphone
    8.             var query = new MediaDeviceQuery(MediaDeviceQuery.Criteria.AudioDevice);
    9.             audioDevice = query.currentDevice as IAudioDevice;
    10.             // Create recorder
    11.             var clock = new RealtimeClock();
    12.             recorder = new MP4Recorder(videoWidth, videoHeight, 25, audioDevice.sampleRate, audioDevice.channelCount);
    13.             // Stream media samples
    14.             cameraInput = new CameraInput(recorder, clock, Camera.main);
    15.             audioDevice.StartRunning((sampleBuffer, timestamp) => recorder.CommitSamples(sampleBuffer, clock.timestamp));
    16.         }
    I've also tried 1280x720 but it crashes on many devices, works on iOS but Several Android devices crash like immediately but even if worked my client will never allow a "deformed" video, I really need to keep the aspect ratio.
     
  44. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Did you check the link I shared in my earlier response? The video width and height must not be odd numbers. Your code does not protect against this, as 'Screen.width / 2' or 'Screen.height / 2' could very easily be odd numbers, ending in the crashes you see.
     
  45. jiraphatK

    jiraphatK

    Joined:
    Sep 29, 2018
    Posts:
    293
    Try this, I used this code to keep the record resolution below fullhd while also keeping aspect ratio and avoiding odd number resolution.

    Code (CSharp):
    1.  
    2.         var width = 0;
    3.         var height = 0;
    4.         if (Input.deviceOrientation == DeviceOrientation.LandscapeLeft || Input.deviceOrientation == DeviceOrientation.LandscapeRight)
    5.         {
    6.             float ratio;
    7.             //landscape mode
    8.             if (Screen.width <= 1920 && Screen.height <= 1080)
    9.             {
    10.                 width = Screen.width;
    11.                 height = Screen.height;
    12.             }
    13.             else
    14.             {
    15.                 ratio = (float)1920 / Screen.width;
    16.                 width = 1920;
    17.                 height = nearestEvenInt(Mathf.FloorToInt(Screen.height * ratio));
    18.             }
    19.             // width = Screen.width <= 1920 ? Screen.width : 1920;
    20.             // height = Screen.height <= 1080 ? Screen.height : 1080;
    21.         }
    22.         else
    23.         {
    24.             //portrait mode
    25.             float ratio;
    26.             if (Screen.height <= 1920 && Screen.width <= 1080)
    27.             {
    28.                 width = Screen.width;
    29.                 height = Screen.height;
    30.             }
    31.             else
    32.             {
    33.                 ratio = (float)1920 / Screen.height;
    34.                 height = 1920;
    35.                 width = nearestEvenInt(Mathf.FloorToInt(Screen.width * ratio));
    36.             }
    37.             // width = Screen.width <= 1080 ? Screen.width : 1080;
    38.             // height = Screen.height <= 1920 ? Screen.height : 1920;
    39.         }
    40.         Debug.Log("Width = " + width + " , " + " Height = " + height);
    41.         isRecord = true;
    42.         recorder = new MP4Recorder(width, height, 30, sampleRate, channelCount);
    43.  
    44. int nearestEvenInt(int to)
    45. {
    46.     return (to % 2 == 0) ? to : (to - 1);
    47. }
    48.  
     
    Lanre likes this.
  46. jiraphatK

    jiraphatK

    Joined:
    Sep 29, 2018
    Posts:
    293
    Lanre, is there an example script on multithread recording? I use Natcorder to record AR with CameraInput. I want all the performance I can squeeze out.
     
  47. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Here's a way to make this more concise:
    Code (CSharp):
    1. // Aspect scale
    2. var width = 1920;
    3. var height = (int)(width * Mathf.Min(Screen.height, Screen.width) / (float)Mathf.Max(Screen.height, Screen.width));
    4. // Ensure divisible by 2
    5. (width, height) = (width >> 1 << 1, height >> 1 << 1);
    6. // Ensure proper orientation
    7. (width, height) = Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight ? (width, height) : (height, width);
    8.  
     
    Last edited: Sep 21, 2020
    jiraphatK likes this.
  48. monda

    monda

    Joined:
    May 14, 2015
    Posts:
    35
    Thanks for this, I've just tried but...maybe I don't understand it properly but my app is always in portrait mode and I can see the last line is all about landscape. Also got an error:
    "Type of conditional expression cannot be determined because there is no implicit conversion between '(int, ?)' and '(int, int)'"
     
  49. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    Hey there. Multithreaded recording won't be helpful when recording with a CameraInput. The reason is that the main bottleneck when recording from game cameras is reading back the pixel data from the GPU. The actual call to `CommitFrame` should take only a few milliseconds. I don't have example code for this use case.

    On the other hand, when doing offline recording, you can trivially thread recording using the Task API:
    Code (CSharp):
    1. var recordingPath = await Task.Run(() => {
    2.     // commit frames
    3.     foreach (var (pixelBuffer, timestamp) in ?)
    4.         recorder.CommitFrame(pixelBuffer, timestamp);
    5.     // finish recording
    6.     return recorder.FinishWriting();
    7. });
     
  50. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,970
    I fixed a typo in the code. It should work now.