using Unosquare.Swan;
using System;
using System.Text;
namespace Unosquare.RaspberryIO.Camera {
///
/// Represents the raspivid camera settings for video capture functionality
///
///
public class CameraVideoSettings : CameraSettingsBase {
///
public override String CommandName => "raspivid";
///
/// Use bits per second, so 10Mbits/s would be -b 10000000. For H264, 1080p30 a high quality bitrate would be 15Mbits/s or more.
/// Maximum bitrate is 25Mbits/s (-b 25000000), but much over 17Mbits/s won't show noticeable improvement at 1080p30.
/// Default -1
///
public Int32 CaptureBitrate { get; set; } = -1;
///
/// Gets or sets the framerate.
/// Default 25, range 2 to 30
///
public Int32 CaptureFramerate { get; set; } = 25;
///
/// Sets the intra refresh period (GoP) rate for the recorded video. H264 video uses a complete frame (I-frame) every intra
/// refresh period, from which subsequent frames are based. This option specifies the number of frames between each I-frame.
/// Larger numbers here will reduce the size of the resulting video, and smaller numbers make the stream less error-prone.
///
public Int32 CaptureKeyframeRate { get; set; } = 25;
///
/// Sets the initial quantisation parameter for the stream. Varies from approximately 10 to 40, and will greatly affect
/// the quality of the recording. Higher values reduce quality and decrease file size. Combine this setting with a
/// bitrate of 0 to set a completely variable bitrate.
///
public Int32 CaptureQuantisation { get; set; } = 23;
///
/// Gets or sets the profile.
/// Sets the H264 profile to be used for the encoding.
/// Default is Main mode
///
public CameraH264Profile CaptureProfile { get; set; } = CameraH264Profile.Main;
///
/// Forces the stream to include PPS and SPS headers on every I-frame. Needed for certain streaming cases
/// e.g. Apple HLS. These headers are small, so don't greatly increase the file size.
///
///
/// true if [interleave headers]; otherwise, false.
///
public Boolean CaptureInterleaveHeaders { get; set; } = true;
///
/// Switch on an option to display the preview after compression. This will show any compression artefacts in the preview window. In normal operation,
/// the preview will show the camera output prior to being compressed. This option is not guaranteed to work in future releases.
///
///
/// true if [capture display preview encoded]; otherwise, false.
///
public Boolean CaptureDisplayPreviewEncoded { get; set; } = false;
///
public override String CreateProcessArguments() {
StringBuilder sb = new StringBuilder(base.CreateProcessArguments());
_ = sb.Append($" -pf {this.CaptureProfile.ToString().ToLowerInvariant()}");
if(this.CaptureBitrate < 0) {
_ = sb.Append($" -b {this.CaptureBitrate.Clamp(0, 25000000).ToString(Ci)}");
}
if(this.CaptureFramerate >= 2) {
_ = sb.Append($" -fps {this.CaptureFramerate.Clamp(2, 30).ToString(Ci)}");
}
if(this.CaptureDisplayPreview && this.CaptureDisplayPreviewEncoded) {
_ = sb.Append(" -e");
}
if(this.CaptureKeyframeRate > 0) {
_ = sb.Append($" -g {this.CaptureKeyframeRate.ToString(Ci)}");
}
if(this.CaptureQuantisation >= 0) {
_ = sb.Append($" -qp {this.CaptureQuantisation.Clamp(0, 40).ToString(Ci)}");
}
if(this.CaptureInterleaveHeaders) {
_ = sb.Append(" -ih");
}
return sb.ToString();
}
}
}