title: Using WebRTC to send an iOS devices’ screen capture using ReplayKit
body: <p>We would like to use WebRTC to send an iOS devices’ screen capture using ReplayKit.
  The ReplayKit has a processSampleBuffer callback which gives CMSampleBuffer.</p>
  
  <p>But here is where we are stuck, we can’t seem to get the CMSampleBuffer to be sent to the connected peer.
  We have tried to create pixelBuffer from the sampleBuffer, and then create RTCVideoFrame.</p>
  
  <p>we also extracted the RTCVideoSource from RTCPeerConnectionFactory and then used an RTCVideoCapturer and stream it to the localVideoSource.</p>
  
  <p>Any idea what we are doing wrong?</p>
  
  <pre><code>var peerConnectionFactory: RTCPeerConnectionFactory?
  
  override func processSampleBuffer(_ sampleBuffer: CMSampleBuffer, with sampleBufferType: RPSampleBufferType) {
   switch sampleBufferType {
             case RPSampleBufferType.video:
  
          // create the CVPixelBuffer
          let pixelBuffer:CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)!;
  
          // create the RTCVideoFrame
          var videoFrame:RTCVideoFrame?;
          let timestamp = NSDate().timeIntervalSince1970 * 1000
          videoFrame = RTCVideoFrame(pixelBuffer: pixelBuffer, rotation: RTCVideoRotation._0, timeStampNs: Int64(timestamp))
  
          // connect the video frames to the WebRTC
          let localVideoSource = self.peerConnectionFactory!.videoSource()
          let videoCapturer = RTCVideoCapturer()
          localVideoSource.capturer(videoCapturer, didCapture: videoFrame!)
  
          let videoTrack : RTCVideoTrack =   self.peerConnectionFactory!.videoTrack(with: localVideoSource, trackId: "100”)
  
          let mediaStream: RTCMediaStream = (self.peerConnectionFactory?.mediaStream(withStreamId: “1"))!
          mediaStream.addVideoTrack(videoTrack)
          self.newPeerConnection!.add(mediaStream)
  
          break
      }
  }
  </code></pre>