OBS这个软件,在26.1的时候引入了 obs-mac-virtualcam 这个插件,实现了虚拟摄像头的功能,但是这个原理是什么,有没有办法做一个自己的虚拟摄像头程序。
调研过程
- coremediaio-dal-minimal-example
这个项目,下载后,直接运行,按照他的readme把生成的plguin放到文件夹/Library/CoreMediaIO/Plug-Ins/DAL/中后,打开quick time,选择CMIOMinimalSample Device摄像头,就能拿到这样的画面。
但是问题来了,如何让虚拟摄像头播放我希望的画面呢?首先,从原理上来分析,这个驱动是通过quick time 程序加载的,我们需要给他发送数据,必然是进程间通讯了,首先尝试的是共享内存,已经不记得为啥失败了,然后是XPC,也失败了,但是这次的失败我现在强烈怀疑是因为quick time的问题,因为即使是后面NSPort的方法,quick time也是不行的,zoom没问题,腾讯会议也没问题。当时失败了以后,我就去看了一下obs的实现方式,发现他就是用的NSPort。 - obs-mac-virtualcam
这个项目是为obs做了一个虚拟摄像头,obs是做直播推流,最新的release已经把这个插件集成到程序中了,回到插件的原理中来看,其实这个插件可以直接理解成coremediaio-dal-minimal-example 加上了一个NSPort的client端,或者说,coremediaio-dal-minimal-example的作者就是在这个基础上删除了NSPort的通讯端代码。好了,一个想法就能出现了,我们只需要实现一个NSPort的服务端就能往obs-mac-virtualcam上发送数据了。
NSPort服务端开发
#define MACH_SERVICE_NAME "com.johnboiles.obs-mac-virtualcam.server"
typedef enum {
//! Initial connect message sent from the client to the server to initate a connection
MachMsgIdConnect = 1,
//! Message containing data for a frame
MachMsgIdFrame = 2,
//! Indicates the server is going to stop sending frames
MachMsgIdStop = 3,
} MachMsgId;
@interface Server : NSObject
- (void)run;
- (void)sendFrameWithSize:(NSSize)size timestamp:(uint64_t)timestamp fpsNumerator:(uint32_t)fpsNumerator fpsDenominator:(uint32_t)fpsDenominator frameBytes:(uint8_t *)frameBytes;
@end
#import "Server.h"
@interface Server () <NSPortDelegate>
@property NSPort *port;
@property NSMutableSet *clientPorts;
@end
@implementation Server
- (id)init {
if (self =