History

lavd/avfoundation: Add basic transport control observation for capable devices.

Thilo Borgmann committed 6 years ago

Commit 39fbd061 parent e38b8b01 file · +59 −8Browse files

Unified · Split

libavdevice/avfoundation.m+59−8
@@ -88,7 +88,6 @@ typedef struct · 87 unmodified lines
8888 int64_t first_pts;
8989 int64_t first_audio_pts;
9090 pthread_mutex_t frame_lock;
91− pthread_cond_t frame_wait_cond;
9291 id avf_delegate;
9392 id avf_audio_delegate;
9493
@@ -130,6 +129,10 @@ typedef struct · 35 unmodified lines
130129 AVCaptureAudioDataOutput *audio_output;
131130 CMSampleBufferRef current_frame;
132131 CMSampleBufferRef current_audio_frame;
132+
133+ AVCaptureDevice *observed_device;
134+ AVCaptureDeviceTransportControlsPlaybackMode observed_mode;
135+ int observed_quit;
133136 } AVFContext;
134137
135138 static void lock_frames(AVFContext* ctx)
@@ -163,10 +166,50 @@ static void unlock_frames(AVFContext* ctx) · 27 unmodified lines
163166 {
164167 if (self = [super init]) {
165168 _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+ }
166180 }
167181 return self;
168182 }
169183
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+
170213 - (void) captureOutput:(AVCaptureOutput *)captureOutput
171214 didOutputSampleBuffer:(CMSampleBufferRef)videoFrame
172215 fromConnection:(AVCaptureConnection *)connection
@@ -179,8 +222,6 @@ static void unlock_frames(AVFContext* ctx) · 6 unmodified lines
179222
180223 _context->current_frame = (CMSampleBufferRef)CFRetain(videoFrame);
181224
182− pthread_cond_signal(&_context->frame_wait_cond);
183−
184225 unlock_frames(_context);
185226
186227 ++_context->frames_captured;
@@ -225,8 +266,6 @@ static void unlock_frames(AVFContext* ctx) · 38 unmodified lines
225266
226267 _context->current_audio_frame = (CMSampleBufferRef)CFRetain(audioFrame);
227268
228− pthread_cond_signal(&_context->frame_wait_cond);
229−
230269 unlock_frames(_context);
231270
232271 ++_context->audio_frames_captured;
@@ -253,7 +292,6 @@ static void destroy_context(AVFContext* ctx) · 20 unmodified lines
253292 av_freep(&ctx->audio_buffer);
254293
255294 pthread_mutex_destroy(&ctx->frame_lock);
256− pthread_cond_destroy(&ctx->frame_wait_cond);
257295
258296 if (ctx->current_frame) {
259297 CFRelease(ctx->current_frame);
@@ -499,6 +537,15 @@ static int add_video_device(AVFormatContext *s, AVCaptureDevice *video_device) · 239 unmodified lines
499537 }
500538 [ctx->video_output setAlwaysDiscardsLateVideoFrames:ctx->drop_late_frames];
501539
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+
502549 ctx->avf_delegate = [[AVFFrameReceiver alloc] initWithContext:ctx];
503550
504551 queue = dispatch_queue_create("avf_queue", NULL);
@@ -709,7 +756,6 @@ static int avf_read_header(AVFormatContext *s) · 204 unmodified lines
709756 ctx->first_audio_pts = av_gettime();
710757
711758 pthread_mutex_init(&ctx->frame_lock, NULL);
712− pthread_cond_init(&ctx->frame_wait_cond, NULL);
713759
714760 #if !TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
715761 CGGetActiveDisplayList(0, NULL, &num_screens);
@@ -1110,7 +1156,12 @@ static int avf_read_packet(AVFormatContext *s, AVPacket *pkt) · 394 unmodified lines
11101156 ctx->current_audio_frame = nil;
11111157 } else {
11121158 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+ }
11141165 }
11151166
11161167 unlock_frames(ctx);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t