sipsorcery-org / sipsorcery

A WebRTC, SIP and VoIP library for C# and .NET. Designed for real-time communications apps.
https://sipsorcery-org.github.io/sipsorcery
Other
1.42k stars 431 forks source link

get error if udp package is longer than 1472 #1045

Open yangbocheng opened 8 months ago

yangbocheng commented 8 months ago

While I try to use webrtc and connect to zlmediakit, i got some exception. I wish to get some suggestion

there is some udp package which length is 2144, so i have changed the UdpReceiver :protected const int RECEIVE_BUFFER_SIZE = 1024 * 16 , Then int bytesRead = m_socket.EndReceiveFrom(ar, ref remoteEP); can work well. image

But i can't solve the next exception: How can i solve it. image it try to copy this array(len=2144) to a buffer(len=1472)

================ here is my code

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Serilog.Extensions.Logging;
using Serilog;
using SIPSorcery.Media;
using SIPSorcery.Net;
using SIPSorceryMedia.Abstractions;
using WebSocketSharp.Server;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Mime;
using Org.BouncyCastle.Asn1.X509;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text.Json;

namespace webrtc1;

internal class pushtest
{
    public async Task test()
    {
        const string ffmpegLibFullPath = @"D:\d\softnormal\ffmpeg\ffmpeg-master-latest-win64-gpl-shared\bin";  //  /!\ A valid path to FFmpeg library
        const string MP4_PATH = @"D:\e\testmedia\trans\t1.mkv";                          //  /!\ A valid path to Video file

        Microsoft.Extensions.Logging.ILogger logger = NullLogger.Instance;

        logger = AddConsoleLogger();

        // See https://aka.ms/new-console-template for more information
        Console.WriteLine("WebRTC MP4 Source Demo");

        // Start web socket.
        await InitConnection();

        // Ctrl-c will gracefully exit the call at any point.
        ManualResetEvent exitMre = new ManualResetEvent(false);
        Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e)
        {
            e.Cancel = true;
            exitMre.Set();

        };

        // Wait for a signal saying the call failed, was cancelled with ctrl-c or completed.
        exitMre.WaitOne();

        async Task InitConnection()
        {
            SIPSorceryMedia.FFmpeg.FFmpegInit.Initialise(SIPSorceryMedia.FFmpeg.FfmpegLogLevelEnum.AV_LOG_VERBOSE, ffmpegLibFullPath, logger);
            var mediaFileSource = new SIPSorceryMedia.FFmpeg.FFmpegFileSource(MP4_PATH, false, new AudioEncoder());
            mediaFileSource.RestrictFormats(x => x.Codec == VideoCodecsEnum.VP8);
            //mediaFileSource.RestrictFormats(x => x.Codec == AudioCodecsEnum.PCMU);
            mediaFileSource.RestrictFormats(x => x.Codec == AudioCodecsEnum.PCMU);

            RTCPeerConnection pc1 = new RTCPeerConnection(new RTCConfiguration()
            {
                X_UseRtpFeedbackProfile = true
            });
            //设置数据源
            MediaStreamTrack videoTrack = new MediaStreamTrack(mediaFileSource.GetVideoSourceFormats(), MediaStreamStatusEnum.SendOnly);
            pc1.addTrack(videoTrack);
            MediaStreamTrack audioTrack = new MediaStreamTrack(mediaFileSource.GetAudioSourceFormats(), MediaStreamStatusEnum.SendOnly);
            pc1.addTrack(audioTrack);

            //mediaFileSource.OnVideoSourceEncodedSample += pc1.SendVideo;
            //mediaFileSource.OnAudioSourceEncodedSample += pc1.SendAudio;

            var offer = pc1.createOffer();
            await pc1.setLocalDescription(offer);
            //发送给远端
            string target = "https://10.219.25.29/index/api/webrtc?app=live&stream=test1&type=push";

            var httphandler = new HttpClientHandler();
            httphandler.ServerCertificateCustomValidationCallback =
                (HttpRequestMessage a, X509Certificate2? b, X509Chain? c, SslPolicyErrors d) => true;
            HttpClient client = new HttpClient(httphandler);

            StringContent content = new StringContent(offer.sdp);
            content.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Text.Plain);
            var re1 = await client.PostAsync(new Uri(target), content);
            var send_result = JsonSerializer.Deserialize<SendOfferResult>(await re1.Content.ReadAsStringAsync());

            Console.WriteLine($"收到 {JsonSerializer.Serialize(send_result)}");
            pc1.setRemoteDescription(new RTCSessionDescriptionInit
            {
                type = RTCSdpType.answer,
                sdp = send_result.sdp
            });

            Console.WriteLine("set remote成功");

        }

    }

    Microsoft.Extensions.Logging.ILogger AddConsoleLogger()
    {
        var seriLogger = new LoggerConfiguration()
            .Enrich.FromLogContext()
            .MinimumLevel.Is(Serilog.Events.LogEventLevel.Debug)
            .WriteTo.Console()
            .CreateLogger();
        var factory = new SerilogLoggerFactory(seriLogger);
        SIPSorcery.LogFactory.Set(factory);
        return factory.CreateLogger<Program>();
    }
}

public class SendOfferResult
{
    public int code { get; set; }
    public string id { get; set; }
    public string sdp { get; set; }
    public string type { get; set; }
}
sipsorcery commented 7 months ago

It does seem like a bug but so far I haven't been able to track down the cause of this.