| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | /*  camera_macos.mm                                                       */ | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							| 
									
										
										
										
											2019-06-17 12:42:05 +02:00
										 |  |  | /*                        https://godotengine.org                         */ | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | /**************************************************************************/ | 
					
						
							| 
									
										
										
										
											2019-06-17 12:42:05 +02:00
										 |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-22 08:35:03 +01:00
										 |  |  | ///@TODO this is a near duplicate of CameraIOS, we should find a way to combine those to minimize code duplication!!!! | 
					
						
							| 
									
										
										
											
												Fix various typos with codespell
Found via `codespell -q 3 -S ./thirdparty,*.po,./DONORS.md -L ackward,ang,ans,ba,beng,cas,childs,childrens,dof,doubleclick,fave,findn,hist,inout,leapyear,lod,nd,numer,ois,ony,paket,seeked,sinc,switchs,te,uint`
											
										 
											2021-07-07 11:17:32 -04:00
										 |  |  | // If you fix something here, make sure you fix it there as well! | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | #include "camera_macos.h" | 
					
						
							| 
									
										
										
										
											2023-06-13 16:56:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | #include "servers/camera/camera_feed.h" | 
					
						
							| 
									
										
										
										
											2021-08-22 00:23:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | #import <AVFoundation/AVFoundation.h> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ////////////////////////////////////////////////////////////////////////// | 
					
						
							|  |  |  | // MyCaptureSession - This is a little helper class so we can capture our frames | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @interface MyCaptureSession : AVCaptureSession <AVCaptureVideoDataOutputSampleBufferDelegate> { | 
					
						
							|  |  |  | 	Ref<CameraFeed> feed; | 
					
						
							|  |  |  | 	size_t width[2]; | 
					
						
							|  |  |  | 	size_t height[2]; | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 	Vector<uint8_t> img_data[2]; | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	AVCaptureDeviceInput *input; | 
					
						
							|  |  |  | 	AVCaptureVideoDataOutput *output; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @implementation MyCaptureSession | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (id)initForFeed:(Ref<CameraFeed>)p_feed andDevice:(AVCaptureDevice *)p_device { | 
					
						
							|  |  |  | 	if (self = [super init]) { | 
					
						
							|  |  |  | 		NSError *error; | 
					
						
							|  |  |  | 		feed = p_feed; | 
					
						
							|  |  |  | 		width[0] = 0; | 
					
						
							|  |  |  | 		height[0] = 0; | 
					
						
							|  |  |  | 		width[1] = 0; | 
					
						
							|  |  |  | 		height[1] = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		[self beginConfiguration]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		input = [AVCaptureDeviceInput deviceInputWithDevice:p_device error:&error]; | 
					
						
							|  |  |  | 		if (!input) { | 
					
						
							|  |  |  | 			print_line("Couldn't get input device for camera"); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			[self addInput:input]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		output = [AVCaptureVideoDataOutput new]; | 
					
						
							|  |  |  | 		if (!output) { | 
					
						
							|  |  |  | 			print_line("Couldn't get output device for camera"); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			NSDictionary *settings = @{ (NSString *)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange) }; | 
					
						
							|  |  |  | 			output.videoSettings = settings; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// discard if the data output queue is blocked (as we process the still image) | 
					
						
							|  |  |  | 			[output setAlwaysDiscardsLateVideoFrames:YES]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// now set ourselves as the delegate to receive new frames. | 
					
						
							|  |  |  | 			[output setSampleBufferDelegate:self queue:dispatch_get_main_queue()]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// this takes ownership | 
					
						
							|  |  |  | 			[self addOutput:output]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		[self commitConfiguration]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// kick off our session.. | 
					
						
							|  |  |  | 		[self startRunning]; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	return self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (void)cleanup { | 
					
						
							|  |  |  | 	// stop running | 
					
						
							|  |  |  | 	[self stopRunning]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// cleanup | 
					
						
							|  |  |  | 	[self beginConfiguration]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// remove input | 
					
						
							|  |  |  | 	if (input) { | 
					
						
							|  |  |  | 		[self removeInput:input]; | 
					
						
							|  |  |  | 		// don't release this | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 		input = nullptr; | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// free up our output | 
					
						
							|  |  |  | 	if (output) { | 
					
						
							|  |  |  | 		[self removeOutput:output]; | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 		[output setSampleBufferDelegate:nil queue:nullptr]; | 
					
						
							|  |  |  | 		output = nullptr; | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	[self commitConfiguration]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { | 
					
						
							|  |  |  | 	// This gets called every time our camera has a new image for us to process. | 
					
						
							|  |  |  | 	// May need to investigate in a way to throttle this if we get more images then we're rendering frames.. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// For now, version 1, we're just doing the bare minimum to make this work... | 
					
						
							|  |  |  | 	CVImageBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); | 
					
						
							|  |  |  | 	// int _width = CVPixelBufferGetWidth(pixelBuffer); | 
					
						
							|  |  |  | 	// int _height = CVPixelBufferGetHeight(pixelBuffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// It says that we need to lock this on the documentation pages but it's not in the samples | 
					
						
							|  |  |  | 	// need to lock our base address so we can access our pixel buffers, better safe then sorry? | 
					
						
							|  |  |  | 	CVPixelBufferLockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get our buffers | 
					
						
							|  |  |  | 	unsigned char *dataY = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0); | 
					
						
							|  |  |  | 	unsigned char *dataCbCr = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1); | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 	if (dataY == nullptr) { | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 		print_line("Couldn't access Y pixel buffer data"); | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 	} else if (dataCbCr == nullptr) { | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 		print_line("Couldn't access CbCr pixel buffer data"); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		Ref<Image> img[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// do Y | 
					
						
							| 
									
										
										
										
											2019-10-24 17:34:18 +03:00
										 |  |  | 			size_t new_width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 0); | 
					
						
							|  |  |  | 			size_t new_height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 0); | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if ((width[0] != new_width) || (height[0] != new_height)) { | 
					
						
							|  |  |  | 				width[0] = new_width; | 
					
						
							|  |  |  | 				height[0] = new_height; | 
					
						
							|  |  |  | 				img_data[0].resize(new_width * new_height); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 			uint8_t *w = img_data[0].ptrw(); | 
					
						
							|  |  |  | 			memcpy(w, dataY, new_width * new_height); | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 			img[0].instantiate(); | 
					
						
							| 
									
										
										
										
											2022-07-22 20:06:19 +02:00
										 |  |  | 			img[0]->set_data(new_width, new_height, 0, Image::FORMAT_R8, img_data[0]); | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// do CbCr | 
					
						
							| 
									
										
										
										
											2019-10-24 17:34:18 +03:00
										 |  |  | 			size_t new_width = CVPixelBufferGetWidthOfPlane(pixelBuffer, 1); | 
					
						
							|  |  |  | 			size_t new_height = CVPixelBufferGetHeightOfPlane(pixelBuffer, 1); | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if ((width[1] != new_width) || (height[1] != new_height)) { | 
					
						
							|  |  |  | 				width[1] = new_width; | 
					
						
							|  |  |  | 				height[1] = new_height; | 
					
						
							|  |  |  | 				img_data[1].resize(2 * new_width * new_height); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-17 18:06:54 -03:00
										 |  |  | 			uint8_t *w = img_data[1].ptrw(); | 
					
						
							|  |  |  | 			memcpy(w, dataCbCr, 2 * new_width * new_height); | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-27 01:07:10 +02:00
										 |  |  | 			///TODO OpenGL doesn't support FORMAT_RG8, need to do some form of conversion | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 			img[1].instantiate(); | 
					
						
							| 
									
										
										
										
											2022-07-22 20:06:19 +02:00
										 |  |  | 			img[1]->set_data(new_width, new_height, 0, Image::FORMAT_RG8, img_data[1]); | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// set our texture... | 
					
						
							| 
									
										
										
										
											2024-09-27 18:10:59 +08:00
										 |  |  | 		feed->set_ycbcr_images(img[0], img[1]); | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// and unlock | 
					
						
							|  |  |  | 	CVPixelBufferUnlockBaseAddress(pixelBuffer, kCVPixelBufferLock_ReadOnly); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ////////////////////////////////////////////////////////////////////////// | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | // CameraFeedMacOS - Subclass for camera feeds in macOS | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | class CameraFeedMacOS : public CameraFeed { | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | private: | 
					
						
							|  |  |  | 	AVCaptureDevice *device; | 
					
						
							|  |  |  | 	MyCaptureSession *capture_session; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	AVCaptureDevice *get_device() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | 	CameraFeedMacOS(); | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	void set_device(AVCaptureDevice *p_device); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool activate_feed(); | 
					
						
							|  |  |  | 	void deactivate_feed(); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | AVCaptureDevice *CameraFeedMacOS::get_device() const { | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	return device; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | CameraFeedMacOS::CameraFeedMacOS() { | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 	device = nullptr; | 
					
						
							|  |  |  | 	capture_session = nullptr; | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | void CameraFeedMacOS::set_device(AVCaptureDevice *p_device) { | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	device = p_device; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// get some info | 
					
						
							|  |  |  | 	NSString *device_name = p_device.localizedName; | 
					
						
							| 
									
										
										
										
											2022-01-06 11:34:10 +02:00
										 |  |  | 	name = String::utf8(device_name.UTF8String); | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	position = CameraFeed::FEED_UNSPECIFIED; | 
					
						
							|  |  |  | 	if ([p_device position] == AVCaptureDevicePositionBack) { | 
					
						
							|  |  |  | 		position = CameraFeed::FEED_BACK; | 
					
						
							|  |  |  | 	} else if ([p_device position] == AVCaptureDevicePositionFront) { | 
					
						
							|  |  |  | 		position = CameraFeed::FEED_FRONT; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | bool CameraFeedMacOS::activate_feed() { | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	if (capture_session) { | 
					
						
							| 
									
										
										
										
											2021-08-22 00:23:55 +03:00
										 |  |  | 		// Already recording! | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2021-08-22 00:23:55 +03:00
										 |  |  | 		// Start camera capture, check permission. | 
					
						
							|  |  |  | 		if (@available(macOS 10.14, *)) { | 
					
						
							|  |  |  | 			AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]; | 
					
						
							|  |  |  | 			if (status == AVAuthorizationStatusAuthorized) { | 
					
						
							|  |  |  | 				capture_session = [[MyCaptureSession alloc] initForFeed:this andDevice:device]; | 
					
						
							|  |  |  | 			} else if (status == AVAuthorizationStatusNotDetermined) { | 
					
						
							|  |  |  | 				// Request permission. | 
					
						
							|  |  |  | 				[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo | 
					
						
							|  |  |  | 										 completionHandler:^(BOOL granted) { | 
					
						
							|  |  |  | 											 if (granted) { | 
					
						
							|  |  |  | 												 capture_session = [[MyCaptureSession alloc] initForFeed:this andDevice:device]; | 
					
						
							|  |  |  | 											 } | 
					
						
							|  |  |  | 										 }]; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			capture_session = [[MyCaptureSession alloc] initForFeed:this andDevice:device]; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | void CameraFeedMacOS::deactivate_feed() { | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	// end camera capture if we have one | 
					
						
							|  |  |  | 	if (capture_session) { | 
					
						
							|  |  |  | 		[capture_session cleanup]; | 
					
						
							| 
									
										
										
										
											2021-04-29 11:47:24 +02:00
										 |  |  | 		capture_session = nullptr; | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ////////////////////////////////////////////////////////////////////////// | 
					
						
							|  |  |  | // MyDeviceNotifications - This is a little helper class gets notifications | 
					
						
							|  |  |  | // when devices are connected/disconnected | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @interface MyDeviceNotifications : NSObject { | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | 	CameraMacOS *camera_server; | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @implementation MyDeviceNotifications | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (void)devices_changed:(NSNotification *)notification { | 
					
						
							|  |  |  | 	camera_server->update_feeds(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | - (id)initForServer:(CameraMacOS *)p_server { | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	if (self = [super init]) { | 
					
						
							|  |  |  | 		camera_server = p_server; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(devices_changed:) name:AVCaptureDeviceWasConnectedNotification object:nil]; | 
					
						
							|  |  |  | 		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(devices_changed:) name:AVCaptureDeviceWasDisconnectedNotification object:nil]; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	return self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - (void)dealloc { | 
					
						
							|  |  |  | 	// remove notifications | 
					
						
							|  |  |  | 	[[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceWasConnectedNotification object:nil]; | 
					
						
							|  |  |  | 	[[NSNotificationCenter defaultCenter] removeObserver:self name:AVCaptureDeviceWasDisconnectedNotification object:nil]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MyDeviceNotifications *device_notifications = nil; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ////////////////////////////////////////////////////////////////////////// | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | // CameraMacOS - Subclass for our camera server on macOS | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | void CameraMacOS::update_feeds() { | 
					
						
							| 
									
										
										
										
											2024-02-20 05:52:00 +11:00
										 |  |  | #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101500 | 
					
						
							|  |  |  | 	AVCaptureDeviceDiscoverySession *session; | 
					
						
							|  |  |  | #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 140000 | 
					
						
							|  |  |  | 	// Avoid deprecated warning if the minimum SDK is 14.0. | 
					
						
							|  |  |  | 	session = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:[NSArray arrayWithObjects:AVCaptureDeviceTypeExternal, AVCaptureDeviceTypeBuiltInWideAngleCamera, nil] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionUnspecified]; | 
					
						
							| 
									
										
										
										
											2020-06-23 22:01:42 +03:00
										 |  |  | #else | 
					
						
							| 
									
										
										
										
											2024-02-20 05:52:00 +11:00
										 |  |  | 	session = [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:[NSArray arrayWithObjects:AVCaptureDeviceTypeExternalUnknown, AVCaptureDeviceTypeBuiltInWideAngleCamera, nil] mediaType:AVMediaTypeVideo position:AVCaptureDevicePositionUnspecified]; | 
					
						
							|  |  |  | #endif | 
					
						
							|  |  |  | 	NSArray<AVCaptureDevice *> *devices = session.devices; | 
					
						
							|  |  |  | #else | 
					
						
							|  |  |  | 	NSArray<AVCaptureDevice *> *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]; | 
					
						
							| 
									
										
										
										
											2020-06-23 22:01:42 +03:00
										 |  |  | #endif | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// remove devices that are gone.. | 
					
						
							|  |  |  | 	for (int i = feeds.size() - 1; i >= 0; i--) { | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | 		Ref<CameraFeedMacOS> feed = (Ref<CameraFeedMacOS>)feeds[i]; | 
					
						
							| 
									
										
										
										
											2024-12-18 10:37:06 +08:00
										 |  |  | 		if (feed.is_null()) { | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (![devices containsObject:feed->get_device()]) { | 
					
						
							|  |  |  | 			// remove it from our array, this will also destroy it ;) | 
					
						
							|  |  |  | 			remove_feed(feed); | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (AVCaptureDevice *device in devices) { | 
					
						
							|  |  |  | 		bool found = false; | 
					
						
							|  |  |  | 		for (int i = 0; i < feeds.size() && !found; i++) { | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | 			Ref<CameraFeedMacOS> feed = (Ref<CameraFeedMacOS>)feeds[i]; | 
					
						
							| 
									
										
										
										
											2024-12-18 10:37:06 +08:00
										 |  |  | 			if (feed.is_null()) { | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 			if (feed->get_device() == device) { | 
					
						
							|  |  |  | 				found = true; | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (!found) { | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | 			Ref<CameraFeedMacOS> newfeed; | 
					
						
							| 
									
										
										
										
											2021-06-17 16:03:09 -06:00
										 |  |  | 			newfeed.instantiate(); | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 			newfeed->set_device(device); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// assume display camera so inverse | 
					
						
							|  |  |  | 			Transform2D transform = Transform2D(-1.0, 0.0, 0.0, -1.0, 1.0, 1.0); | 
					
						
							|  |  |  | 			newfeed->set_transform(transform); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			add_feed(newfeed); | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-20 09:28:22 +03:00
										 |  |  | CameraMacOS::CameraMacOS() { | 
					
						
							| 
									
										
										
										
											2017-08-21 00:17:24 +10:00
										 |  |  | 	// Find available cameras we have at this time | 
					
						
							|  |  |  | 	update_feeds(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// should only have one of these.... | 
					
						
							|  |  |  | 	device_notifications = [[MyDeviceNotifications alloc] initForServer:this]; | 
					
						
							|  |  |  | } |