Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVCaptureSession rotate | orientation while video transmitting

I am developing video streaming application, in which i need to capture front camera video frame and encode then transfer to other end, a typical flow is like this

AVCaptureSession -> AVCaptureDeviceInput -> AVCaptureVideoDataOutput -> capture frame --> encode frame --> send frame to other end,

it works fine, i have setup the kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange as a frame format.

also preview layer being used to show the preview,

the problem comes when device orientation gets changes, if device moved from portrait to landscape, then on the other end frames gets rotate by 90, i was expecting since orientation being supported in the preview layer so i will automatically received rotated buffer in the Capture callback, but it looks like, preview layer just show me the preview of the buffer captured and UI orates the buffer, while on the other end i would get the roared buffer,

So i want to know, Are there any configuration to make it change , or do i need to rotate/transform buffer in the Capture buffer callback.

like image 455
Amitg2k12 Avatar asked Feb 12 '23 20:02

Amitg2k12


1 Answers

Thanks for looking into it, basically the solution is, orientation of connection should be set, and i was playing with the preview layer, so its affecting the preview layer but not the orientation.

here goes the code snippet

-(void) orientationChanged
{
    // get the new orientation from device 
    AVCaptureVideoOrientation newOrientation = [self videoOrientationFromDeviceOrientation];

    // set the orientation of preview layer :( which will be displayed in the device )
    [previewLayer.connection setVideoOrientation:newOrientation];

    // set the orientation of the connection: which will take care of capture
    [pCaptureConnection setVideoOrientation:newOrientation];

}
like image 142
Amitg2k12 Avatar answered Feb 16 '23 02:02

Amitg2k12