sipsorcery icon indicating copy to clipboard operation
sipsorcery copied to clipboard

Multiple external video stream in one session

Open chauhan19911 opened this issue 5 years ago • 6 comments

Is it possible to set multiple external video tracker in same session: My requirement is to send all the configured camera stream to mobile. My code to add single external video stream from EmguCV which is working fine:

                    var videoEncoderEndPoint = new VideoEncoderEndPoint();

                    MediaStreamTrack videoTrack = new MediaStreamTrack(videoEncoderEndPoint.GetVideoSourceFormats(), MediaStreamStatusEnum.SendOnly);
                    session.PeerConnection.addTrack(videoTrack);

                    var testPatternSource = new VideoTestPatternSource();
                    testPatternSource.SetFrameRate(30);
                    testPatternSource.OnVideoSourceRawSample += (durationMilliseconds, width, height, sample, pixelFormat) =>
                    {
                        //External video stream from EmguCV
                        DeviceFrameHandler.Invoke(_frames.First().Key, out byte[] frame, out int _width, out int _height);
                        if (frame != null)
                        {
                            videoEncoderEndPoint.ExternalVideoSourceRawSample(durationMilliseconds, _width, _height, frame, VideoPixelFormatsEnum.Bgr);
                        }
                    };

                    videoEncoderEndPoint.OnVideoSourceEncodedSample += session.PeerConnection.SendVideo;

                    session.PeerConnection.OnVideoFormatsNegotiated += (formats) => videoEncoderEndPoint.SetVideoSourceFormat(formats.First());

                    session.PeerConnection.onconnectionstatechange += async (state) =>
                    {
                        Console.WriteLine($"Peer connection state change to {state}.");

                        if (state == RTCPeerConnectionState.connected)
                        {
                            await testPatternSource.StartVideo();
                        }
                        else if (state == RTCPeerConnectionState.failed || state == RTCPeerConnectionState.disconnected || state == RTCPeerConnectionState.closed)
                        {
                            session.PeerConnection.Close($"ice {state}");
                            await testPatternSource.CloseVideo();
                        }
                    };

But when I try to add another video tracker, I'm getting this exception:

session.PeerConnection.addTrack(videoTrack);

//A local video track has already been set on this session.
//at SIPSorcery.Net.RTPSession.AddLocalTrack(MediaStreamTrack track)
//at SIPSorcery.Net.RTPSession.addTrack(MediaStreamTrack track)

chauhan19911 avatar Feb 13 '21 11:02 chauhan19911

No sadly multiple audio or video streams are not yet supported. It's something I think about regularly but then something else comes up.

Thanks for opening the issue though. It provides some extra motivation.

sipsorcery avatar Feb 13 '21 11:02 sipsorcery

Any plans on implementing this soon?

timothyghanim avatar Mar 14 '22 15:03 timothyghanim

Any plans on implementing this soon?

It's not something I'll be able to put any time into in the near future. So unless someone else picks it up, no, probably won't be implemented any time soon.

sipsorcery avatar Mar 16 '22 08:03 sipsorcery

FYI,

I made a specific branch (Secondary_Video_Stream) in my own repository to support a secondary video stream (input and/or output) - more as proof of concept than a real update.

My goal was to understand how SIPSorcery should be updated to have this kind of feature but the way I did it it's not really accurate.

I'm currently working on a way to do it correctly. The goal being that it supports as many video streams as desired.

Then I'll work to have the same for audio streams.

ChristopheI avatar Mar 30 '22 12:03 ChristopheI

Thank you so very much for starting this effort. Please keep us posted on when you think SIPSorcery can safely support multiple audio/video tracks in the same RTCPeerConnection.

timothyghanim avatar Mar 30 '22 12:03 timothyghanim

Now in master branch and since release v6.0.8 it's possible to have on the same PeerConnection several Audio stream and/or several Video stream.

To manage them you have access to this new objects / events:

  • PeerConnection.AudioStreamList
  • PeerConnection.VideoStreamList
  • PeerConnection.OnAudioFormatsNegotiatedByIndex
  • PeerConnection.OnVideoFormatsNegotiatedByIndex
  • PeerConnection.OnVideoFrameReceivedByIndex
  • PeerConnection.OnRtpPacketReceivedByIndex
  • PeerConnection.OnRtpEventByIndex

ChristopheI avatar Jun 15 '22 09:06 ChristopheI