     }
   }
   
-  func render(texture:CVMetalTexture) {
+  func render(metalTexture:CVMetalTexture) {
     guard let drawable = metalLayer.nextDrawable(),
       let pipelineState = self.pipelineState,
-      let commandBuffer = commandQueue?.makeCommandBuffer() else {
+      let commandBuffer = commandQueue?.makeCommandBuffer(),
+      let texture = CVMetalTextureGetTexture(metalTexture) else {
+        print("something is wrong")
         return
     }
+    
     let renderPassDescriptor = MTLRenderPassDescriptor()
     renderPassDescriptor.colorAttachments[0].texture = drawable.texture
     renderPassDescriptor.colorAttachments[0].loadAction = .clear

        let status = CVMetalTextureCacheCreateTextureFromImage(nil, textureCache, pixelBuffer, nil, MTLPixelFormat.r8Unorm, width, height, 0, &texture)
        if let texture = texture, status == kCVReturnSuccess {
          //print("capture", width, height, texture)
          render(texture:texture)
        } else {
          print("capture failed")
        }

  }
}

// https://github.com/McZonk/MetalCameraSample

extension VideoSessionController : AVCaptureAudioDataOutputSampleBufferDelegate,
                                   AVCaptureVideoDataOutputSampleBufferDelegate {
  public func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
