@livedesk/client 0.1.118 → 0.1.120

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.
@@ -0,0 +1,27 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { dirname, join, resolve } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ const __dirname = dirname(fileURLToPath(import.meta.url));
6
+ const packageRoot = resolve(__dirname, '..');
7
+
8
+ export function readClientPackageVersion() {
9
+ try {
10
+ const packageInfo = JSON.parse(readFileSync(join(packageRoot, 'package.json'), 'utf8'));
11
+ return String(packageInfo.version || '0.0.0');
12
+ } catch {
13
+ return '0.0.0';
14
+ }
15
+ }
16
+
17
+ export function formatClientVersionBanner(env = process.env) {
18
+ const clientPackage = `@livedesk/client@${readClientPackageVersion()}`;
19
+ const launcherName = String(env.LIVEDESK_NPM_LAUNCHER_NAME || '').trim();
20
+ const launcherVersion = String(env.LIVEDESK_NPM_LAUNCHER_VERSION || '').trim();
21
+
22
+ if (launcherName && launcherVersion) {
23
+ return `[LiveDesk Client] npm packages: ${launcherName}@${launcherVersion} -> ${clientPackage}`;
24
+ }
25
+
26
+ return `[LiveDesk Client] npm package: ${clientPackage}`;
27
+ }
@@ -8,6 +8,7 @@ import { createServer } from 'node:http';
8
8
  import { createRequire } from 'node:module';
9
9
  import net from 'node:net';
10
10
  import os from 'node:os';
11
+ import { formatClientVersionBanner, readClientPackageVersion } from './client-version.js';
11
12
 
12
13
  const __dirname = dirname(fileURLToPath(import.meta.url));
13
14
  const require = createRequire(import.meta.url);
@@ -74,12 +75,7 @@ Enable Windows auto-start from the connection page when this client signs in.
74
75
  }
75
76
 
76
77
  function readPackageVersion() {
77
- try {
78
- const packageInfo = JSON.parse(readFileSync(join(packageRoot, 'package.json'), 'utf8'));
79
- return packageInfo.version || '0.0.0';
80
- } catch {
81
- return '0.0.0';
82
- }
78
+ return readClientPackageVersion();
83
79
  }
84
80
 
85
81
  function isTruthy(value) {
@@ -2727,6 +2723,8 @@ async function main() {
2727
2723
  return;
2728
2724
  }
2729
2725
 
2726
+ console.log(formatClientVersionBanner());
2727
+
2730
2728
  let connectionPage = null;
2731
2729
  while (true) {
2732
2730
  const prepared = await prepareLoginConnection(parsed, connectionPage);
@@ -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.118",
3
+ "version": "0.1.120",
4
4
  "description": "LiveDesk local remote client",
5
5
  "type": "module",
6
6
  "bin": {
@@ -16,7 +16,8 @@
16
16
  "THIRD_PARTY_NOTICES.md"
17
17
  ],
18
18
  "scripts": {
19
- "check": "node --check bin/livedesk-client.js && node --check bin/livedesk-client-node.js && node --check bin/livedesk-client-fast.js",
19
+ "check": "node --check bin/client-version.js && node --check bin/livedesk-client.js && node --check bin/livedesk-client-node.js && node --check bin/livedesk-client-fast.js",
20
+ "test:version": "node --test tests/client-version.test.mjs",
20
21
  "pack:dry": "npm pack --dry-run"
21
22
  },
22
23
  "keywords": [