Skip to content

Commit

Permalink
Merge pull request #38 from ChristopheI/master
Browse files Browse the repository at this point in the history
For coherency remove event OnEndOfFile and use instead OnAudioSourceError or OnVideoSourceError.
  • Loading branch information
ChristopheI authored Jul 1, 2022
2 parents a0416aa + 3cfbb0d commit c38a18a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 86 deletions.
8 changes: 1 addition & 7 deletions src/FFmpegAudioDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,13 +315,7 @@ private unsafe void RunDecodeLoop()
}
else
{
if (_isMicrophone)
{
RaiseError("Cannot read more frame");
return;
}
else
OnEndOfFile?.Invoke();
OnEndOfFile?.Invoke();
}
}
}
Expand Down
54 changes: 26 additions & 28 deletions src/FFmpegAudioSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class FFmpegAudioSource : IAudioSource, IDisposable
internal byte[] buffer; // Avoid to create buffer of same size

private int frameSize;
private String path;

private BasicBufferShort _incomingSamples = new BasicBufferShort(48000);

Expand All @@ -32,7 +33,6 @@ public class FFmpegAudioSource : IAudioSource, IDisposable
internal MediaFormatManager<AudioFormat> _audioFormatManager;

public event EncodedSampleDelegate? OnAudioSourceEncodedSample;
public event Action? OnEndOfFile;
public event SourceErrorDelegate? OnAudioSourceError;

#pragma warning disable CS0067
Expand All @@ -55,25 +55,26 @@ public FFmpegAudioSource(IAudioEncoder audioEncoder, uint frameSize = 960)

public unsafe void CreateAudioDecoder(String path, AVInputFormat* avInputFormat, bool repeat = false, bool isMicrophone = false)
{
this.path = path;
_audioDecoder = new FFmpegAudioDecoder(path, avInputFormat, repeat, isMicrophone);

_audioDecoder.OnAudioFrame += AudioDecoder_OnAudioFrame;
_audioDecoder.OnError += AudioDecoder_OnError;
_audioDecoder.OnEndOfFile += AudioDecoder_OnEndOfFile;
}

_audioDecoder.OnError += (msg) =>
{
OnAudioSourceError?.Invoke(msg);
Dispose();
};

private void AudioDecoder_OnEndOfFile()
{
AudioDecoder_OnError("End of file");
}

_audioDecoder.OnEndOfFile += () =>
{
logger.LogDebug($"File source decode complete for {path}.");
OnEndOfFile?.Invoke();
_audioDecoder.Dispose();
};
private void AudioDecoder_OnError(string errorMessage)
{
logger.LogDebug($"Audio - Source error for {path} - ErrorMessage:[{errorMessage}]");
OnAudioSourceError?.Invoke(errorMessage);
Dispose();
}

public Boolean InitialiseDecoder()
{
return _audioDecoder?.InitialiseSource(_audioFormatManager.SelectedFormat.ClockRate) == true;
Expand All @@ -90,12 +91,9 @@ public List<AudioFormat> GetAudioSourceFormats()

public void SetAudioSourceFormat(AudioFormat audioFormat)
{
if (_audioFormatManager != null)
{
logger.LogDebug($"Setting audio source format to {audioFormat.FormatID}:{audioFormat.Codec} {audioFormat.ClockRate}.");
_audioFormatManager.SetSelectedFormat(audioFormat);
InitialiseDecoder();
}
logger.LogDebug($"Setting audio source format to {audioFormat.FormatID}:{audioFormat.Codec} {audioFormat.ClockRate}.");
_audioFormatManager.SetSelectedFormat(audioFormat);
InitialiseDecoder();
}

public void RestrictFormats(Func<AudioFormat, bool> filter)
Expand Down Expand Up @@ -164,14 +162,9 @@ public Task Start()
{
if (!_isStarted)
{
if (_audioDecoder == null)
{
OnAudioSourceError?.Invoke("Initialization has not be done correctly");
}
else
if (_audioDecoder != null)
{
_isStarted = true;
InitialiseDecoder();
_audioDecoder.StartDecode();
}
}
Expand Down Expand Up @@ -221,9 +214,14 @@ public void Dispose()
_isStarted = false;

if (_audioDecoder != null)
_audioDecoder.Dispose();
{
_audioDecoder.OnAudioFrame -= AudioDecoder_OnAudioFrame;
_audioDecoder.OnError -= AudioDecoder_OnError;
_audioDecoder.OnEndOfFile -= AudioDecoder_OnEndOfFile;

_audioDecoder = null;
_audioDecoder.Dispose();
_audioDecoder = null;
}
}
}
}
Expand Down
17 changes: 0 additions & 17 deletions src/FFmpegFileSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class FFmpegFileSource: IAudioSource, IVideoSource, IDisposable
public event EncodedSampleDelegate? OnVideoSourceEncodedSample;
public event RawVideoSampleFasterDelegate? OnVideoSourceRawSampleFaster;

