denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 11 | #import "WebRTC/RTCFileVideoCapturer.h" |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 12 | |
| 13 | #import "WebRTC/RTCLogging.h" |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 14 | #import "WebRTC/RTCVideoFrameBuffer.h" |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 15 | |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 16 | NSString *const kRTCFileVideoCapturerErrorDomain = @"org.webrtc.RTCFileVideoCapturer"; |
| 17 | |
| 18 | typedef NS_ENUM(NSInteger, RTCFileVideoCapturerErrorCode) { |
| 19 | RTCFileVideoCapturerErrorCode_CapturerRunning = 2000, |
| 20 | RTCFileVideoCapturerErrorCode_FileNotFound |
| 21 | }; |
| 22 | |
| 23 | typedef NS_ENUM(NSInteger, RTCFileVideoCapturerStatus) { |
| 24 | RTCFileVideoCapturerStatusNotInitialized, |
| 25 | RTCFileVideoCapturerStatusStarted, |
| 26 | RTCFileVideoCapturerStatusStopped |
| 27 | }; |
| 28 | |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 29 | @implementation RTCFileVideoCapturer { |
| 30 | AVAssetReader *_reader; |
| 31 | AVAssetReaderTrackOutput *_outTrack; |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 32 | RTCFileVideoCapturerStatus _status; |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 33 | CMTime _lastPresentationTime; |
| 34 | dispatch_queue_t _frameQueue; |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 35 | NSURL *_fileURL; |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 38 | - (void)startCapturingFromFileNamed:(NSString *)nameOfFile |
| 39 | onError:(RTCFileVideoCapturerErrorBlock)errorBlock { |
| 40 | if (_status == RTCFileVideoCapturerStatusStarted) { |
| 41 | NSError *error = |
| 42 | [NSError errorWithDomain:kRTCFileVideoCapturerErrorDomain |
| 43 | code:RTCFileVideoCapturerErrorCode_CapturerRunning |
| 44 | userInfo:@{NSUnderlyingErrorKey : @"Capturer has been started."}]; |
| 45 | |
| 46 | errorBlock(error); |
| 47 | return; |
| 48 | } else { |
| 49 | _status = RTCFileVideoCapturerStatusStarted; |
| 50 | } |
| 51 | |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 52 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 53 | NSString *pathForFile = [self pathForFileName:nameOfFile]; |
| 54 | if (!pathForFile) { |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 55 | NSString *errorString = |
| 56 | [NSString stringWithFormat:@"File %@ not found in bundle", nameOfFile]; |
| 57 | NSError *error = [NSError errorWithDomain:kRTCFileVideoCapturerErrorDomain |
| 58 | code:RTCFileVideoCapturerErrorCode_FileNotFound |
| 59 | userInfo:@{NSUnderlyingErrorKey : errorString}]; |
| 60 | errorBlock(error); |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | _lastPresentationTime = CMTimeMake(0, 0); |
| 65 | |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 66 | _fileURL = [NSURL fileURLWithPath:pathForFile]; |
| 67 | [self setupReaderOnError:errorBlock]; |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 68 | }); |
| 69 | } |
| 70 | |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 71 | - (void)setupReaderOnError:(RTCFileVideoCapturerErrorBlock)errorBlock { |
| 72 | AVURLAsset *asset = [AVURLAsset URLAssetWithURL:_fileURL options:nil]; |
| 73 | |
| 74 | NSArray *allTracks = [asset tracksWithMediaType:AVMediaTypeVideo]; |
| 75 | NSError *error = nil; |
| 76 | |
| 77 | _reader = [[AVAssetReader alloc] initWithAsset:asset error:&error]; |
| 78 | if (error) { |
| 79 | errorBlock(error); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | NSDictionary *options = @{ |
| 84 | (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) |
| 85 | }; |
| 86 | _outTrack = |
| 87 | [[AVAssetReaderTrackOutput alloc] initWithTrack:allTracks.firstObject outputSettings:options]; |
| 88 | [_reader addOutput:_outTrack]; |
| 89 | |
| 90 | [_reader startReading]; |
| 91 | RTCLog(@"File capturer started reading"); |
| 92 | [self readNextBuffer]; |
| 93 | } |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 94 | - (void)stopCapture { |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 95 | _status = RTCFileVideoCapturerStatusStopped; |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 96 | RTCLog(@"File capturer stopped."); |
| 97 | } |
| 98 | |
| 99 | #pragma mark - Private |
| 100 | |
| 101 | - (nullable NSString *)pathForFileName:(NSString *)fileName { |
| 102 | NSArray *nameComponents = [fileName componentsSeparatedByString:@"."]; |
| 103 | if (nameComponents.count != 2) { |
| 104 | return nil; |
| 105 | } |
| 106 | |
| 107 | NSString *path = |
| 108 | [[NSBundle mainBundle] pathForResource:nameComponents[0] ofType:nameComponents[1]]; |
| 109 | return path; |
| 110 | } |
| 111 | |
| 112 | - (dispatch_queue_t)frameQueue { |
| 113 | if (!_frameQueue) { |
| 114 | _frameQueue = dispatch_queue_create("org.webrtc.filecapturer.video", DISPATCH_QUEUE_SERIAL); |
| 115 | dispatch_set_target_queue(_frameQueue, |
| 116 | dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)); |
| 117 | } |
| 118 | return _frameQueue; |
| 119 | } |
| 120 | |
| 121 | - (void)readNextBuffer { |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 122 | if (_status == RTCFileVideoCapturerStatusStopped) { |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 123 | [_reader cancelReading]; |
| 124 | _reader = nil; |
| 125 | return; |
| 126 | } |
| 127 | |
Daniela | 012b56b | 2017-11-15 13:15:24 +0100 | [diff] [blame] | 128 | if (_reader.status == AVAssetReaderStatusCompleted) { |
| 129 | [_reader cancelReading]; |
| 130 | _reader = nil; |
| 131 | [self setupReaderOnError:nil]; |
| 132 | return; |
| 133 | } |
| 134 | |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 135 | CMSampleBufferRef sampleBuffer = [_outTrack copyNextSampleBuffer]; |
| 136 | if (!sampleBuffer) { |
| 137 | [self readNextBuffer]; |
| 138 | return; |
| 139 | } |
| 140 | if (CMSampleBufferGetNumSamples(sampleBuffer) != 1 || !CMSampleBufferIsValid(sampleBuffer) || |
| 141 | !CMSampleBufferDataIsReady(sampleBuffer)) { |
Peter Hanspers | d9b64cd | 2018-01-12 16:16:18 +0100 | [diff] [blame] | 142 | CFRelease(sampleBuffer); |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 143 | [self readNextBuffer]; |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | [self publishSampleBuffer:sampleBuffer]; |
| 148 | } |
| 149 | |
| 150 | - (void)publishSampleBuffer:(CMSampleBufferRef)sampleBuffer { |
| 151 | CMTime presentationTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer); |
| 152 | Float64 presentationDifference = |
| 153 | CMTimeGetSeconds(CMTimeSubtract(presentationTime, _lastPresentationTime)); |
| 154 | _lastPresentationTime = presentationTime; |
| 155 | int64_t presentationDifferenceRound = lroundf(presentationDifference * NSEC_PER_SEC); |
| 156 | |
| 157 | __block dispatch_source_t timer = [self createStrictTimer]; |
| 158 | // Strict timer that will fire |presentationDifferenceRound| ns from now and never again. |
| 159 | dispatch_source_set_timer(timer, |
| 160 | dispatch_time(DISPATCH_TIME_NOW, presentationDifferenceRound), |
| 161 | DISPATCH_TIME_FOREVER, |
| 162 | 0); |
| 163 | dispatch_source_set_event_handler(timer, ^{ |
| 164 | dispatch_source_cancel(timer); |
| 165 | timer = nil; |
| 166 | |
| 167 | CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); |
| 168 | if (!pixelBuffer) { |
| 169 | CFRelease(sampleBuffer); |
| 170 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| 171 | [self readNextBuffer]; |
| 172 | }); |
| 173 | return; |
| 174 | } |
| 175 | |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 176 | RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer]; |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 177 | NSTimeInterval timeStampSeconds = CACurrentMediaTime(); |
| 178 | int64_t timeStampNs = lroundf(timeStampSeconds * NSEC_PER_SEC); |
| 179 | RTCVideoFrame *videoFrame = |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 180 | [[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuffer rotation:0 timeStampNs:timeStampNs]; |
denicija | 0d4d57f | 2017-06-02 07:15:14 -0700 | [diff] [blame] | 181 | CFRelease(sampleBuffer); |
| 182 | |
| 183 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
| 184 | [self readNextBuffer]; |
| 185 | }); |
| 186 | |
| 187 | [self.delegate capturer:self didCaptureVideoFrame:videoFrame]; |
| 188 | }); |
| 189 | dispatch_activate(timer); |
| 190 | } |
| 191 | |
| 192 | - (dispatch_source_t)createStrictTimer { |
| 193 | dispatch_source_t timer = dispatch_source_create( |
| 194 | DISPATCH_SOURCE_TYPE_TIMER, 0, DISPATCH_TIMER_STRICT, [self frameQueue]); |
| 195 | return timer; |
| 196 | } |
| 197 | |
| 198 | - (void)dealloc { |
| 199 | [self stopCapture]; |
| 200 | } |
| 201 | |
| 202 | @end |