MP4Recorder.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * NatCorder
  3. * Copyright (c) 2020 Yusuf Olokoba.
  4. */
  5. namespace NatSuite.Recorders {
  6. using System;
  7. using System.Threading.Tasks;
  8. using Internal;
  9. /// <summary>
  10. /// MP4 video recorder.
  11. /// </summary>
  12. public sealed class MP4Recorder : IMediaRecorder {
  13. #region --Client API--
  14. /// <summary>
  15. /// Video size.
  16. /// </summary>
  17. public (int width, int height) frameSize => recorder.frameSize;
  18. /// <summary>
  19. /// Create an MP4 recorder.
  20. /// </summary>
  21. /// <param name="width">Video width.</param>
  22. /// <param name="height">Video height.</param>
  23. /// <param name="frameRate">Video frame rate.</param>
  24. /// <param name="sampleRate">Audio sample rate. Pass 0 for no audio.</param>
  25. /// <param name="channelCount">Audio channel count. Pass 0 for no audio.</param>
  26. /// <param name="bitrate">Video bitrate in bits per second.</param>
  27. /// <param name="keyframeInterval">Keyframe interval in seconds.</param>
  28. public MP4Recorder (int width, int height, float frameRate, int sampleRate = 0, int channelCount = 0, int bitrate = (int)(960 * 540 * 11.4f), int keyframeInterval = 3) => this.recorder = new NativeRecorder((callback, context) => Bridge.CreateMP4Recorder(width, height, frameRate, bitrate, keyframeInterval, sampleRate, channelCount, Utility.GetPath(@".mp4"), callback, context));
  29. public MP4Recorder(string path,int width, int height, float frameRate,int sampleRate = 0, int channelCount = 0, int bitrate = (int)(960 * 540 * 11.4f), int keyframeInterval = 3) => this.recorder = new NativeRecorder((callback, context) => Bridge.CreateMP4Recorder(width, height, frameRate, bitrate, keyframeInterval, sampleRate, channelCount, path, callback, context));
  30. /// <summary>
  31. /// Commit a video pixel buffer for encoding.
  32. /// The pixel buffer MUST have an RGBA8888 pixel layout.
  33. /// </summary>
  34. /// <param name="pixelBuffer">Pixel buffer containing video frame to commit.</param>
  35. /// <param name="timestamp">Frame timestamp in nanoseconds.</param>
  36. public void CommitFrame<T> (T[] pixelBuffer, long timestamp) where T : struct => recorder.CommitFrame(pixelBuffer, timestamp);
  37. /// <summary>
  38. /// Commit a video pixel buffer for encoding.
  39. /// The pixel buffer MUST have an RGBA8888 pixel layout.
  40. /// </summary>
  41. /// <param name="nativeBuffer">Pixel buffer in native memory to commit.</param>
  42. /// <param name="timestamp">Frame timestamp in nanoseconds.</param>
  43. public void CommitFrame (IntPtr nativeBuffer, long timestamp) => recorder.CommitFrame(nativeBuffer, timestamp);
  44. /// <summary>
  45. /// Commit an audio sample buffer for encoding.
  46. /// </summary>
  47. /// <param name="sampleBuffer">Linear PCM audio sample buffer, interleaved by channel.</param>
  48. /// <param name="timestamp">Sample buffer timestamp in nanoseconds.</param>
  49. public void CommitSamples (float[] sampleBuffer, long timestamp) => recorder.CommitSamples(sampleBuffer, timestamp);
  50. /// <summary>
  51. /// Finish writing and return the path to the recorded media file.
  52. /// </summary>
  53. public Task<string> FinishWriting () => recorder.FinishWriting();
  54. #endregion
  55. private readonly IMediaRecorder recorder;
  56. }
  57. }