@livedesk/client 0.1.119 → 0.1.121

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -24,4 +24,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
24
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
25
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
26
  SOFTWARE.
27
-
@@ -0,0 +1,213 @@
1
+ import AudioToolbox
2
+ import AVFAudio
3
+ import CoreGraphics
4
+ import CoreMedia
5
+ import Foundation
6
+ import ScreenCaptureKit
7
+
8
+ @available(macOS 13.0, *)
9
+ final class LiveDeskSystemAudioCapture: NSObject, SCStreamOutput, SCStreamDelegate {
10
+ private let sampleRate: Int
11
+ private let channelCount: Int
12
+ private let queue = DispatchQueue(label: "dev.livedesk.screencapturekit.audio", qos: .userInteractive)
13
+ private let writeLock = NSLock()
14
+ private var stream: SCStream?
15
+ private var audioFrameCount = 0
16
+ private var loggedFormat = false
17
+
18
+ init(sampleRate: Int, channelCount: Int) {
19
+ self.sampleRate = max(8_000, min(192_000, sampleRate))
20
+ self.channelCount = max(1, min(2, channelCount))
21
+ super.init()
22
+ }
23
+
24
+ func start() async throws {
25
+ let content = try await SCShareableContent.excludingDesktopWindows(false, onScreenWindowsOnly: true)
26
+ guard !content.displays.isEmpty else {
27
+ throw LiveDeskSystemAudioError.noDisplays
28
+ }
29
+
30
+ let mainDisplayID = CGMainDisplayID()
31
+ let display = content.displays.sorted { left, right in
32
+ let leftIsMain = left.displayID == mainDisplayID
33
+ let rightIsMain = right.displayID == mainDisplayID
34
+ if leftIsMain != rightIsMain {
35
+ return leftIsMain
36
+ }
37
+ return left.displayID < right.displayID
38
+ }[0]
39
+
40
+ let configuration = SCStreamConfiguration()
41
+ configuration.width = 2
42
+ configuration.height = 2
43
+ configuration.minimumFrameInterval = CMTime(value: 1, timescale: 1)
44
+ configuration.queueDepth = 8
45
+ configuration.capturesAudio = true
46
+ configuration.sampleRate = sampleRate
47
+ configuration.channelCount = channelCount
48
+ configuration.excludesCurrentProcessAudio = true
49
+
50
+ let stream = SCStream(filter: SCContentFilter(display: display, excludingWindows: []), configuration: configuration, delegate: self)
51
+ try stream.addStreamOutput(self, type: .audio, sampleHandlerQueue: queue)
52
+ self.stream = stream
53
+ try await stream.startCapture()
54
+ fputs("LiveDeskSCKAudio started displayID=\(display.displayID) sampleRate=\(sampleRate) channels=\(channelCount)\n", stderr)
55
+
56
+ while !Task.isCancelled {
57
+ try await Task.sleep(nanoseconds: 1_000_000_000)
58
+ }
59
+ }
60
+
61
+ func stream(_ stream: SCStream, didOutputSampleBuffer sampleBuffer: CMSampleBuffer, of type: SCStreamOutputType) {
62
+ guard type == .audio, sampleBuffer.isValid, CMSampleBufferDataIsReady(sampleBuffer) else {
63
+ return
64
+ }
65
+ writePcm(sampleBuffer)
66
+ }
67
+
68
+ func stream(_ stream: SCStream, didStopWithError error: Error) {
69
+ fputs("LiveDeskSCKAudio stream stopped: \(error)\n", stderr)
70
+ exit(3)
71
+ }
72
+
73
+ private func writePcm(_ sampleBuffer: CMSampleBuffer) {
74
+ guard let description = sampleBuffer.formatDescription?.audioStreamBasicDescription else {
75
+ return
76
+ }
77
+
78
+ let channels = Int(description.mChannelsPerFrame)
79
+ let frames = Int(CMSampleBufferGetNumSamples(sampleBuffer))
80
+ let isFloat32 = description.mFormatID == kAudioFormatLinearPCM
81
+ && description.mBitsPerChannel == 32
82
+ && (description.mFormatFlags & kAudioFormatFlagIsFloat) != 0
83
+ let isInterleaved = (description.mFormatFlags & kAudioFormatFlagIsNonInterleaved) == 0
84
+ guard isFloat32, frames > 0, channels > 0, channels <= 8 else {
85
+ if !loggedFormat {
86
+ loggedFormat = true
87
+ fputs("LiveDeskSCKAudio unsupported PCM format id=\(description.mFormatID) flags=\(description.mFormatFlags) bits=\(description.mBitsPerChannel) channels=\(channels)\n", stderr)
88
+ }
89
+ return
90
+ }
91
+
92
+ do {
93
+ try sampleBuffer.withAudioBufferList { audioBufferList, _ in
94
+ let bytesPerSample = MemoryLayout<Float>.size
95
+ let bytesPerChannel = frames * bytesPerSample
96
+ let outputBytes = bytesPerChannel * channels
97
+ guard audioBufferList.count > 0, outputBytes > 0 else {
98
+ return
99
+ }
100
+
101
+ let output: Data
102
+ if isInterleaved {
103
+ let sourceBuffer = audioBufferList[0]
104
+ guard let source = sourceBuffer.mData, Int(sourceBuffer.mDataByteSize) >= outputBytes else {
105
+ return
106
+ }
107
+ output = Data(bytes: source, count: outputBytes)
108
+ } else {
109
+ guard audioBufferList.count >= channels else {
110
+ return
111
+ }
112
+ var interleaved = Data(count: outputBytes)
113
+ var copied = true
114
+ interleaved.withUnsafeMutableBytes { rawBuffer in
115
+ guard let destination = rawBuffer.baseAddress?.assumingMemoryBound(to: Float.self) else {
116
+ copied = false
117
+ return
118
+ }
119
+ for channelIndex in 0..<channels {
120
+ let sourceBuffer = audioBufferList[channelIndex]
121
+ guard let source = sourceBuffer.mData,
122
+ Int(sourceBuffer.mDataByteSize) >= bytesPerChannel else {
123
+ copied = false
124
+ return
125
+ }
126
+ let sourceSamples = source.assumingMemoryBound(to: Float.self)
127
+ for frameIndex in 0..<frames {
128
+ destination[frameIndex * channels + channelIndex] = sourceSamples[frameIndex]
129
+ }
130
+ }
131
+ }
132
+ guard copied else {
133
+ return
134
+ }
135
+ output = interleaved
136
+ }
137
+
138
+ if !loggedFormat {
139
+ loggedFormat = true
140
+ fputs("LiveDeskSCKAudio format sampleRate=\(Int(description.mSampleRate.rounded())) channels=\(channels) frames=\(frames) interleaved=\(isInterleaved ? 1 : 0)\n", stderr)
141
+ }
142
+ writePacket(output, sampleRate: Int(description.mSampleRate.rounded()), channels: channels)
143
+ audioFrameCount += 1
144
+ if audioFrameCount == 1 || audioFrameCount % 250 == 0 {
145
+ fputs("LiveDeskSCKAudio frames=\(audioFrameCount) bytes=\(output.count)\n", stderr)
146
+ }
147
+ }
148
+ } catch {
149
+ fputs("LiveDeskSCKAudio audio buffer error: \(error)\n", stderr)
150
+ }
151
+ }
152
+
153
+ private func writePacket(_ pcm: Data, sampleRate: Int, channels: Int) {
154
+ guard sampleRate > 0, channels > 0, pcm.count <= Int(UInt32.max) else {
155
+ return
156
+ }
157
+ var packet = Data()
158
+ packet.reserveCapacity(16 + pcm.count)
159
+ packet.append(contentsOf: [0x4c, 0x44, 0x41, 0x55])
160
+ appendLittleEndian(UInt16(1), to: &packet)
161
+ appendLittleEndian(UInt16(min(channels, Int(UInt16.max))), to: &packet)
162
+ appendLittleEndian(UInt32(min(sampleRate, Int(UInt32.max))), to: &packet)
163
+ appendLittleEndian(UInt32(pcm.count), to: &packet)
164
+ packet.append(pcm)
165
+
166
+ writeLock.lock()
167
+ FileHandle.standardOutput.write(packet)
168
+ writeLock.unlock()
169
+ }
170
+
171
+ private func appendLittleEndian<T: FixedWidthInteger>(_ value: T, to data: inout Data) {
172
+ var littleEndian = value.littleEndian
173
+ withUnsafeBytes(of: &littleEndian) { bytes in
174
+ data.append(contentsOf: bytes)
175
+ }
176
+ }
177
+ }
178
+
179
+ enum LiveDeskSystemAudioError: Error {
180
+ case noDisplays
181
+ }
182
+
183
+ func intArg(_ name: String, default defaultValue: Int) -> Int {
184
+ let args = CommandLine.arguments
185
+ guard let index = args.firstIndex(of: name), index + 1 < args.count else {
186
+ return defaultValue
187
+ }
188
+ return Int(args[index + 1]) ?? defaultValue
189
+ }
190
+
191
+ @main
192
+ struct LiveDeskSystemAudioMain {
193
+ static func main() async {
194
+ guard #available(macOS 13.0, *) else {
195
+ fputs("LiveDeskSCKAudio requires macOS 13 or later.\n", stderr)
196
+ exit(2)
197
+ }
198
+ guard CGPreflightScreenCaptureAccess() else {
199
+ fputs("LiveDeskSCKAudio Screen & System Audio Recording permission is not granted.\n", stderr)
200
+ exit(10)
201
+ }
202
+
203
+ let sampleRate = intArg("--sample-rate", default: 48_000)
204
+ let channels = intArg("--channels", default: 2)
205
+ do {
206
+ let capture = LiveDeskSystemAudioCapture(sampleRate: sampleRate, channelCount: channels)
207
+ try await capture.start()
208
+ } catch {
209
+ fputs("LiveDeskSCKAudio failed: \(error)\n", stderr)
210
+ exit(1)
211
+ }
212
+ }
213
+ }
@@ -0,0 +1,213 @@
1
+ import AudioToolbox
2
+ import AVFAudio
3
+ import CoreGraphics
4
+ import CoreMedia
5
+ import Foundation
6
+ import ScreenCaptureKit
7
+
8
+ @available(macOS 13.0, *)
9
+ final class LiveDeskSystemAudioCapture: NSObject, SCStreamOutput, SCStreamDelegate {
10
+ private let sampleRate: Int
11
+ private let channelCount: Int
12
+ private let queue = DispatchQueue(label: "dev.livedesk.screencapturekit.audio", qos: .userInteractive)
13
+ private let writeLock = NSLock()
14
+ private var stream: SCStream?
15
+ private var audioFrameCount = 0
16
+ private var loggedFormat = false
17
+
18
+ init(sampleRate: Int, channelCount: Int) {
19
+ self.sampleRate = max(8_000, min(192_000, sampleRate))
20
+ self.channelCount = max(1, min(2, channelCount))
21
+ super.init()
22
+ }
23
+
24
+ func start() async throws {
25
+ let content = try await SCShareableContent.excludingDesktopWindows(false, onScreenWindowsOnly: true)
26
+ guard !content.displays.isEmpty else {
27
+ throw LiveDeskSystemAudioError.noDisplays
28
+ }
29
+
30
+ let mainDisplayID = CGMainDisplayID()
31
+ let display = content.displays.sorted { left, right in
32
+ let leftIsMain = left.displayID == mainDisplayID
33
+ let rightIsMain = right.displayID == mainDisplayID
34
+ if leftIsMain != rightIsMain {
35
+ return leftIsMain
36
+ }
37
+ return left.displayID < right.displayID
38
+ }[0]
39
+
40
+ let configuration = SCStreamConfiguration()
41
+ configuration.width = 2
42
+ configuration.height = 2
43
+ configuration.minimumFrameInterval = CMTime(value: 1, timescale: 1)
44
+ configuration.queueDepth = 8
45
+ configuration.capturesAudio = true
46
+ configuration.sampleRate = sampleRate
47
+ configuration.channelCount = channelCount
48
+ configuration.excludesCurrentProcessAudio = true
49
+
50
+ let stream = SCStream(filter: SCContentFilter(display: display, excludingWindows: []), configuration: configuration, delegate: self)
51
+ try stream.addStreamOutput(self, type: .audio, sampleHandlerQueue: queue)
52
+ self.stream = stream
53
+ try await stream.startCapture()
54
+ fputs("LiveDeskSCKAudio started displayID=\(display.displayID) sampleRate=\(sampleRate) channels=\(channelCount)\n", stderr)
55
+
56
+ while !Task.isCancelled {
57
+ try await Task.sleep(nanoseconds: 1_000_000_000)
58
+ }
59
+ }
60
+
61
+ func stream(_ stream: SCStream, didOutputSampleBuffer sampleBuffer: CMSampleBuffer, of type: SCStreamOutputType) {
62
+ guard type == .audio, sampleBuffer.isValid, CMSampleBufferDataIsReady(sampleBuffer) else {
63
+ return
64
+ }
65
+ writePcm(sampleBuffer)
66
+ }
67
+
68
+ func stream(_ stream: SCStream, didStopWithError error: Error) {
69
+ fputs("LiveDeskSCKAudio stream stopped: \(error)\n", stderr)
70
+ exit(3)
71
+ }
72
+
73
+ private func writePcm(_ sampleBuffer: CMSampleBuffer) {
74
+ guard let description = sampleBuffer.formatDescription?.audioStreamBasicDescription else {
75
+ return
76
+ }
77
+
78
+ let channels = Int(description.mChannelsPerFrame)
79
+ let frames = Int(CMSampleBufferGetNumSamples(sampleBuffer))
80
+ let isFloat32 = description.mFormatID == kAudioFormatLinearPCM
81
+ && description.mBitsPerChannel == 32
82
+ && (description.mFormatFlags & kAudioFormatFlagIsFloat) != 0
83
+ let isInterleaved = (description.mFormatFlags & kAudioFormatFlagIsNonInterleaved) == 0
84
+ guard isFloat32, frames > 0, channels > 0, channels <= 8 else {
85
+ if !loggedFormat {
86
+ loggedFormat = true
87
+ fputs("LiveDeskSCKAudio unsupported PCM format id=\(description.mFormatID) flags=\(description.mFormatFlags) bits=\(description.mBitsPerChannel) channels=\(channels)\n", stderr)
88
+ }
89
+ return
90
+ }
91
+
92
+ do {
93
+ try sampleBuffer.withAudioBufferList { audioBufferList, _ in
94
+ let bytesPerSample = MemoryLayout<Float>.size
95
+ let bytesPerChannel = frames * bytesPerSample
96
+ let outputBytes = bytesPerChannel * channels
97
+ guard audioBufferList.count > 0, outputBytes > 0 else {
98
+ return
99
+ }
100
+
101
+ let output: Data
102
+ if isInterleaved {
103
+ let sourceBuffer = audioBufferList[0]
104
+ guard let source = sourceBuffer.mData, Int(sourceBuffer.mDataByteSize) >= outputBytes else {
105
+ return
106
+ }
107
+ output = Data(bytes: source, count: outputBytes)
108
+ } else {
109
+ guard audioBufferList.count >= channels else {
110
+ return
111
+ }
112
+ var interleaved = Data(count: outputBytes)
113
+ var copied = true
114
+ interleaved.withUnsafeMutableBytes { rawBuffer in
115
+ guard let destination = rawBuffer.baseAddress?.assumingMemoryBound(to: Float.self) else {
116
+ copied = false
117
+ return
118
+ }
119
+ for channelIndex in 0..<channels {
120
+ let sourceBuffer = audioBufferList[channelIndex]
121
+ guard let source = sourceBuffer.mData,
122
+ Int(sourceBuffer.mDataByteSize) >= bytesPerChannel else {
123
+ copied = false
124
+ return
125
+ }
126
+ let sourceSamples = source.assumingMemoryBound(to: Float.self)
127
+ for frameIndex in 0..<frames {
128
+ destination[frameIndex * channels + channelIndex] = sourceSamples[frameIndex]
129
+ }
130
+ }
131
+ }
132
+ guard copied else {
133
+ return
134
+ }
135
+ output = interleaved
136
+ }
137
+
138
+ if !loggedFormat {
139
+ loggedFormat = true
140
+ fputs("LiveDeskSCKAudio format sampleRate=\(Int(description.mSampleRate.rounded())) channels=\(channels) frames=\(frames) interleaved=\(isInterleaved ? 1 : 0)\n", stderr)
141
+ }
142
+ writePacket(output, sampleRate: Int(description.mSampleRate.rounded()), channels: channels)
143
+ audioFrameCount += 1
144
+ if audioFrameCount == 1 || audioFrameCount % 250 == 0 {
145
+ fputs("LiveDeskSCKAudio frames=\(audioFrameCount) bytes=\(output.count)\n", stderr)
146
+ }
147
+ }
148
+ } catch {
149
+ fputs("LiveDeskSCKAudio audio buffer error: \(error)\n", stderr)
150
+ }
151
+ }
152
+
153
+ private func writePacket(_ pcm: Data, sampleRate: Int, channels: Int) {
154
+ guard sampleRate > 0, channels > 0, pcm.count <= Int(UInt32.max) else {
155
+ return
156
+ }
157
+ var packet = Data()
158
+ packet.reserveCapacity(16 + pcm.count)
159
+ packet.append(contentsOf: [0x4c, 0x44, 0x41, 0x55])
160
+ appendLittleEndian(UInt16(1), to: &packet)
161
+ appendLittleEndian(UInt16(min(channels, Int(UInt16.max))), to: &packet)
162
+ appendLittleEndian(UInt32(min(sampleRate, Int(UInt32.max))), to: &packet)
163
+ appendLittleEndian(UInt32(pcm.count), to: &packet)
164
+ packet.append(pcm)
165
+
166
+ writeLock.lock()
167
+ FileHandle.standardOutput.write(packet)
168
+ writeLock.unlock()
169
+ }
170
+
171
+ private func appendLittleEndian<T: FixedWidthInteger>(_ value: T, to data: inout Data) {
172
+ var littleEndian = value.littleEndian
173
+ withUnsafeBytes(of: &littleEndian) { bytes in
174
+ data.append(contentsOf: bytes)
175
+ }
176
+ }
177
+ }
178
+
179
+ enum LiveDeskSystemAudioError: Error {
180
+ case noDisplays
181
+ }
182
+
183
+ func intArg(_ name: String, default defaultValue: Int) -> Int {
184
+ let args = CommandLine.arguments
185
+ guard let index = args.firstIndex(of: name), index + 1 < args.count else {
186
+ return defaultValue
187
+ }
188
+ return Int(args[index + 1]) ?? defaultValue
189
+ }
190
+
191
+ @main
192
+ struct LiveDeskSystemAudioMain {
193
+ static func main() async {
194
+ guard #available(macOS 13.0, *) else {
195
+ fputs("LiveDeskSCKAudio requires macOS 13 or later.\n", stderr)
196
+ exit(2)
197
+ }
198
+ guard CGPreflightScreenCaptureAccess() else {
199
+ fputs("LiveDeskSCKAudio Screen & System Audio Recording permission is not granted.\n", stderr)
200
+ exit(10)
201
+ }
202
+
203
+ let sampleRate = intArg("--sample-rate", default: 48_000)
204
+ let channels = intArg("--channels", default: 2)
205
+ do {
206
+ let capture = LiveDeskSystemAudioCapture(sampleRate: sampleRate, channelCount: channels)
207
+ try await capture.start()
208
+ } catch {
209
+ fputs("LiveDeskSCKAudio failed: \(error)\n", stderr)
210
+ exit(1)
211
+ }
212
+ }
213
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livedesk/client",
3
- "version": "0.1.119",
3
+ "version": "0.1.121",
4
4
  "description": "LiveDesk local remote client",
5
5
  "type": "module",
6
6
  "bin": {