Giter Site home page Giter Site logo

johnnyclem / avcapturemultipeervideodataoutput Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pj4533/avcapturemultipeervideodataoutput

1.0 2.0 0.0 248 KB

Sends video using Multipeer Connectivity

License: MIT License

Ruby 4.87% Objective-C 95.13%

avcapturemultipeervideodataoutput's Introduction

AVCaptureMultipeerVideoDataOutput

Advertises one stream of video over a multipeer connection.

Use SGSMultipeerVideoMixer for an example of how to view.

Details

Multipeer connection stuff is abstracted inside an AVCaptureVideoDataOutput subclass so setup is easy using the normal AV pipeline:

// Create the AVCaptureSession
self.captureSession = [[AVCaptureSession alloc] init];

// Setup the preview view
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
captureVideoPreviewLayer.frame = CGRectMake(0,0, 320, 320);
[self.previewView.layer addSublayer:captureVideoPreviewLayer];

// Create video device input
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *videoDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:videoDevice error:nil];
[self.captureSession addInput:videoDeviceInput];

// Create output
AVCaptureMultipeerVideoDataOutput *multipeerVideoOutput = [[AVCaptureMultipeerVideoDataOutput alloc] initWithDisplayName:[[UIDevice currentDevice] name]];
[self.captureSession addOutput:multipeerVideoOutput];

[self.captureSession startRunning];

Thats it! Look in the AVCaptureMultipeerVideoDataOutput class for more details on the format & size of the image sent. The NSData that gets sent over the wire is described below.

Peer Setup

The peer needs to be looking for service type 'multipeer-video'. If you use the built in browser viewcontroller (like SGSMultipeerVideoMixer uses) it will look something like this:

- (void) showAssistant {
    MCBrowserViewController* browserVC = [[MCBrowserViewController alloc] initWithServiceType:@"multipeer-video" session:_session];
    browserVC.delegate = self;
    [self presentViewController:browserVC animated:YES completion:nil];
}

AVCaptureMultipeerVideoDataOutput sends a NSDictionary structured like this:

- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID {
    
    NSDictionary* dict = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:data];

    // the actual image data (as JPG)
    UIImage* image = [UIImage imageWithData:dict[@"image"] scale:2.0];
    
    // the current FPS 
    NSNumber* framesPerSecond = dict[@"framesPerSecond"];
    
    // the timestamp of this frame
    NSNumber* timestamp = dict[@"timestamp"];
}

Advertiser Setup

Uses cocoapods, so just add to your Podfile

pod "AVCaptureMultipeerVideoDataOutput"

See the Sample project for an example implementation. To run the sample:

pod install

Then open 'MultipeerVideoOutputSample.xcworkspace'

To Do

  • Expose more of the output format variables
  • Implement the no-UI multipeer connectivity classes

Contact

PJ Gray

License

AVCaptureMultipeerVideoDataOutput is available under the MIT license. See the LICENSE file for more info.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.