简体   繁体   中英

WebRTC VideoCall Rendering in SwiftUI

i am using webRTC for video call between two iphone devices. The webrtc peer connection is established successfully. The video stream should show in swiftui. The webRTC object RTCEAGLVideoView that holds the video need to render in swiftui. In the model class i declared this object as

var remoteVideoView : RTCEAGLVideoView?
    {
        willSet {
            objectWillChange.send()
        }
    }

in the SwiftUI class, the remoteVideoView should render.

VStack()
{
//show remoteVideoView  here
}

what kind of object should use to render this videoview.

Code in short enter image description here

First you need to create RTCMediaStream which contains audio tracks and video tracks:

@property RTCMediaStream * _Nullable mediaStream;

The streams which you are getting from your media server with streamId convert those into RTCMediaStream and then use the below code to render it in RTCEAGLVideoView

 if (mediaStream.videoTracks.count > 0) {
     RTCVideoTrack *videoTrack = [self.mediaStream.videoTracks objectAtIndex:0];
     [videoTrack addRenderer:remoteView];
   }

Also Create a IBOutlet of RTCEAGLVideoView like below:

@property (weak, nonatomic) IBOutlet RTCEAGLVideoView *remoteView;

Now Add a view in your view controller and assign class as RTCEAGLVideoView to that view. Next connect the videoView outlet.

In case you are not using storyboard use below code:

 RTCEAGLVideoView *remoteView = [[RTCEAGLVideoView alloc] initWithFrame:self.frame]; // pass CGRect frame here.
 remoteView.delegate = self;
 [yourView addSubview:remoteView];

Now you can see your video.

Add RTCEAGLVideoView in VStack() use below:

struct RemoteView: UIViewRepresentable {
    func remoteView(context: Context) -> RTCEAGLVideoView {
        //create frame for RTCEAGLVideoView here
    }
}


struct RemoteView_Preview: PreviewProvider {
    static var previews: some View {
        RemoteView(frame: .zero)
    }
}

In VStack use like:

 VStack {
          RemoteView(frame)
      VStack{
           Text("")
           }
       }
struct RemoteView : UIViewRepresentable {
    @Binding var video: VideoCall
    @Binding var remoteView: RTCEAGLVideoView
    func updateUIView(_ uiView: RTCEAGLVideoView, context: UIViewRepresentableContext<RemoteView>) {
   }

    func makeUIView(context: Context) -> RTCEAGLVideoView  {
        self.remoteView.frame = CGRect(x: 20, y: 20, width: 200, height: 300)
        self.remoteView = self.video.remoteVideoView!
        return self.remoteView
    }

} 

I called this from another swiftui struct as

VStack()
{ 
      RemoteView() 
}

but i am getting compiler error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM