Unified · Split
| @@ -88,7 +88,6 @@ typedef struct · 87 unmodified lines | ||
| 88 | 88 | int64_t first_pts; |
| 89 | 89 | int64_t first_audio_pts; |
| 90 | 90 | pthread_mutex_t frame_lock; |
| 91 | − pthread_cond_t frame_wait_cond; | |
| 92 | 91 | id avf_delegate; |
| 93 | 92 | id avf_audio_delegate; |
| 94 | 93 | |
| @@ -130,6 +129,10 @@ typedef struct · 35 unmodified lines | ||
| 130 | 129 | AVCaptureAudioDataOutput *audio_output; |
| 131 | 130 | CMSampleBufferRef current_frame; |
| 132 | 131 | CMSampleBufferRef current_audio_frame; |
| 132 | + | |
| 133 | + AVCaptureDevice *observed_device; | |
| 134 | + AVCaptureDeviceTransportControlsPlaybackMode observed_mode; | |
| 135 | + int observed_quit; | |
| 133 | 136 | } AVFContext; |
| 134 | 137 | |
| 135 | 138 | static void lock_frames(AVFContext* ctx) |
| @@ -163,10 +166,50 @@ static void unlock_frames(AVFContext* ctx) · 27 unmodified lines | ||
| 163 | 166 | { |
| 164 | 167 | if (self = [super init]) { |
| 165 | 168 | _context = context; |
| 169 | + | |
| 170 | + // start observing if a device is set for it | |
| 171 | + if (_context->observed_device) { | |
| 172 | + NSString *keyPath = NSStringFromSelector(@selector(transportControlsPlaybackMode)); | |
| 173 | + NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew; | |
| 174 | + | |
| 175 | + [_context->observed_device addObserver: self | |
| 176 | + forKeyPath: keyPath | |
| 177 | + options: options | |
| 178 | + context: _context]; | |
| 179 | + } | |
| 166 | 180 | } |
| 167 | 181 | return self; |
| 168 | 182 | } |
| 169 | 183 | |
| 184 | +- (void)dealloc { | |
| 185 | + // stop observing if a device is set for it | |
| 186 | + NSString *keyPath = NSStringFromSelector(@selector(transportControlsPlaybackMode)); | |
| 187 | + [_context->observed_device removeObserver: self forKeyPath: keyPath]; | |
| 188 | + [super dealloc]; | |
| 189 | +} | |
| 190 | + | |
| 191 | +- (void)observeValueForKeyPath:(NSString *)keyPath | |
| 192 | + ofObject:(id)object | |
| 193 | + change:(NSDictionary *)change | |
| 194 | + context:(void *)context { | |
| 195 | + if (context == _context) { | |
| 196 | + AVCaptureDeviceTransportControlsPlaybackMode mode = | |
| 197 | + [change[NSKeyValueChangeNewKey] integerValue]; | |
| 198 | + | |
| 199 | + if (mode != _context->observed_mode) { | |
| 200 | + if (mode == AVCaptureDeviceTransportControlsNotPlayingMode) { | |
| 201 | + _context->observed_quit = 1; | |
| 202 | + } | |
| 203 | + _context->observed_mode = mode; | |
| 204 | + } | |
| 205 | + } else { | |
| 206 | + [super observeValueForKeyPath: keyPath | |
| 207 | + ofObject: object | |
| 208 | + change: change | |
| 209 | + context: context]; | |
| 210 | + } | |
| 211 | +} | |
| 212 | + | |
| 170 | 213 | - (void) captureOutput:(AVCaptureOutput *)captureOutput |
| 171 | 214 | didOutputSampleBuffer:(CMSampleBufferRef)videoFrame |
| 172 | 215 | fromConnection:(AVCaptureConnection *)connection |
| @@ -179,8 +222,6 @@ static void unlock_frames(AVFContext* ctx) · 6 unmodified lines | ||
| 179 | 222 | |
| 180 | 223 | _context->current_frame = (CMSampleBufferRef)CFRetain(videoFrame); |
| 181 | 224 | |
| 182 | − pthread_cond_signal(&_context->frame_wait_cond); | |
| 183 | − | |
| 184 | 225 | unlock_frames(_context); |
| 185 | 226 | |
| 186 | 227 | ++_context->frames_captured; |
| @@ -225,8 +266,6 @@ static void unlock_frames(AVFContext* ctx) · 38 unmodified lines | ||
| 225 | 266 | |
| 226 | 267 | _context->current_audio_frame = (CMSampleBufferRef)CFRetain(audioFrame); |
| 227 | 268 | |
| 228 | − pthread_cond_signal(&_context->frame_wait_cond); | |
| 229 | − | |
| 230 | 269 | unlock_frames(_context); |
| 231 | 270 | |
| 232 | 271 | ++_context->audio_frames_captured; |
| @@ -253,7 +292,6 @@ static void destroy_context(AVFContext* ctx) · 20 unmodified lines | ||
| 253 | 292 | av_freep(&ctx->audio_buffer); |
| 254 | 293 | |
| 255 | 294 | pthread_mutex_destroy(&ctx->frame_lock); |
| 256 | − pthread_cond_destroy(&ctx->frame_wait_cond); | |
| 257 | 295 | |
| 258 | 296 | if (ctx->current_frame) { |
| 259 | 297 | CFRelease(ctx->current_frame); |
| @@ -499,6 +537,15 @@ static int add_video_device(AVFormatContext *s, AVCaptureDevice *video_device) · 239 unmodified lines | ||
| 499 | 537 | } |
| 500 | 538 | [ctx->video_output setAlwaysDiscardsLateVideoFrames:ctx->drop_late_frames]; |
| 501 | 539 | |
| 540 | + // check for transport control support and set observer device if supported | |
| 541 | + int trans_ctrl = [video_device transportControlsSupported]; | |
| 542 | + AVCaptureDeviceTransportControlsPlaybackMode trans_mode = [video_device transportControlsPlaybackMode]; | |
| 543 | + | |
| 544 | + if (trans_ctrl) { | |
| 545 | + ctx->observed_mode = trans_mode; | |
| 546 | + ctx->observed_device = video_device; | |
| 547 | + } | |
| 548 | + | |
| 502 | 549 | ctx->avf_delegate = [[AVFFrameReceiver alloc] initWithContext:ctx]; |
| 503 | 550 | |
| 504 | 551 | queue = dispatch_queue_create("avf_queue", NULL); |
| @@ -709,7 +756,6 @@ static int avf_read_header(AVFormatContext *s) · 204 unmodified lines | ||
| 709 | 756 | ctx->first_audio_pts = av_gettime(); |
| 710 | 757 | |
| 711 | 758 | pthread_mutex_init(&ctx->frame_lock, NULL); |
| 712 | − pthread_cond_init(&ctx->frame_wait_cond, NULL); | |
| 713 | 759 | |
| 714 | 760 | #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 |
| 715 | 761 | CGGetActiveDisplayList(0, NULL, &num_screens); |
| @@ -1110,7 +1156,12 @@ static int avf_read_packet(AVFormatContext *s, AVPacket *pkt) · 394 unmodified lines | ||
| 1110 | 1156 | ctx->current_audio_frame = nil; |
| 1111 | 1157 | } else { |
| 1112 | 1158 | pkt->data = NULL; |
| 1113 | − pthread_cond_wait(&ctx->frame_wait_cond, &ctx->frame_lock); | |
| 1159 | + unlock_frames(ctx); | |
| 1160 | + if (ctx->observed_quit) { | |
| 1161 | + return AVERROR_EOF; | |
| 1162 | + } else { | |
| 1163 | + return AVERROR(EAGAIN); | |
| 1164 | + } | |
| 1114 | 1165 | } |
| 1115 | 1166 | |
| 1116 | 1167 | unlock_frames(ctx); |