public event Action? OnEndOfFile;

public event SourceErrorDelegate? OnAudioSourceError;
public event SourceErrorDelegate? OnVideoSourceError;

Expand All @@ -50,19 +48,16 @@ public unsafe FFmpegFileSource(string path, bool repeat, IAudioEncoder? audioEnc

_FFmpegAudioSource.OnAudioSourceEncodedSample += _FFmpegAudioSource_OnAudioSourceEncodedSample;
_FFmpegAudioSource.OnAudioSourceRawSample += _FFmpegAudioSource_OnAudioSourceRawSample;
_FFmpegAudioSource.OnEndOfFile += _FFmpegAudioSource_OnEndOfFile;
_FFmpegAudioSource.OnAudioSourceError += _FFmpegAudioSource_OnAudioSourceError;
}

if (useVideo)
{
_FFmpegVideoSource = new FFmpegVideoSource();
_FFmpegVideoSource.CreateVideoDecoder(path, null, repeat, false);
_FFmpegVideoSource.InitialiseDecoder();

_FFmpegVideoSource.OnVideoSourceEncodedSample += _FFmpegVideoSource_OnVideoSourceEncodedSample;
_FFmpegVideoSource.OnVideoSourceRawSampleFaster += _FFmpegVideoSource_OnVideoSourceRawSampleFaster;
_FFmpegVideoSource.OnEndOfFile += _FFmpegVideoSource_OnEndOfFile;
_FFmpegVideoSource.OnVideoSourceError += _FFmpegVideoSource_OnVideoSourceError;
}
}
Expand All @@ -77,16 +72,6 @@ private void _FFmpegVideoSource_OnVideoSourceError(string errorMessage)
OnVideoSourceError?.Invoke(errorMessage);
}

private void _FFmpegVideoSource_OnEndOfFile()
{
OnEndOfFile?.Invoke();
}

private void _FFmpegAudioSource_OnEndOfFile()
{
OnEndOfFile?.Invoke();
}

private void _FFmpegVideoSource_OnVideoSourceEncodedSample(uint durationRtpUnits, byte[] sample)
{
OnVideoSourceEncodedSample?.Invoke(durationRtpUnits, sample);
Expand Down Expand Up @@ -288,7 +273,6 @@ public void Dispose()
{
_FFmpegAudioSource.OnAudioSourceEncodedSample -= _FFmpegAudioSource_OnAudioSourceEncodedSample;
_FFmpegAudioSource.OnAudioSourceRawSample -= _FFmpegAudioSource_OnAudioSourceRawSample;
_FFmpegAudioSource.OnEndOfFile -= _FFmpegAudioSource_OnEndOfFile;
_FFmpegAudioSource.OnAudioSourceError -= _FFmpegAudioSource_OnAudioSourceError;

_FFmpegAudioSource.Dispose();
Expand All @@ -300,7 +284,6 @@ public void Dispose()
{
_FFmpegVideoSource.OnVideoSourceEncodedSample -= _FFmpegVideoSource_OnVideoSourceEncodedSample;
_FFmpegVideoSource.OnVideoSourceRawSampleFaster -= _FFmpegVideoSource_OnVideoSourceRawSampleFaster;
_FFmpegVideoSource.OnEndOfFile -= _FFmpegVideoSource_OnEndOfFile;
_FFmpegVideoSource.OnVideoSourceError -= _FFmpegVideoSource_OnVideoSourceError;

_FFmpegVideoSource.Dispose();
Expand Down
8 changes: 1 addition & 7 deletions src/FFmpegVideoDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,7 @@ private void RunDecodeLoop()
}
else
{
if (_isCamera)
{
RaiseError("Cannot read more frame");
return;
}
else
OnEndOfFile?.Invoke();
OnEndOfFile?.Invoke();
}
}
}
Expand Down
56 changes: 34 additions & 22 deletions src/FFmpegVideoSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class FFmpegVideoSource: IVideoSource, IDisposable
internal bool _isPaused;
internal bool _isClosed;

private String path;

internal FFmpegVideoDecoder ? _videoDecoder;

internal VideoFrameConverter? _videoFrameYUV420PConverter = null;
Expand All @@ -34,8 +36,6 @@ public class FFmpegVideoSource: IVideoSource, IDisposable

public event SourceErrorDelegate? OnVideoSourceError;

public event Action? OnEndOfFile;

#pragma warning disable CS0067
public event RawVideoSampleDelegate? OnVideoSourceRawSample;
#pragma warning restore CS0067
Expand All @@ -44,25 +44,29 @@ public FFmpegVideoSource()
{
_videoFormatManager = new MediaFormatManager<VideoFormat>(_supportedVideoFormats);
_videoEncoder = new FFmpegVideoEncoder();
path = "";
}

public unsafe void CreateVideoDecoder(String path, AVInputFormat* avInputFormat, bool repeat = false, bool isCamera = false)
{
this.path = path;
_videoDecoder = new FFmpegVideoDecoder(path, avInputFormat, repeat, isCamera);

_videoDecoder.OnVideoFrame += VideoDecoder_OnVideoFrame;
_videoDecoder.OnError += VideoDecoder_OnError;
_videoDecoder.OnEndOfFile += VideoDecoder_OnEndOfFile;
}

_videoDecoder.OnError += (msg) =>
{
OnVideoSourceError?.Invoke(msg);
Dispose();
};
private void VideoDecoder_OnEndOfFile()
{
VideoDecoder_OnError("End of file");
}

_videoDecoder.OnEndOfFile += () =>
{
logger.LogDebug($"File source decode complete for {path}.");
OnEndOfFile?.Invoke();
Dispose();
};
private void VideoDecoder_OnError(string errorMessage)
{
logger.LogDebug($"Video - Source error for {path} - ErrorMessage:[{errorMessage}]");
OnVideoSourceError?.Invoke(errorMessage);
Dispose();
}

public Boolean InitialiseDecoder(Dictionary<string, string>? decoderOptions = null)
Expand All @@ -81,7 +85,9 @@ public void SetVideoSourceFormat(VideoFormat videoFormat)
{
logger.LogDebug($"Setting video source format to {videoFormat.FormatID}:{videoFormat.Codec} {videoFormat.ClockRate}.");
_videoFormatManager.SetSelectedFormat(videoFormat);
InitialiseDecoder();
}

public void RestrictFormats(Func<VideoFormat, bool> filter)
{
_videoFormatManager.RestrictFormats(filter);
Expand Down Expand Up @@ -178,11 +184,7 @@ public Task Start()
{
if (!_isStarted)
{
if (_videoDecoder == null)
{
OnVideoSourceError?.Invoke("Initialization has not be done correctly");
}
else
if (_videoDecoder != null)
{
_isStarted = true;
_videoDecoder?.StartDecode();
Expand Down Expand Up @@ -230,11 +232,21 @@ public void Dispose()
{
_isStarted = false;

_videoDecoder?.Dispose();
_videoDecoder = null;
if (_videoDecoder != null)
{
_videoDecoder.OnVideoFrame -= VideoDecoder_OnVideoFrame;
_videoDecoder.OnError -= VideoDecoder_OnError;
_videoDecoder.OnEndOfFile -= VideoDecoder_OnEndOfFile;

_videoEncoder?.Dispose();
_videoEncoder = null;
_videoDecoder.Dispose();
_videoDecoder = null;
}

if (_videoEncoder != null)
{
_videoEncoder.Dispose();
_videoEncoder = null;
}
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/SIPSorceryMedia.FFmpeg.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>1.2</Version>
<Version>1.2.1</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
Expand Down
15 changes: 11 additions & 4 deletions test/FFmpegFileAndDevicesTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,16 @@ static private Task<RTCPeerConnection> CreatePeerConnection()
if ((AudioSourceType == AUDIO_SOURCE.FILE_OR_STREAM) && (AudioSourceFile == VideoSourceFile))
{
SIPSorceryMedia.FFmpeg.FFmpegFileSource fileSource = new SIPSorceryMedia.FFmpeg.FFmpegFileSource(VideoSourceFile, RepeatVideoFile, new AudioEncoder(), 960, true);
fileSource.OnEndOfFile += () => PeerConnection.Close("source eof");
fileSource.OnAudioSourceError += (msg) => PeerConnection.Close(msg);
fileSource.OnVideoSourceError += (msg) => PeerConnection.Close(msg);

videoSource = fileSource as IVideoSource;
audioSource = fileSource as IAudioSource;
}
else
{
SIPSorceryMedia.FFmpeg.FFmpegFileSource fileSource = new SIPSorceryMedia.FFmpeg.FFmpegFileSource(VideoSourceFile, RepeatVideoFile, new AudioEncoder(), 960, true);
fileSource.OnEndOfFile += () => PeerConnection.Close("source eof");
fileSource.OnVideoSourceError += (msg) => PeerConnection.Close(msg);

videoSource = fileSource as IVideoSource;
}
Expand All @@ -164,7 +165,10 @@ static private Task<RTCPeerConnection> CreatePeerConnection()
camera = cameras.Last();
}
if (camera != null)
{
videoSource = new SIPSorceryMedia.FFmpeg.FFmpegCameraSource(camera.Path);
videoSource.OnVideoSourceError += (msg) => PeerConnection.Close(msg);
}
else
throw new NotSupportedException($"Cannot find adequate camera ...");

Expand All @@ -187,8 +191,11 @@ static private Task<RTCPeerConnection> CreatePeerConnection()
primaryMonitor = monitors[0];
}

if(primaryMonitor != null)
if (primaryMonitor != null)
{
videoSource = new SIPSorceryMedia.FFmpeg.FFmpegScreenSource(primaryMonitor.Path, primaryMonitor.Rect, 10);
videoSource.OnVideoSourceError += (msg) => PeerConnection.Close(msg);
}
else
throw new NotSupportedException($"Cannot find adequate monitor ...");
break;
Expand All @@ -200,7 +207,7 @@ static private Task<RTCPeerConnection> CreatePeerConnection()
{
case AUDIO_SOURCE.FILE_OR_STREAM:
SIPSorceryMedia.FFmpeg.FFmpegFileSource fileSource = new SIPSorceryMedia.FFmpeg.FFmpegFileSource(AudioSourceFile, RepeatAudioFile, new AudioEncoder(), 960, false);
fileSource.OnEndOfFile += () => PeerConnection.Close("source eof");
fileSource.OnAudioSourceError += (msg) => PeerConnection.Close(msg);

audioSource = fileSource as IAudioSource;
break;
Expand Down

0 comments on commit c38a18a

Please sign in to comment.