webrtc_m130/sdk/objc/components/capturer/RTCFileVideoCapturer.m

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

210 lines
6.8 KiB
Mathematica
Raw Normal View History

/**
* Copyright 2017 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#import "RTCFileVideoCapturer.h"
#import "base/RTCLogging.h"
#import "base/RTCVideoFrameBuffer.h"
#import "components/video_frame_buffer/RTCCVPixelBuffer.h"
NSString *const kRTCFileVideoCapturerErrorDomain = @"org.webrtc.RTCFileVideoCapturer";
typedef NS_ENUM(NSInteger, RTCFileVideoCapturerErrorCode) {
RTCFileVideoCapturerErrorCode_CapturerRunning = 2000,
RTCFileVideoCapturerErrorCode_FileNotFound
};
typedef NS_ENUM(NSInteger, RTCFileVideoCapturerStatus) {
RTCFileVideoCapturerStatusNotInitialized,
RTCFileVideoCapturerStatusStarted,
RTCFileVideoCapturerStatusStopped
};
@interface RTCFileVideoCapturer ()
@property(nonatomic, assign) CMTime lastPresentationTime;
@property(nonatomic, strong) NSURL *fileURL;
@end
@implementation RTCFileVideoCapturer {
AVAssetReader *_reader;
AVAssetReaderTrackOutput *_outTrack;
RTCFileVideoCapturerStatus _status;
dispatch_queue_t _frameQueue;
}
@synthesize lastPresentationTime = _lastPresentationTime;
@synthesize fileURL = _fileURL;
- (void)startCapturingFromFileNamed:(NSString *)nameOfFile
onError:(RTCFileVideoCapturerErrorBlock)errorBlock {
if (_status == RTCFileVideoCapturerStatusStarted) {
NSError *error =
[NSError errorWithDomain:kRTCFileVideoCapturerErrorDomain
code:RTCFileVideoCapturerErrorCode_CapturerRunning
userInfo:@{NSUnderlyingErrorKey : @"Capturer has been started."}];
errorBlock(error);
return;
} else {
_status = RTCFileVideoCapturerStatusStarted;
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *pathForFile = [self pathForFileName:nameOfFile];
if (!pathForFile) {
NSString *errorString =
[NSString stringWithFormat:@"File %@ not found in bundle", nameOfFile];
NSError *error = [NSError errorWithDomain:kRTCFileVideoCapturerErrorDomain
code:RTCFileVideoCapturerErrorCode_FileNotFound
userInfo:@{NSUnderlyingErrorKey : errorString}];
errorBlock(error);
return;
}
self.lastPresentationTime = CMTimeMake(0, 0);
self.fileURL = [NSURL fileURLWithPath:pathForFile];
[self setupReaderOnError:errorBlock];
});
}
- (void)setupReaderOnError:(RTCFileVideoCapturerErrorBlock)errorBlock {
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:_fileURL options:nil];
NSArray *allTracks = [asset tracksWithMediaType:AVMediaTypeVideo];
NSError *error = nil;
_reader = [[AVAssetReader alloc] initWithAsset:asset error:&error];
if (error) {
errorBlock(error);
return;
}
NSDictionary *options = @{
(NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)
};
_outTrack =
[[AVAssetReaderTrackOutput alloc] initWithTrack:allTracks.firstObject outputSettings:options];
[_reader addOutput:_outTrack];
[_reader startReading];
RTCLog(@"File capturer started reading");
[self readNextBuffer];
}
- (void)stopCapture {
_status = RTCFileVideoCapturerStatusStopped;
RTCLog(@"File capturer stopped.");
}
#pragma mark - Private
- (nullable NSString *)pathForFileName:(NSString *)fileName {
NSArray *nameComponents = [fileName componentsSeparatedByString:@"."];
if (nameComponents.count != 2) {
return nil;
}
NSString *path =
[[NSBundle mainBundle] pathForResource:nameComponents[0] ofType:nameComponents[1]];
return path;
}
- (dispatch_queue_t)frameQueue {
if (!_frameQueue) {
_frameQueue = dispatch_queue_create("org.webrtc.filecapturer.video", DISPATCH_QUEUE_SERIAL);
dispatch_set_target_queue(_frameQueue,
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0));
}
return _frameQueue;
}
- (void)readNextBuffer {
if (_status == RTCFileVideoCapturerStatusStopped) {
[_reader cancelReading];
_reader = nil;
return;
}
if (_reader.status == AVAssetReaderStatusCompleted) {
[_reader cancelReading];
_reader = nil;
[self setupReaderOnError:nil];
return;
}
CMSampleBufferRef sampleBuffer = [_outTrack copyNextSampleBuffer];
if (!sampleBuffer) {
[self readNextBuffer];
return;
}
if (CMSampleBufferGetNumSamples(sampleBuffer) != 1 || !CMSampleBufferIsValid(sampleBuffer) ||
!CMSampleBufferDataIsReady(sampleBuffer)) {
CFRelease(sampleBuffer);
[self readNextBuffer];
return;
}
[self publishSampleBuffer:sampleBuffer];
}
- (void)publishSampleBuffer:(CMSampleBufferRef)sampleBuffer {
CMTime presentationTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
Float64 presentationDifference =
CMTimeGetSeconds(CMTimeSubtract(presentationTime, _lastPresentationTime));
_lastPresentationTime = presentationTime;
int64_t presentationDifferenceRound = lroundf(presentationDifference * NSEC_PER_SEC);
__block dispatch_source_t timer = [self createStrictTimer];
// Strict timer that will fire |presentationDifferenceRound| ns from now and never again.
dispatch_source_set_timer(timer,
dispatch_time(DISPATCH_TIME_NOW, presentationDifferenceRound),
DISPATCH_TIME_FOREVER,
0);
dispatch_source_set_event_handler(timer, ^{
dispatch_source_cancel(timer);
timer = nil;
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
if (!pixelBuffer) {
CFRelease(sampleBuffer);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self readNextBuffer];
});
return;
}
Revert "Revert "Revert "Revert "Support more formats in RTCVideoFrame"""" This reverts commit 1cfeb435427a2fa677a495e34c882096efc193d0. Reason for revert: Fix unit test Original change's description: > Revert "Revert "Revert "Support more formats in RTCVideoFrame""" > > This reverts commit 7583390d1a3a7c4e9a77da0d77250abac0c34d1d. > > Reason for revert: Breaks unit tests > > Original change's description: > > Revert "Revert "Support more formats in RTCVideoFrame"" > > > > This reverts commit 0789dab2cbd1617e94d7300e375163d42345f3d4. > > > > Reason for revert: Include obc_corevideoframebuffer target > > > > Original change's description: > > > Revert "Support more formats in RTCVideoFrame" > > > > > > This reverts commit bd2220a9c496ef2e8567b68d4be9435a110bdc34. > > > > > > Reason for revert: Broke external clients > > > > > > Original change's description: > > > > Support more formats in RTCVideoFrame > > > > > > > > Implement Obj-C version of webrtc::VideoFrameBuffer and use that in > > > > RTCVideoFrame. > > > > > > > > Bug: webrtc:7785 > > > > Change-Id: I49f42bcf451dd6769b3a79a65fe7b400dce22677 > > > > Reviewed-on: https://chromium-review.googlesource.com/536773 > > > > Commit-Queue: Anders Carlsson <andersc@webrtc.org> > > > > Reviewed-by: Magnus Jedvert <magjed@webrtc.org> > > > > Cr-Commit-Position: refs/heads/master@{#18691} > > > > > > TBR=magjed@webrtc.org,andersc@webrtc.org > > > > > > Change-Id: Id765dd9543ed0613a6b2de108b268c3501025fcd > > > No-Presubmit: true > > > No-Tree-Checks: true > > > No-Try: true > > > Bug: webrtc:7785 > > > Reviewed-on: https://chromium-review.googlesource.com/542837 > > > Reviewed-by: Anders Carlsson <andersc@webrtc.org> > > > Commit-Queue: Anders Carlsson <andersc@webrtc.org> > > > Cr-Commit-Position: refs/heads/master@{#18697} > > > > TBR=magjed@webrtc.org,andersc@webrtc.org > > > > Change-Id: I1ef5313b4a6c56eb8c7fd02d95db62c4e3c00255 > > No-Presubmit: true > > No-Tree-Checks: true > > No-Try: true > > Bug: webrtc:7785 > > Reviewed-on: https://chromium-review.googlesource.com/542838 > > Commit-Queue: Anders Carlsson <andersc@webrtc.org> > > Reviewed-by: Anders Carlsson <andersc@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#18716} > > TBR=magjed@webrtc.org,andersc@webrtc.org > > Change-Id: Id12f33698eb02041607cb9a5c54f37f01bfac5b1 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:7785 > Reviewed-on: https://chromium-review.googlesource.com/544840 > Reviewed-by: Anders Carlsson <andersc@webrtc.org> > Commit-Queue: Anders Carlsson <andersc@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#18718} TBR=magjed@webrtc.org,andersc@webrtc.org Change-Id: I184303ecba8db91ef7de709f982a295a2efe92eb Bug: webrtc:7785 Reviewed-on: https://chromium-review.googlesource.com/544841 Commit-Queue: Magnus Jedvert <magjed@webrtc.org> Reviewed-by: Anders Carlsson <andersc@webrtc.org> Cr-Commit-Position: refs/heads/master@{#18731}
2017-06-22 15:26:30 +02:00
RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer];
NSTimeInterval timeStampSeconds = CACurrentMediaTime();
int64_t timeStampNs = lroundf(timeStampSeconds * NSEC_PER_SEC);
RTCVideoFrame *videoFrame =
Revert "Revert "Revert "Revert "Support more formats in RTCVideoFrame"""" This reverts commit 1cfeb435427a2fa677a495e34c882096efc193d0. Reason for revert: Fix unit test Original change's description: > Revert "Revert "Revert "Support more formats in RTCVideoFrame""" > > This reverts commit 7583390d1a3a7c4e9a77da0d77250abac0c34d1d. > > Reason for revert: Breaks unit tests > > Original change's description: > > Revert "Revert "Support more formats in RTCVideoFrame"" > > > > This reverts commit 0789dab2cbd1617e94d7300e375163d42345f3d4. > > > > Reason for revert: Include obc_corevideoframebuffer target > > > > Original change's description: > > > Revert "Support more formats in RTCVideoFrame" > > > > > > This reverts commit bd2220a9c496ef2e8567b68d4be9435a110bdc34. > > > > > > Reason for revert: Broke external clients > > > > > > Original change's description: > > > > Support more formats in RTCVideoFrame > > > > > > > > Implement Obj-C version of webrtc::VideoFrameBuffer and use that in > > > > RTCVideoFrame. > > > > > > > > Bug: webrtc:7785 > > > > Change-Id: I49f42bcf451dd6769b3a79a65fe7b400dce22677 > > > > Reviewed-on: https://chromium-review.googlesource.com/536773 > > > > Commit-Queue: Anders Carlsson <andersc@webrtc.org> > > > > Reviewed-by: Magnus Jedvert <magjed@webrtc.org> > > > > Cr-Commit-Position: refs/heads/master@{#18691} > > > > > > TBR=magjed@webrtc.org,andersc@webrtc.org > > > > > > Change-Id: Id765dd9543ed0613a6b2de108b268c3501025fcd > > > No-Presubmit: true > > > No-Tree-Checks: true > > > No-Try: true > > > Bug: webrtc:7785 > > > Reviewed-on: https://chromium-review.googlesource.com/542837 > > > Reviewed-by: Anders Carlsson <andersc@webrtc.org> > > > Commit-Queue: Anders Carlsson <andersc@webrtc.org> > > > Cr-Commit-Position: refs/heads/master@{#18697} > > > > TBR=magjed@webrtc.org,andersc@webrtc.org > > > > Change-Id: I1ef5313b4a6c56eb8c7fd02d95db62c4e3c00255 > > No-Presubmit: true > > No-Tree-Checks: true > > No-Try: true > > Bug: webrtc:7785 > > Reviewed-on: https://chromium-review.googlesource.com/542838 > > Commit-Queue: Anders Carlsson <andersc@webrtc.org> > > Reviewed-by: Anders Carlsson <andersc@webrtc.org> > > Cr-Commit-Position: refs/heads/master@{#18716} > > TBR=magjed@webrtc.org,andersc@webrtc.org > > Change-Id: Id12f33698eb02041607cb9a5c54f37f01bfac5b1 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Bug: webrtc:7785 > Reviewed-on: https://chromium-review.googlesource.com/544840 > Reviewed-by: Anders Carlsson <andersc@webrtc.org> > Commit-Queue: Anders Carlsson <andersc@webrtc.org> > Cr-Commit-Position: refs/heads/master@{#18718} TBR=magjed@webrtc.org,andersc@webrtc.org Change-Id: I184303ecba8db91ef7de709f982a295a2efe92eb Bug: webrtc:7785 Reviewed-on: https://chromium-review.googlesource.com/544841 Commit-Queue: Magnus Jedvert <magjed@webrtc.org> Reviewed-by: Anders Carlsson <andersc@webrtc.org> Cr-Commit-Position: refs/heads/master@{#18731}
2017-06-22 15:26:30 +02:00
[[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuffer rotation:0 timeStampNs:timeStampNs];
CFRelease(sampleBuffer);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self readNextBuffer];
});
[self.delegate capturer:self didCaptureVideoFrame:videoFrame];
});
dispatch_activate(timer);
}
- (dispatch_source_t)createStrictTimer {
dispatch_source_t timer = dispatch_source_create(
DISPATCH_SOURCE_TYPE_TIMER, 0, DISPATCH_TIMER_STRICT, [self frameQueue]);
return timer;
}
- (void)dealloc {
[self stopCapture];
}
@end