Commit f31e00fc authored by Kateryna Kostiuk's avatar Kateryna Kostiuk

video cleanup: do not use loop for video input

Change-Id: I5cce6cdc5b1310cdf14e7c34049585c0f60b0285
parent 62fbb7ab
......@@ -188,15 +188,51 @@ extern "C" {
return frame;
}
+ (int)getForrmatFromAppleFormat:(OSType)format {
switch (format) {
case kCVPixelFormatType_420YpCbCr8Planar:
return AV_PIX_FMT_YUV420P;
case kCVPixelFormatType_422YpCbCr8_yuvs:
return AV_PIX_FMT_YUYV422;
case kCVPixelFormatType_422YpCbCr8:
return AV_PIX_FMT_UYVY422;
case kCVPixelFormatType_32BGRA:
return AV_PIX_FMT_BGR0;
case kCVPixelFormatType_24RGB:
return AV_PIX_FMT_RGB24;
case kCVPixelFormatType_24BGR:
return AV_PIX_FMT_BGR24;
case kCVPixelFormatType_32ARGB:
return AV_PIX_FMT_0RGB;
case kCVPixelFormatType_32ABGR:
return AV_PIX_FMT_0BGR;
case kCVPixelFormatType_32RGBA:
return AV_PIX_FMT_RGB0;
case kCVPixelFormatType_48RGB:
return AV_PIX_FMT_BGR48BE;
case kCVPixelFormatType_444YpCbCr8:
return AV_PIX_FMT_YUV444P;
case kCVPixelFormatType_4444AYpCbCr16:
return AV_PIX_FMT_YUVA444P16LE;
case kCVPixelFormatType_4444YpCbCrA8R:
return AV_PIX_FMT_YUVA444P;
case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
case kCVPixelFormatType_420YpCbCr8BiPlanarFullRange:
default:
return AV_PIX_FMT_NV12;
}
}
+ (AVFrame*)configureFrame:(AVFrame*)frame
fromImageBuffer: (CVImageBufferRef)image
angle:(int) angle {
CVPixelBufferLockBaseAddress(image, 0);
int width = static_cast<int>(CVPixelBufferGetWidth(image));
int height = static_cast<int>(CVPixelBufferGetHeight(image));
const OSType pixelFormat = CVPixelBufferGetPixelFormatType(image);
frame->width = width;
frame->height = height;
frame->format = AV_PIX_FMT_NV12;
frame->format = [Utils getForrmatFromAppleFormat: pixelFormat];
if (CVPixelBufferIsPlanar(image)) {
int planes = static_cast<int>(CVPixelBufferGetPlaneCount(image));
for (int i = 0; i < planes; i++) {
......
......@@ -35,7 +35,6 @@
- (void)writeOutgoingFrameWithBuffer:(CVImageBufferRef)image
angle:(int)angle
useHardwareAcceleration:(BOOL)hardwareAccelerated;
- (void)writeOutgoingFrameWithImage:(UIImage*)image;
- (void)setDecodingAccelerated:(BOOL)state;
- (BOOL)getDecodingAccelerated;
- (void)switchInput:(NSString*)deviceName;
......
......@@ -204,28 +204,6 @@ static id <VideoAdapterDelegate> _delegate;
DRing::publishFrame();
}
- (void)writeOutgoingFrameWithImage:(UIImage *)image {
unsigned capacity = 4 * image.size.width * image.size.height;
uint8_t* buf = (uint8_t*)DRing::obtainFrame(capacity);
if (buf){
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGImageRef imageRef = [image CGImage];
CGContextRef bitmap = CGBitmapContextCreate(buf,
image.size.width,
image.size.height,
8,
image.size.width * 4,
colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
CGContextTranslateCTM(bitmap, image.size.width, 0);
CGContextScaleCTM(bitmap, -1.0, 1.0);
CGContextDrawImage(bitmap, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);
CGContextRelease(bitmap);
CGColorSpaceRelease(colorSpace);
}
DRing::releaseFrame((void*)buf);
}
- (void)addVideoDeviceWithName:(NSString*)deviceName withDevInfo:(NSDictionary*)deviceInfoDict {
std::vector<std::map<std::string, std::string>> devInfo;
auto setting = [Utils dictionnaryToMap:deviceInfoDict];
......
......@@ -43,17 +43,12 @@ enum VideoError: Error {
protocol FrameExtractorDelegate: class {
func captured(imageBuffer: CVImageBuffer?, image: UIImage)
func supportAVPixelFormat(support: Bool)
//func useHardwareAcceleration()-> Bool
//func updateVideoInputDevices()
}
class FrameExtractor: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
let nameLandscape = "frontCameraLanscape"
let namePortrait = "frontCameraPortrait"
let nameDevice1280_720 = "frontCameraPortrait1280_720"
let nameCamera = "camera://"
private let log = SwiftyBeaver.self
......@@ -84,7 +79,7 @@ class FrameExtractor: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
super.init()
}
func getDeviceInfo(forPosition position: AVCaptureDevice.Position, orientation: UIDeviceOrientation, quality: AVCaptureSession.Preset) throws -> DeviceInfo {
func getDeviceInfo(forPosition position: AVCaptureDevice.Position, quality: AVCaptureSession.Preset) throws -> DeviceInfo {
guard self.permissionGranted.value else {
throw VideoError.needPermission
}
......@@ -102,20 +97,11 @@ class FrameExtractor: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
bestRate = frameRates.maxFrameRate
}
}
if orientation == .portrait ||
orientation == .portraitUpsideDown {
let devInfo: DeviceInfo = ["format": "BGRA",
"width": String(dimensions.height),
"height": String(dimensions.width),
"rate": String(bestRate)]
return devInfo
} else {
let devInfo: DeviceInfo = ["format": "BGRA",
"width": String(dimensions.width),
"height": String(dimensions.height),
"rate": String(bestRate)]
return devInfo
}
let devInfo: DeviceInfo = ["format": "BGRA",
"width": String(dimensions.height),
"height": String(dimensions.width),
"rate": String(bestRate)]
return devInfo
}
func startCapturing() {
......@@ -179,7 +165,6 @@ class FrameExtractor: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
if types.contains(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange) {
let settings = [kCVPixelBufferPixelFormatTypeKey as NSString: kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange]
videoOutput.videoSettings = settings as [String: Any]
self.delegate?.supportAVPixelFormat(support: true)
}
videoOutput.setSampleBufferDelegate(self, queue: sessionQueue)
guard captureSession.canAddOutput(videoOutput) else {
......@@ -316,7 +301,6 @@ class VideoService: FrameExtractorDelegate {
private let log = SwiftyBeaver.self
private var hardwareAccelerated = true
var angle: Int = 0
var supportAVPixelFormat = false
fileprivate let disposeBag = DisposeBag()
......@@ -344,25 +328,25 @@ class VideoService: FrameExtractorDelegate {
do {
try camera.configureSession(withPosition: AVCaptureDevice.Position.front, withOrientation: AVCaptureVideoOrientation.portrait)
self.log.debug("Camera successfully configured")
let hd1280x720Device: [String: String] = try camera.getDeviceInfo(forPosition: AVCaptureDevice.Position.front, orientation: .portrait, quality: AVCaptureSession.Preset.hd1280x720)
let frontLandscapeCameraDevInfo: [String: String] =
try camera
.getDeviceInfo(forPosition: AVCaptureDevice.Position.front, orientation: .landscapeLeft,
let hd1280x720Device: [String: String] = try camera
.getDeviceInfo(forPosition: AVCaptureDevice.Position.front,
quality: AVCaptureSession.Preset.hd1280x720)
let frontPortraitCameraDevInfo: [String: String] = try camera
.getDeviceInfo(forPosition: AVCaptureDevice.Position.front,
quality: AVCaptureSession.Preset.medium)
let frontPortraitCameraDevInfo: [String: String] =
try camera.getDeviceInfo(forPosition: AVCaptureDevice.Position.front, orientation: .portrait,
quality: AVCaptureSession.Preset.medium)
videoAdapter.addVideoDevice(withName: camera.nameLandscape,
withDevInfo: frontLandscapeCameraDevInfo)
if self.hardwareAccelerated {
self.camera.setQuality(quality: AVCaptureSession.Preset.hd1280x720)
videoAdapter.addVideoDevice(withName: camera.namePortrait, withDevInfo: frontPortraitCameraDevInfo)
videoAdapter.addVideoDevice(withName: camera.nameDevice1280_720, withDevInfo: hd1280x720Device)
videoAdapter.addVideoDevice(withName: camera.namePortrait,
withDevInfo: frontPortraitCameraDevInfo)
videoAdapter.addVideoDevice(withName: camera.nameDevice1280_720,
withDevInfo: hd1280x720Device)
return
}
self.camera.setQuality(quality: AVCaptureSession.Preset.medium)
videoAdapter.addVideoDevice(withName: camera.nameDevice1280_720, withDevInfo: hd1280x720Device)
videoAdapter.addVideoDevice(withName: camera.namePortrait, withDevInfo: frontPortraitCameraDevInfo)
videoAdapter.addVideoDevice(withName: camera.nameDevice1280_720,
withDevInfo: hd1280x720Device)
videoAdapter.addVideoDevice(withName: camera.namePortrait,
withDevInfo: frontPortraitCameraDevInfo)
} catch let e as VideoError {
self.log.error("Error during capture device enumeration: \(e)")
......@@ -374,7 +358,7 @@ class VideoService: FrameExtractorDelegate {
func switchCamera() {
self.camera.switchCamera()
.subscribe(onCompleted: {
print ("camera switched")
print("camera switched")
}, onError: { error in
print(error)
}).disposed(by: self.disposeBag)
......@@ -400,20 +384,6 @@ class VideoService: FrameExtractorDelegate {
}
self.angle = self.mapDeviceOrientation(orientation: newOrientation)
self.currentOrientation = newOrientation
// in this case rotation will be performed when configure AVFrame
if hardwareAccelerated || supportAVPixelFormat {
return
}
let deviceName: String =
(orientation == .landscapeLeft || orientation == .landscapeRight) ?
self.camera.nameLandscape : self.camera.namePortrait
self.switchInput(toDevice: self.camera.nameCamera + deviceName, callID: callID)
self.camera.rotateCamera(orientation: newOrientation)
.subscribe(onCompleted: { [unowned self] in
self.log.debug("new camera orientation isPortrait: \(orientation.isPortrait)")
}, onError: { error in
self.log.debug("camera re-orientation error: \(error)")
}).disposed(by: self.disposeBag)
}
func mapDeviceOrientation(orientation: AVCaptureVideoOrientation) -> Int {
......@@ -505,24 +475,15 @@ extension VideoService: VideoAdapterDelegate {
}
func captured(imageBuffer: CVImageBuffer?, image: UIImage) {
if self.hardwareAccelerated || self.supportAVPixelFormat {
if let cgImage = image.cgImage {
self.capturedVideoFrame
.onNext(UIImage(cgImage: cgImage,
scale: 1.0 ,
orientation: self.getImageOrienation()))
}
videoAdapter.writeOutgoingFrame(with: imageBuffer,
angle: Int32(self.angle),
useHardwareAcceleration: self.hardwareAccelerated)
} else {
self.capturedVideoFrame.onNext(image)
videoAdapter.writeOutgoingFrame(with: image)
if let cgImage = image.cgImage {
self.capturedVideoFrame
.onNext(UIImage(cgImage: cgImage,
scale: 1.0 ,
orientation: self.getImageOrienation()))
}
}
func supportAVPixelFormat(support: Bool) {
supportAVPixelFormat = support
videoAdapter.writeOutgoingFrame(with: imageBuffer,
angle: Int32(self.angle),
useHardwareAcceleration: self.hardwareAccelerated)
}
func stopAudioDevice() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment