简体   繁体   English

macOS 上的内核与用户空间音频设备驱动程序

[英]kernel vs user-space audio device driver on macOS

I'm in a need to develop an audio device driver for System Audio Capture(based on Soundflower).我需要为系统音频捕获(基于 Soundflower)开发音频设备驱动程序。
But soon a problem appeared that it seems IOAudioFamily stack is being deprecated in OSX 10.10 and later.但很快就出现了一个问题,似乎 IOAudioFamily 堆栈在 OSX 10.10 及更高版本中已被弃用。
Looking through the IOAudioDevice and IOAudioEngine header files it seems that apple recommends now using the <CoreAudio/AudioServerPlugIn.h> API which runs in user-space.查看IOAudioDeviceIOAudioEngine头文件,似乎苹果现在建议使用在用户空间中运行的<CoreAudio/AudioServerPlugIn.h> API。 But I can't find lots of information on this user-space device drivers topic.但是我找不到有关此用户空间设备驱动程序主题的大量信息。 It seems that the only resource is the Apple provided sample devices from https://developer.apple.com/library/prerelease/content/samplecode/AudioDriverExamples/Introduction/Intro.html似乎唯一的资源是来自https://developer.apple.com/library/prerelease/content/samplecode/AudioDriverExamples/Introduction/Intro.html的 Apple 提供的示例设备
Looking through the examples I find that its a lot harder and more work to develop a user-space driver instead of I/O Kit kernel based.通过查看示例,我发现开发用户空间驱动程序而不是基于 I/O Kit 内核的驱动程序要困难得多,工作也多得多。
So the question arises what should motivate to develop a device driver in user-space instead of kernel space?那么问题来了,在用户空间而不是内核空间开发设备驱动程序的动机是什么?

The "SimpleAudioDriver" example is somewhat misnamed. “SimpleAudioDriver”示例的名称有些错误。 It demonstrates pretty much every feature of the API.它展示了 API 的几乎所有功能。 This is handy as a reference if you actually need to use those features.如果您确实需要使用这些功能,这可以作为参考。 It's also structured in a way that's maybe a little more complicated than necessary.它的结构也可能比必要的复杂一些。

For a virtual device, the NullAudioDriver is probably a much better base, and much, much easier to understand (single source file, if I remember correctly).对于虚拟设备,NullAudioDriver 可能是一个更好的基础,并且更容易理解(单个源文件,如果我没记错的话)。 SimpleAudioDriver is more useful for dealing with issues such as hotplugging, multiple instances of identical devices, etc. SimpleAudioDriver 在处理诸如热插拔、相同设备的多个实例等问题时更有用。

IOAudioEngine is deprecated as you say, and has been since OS X 10.10. IOAudioEngine如您所说已被弃用,并且自 OS X 10.10 以来已被弃用。 Expect it to go away eventually, so if you build your driver with it, you'll probably need to rewrite it sooner than if you create a Core Audio Server Plugin based one.预计它最终会消失,所以如果你用它构建你的驱动程序,你可能需要比创建一个基于 Core Audio Server Plugin 的驱动程序更早地重写它。

Testing and debugging audio drivers is awkward either way (due to being so time sensitive), but I'd say userspace ones are slightly less frustrating to deal with.无论哪种方式,测试和调试音频驱动程序都很尴尬(因为对时间很敏感),但我想说用户空间的驱动程序处理起来不那么令人沮丧。 You'll still want to test on a different machine than your development Mac, because if coreaudiod crashes or hangs, apps usually start locking up too, so being able to just ssh in, delete your plugin and kill coreaudiod is handy.您仍然希望在与您的开发 Mac 不同的机器上进行测试,因为如果 coreaudiod 崩溃或挂起,应用程序通常也会开始锁定,因此能够通过 ssh 进入、删除您的插件并杀死 coreaudiod 很方便。 Certainly quicker turnaround than having to reboot.当然比必须重新启动更快的周转。

(FWIW, I've shipped both kernel and userspace OS X audio drivers, and I spend a lot of time working on kexts.) (FWIW,我已经发布了内核和用户空间 OS X 音频驱动程序,我花了很多时间在 kexts 上。)

There is a great book on this subject, available free online here:有一本关于这个主题的好书,可在此处免费在线获取:

http://free-electrons.com/doc/books/ldd3.pdf http://free-electrons.com/doc/books/ldd3.pdf

See page 37 for a summary of why you might want a user-space driver, copied here for convenience:有关为什么需要用户空间驱动程序的摘要,请参见第 37 页,为方便起见,复制到此处:

The advantages of user-space drivers are:用户空间驱动程序的优点是:

  • The full C library can be linked in. The driver can perform many exotic tasks without resorting to external programs (the utility programs implementing usage policies that are usually distributed along with the driver itself).可以链接完整的 C 库。驱动程序可以执行许多奇特的任务,而无需求助于外部程序(实现使用策略的实用程序通常与驱动程序本身一起分发)。
  • The programmer can run a conventional debugger on the driver code without having to go through contortions to debug a running kernel.程序员可以在驱动程序代码上运行传统的调试器,而无需通过扭曲来调试正在运行的内核。
  • If a user-space driver hangs, you can simply kill it.如果用户空间驱动程序挂起,您可以简单地杀死它。 Problems with the driver are unlikely to hang the entire system, unless the hardware being controlled is really misbehaving.驱动程序的问题不太可能挂起整个系统,除非被控制的硬件确实行为不端。
  • User memory is swappable, unlike kernel memory.与内核内存不同,用户内存是可交换的。 An infrequently used device with a huge driver won't occupy RAM that other programs could be using, except when it is actually in use.一个不经常使用的带有巨大驱动程序的设备不会占用其他程序可能使用的 RAM,除非它在实际使用中。
  • A well-designed driver program can still, like kernel-space drivers, allow concurrent access to a device.设计良好的驱动程序仍然可以像内核空间驱动程序一样允许并发访问设备。
  • If you must write a closed-source driver, the user-space option makes it easier for you to avoid ambiguous licensing situations and problems with changing kernel interfaces.如果您必须编写闭源驱动程序,用户空间选项可以让您更轻松地避免不明确的许可情况和更改内核接口的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM