Skip to content
This repository
tree: faa7f8498b
Fetching contributors…

Cannot retrieve contributors at this time

file 70 lines (57 sloc) 2.69 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
//
// ZappVideoController.m
// Zapp
//
// Created by Jim Puls on 8/24/11.
// Licensed to Square, Inc. under one or more contributor license agreements.
// See the LICENSE file distributed with this work for the terms under
// which Square, Inc. licenses this file to you.

#import "ZappVideoController.h"
#import <AVFoundation/AVFoundation.h>

@interface ZappVideoController () <AVCaptureFileOutputRecordingDelegate>

@property (strong) AVCaptureSession *captureSession;

@end

@implementation ZappVideoController

@synthesize outputURL;
@synthesize captureSession;

- (void)start;
{
    if (!self.outputURL) {
        return;
    }
    
    CGDirectDisplayID displayID = 0;
    CGWindowID windowID = 0;
    NSArray *windowList = objc_retainedObject(CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID));
    CGRect windowRect;
    for (NSDictionary *info in windowList) {
        if ([[info objectForKey:(NSString *)kCGWindowOwnerName] isEqualToString:@"iOS Simulator"] && ![[info objectForKey:(NSString *)kCGWindowName] isEqualToString:@""]) {
            windowID = [[info objectForKey:(NSString *)kCGWindowNumber] unsignedIntValue];
            CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)objc_unretainedPointer([info objectForKey:(NSString *)kCGWindowBounds]), &windowRect);
            CGGetDisplaysWithRect(windowRect, 1, &displayID, NULL);
        }
    }
    if (windowID) {
        AVCaptureScreenInput *input = [[AVCaptureScreenInput alloc] initWithDisplayID:displayID];
        CGRect displayBounds = CGDisplayBounds(displayID);
        input.cropRect = CGRectMake(windowRect.origin.x - displayBounds.origin.x, displayBounds.size.height - displayBounds.origin.y - windowRect.origin.y - windowRect.size.height, windowRect.size.width, windowRect.size.height);
        AVCaptureMovieFileOutput *output = [[AVCaptureMovieFileOutput alloc] init];
        self.captureSession = [[AVCaptureSession alloc] init];
        [self.captureSession startRunning];
        [self.captureSession addInput:input];
        [self.captureSession addOutput:output];
        [output startRecordingToOutputFileURL:self.outputURL recordingDelegate:self];
    }
}

- (void)stop;
{
    [self.captureSession stopRunning];
    self.captureSession = nil;
}

#pragma mark AVCaptureFileOutputRecordingDelegate

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections;
{
}

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error;
{
}

@end
Something went wrong with that request. Please try again.