Skip to content

Commit

Permalink
Adjustments for ffmpeg file processing fixes. (sipsorcery-org#1279)
Browse files Browse the repository at this point in the history
* Adjustments for ffmpeg file processing fixes.

* Removed ffmpeg project reference from webrtc examples.

* Updated the ffmpeg nuget version in the mp4 demo.
  • Loading branch information
sipsorcery authored Jan 11, 2025
1 parent d7833a6 commit dedc327
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 28 deletions.
6 changes: 3 additions & 3 deletions examples/WebRTCExamples/WebRTCEchoServer/WebRTCEchoServer.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public async Task<RTCSessionDescriptionInit> GotOffer(RTCSessionDescriptionInit
{
_logger.LogDebug($"RTP event received: {ev.EventID}.");
// Echo the DTMF event back.
var echoEvent = new RTPEvent(ev.EventID, true, RTPEvent.DEFAULT_VOLUME, RTPSession.DTMF_EVENT_DURATION, RTPSession.DTMF_EVENT_PAYLOAD_ID);
var echoEvent = new RTPEvent(ev.EventID, true, RTPEvent.DEFAULT_VOLUME, RTPSession.DTMF_EVENT_DURATION, RTPSession.DEFAULT_DTMF_EVENT_PAYLOAD_ID);
await pc.SendDtmfEvent(echoEvent, CancellationToken.None).ConfigureAwait(false);
}

Expand All @@ -109,15 +109,15 @@ public async Task<RTCSessionDescriptionInit> GotOffer(RTCSessionDescriptionInit
{
_logger.LogDebug($"RTP event received: {ev.EventID}.");
// Echo the DTMF event back.
var echoEvent = new RTPEvent(ev.EventID, true, RTPEvent.DEFAULT_VOLUME, RTPSession.DTMF_EVENT_DURATION, RTPSession.DTMF_EVENT_PAYLOAD_ID);
var echoEvent = new RTPEvent(ev.EventID, true, RTPEvent.DEFAULT_VOLUME, RTPSession.DTMF_EVENT_DURATION, RTPSession.DEFAULT_DTMF_EVENT_PAYLOAD_ID);
await pc.SendDtmfEvent(echoEvent, CancellationToken.None).ConfigureAwait(false);
}
else if (!ev.EndOfEvent)
{
_rtpEventSsrc = hdr.SyncSource;
_logger.LogDebug($"RTP event received: {ev.EventID}.");
// Echo the DTMF event back.
var echoEvent = new RTPEvent(ev.EventID, true, RTPEvent.DEFAULT_VOLUME, RTPSession.DTMF_EVENT_DURATION, RTPSession.DTMF_EVENT_PAYLOAD_ID);
var echoEvent = new RTPEvent(ev.EventID, true, RTPEvent.DEFAULT_VOLUME, RTPSession.DTMF_EVENT_DURATION, RTPSession.DEFAULT_DTMF_EVENT_PAYLOAD_ID);
await pc.SendDtmfEvent(echoEvent, CancellationToken.None).ConfigureAwait(false);
}
}
Expand Down
28 changes: 21 additions & 7 deletions examples/WebRTCExamples/WebRTCMp4Source/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//-----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
Expand All @@ -33,8 +32,8 @@ namespace demo
{
class Program
{
//private const string MP4_PATH = @"media\max_intro.mp4"; // /!\ A valid path to Video file
private const string MP4_PATH = @"media\big_buck_bunny.mp4";
private const string MP4_PATH = @"media\max_intro.mp4"; // /!\ A valid path to Video file
//private const string MP4_PATH = @"media\big_buck_bunny.mp4";

private const int WEBSOCKET_PORT = 8081;
private const string STUN_URL = "stun:stun.sipsorcery.com";
Expand Down Expand Up @@ -72,14 +71,15 @@ private static Task<RTCPeerConnection> CreatePeerConnection()
{
RTCConfiguration config = new RTCConfiguration
{
iceServers = new List<RTCIceServer> { new RTCIceServer { urls = STUN_URL } }
//iceServers = new List<RTCIceServer> { new RTCIceServer { urls = STUN_URL } }
};
var pc = new RTCPeerConnection(config);

SIPSorceryMedia.FFmpeg.FFmpegInit.Initialise(SIPSorceryMedia.FFmpeg.FfmpegLogLevelEnum.AV_LOG_VERBOSE, null, logger);
var mediaFileSource = new SIPSorceryMedia.FFmpeg.FFmpegFileSource(MP4_PATH, false, new AudioEncoder());
var mediaFileSource = new SIPSorceryMedia.FFmpeg.FFmpegFileSource(MP4_PATH, true, new AudioEncoder());
mediaFileSource.RestrictFormats(x => x.Codec == VideoCodecsEnum.VP8);
mediaFileSource.RestrictFormats(x => x.Codec == AudioCodecsEnum.PCMU);

//mediaFileSource.RestrictFormats(x => x.Codec == AudioCodecsEnum.OPUS);
//mediaFileSource..OnEndOfFile += () => pc.Close("source eof");

MediaStreamTrack videoTrack = new MediaStreamTrack(mediaFileSource.GetVideoSourceFormats(), MediaStreamStatusEnum.SendRecv);
Expand All @@ -100,7 +100,7 @@ private static Task<RTCPeerConnection> CreatePeerConnection()
{
pc.Close("ice disconnection");
}
else if (state == RTCPeerConnectionState.closed)
else if (state is RTCPeerConnectionState.closed or RTCPeerConnectionState.disconnected)
{
await mediaFileSource.CloseVideo();
}
Expand All @@ -110,6 +110,20 @@ private static Task<RTCPeerConnection> CreatePeerConnection()
}
};

pc.onsignalingstatechange += () =>
{
logger.LogDebug($"Signalling state change to {pc.signalingState}.");

if (pc.signalingState == RTCSignalingState.have_local_offer)
{
logger.LogDebug($"Local SDP offer:\n{pc.localDescription.sdp}");
}
else if (pc.signalingState == RTCSignalingState.stable)
{
logger.LogDebug($"Remote SDP offer:\n{pc.remoteDescription.sdp}");
}
};

// Diagnostics.
//pc.OnReceiveReport += (re, media, rr) => logger.LogDebug($"RTCP Receive for {media} from {re}\n{rr.GetDebugSummary()}");
//pc.OnSendReport += (media, sr) => logger.LogDebug($"RTCP Send for {media}\n{sr.GetDebugSummary()}");
Expand Down
12 changes: 5 additions & 7 deletions examples/WebRTCExamples/WebRTCMp4Source/WebRTCMp4Source.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8</TargetFramework>
</PropertyGroup>

<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Prefer32Bit>false</Prefer32Bit>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -25,11 +23,11 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="SIPSorcery.WebSocketSharp" Version="0.0.1" />
<PackageReference Include="SIPSorceryMedia.FFmpeg" Version="8.0.7" />
<PackageReference Include="SIPSorceryMedia.FFmpeg" Version="8.0.8" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/net/RTP/AudioStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public async Task SendDtmfEvent(RTPEvent rtpEvent, CancellationToken cancellatio
/// be used to cancel the send.</param>
public virtual Task SendDtmf(byte key, CancellationToken ct)
{
var dtmfEvent = new RTPEvent(key, false, RTPEvent.DEFAULT_VOLUME, RTPSession.DTMF_EVENT_DURATION, RTPSession.DTMF_EVENT_PAYLOAD_ID);
var dtmfEvent = new RTPEvent(key, false, RTPEvent.DEFAULT_VOLUME, RTPSession.DTMF_EVENT_DURATION, NegotiatedRtpEventPayloadID);
return SendDtmfEvent(dtmfEvent, ct);
}

Expand Down
12 changes: 6 additions & 6 deletions src/net/RTP/MediaStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ public bool IsClosed

/// <summary>
/// In order to detect RTP events from the remote party this property needs to
/// be set to the payload ID they are using.
/// be negotiated to a common payload ID. RTP events are typically DTMF tones.
/// </summary>
public int RemoteRtpEventPayloadID { get; set; } = RTPSession.DEFAULT_DTMF_EVENT_PAYLOAD_ID;
public int NegotiatedRtpEventPayloadID { get; set; } = RTPSession.DEFAULT_DTMF_EVENT_PAYLOAD_ID;

/// <summary>
/// To type of this media
Expand Down Expand Up @@ -172,7 +172,7 @@ public MediaStreamTrack LocalTrack
if (MediaType == SDPMediaTypesEnum.audio)
{
if (m_localTrack.Capabilities != null && !m_localTrack.NoDtmfSupport &&
!m_localTrack.Capabilities.Any(x => x.ID == RTPSession.DTMF_EVENT_PAYLOAD_ID))
!m_localTrack.Capabilities.Any(x => x.ID == RTPSession.DEFAULT_DTMF_EVENT_PAYLOAD_ID))
{
m_localTrack.Capabilities.Add(DefaultRTPEventFormat);
}
Expand Down Expand Up @@ -220,7 +220,7 @@ public static SDPAudioVideoMediaFormat DefaultRTPEventFormat
{
return new SDPAudioVideoMediaFormat(
SDPMediaTypesEnum.audio,
RTPSession.DTMF_EVENT_PAYLOAD_ID,
RTPSession.DEFAULT_DTMF_EVENT_PAYLOAD_ID,
SDP.TELEPHONE_EVENT_ATTRIBUTE,
RTPSession.DEFAULT_AUDIO_CLOCK_RATE,
SDPAudioVideoMediaFormat.DEFAULT_AUDIO_CHANNEL_COUNT,
Expand Down Expand Up @@ -647,7 +647,7 @@ public void SendRtcpFeedback(RTCPFeedback feedback)
public void OnReceiveRTPPacket(RTPHeader hdr, int localPort, IPEndPoint remoteEndPoint, byte[] buffer, VideoStream videoStream = null)
{
RTPPacket rtpPacket;
if (RemoteRtpEventPayloadID != 0 && hdr.PayloadType == RemoteRtpEventPayloadID)
if (NegotiatedRtpEventPayloadID != 0 && hdr.PayloadType == NegotiatedRtpEventPayloadID)
{
if (!EnsureBufferUnprotected(buffer, hdr, out rtpPacket))
{
Expand Down Expand Up @@ -919,7 +919,7 @@ public SDPAudioVideoMediaFormat GetSendingFormat()
if (MediaType == SDPMediaTypesEnum.audio)
{
format = SDPAudioVideoMediaFormat.GetCompatibleFormats(RemoteTrack.Capabilities, LocalTrack.Capabilities)
.Where(x => x.ID != RemoteRtpEventPayloadID).FirstOrDefault();
.Where(x => x.ID != NegotiatedRtpEventPayloadID).FirstOrDefault();
}
else
{
Expand Down
5 changes: 2 additions & 3 deletions src/net/RTP/RTPSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public class RTPSession : IMediaSession, IDisposable
public const string RTP_SECUREMEDIA_PROFILE = "RTP/SAVP";
protected const int SDP_SESSIONID_LENGTH = 10; // The length of the pseudo-random string to use for the session ID.
public const int DTMF_EVENT_DURATION = 1200; // Default duration for a DTMF event.
public const int DTMF_EVENT_PAYLOAD_ID = 101;

/// <summary>
/// When there are no RTP packets being sent for an audio or video stream webrtc.lib
Expand Down Expand Up @@ -1080,7 +1079,7 @@ public virtual SetDescriptionResultEnum SetRemoteDescription(SdpType sdpType, SD
SDPAudioVideoMediaFormat commonEventFormat = SDPAudioVideoMediaFormat.GetCommonRtpEventFormat(announcement.MediaFormats.Values.ToList(), currentMediaStream.LocalTrack.Capabilities);
if (!commonEventFormat.IsEmpty())
{
currentMediaStream.RemoteRtpEventPayloadID = commonEventFormat.ID;
currentMediaStream.NegotiatedRtpEventPayloadID = commonEventFormat.ID;
}
}

Expand Down Expand Up @@ -1973,7 +1972,7 @@ public void SendVideo(uint durationRtpUnits, byte[] sample)
}

/// <summary>
/// Sends a DTMF toneas an RTP event to the remote party. (on the primary one)
/// Sends a DTMF tone as an RTP event to the remote party. (on the primary one)
/// </summary>
/// <param name="key">The DTMF tone to send.</param>
/// <param name="ct">RTP events can span multiple RTP packets. This token can
Expand Down
2 changes: 1 addition & 1 deletion src/net/RTP/VideoStream.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void CheckVideoFormatsNegotiation()
public VideoStream(RtpSessionConfig config, int index) : base(config, index)
{
MediaType = SDPMediaTypesEnum.video;
RemoteRtpEventPayloadID = 0;
NegotiatedRtpEventPayloadID = 0;
}
}
}

0 comments on commit dedc327

Please sign in to comment.