SIPSorceryMedia.FFmpeg icon indicating copy to clipboard operation
SIPSorceryMedia.FFmpeg copied to clipboard

How to change width and height & fps form Camera?

Open Riton2013 opened this issue 6 months ago • 0 comments

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 OnVideoSourceRawSampleFaster callback to display on pictureBox, it also has high delay. I guest the resolution is too high and FFMPEG encoding 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);
    }

Riton2013 avatar Aug 09 '24 02:08 Riton2013