RawVideoPipeDataWriter updated

Former-commit-id: 5ad1a3931a
This commit is contained in:
Максим Багрянцев 2020-05-02 13:39:48 +03:00
parent b9b13052b9
commit c7047b6a5f

View file

@ -1,4 +1,5 @@
using FFMpegCore.FFMPEG.Argument; using FFMpegCore.FFMPEG.Argument;
using FFMpegCore.FFMPEG.Exceptions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@ -15,6 +16,7 @@ public class RawVideoPipeDataWriter : IPipeDataWriter
public int Width { get; private set; } public int Width { get; private set; }
public int Height { get; private set; } public int Height { get; private set; }
public int FrameRate { get; set; } = 25; public int FrameRate { get; set; } = 25;
private bool formatInitialized = false;
private IEnumerator<IVideoFrame> framesEnumerator; private IEnumerator<IVideoFrame> framesEnumerator;
public RawVideoPipeDataWriter(IEnumerator<IVideoFrame> framesEnumerator) public RawVideoPipeDataWriter(IEnumerator<IVideoFrame> framesEnumerator)
@ -26,16 +28,20 @@ public RawVideoPipeDataWriter(IEnumerable<IVideoFrame> framesEnumerator) : this(
public string GetFormat() public string GetFormat()
{ {
//see input format references https://lists.ffmpeg.org/pipermail/ffmpeg-user/2012-July/007742.html if (!formatInitialized)
if (framesEnumerator.Current == null)
{ {
if (!framesEnumerator.MoveNext()) //see input format references https://lists.ffmpeg.org/pipermail/ffmpeg-user/2012-July/007742.html
throw new InvalidOperationException("Enumerator is empty, unable to get frame"); if (framesEnumerator.Current == null)
} {
StreamFormat = framesEnumerator.Current.Format; if (!framesEnumerator.MoveNext())
Width = framesEnumerator.Current.Width; throw new InvalidOperationException("Enumerator is empty, unable to get frame");
Height = framesEnumerator.Current.Height; }
StreamFormat = framesEnumerator.Current.Format;
Width = framesEnumerator.Current.Width;
Height = framesEnumerator.Current.Height;
formatInitialized = true;
}
return $"-f rawvideo -r {FrameRate} -pix_fmt {StreamFormat} -s {Width}x{Height}"; return $"-f rawvideo -r {FrameRate} -pix_fmt {StreamFormat} -s {Width}x{Height}";
} }
@ -44,11 +50,13 @@ public void WriteData(System.IO.Stream stream)
{ {
if (framesEnumerator.Current != null) if (framesEnumerator.Current != null)
{ {
CheckFrameAndThrow(framesEnumerator.Current);
framesEnumerator.Current.Serialize(stream); framesEnumerator.Current.Serialize(stream);
} }
while (framesEnumerator.MoveNext()) while (framesEnumerator.MoveNext())
{ {
CheckFrameAndThrow(framesEnumerator.Current);
framesEnumerator.Current.Serialize(stream); framesEnumerator.Current.Serialize(stream);
} }
} }
@ -66,5 +74,12 @@ public async Task WriteDataAsync(System.IO.Stream stream)
} }
} }
private void CheckFrameAndThrow(IVideoFrame frame)
{
if (frame.Width != Width || frame.Height != Height || frame.Format != StreamFormat)
throw new FFMpegException(FFMpegExceptionType.Operation, "Video frame is not the same format as created raw video stream\r\n" +
$"Frame format: {frame.Width}x{frame.Height} pix_fmt: {frame.Format}\r\n" +
$"Stream format: {Width}x{Height} pix_fmt: {StreamFormat}");
}
} }
} }