Пример #1
0
        public async Task CaptureMedia(long fromUid, bool r = false)
        {
            LocalMedia = Media.CreateMedia();                                                  //创建一个Media对象

            RTCMediaStreamConstraints mediaStreamConstraints = new RTCMediaStreamConstraints() //设置要获取的流
            {
                audioEnabled = true,
                videoEnabled = true
            };

            //音频播放
            var apd = LocalMedia.GetAudioPlayoutDevices();

            if (apd.Count > 0)
            {
                LocalMedia.SelectAudioPlayoutDevice(apd[0]);
            }



            if (fromUid == 0)
            {
                //音频捕获
                var acd = LocalMedia.GetAudioCaptureDevices();
                if (acd.Count > 0)
                {
                    mediaStreamConstraints.audioEnabled = true;
                    LocalMedia.SelectAudioCaptureDevice(acd[0]);
                }
                //视频捕获
                var vcd = LocalMedia.GetVideoCaptureDevices();
                if (vcd.Count > 0)
                {
                    mediaStreamConstraints.videoEnabled = true;
                    LocalMedia.SelectVideoDevice(vcd.First(p => p.Location.Panel == Windows.Devices.Enumeration.Panel.Front));//设置视频捕获设备
                }
            }



            var mediaStream = await LocalMedia.GetUserMedia(mediaStreamConstraints);//获取视频流 这里视频和音频是一起传输的

            if (fromUid == 0)
            {
                var videotracs = mediaStream.GetVideoTracks();
                if (videotracs.Count > 0)
                {
                    var source = LocalMedia.CreateMediaSource(videotracs.FirstOrDefault(), mediaStream.Id); //创建播放源
                    LocalMediaPlayer.SetMediaStreamSource(source);                                          //设置MediaElement的播放源
                    LocalMediaPlayer.Play();
                }
                await CreatePublisher(mediaStream);

                //await CreateServer(mediaStream);
            }
            else
            {
                await CreateReceiver(mediaStream, fromUid);
            }
        }
Пример #2
0
        private void Conn_OnAddStream(MediaStreamEvent __param0)
        {
            var stream = __param0.Stream;

            var videotracks = stream.GetVideoTracks();
            //var media = Media.CreateMedia();

            //var apd = media.GetAudioPlayoutDevices();
            //if (apd.Count > 0)
            //{
            //    media.SelectAudioPlayoutDevice(apd[0]);
            //}

            var source = LocalMedia.CreateMediaSource(videotracks.FirstOrDefault(), stream.Id);

            RemoteMediaPlayer.SetMediaStreamSource(source);

            RemoteMediaPlayer.Play();
        }
Пример #3
0
        public async Task CreateReceiver(MediaStream mediaStream, long fromUid)
        {
            var conn = new RTCPeerConnection(RtcConfig);

            CurrentRoom.Recvs.Add(fromUid, conn);
            CurrentRoom.Recvs[fromUid].AddStream(mediaStream);
            CurrentRoom.Recvs[fromUid].OnIceCandidate += async(p) =>
            {
                var Candidate = p.Candidate;
                var m         = new SendCadidatModel();
                m.candidate = new CandidateModel
                {
                    candidate     = Candidate.Candidate,
                    sdpMlineindex = Candidate.SdpMLineIndex,
                    sdpMid        = Candidate.SdpMid,
                };
                m.uid     = Uid;
                m.fromUid = fromUid;
                Candidates.Add(m);
                await SendCandidate(m);
            };
            CurrentRoom.Recvs[fromUid].OnAddStream += (p) =>
            {
                var stream = p.Stream;

                var videotracks = stream.GetVideoTracks();


                var source = LocalMedia.CreateMediaSource(videotracks.FirstOrDefault(), stream.Id);

                RemoteMediaPlayer.SetMediaStreamSource(source);

                RemoteMediaPlayer.Play();
            };

            await CreatOffer(Uid, fromUid);
        }