sipsorcery-org / SIPSorceryMedia.FFmpeg

GNU Lesser General Public License v2.1
34 stars 25 forks source link

How to change width and height & fps form Camera? #79

Open Riton2013 opened 1 month ago

Riton2013 commented 1 month ago

Hi, everyone I am currently using SIPSorceryMedia FFmpeg pushed the stream from the camera (2K) to the streaming server and found that the stream delay was very high, not just on the Server side. I also used the OnVideoSourceRawSampleFastercallback to display on pictureBox, it also has high delay. I guest the resolution is too high and FFMPEGencoding takes too long. I want to lower the resolution of the image and increase FPS. What should I do? Thanks

My code below:

Init

       SIPSorceryMedia.FFmpeg.FFmpegInit.Initialise(FfmpegLogLevelEnum.AV_LOG_FATAL, LIB_PATH);
        var camera = FFmpegCameraManager.GetCameraDevices();
        videoSource = new FFmpegCameraSource(camera.FirstOrDefault().Path);
        videoSource.OnVideoSourceError += VideoSource_OnVideoSourceError;

        string MicrophoneDevicePath = "audio= (Realtek High Definition Audio)";
        audioSource = new SIPSorceryMedia.FFmpeg.FFmpegMicrophoneSource(MicrophoneDevicePath, new AudioEncoder());

        localPC.onicecandidate += LocalPC_onicecandidate1;`

CreatePeerConnection

        public void CreateLocalPeerConnection()
     {
        if (videoSource != null)
        {
            videoSource.RestrictFormats(x => x.Codec == VideoCodec);

            MediaStreamTrack videoTrack = new MediaStreamTrack(videoSource.GetVideoSourceFormats(), MediaStreamStatusEnum.SendOnly);
            localPC.addTrack(videoTrack);

            videoSource.OnVideoSourceRawSampleFaster += VideoSource_OnVideoSourceRawSampleFaster;
            videoSource.OnVideoSourceEncodedSample += localPC.SendVideo;
            localPC.OnVideoFormatsNegotiated += (videoFormats) => videoSource.SetVideoSourceFormat(videoFormats.First());
            localPC.OnVideoFormatsNegotiated += LocalPC_OnVideoFormatsNegotiated;

        }

        if (audioSource != null)
        {
            audioSource.RestrictFormats(x => x.Codec == AudioCodec);

            MediaStreamTrack audioTrack = new MediaStreamTrack(audioSource.GetAudioSourceFormats(), MediaStreamStatusEnum.SendOnly);
            localPC.addTrack(audioTrack);

            audioSource.OnAudioSourceError += AudioSource_OnAudioSourceError;

            audioSource.OnAudioSourceEncodedSample += AudioSource_OnAudioSourceEncodedSample;
            localPC.OnAudioFormatsNegotiated += LocalPC_OnAudioFormatsNegotiated;
        }

        //Exchange SDP with streaming media
        RTCSessionDescriptionInit localSDP = localPC.createOffer(null);
        PushVideo(localSDP.sdp);
    }

         public bool PushVideo(string sdp)
    {
        try
        {
            var content = new StringContent(sdp, Encoding.UTF8, "application/json");
            SdpModel result = Task.Run(async () => await PostAsync(pushUrl, content)).Result;

            localPC.SetRemoteDescription(SdpType.answer, SDP.ParseSDPDescription(result.sdp));
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

CallBack

    private void VideoSource_OnVideoSourceRawSampleFaster(uint durationMilliseconds, RawImage rawImage)
    {
        localPictureBox.Invoke(new Action(() =>
        {
            Bitmap bmpImage = new Bitmap(rawImage.Width, rawImage.Height, rawImage.Stride, PixelFormat.Format24bppRgb, rawImage.Sample);
            localPictureBox.Image = bmpImage;
        }));
    }

    private void LocalPC_OnAudioFormatsNegotiated(List<AudioFormat> obj)
    {
        audioSource.SetAudioSourceFormat(obj.First());
        audioSource.StartAudio();
    }

    private void LocalPC_OnVideoFormatsNegotiated(List<VideoFormat> obj)
    {
        videoSource.SetVideoSourceFormat(obj.First());
        videoSource.StartVideo();
    }

    private void AudioSource_OnAudioSourceEncodedSample(uint durationRtpUnits, byte[] sample)
    {
        localPC.SendAudio(durationRtpUnits, sample);
    }
hossam14102010 commented 2 days ago

you can set the preset to ultrafast in the decoder options, also you can use SkiaSharp's SKGLView instead of PictureBox, but for this you will also need to change the pixel format that the FFmpegVideoEndPoint converts to after decoding from Bgr24 to Bgra32