Skip to content

Commit

Permalink
Possible sound fix
Browse files Browse the repository at this point in the history
  • Loading branch information
devand123 committed Feb 6, 2015
1 parent 75cb3a4 commit ec8840a
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 5 deletions.
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@

<header-file src="src/ios/MyAudioDevice.h" />
<header-file src="src/ios/UIView+JTViewToImage.h" />
<!-- <header-file src="TBExamplePhotoVideoCapture.h" /> -->
<header-file src="src/ios/OpenTokPlugin.h" />
<!-- <header-file src="src/ios/Rollerr-Bridging-Header.h" /> -->
<source-file src="src/ios/MyAudioDevice.m" />
<source-file src="src/ios/UIView+JTViewToImage.m" />
<!-- <source-file src="TBExamplePhotoVideoCapture.m" /> -->
<source-file src="src/ios/OpenTokPlugin.m" />
<!-- <source-file src="src/ios/VideoView.swift" /> -->

Expand Down
13 changes: 8 additions & 5 deletions src/ios/OpenTokPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#import "OpentokPlugin.h"
#import "UIView+JTViewToImage.h"
// #import "OpenTokPlugin-Swift.h"
#import "MyAudioDevice.h"

@implementation OpenTokPlugin{
Expand Down Expand Up @@ -111,6 +110,9 @@ -(void) stopVideo:(CDVInvokedUrlCommand*)command {
if([layer.name isEqualToString:@"VideoView"]) {
[layer removeFromSuperlayer];
[captureSession removeInput:previousInput];
previousInput = nil;
captureSession = nil;
previewLayer = nil;
}
}

Expand Down Expand Up @@ -192,10 +194,11 @@ -(void)initSession:(CDVInvokedUrlCommand*)command{
// [videoView stopRunning];
// NSLog(@"subviews count: %@ ", self.webView.layer);

// if([[OTAudioDeviceManager currentAudioDevice] isKindOfClass:[MyAudioDevice class]]) {
_myAudioDevice = [[MyAudioDevice alloc] init];
[OTAudioDeviceManager setAudioDevice:_myAudioDevice];
// }
if(![[OTAudioDeviceManager currentAudioDevice] isKindOfClass:[MyAudioDevice class]]) {
NSLog(@"audioDevice is allocated...");
_myAudioDevice = [[MyAudioDevice alloc] init];
[OTAudioDeviceManager setAudioDevice:_myAudioDevice];
}

// Get Parameters
NSString* apiKey = [command.arguments objectAtIndex:0];
Expand Down
17 changes: 17 additions & 0 deletions src/ios/TBExamplePhotoVideoCapture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// TBExampleVideoCapture+PhotoCapture.h
// Live-Photo-Capture
//
// Created by Charley Robinson on 12/16/13.
// Copyright (c) 2013 TokBox, Inc. All rights reserved.
//

#import "TBExampleVideoCapture.h"

@interface TBExamplePhotoVideoCapture : TBExampleVideoCapture

@property (readonly) BOOL isTakingPhoto;
- (void)takePhotoWithCompletionHandler:(void (^)(UIImage* photo))block;


@end
111 changes: 111 additions & 0 deletions src/ios/TBExamplePhotoVideoCapture.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// TBExampleVideoCapture+PhotoCapture.m
// Live-Photo-Capture
//
// Copyright (c) 2013 TokBox, Inc. All rights reserved.
//

#import "TBExamplePhotoVideoCapture.h"
#import <Availability.h>
#import <UIKit/UIKit.h>
#import <ImageIO/CGImageProperties.h>

@implementation TBExamplePhotoVideoCapture {
BOOL _isTakingPhoto;
AVCaptureStillImageOutput* _stillImageOutput;
}

- (NSString*)pauseVideoCaptureForPhoto {
[self.captureSession beginConfiguration];
NSString* oldPreset = self.captureSession.sessionPreset;
[self.captureSession setSessionPreset:AVCaptureSessionPresetPhoto];
_stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc]
initWithObjectsAndKeys:
AVVideoCodecJPEG, AVVideoCodecKey,
nil];
[_stillImageOutput setOutputSettings:outputSettings];
[self.captureSession addOutput:_stillImageOutput];
[self.captureSession commitConfiguration];
double startTime = CACurrentMediaTime();
// if your images are coming out dark, you might try increasing this timeout
double timeout = 1.0;
while ((timeout > (CACurrentMediaTime() - startTime)) &&
(self.videoInput.device.isAdjustingExposure ||
self.videoInput.device.isAdjustingFocus)) {
// wait for sensor to adjust. this should not take long
}
return oldPreset;
}

- (void)resumeVideoCapture:(NSString*)oldPreset {
[self.captureSession beginConfiguration];
[self.captureSession setSessionPreset:oldPreset];
[self.captureSession removeOutput:_stillImageOutput];
[_stillImageOutput release];
[self.captureSession commitConfiguration];
}


- (UIImage*)doPhotoCapture {

AVCaptureConnection *videoConnection = nil;
for (AVCaptureConnection *connection in _stillImageOutput.connections)
{
for (AVCaptureInputPort *port in [connection inputPorts])
{
if ([[port mediaType] isEqual:AVMediaTypeVideo] )
{
videoConnection = connection;
break;
}
}
if (videoConnection)
{
break;
}
}

dispatch_semaphore_t imageCaptureSemaphore = dispatch_semaphore_create(0);
__block UIImage* resultImage = nil;
[_stillImageOutput
captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler: ^(CMSampleBufferRef imageSampleBuffer,
NSError *error)
{
NSData *imageData =
[AVCaptureStillImageOutput
jpegStillImageNSDataRepresentation:imageSampleBuffer];
resultImage = [[UIImage alloc] initWithData:imageData];
dispatch_semaphore_signal(imageCaptureSemaphore);
}];

dispatch_time_t timeout = dispatch_walltime(DISPATCH_TIME_NOW,
30 * NSEC_PER_SEC);
dispatch_semaphore_wait(imageCaptureSemaphore, timeout);
dispatch_release(imageCaptureSemaphore);

return resultImage;
}

- (void)takePhotoWithCompletionHandler:(void (^)(UIImage* photo))block {
if (self.isTakingPhoto) {
return;
}
_isTakingPhoto = YES;
dispatch_async(_capture_queue, ^() {
NSString* oldPreset = [self pauseVideoCaptureForPhoto];
UIImage* result = [self doPhotoCapture];
dispatch_async(dispatch_get_main_queue(), ^() {
block([result autorelease]);
});
[self resumeVideoCapture:oldPreset];
_isTakingPhoto = NO;
});
}

- (BOOL)isTakingPhoto {
return _isTakingPhoto;
}

@end

0 comments on commit ec8840a

Please sign in to comment.