@appium/media-proxy 0.0.1 → 1.1.0
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.
- package/CHANGELOG.md +33 -0
- package/LICENSE +201 -0
- package/README.md +183 -2
- package/THIRD_PARTY_NOTICES.md +29 -0
- package/binding.gyp +90 -0
- package/lib/src/background.d.ts +5 -0
- package/lib/src/background.d.ts.map +1 -0
- package/lib/src/background.js +16 -0
- package/lib/src/background.js.map +1 -0
- package/lib/src/capabilities.d.ts +9 -0
- package/lib/src/capabilities.d.ts.map +1 -0
- package/lib/src/capabilities.js +11 -0
- package/lib/src/capabilities.js.map +1 -0
- package/lib/src/errors.d.ts +26 -0
- package/lib/src/errors.d.ts.map +1 -0
- package/lib/src/errors.js +40 -0
- package/lib/src/errors.js.map +1 -0
- package/lib/src/framing/apst.d.ts +43 -0
- package/lib/src/framing/apst.d.ts.map +1 -0
- package/lib/src/framing/apst.js +126 -0
- package/lib/src/framing/apst.js.map +1 -0
- package/lib/src/framing/mjpeg-http.d.ts +30 -0
- package/lib/src/framing/mjpeg-http.d.ts.map +1 -0
- package/lib/src/framing/mjpeg-http.js +148 -0
- package/lib/src/framing/mjpeg-http.js.map +1 -0
- package/lib/src/framing/mjpeg-multipart.d.ts +11 -0
- package/lib/src/framing/mjpeg-multipart.d.ts.map +1 -0
- package/lib/src/framing/mjpeg-multipart.js +26 -0
- package/lib/src/framing/mjpeg-multipart.js.map +1 -0
- package/lib/src/index.d.ts +10 -0
- package/lib/src/index.d.ts.map +1 -0
- package/lib/src/index.js +7 -0
- package/lib/src/index.js.map +1 -0
- package/lib/src/native.d.ts +12 -0
- package/lib/src/native.d.ts.map +1 -0
- package/lib/src/native.js +36 -0
- package/lib/src/native.js.map +1 -0
- package/lib/src/pkg-root.d.ts +6 -0
- package/lib/src/pkg-root.d.ts.map +1 -0
- package/lib/src/pkg-root.js +12 -0
- package/lib/src/pkg-root.js.map +1 -0
- package/lib/src/queue.d.ts +34 -0
- package/lib/src/queue.d.ts.map +1 -0
- package/lib/src/queue.js +123 -0
- package/lib/src/queue.js.map +1 -0
- package/lib/src/transcoder.d.ts +81 -0
- package/lib/src/transcoder.d.ts.map +1 -0
- package/lib/src/transcoder.js +422 -0
- package/lib/src/transcoder.js.map +1 -0
- package/lib/src/types.d.ts +147 -0
- package/lib/src/types.d.ts.map +1 -0
- package/lib/src/types.js +2 -0
- package/lib/src/types.js.map +1 -0
- package/package.json +53 -4
- package/prebuilds/darwin-arm64/@appium+media-proxy.node +0 -0
- package/prebuilds/darwin-arm64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
- package/prebuilds/darwin-arm64/ffmpeg-build-info.txt +3 -0
- package/prebuilds/darwin-arm64/libavcodec-appium.62.dylib +0 -0
- package/prebuilds/darwin-arm64/libavutil-appium.60.dylib +0 -0
- package/prebuilds/darwin-arm64/libswscale-appium.9.dylib +0 -0
- package/prebuilds/darwin-x64/@appium+media-proxy.node +0 -0
- package/prebuilds/darwin-x64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
- package/prebuilds/darwin-x64/ffmpeg-build-info.txt +3 -0
- package/prebuilds/darwin-x64/libavcodec-appium.62.dylib +0 -0
- package/prebuilds/darwin-x64/libavutil-appium.60.dylib +0 -0
- package/prebuilds/darwin-x64/libswscale-appium.9.dylib +0 -0
- package/prebuilds/linux-arm64/@appium+media-proxy.node +0 -0
- package/prebuilds/linux-arm64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
- package/prebuilds/linux-arm64/ffmpeg-build-info.txt +3 -0
- package/prebuilds/linux-arm64/libavcodec-appium.so.62 +0 -0
- package/prebuilds/linux-arm64/libavutil-appium.so.60 +0 -0
- package/prebuilds/linux-arm64/libswscale-appium.so.9 +0 -0
- package/prebuilds/linux-x64/@appium+media-proxy.node +0 -0
- package/prebuilds/linux-x64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
- package/prebuilds/linux-x64/ffmpeg-build-info.txt +3 -0
- package/prebuilds/linux-x64/libavcodec-appium.so.62 +0 -0
- package/prebuilds/linux-x64/libavutil-appium.so.60 +0 -0
- package/prebuilds/linux-x64/libswscale-appium.so.9 +0 -0
- package/prebuilds/win32-arm64/@appium+media-proxy.node +0 -0
- package/prebuilds/win32-arm64/avcodec-appium-62.dll +0 -0
- package/prebuilds/win32-arm64/avutil-appium-60.dll +0 -0
- package/prebuilds/win32-arm64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
- package/prebuilds/win32-arm64/ffmpeg-build-info.txt +3 -0
- package/prebuilds/win32-arm64/swscale-appium-9.dll +0 -0
- package/prebuilds/win32-x64/@appium+media-proxy.node +0 -0
- package/prebuilds/win32-x64/avcodec-appium-62.dll +0 -0
- package/prebuilds/win32-x64/avutil-appium-60.dll +0 -0
- package/prebuilds/win32-x64/ffmpeg-LICENSE-LGPL-2.1.txt +502 -0
- package/prebuilds/win32-x64/ffmpeg-build-info.txt +3 -0
- package/prebuilds/win32-x64/swscale-appium-9.dll +0 -0
- package/scripts/install.mjs +29 -0
- package/src/background.ts +15 -0
- package/src/capabilities.ts +12 -0
- package/src/errors.ts +45 -0
- package/src/framing/apst.ts +158 -0
- package/src/framing/mjpeg-http.ts +186 -0
- package/src/framing/mjpeg-multipart.ts +38 -0
- package/src/index.ts +43 -0
- package/src/native/addon.cc +402 -0
- package/src/native/audio_pipeline.cc +426 -0
- package/src/native/audio_pipeline.h +63 -0
- package/src/native/ffmpeg_util.h +37 -0
- package/src/native/h264_encoder.cc +133 -0
- package/src/native/h264_encoder.h +48 -0
- package/src/native/mediafoundation_encoder.cc +442 -0
- package/src/native/mediafoundation_encoder.h +14 -0
- package/src/native/napi_util.h +20 -0
- package/src/native/openh264_encoder.cc +285 -0
- package/src/native/openh264_encoder.h +14 -0
- package/src/native/pipeline.cc +555 -0
- package/src/native/pipeline.h +108 -0
- package/src/native/vendor/openh264/LICENSE +23 -0
- package/src/native/vendor/openh264/codec_api.h +592 -0
- package/src/native/vendor/openh264/codec_app_def.h +820 -0
- package/src/native/vendor/openh264/codec_def.h +216 -0
- package/src/native/vendor/openh264/codec_ver.h +15 -0
- package/src/native/videotoolbox_encoder.cc +314 -0
- package/src/native/videotoolbox_encoder.h +13 -0
- package/src/native.ts +51 -0
- package/src/pkg-root.ts +14 -0
- package/src/queue.ts +133 -0
- package/src/transcoder.ts +480 -0
- package/src/types.ts +158 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import {MediaProxyProtocolError} from '../errors.js';
|
|
2
|
+
import type {JpegFrame, VideoAccessUnit} from '../types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* APST wire framing, identical to the Android settings app's streaming protocol
|
|
6
|
+
* (`io.appium.settings.streaming.StreamProtocol`), so one client can consume both platforms.
|
|
7
|
+
* 28-byte big-endian header: magic "APST", version, track, flags, reserved, u64 sequence,
|
|
8
|
+
* u64 timestampMicros, u32 payloadLength; then the payload.
|
|
9
|
+
*/
|
|
10
|
+
export enum ApstTrack {
|
|
11
|
+
Jpeg = 0,
|
|
12
|
+
Video = 1,
|
|
13
|
+
Audio = 2,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const APST_VERSION = 1;
|
|
17
|
+
export const APST_HEADER_SIZE = 28;
|
|
18
|
+
const MAGIC = Buffer.from('APST', 'ascii');
|
|
19
|
+
const FLAG_CONFIG = 0x01;
|
|
20
|
+
const FLAG_KEYFRAME = 0x02;
|
|
21
|
+
const FLAG_EOS = 0x04;
|
|
22
|
+
/** Sanity bound on a single payload; anything larger means the stream is desynchronized. */
|
|
23
|
+
const MAX_PAYLOAD = 64 * 1024 * 1024;
|
|
24
|
+
|
|
25
|
+
export interface ApstFrame {
|
|
26
|
+
track: ApstTrack;
|
|
27
|
+
sequence: number;
|
|
28
|
+
timestampMicros: number;
|
|
29
|
+
data: Buffer;
|
|
30
|
+
isConfig: boolean;
|
|
31
|
+
isKeyFrame: boolean;
|
|
32
|
+
isEndOfStream: boolean;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function writeHeader(
|
|
36
|
+
track: ApstTrack,
|
|
37
|
+
flags: number,
|
|
38
|
+
sequence: number,
|
|
39
|
+
timestampMicros: number,
|
|
40
|
+
payloadLength: number,
|
|
41
|
+
): Buffer {
|
|
42
|
+
const header = Buffer.alloc(APST_HEADER_SIZE);
|
|
43
|
+
MAGIC.copy(header, 0);
|
|
44
|
+
header.writeUInt8(APST_VERSION, 4);
|
|
45
|
+
header.writeUInt8(track, 5);
|
|
46
|
+
header.writeUInt8(flags, 6);
|
|
47
|
+
header.writeBigUInt64BE(BigInt(sequence), 8);
|
|
48
|
+
header.writeBigUInt64BE(BigInt(timestampMicros), 16);
|
|
49
|
+
header.writeUInt32BE(payloadLength, 24);
|
|
50
|
+
return header;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Returns `[header, payload]` so callers can `socket.write` both without concatenating. */
|
|
54
|
+
export function encodeApstFrame(unit: VideoAccessUnit | JpegFrame): [Buffer, Buffer] {
|
|
55
|
+
const isVideo = 'track' in unit;
|
|
56
|
+
const flags = isVideo && unit.isKeyFrame ? FLAG_KEYFRAME : 0;
|
|
57
|
+
let track = ApstTrack.Jpeg;
|
|
58
|
+
if (isVideo) {
|
|
59
|
+
track = unit.track === 'audio' ? ApstTrack.Audio : ApstTrack.Video;
|
|
60
|
+
}
|
|
61
|
+
return [writeHeader(track, flags, unit.sequence, unit.timestampMicros, unit.data.length), unit.data];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Single concatenated buffer variant of {@link encodeApstFrame}. */
|
|
65
|
+
export function encodeApstFrameBuffer(unit: VideoAccessUnit | JpegFrame): Buffer {
|
|
66
|
+
return Buffer.concat(encodeApstFrame(unit));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function encodeApstEndOfStream(track: ApstTrack, sequence: number, timestampMicros: number): Buffer {
|
|
70
|
+
return writeHeader(track, FLAG_EOS, sequence, timestampMicros, 0);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface PendingHeader {
|
|
74
|
+
track: ApstTrack;
|
|
75
|
+
flags: number;
|
|
76
|
+
sequence: number;
|
|
77
|
+
timestampMicros: number;
|
|
78
|
+
payloadLength: number;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Incremental parser; carries a trailing partial frame across `push` calls. Chunks are queued and
|
|
83
|
+
* the header is parsed once, so a large frame arriving in small chunks is copied once, not per chunk.
|
|
84
|
+
*/
|
|
85
|
+
export class ApstParser {
|
|
86
|
+
private chunks: Buffer[] = [];
|
|
87
|
+
private size = 0;
|
|
88
|
+
private header: PendingHeader | undefined;
|
|
89
|
+
|
|
90
|
+
/** @throws {MediaProxyProtocolError} on bad magic/version or an implausible payload length. */
|
|
91
|
+
*push(chunk: Buffer): Generator<ApstFrame> {
|
|
92
|
+
if (chunk.length > 0) {
|
|
93
|
+
this.chunks.push(chunk);
|
|
94
|
+
this.size += chunk.length;
|
|
95
|
+
}
|
|
96
|
+
for (;;) {
|
|
97
|
+
if (!this.header) {
|
|
98
|
+
if (this.size < APST_HEADER_SIZE) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
this.header = this.parseHeader(this.take(APST_HEADER_SIZE));
|
|
102
|
+
}
|
|
103
|
+
if (this.size < this.header.payloadLength) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const {track, flags, sequence, timestampMicros, payloadLength} = this.header;
|
|
107
|
+
this.header = undefined;
|
|
108
|
+
yield {
|
|
109
|
+
track,
|
|
110
|
+
sequence,
|
|
111
|
+
timestampMicros,
|
|
112
|
+
data: this.take(payloadLength),
|
|
113
|
+
isConfig: (flags & FLAG_CONFIG) !== 0,
|
|
114
|
+
isKeyFrame: (flags & FLAG_KEYFRAME) !== 0,
|
|
115
|
+
isEndOfStream: (flags & FLAG_EOS) !== 0,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
private parseHeader(buf: Buffer): PendingHeader {
|
|
121
|
+
if (!buf.subarray(0, 4).equals(MAGIC)) {
|
|
122
|
+
throw new MediaProxyProtocolError('Stream desynchronized: unexpected APST magic bytes');
|
|
123
|
+
}
|
|
124
|
+
if (buf.readUInt8(4) !== APST_VERSION) {
|
|
125
|
+
throw new MediaProxyProtocolError(`Unsupported APST version ${buf.readUInt8(4)}`);
|
|
126
|
+
}
|
|
127
|
+
const payloadLength = buf.readUInt32BE(24);
|
|
128
|
+
if (payloadLength > MAX_PAYLOAD) {
|
|
129
|
+
throw new MediaProxyProtocolError(`Implausible APST payload length ${payloadLength}`);
|
|
130
|
+
}
|
|
131
|
+
return {
|
|
132
|
+
track: buf.readUInt8(5) as ApstTrack,
|
|
133
|
+
flags: buf.readUInt8(6),
|
|
134
|
+
sequence: Number(buf.readBigUInt64BE(8)),
|
|
135
|
+
timestampMicros: Number(buf.readBigUInt64BE(16)),
|
|
136
|
+
payloadLength,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Removes the first `n` queued bytes into one new buffer (a single copy). */
|
|
141
|
+
private take(n: number): Buffer {
|
|
142
|
+
const out = Buffer.allocUnsafe(n);
|
|
143
|
+
let offset = 0;
|
|
144
|
+
while (offset < n) {
|
|
145
|
+
const head = this.chunks[0];
|
|
146
|
+
const count = Math.min(head.length, n - offset);
|
|
147
|
+
head.copy(out, offset, 0, count);
|
|
148
|
+
offset += count;
|
|
149
|
+
if (count === head.length) {
|
|
150
|
+
this.chunks.shift();
|
|
151
|
+
} else {
|
|
152
|
+
this.chunks[0] = head.subarray(count);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
this.size -= n;
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import http from 'node:http';
|
|
2
|
+
import type {ServerResponse} from 'node:http';
|
|
3
|
+
import type {AddressInfo} from 'node:net';
|
|
4
|
+
|
|
5
|
+
import {ignoreRejection, releaseQuietly} from '../background.js';
|
|
6
|
+
import type {JpegFrame} from '../types.js';
|
|
7
|
+
import {
|
|
8
|
+
assertValidMjpegBoundary,
|
|
9
|
+
DEFAULT_MJPEG_BOUNDARY,
|
|
10
|
+
formatMjpegPart,
|
|
11
|
+
mjpegContentType,
|
|
12
|
+
} from './mjpeg-multipart.js';
|
|
13
|
+
|
|
14
|
+
export interface MjpegServerOptions {
|
|
15
|
+
/** Defaults to 127.0.0.1. */
|
|
16
|
+
host?: string;
|
|
17
|
+
/** 0 (default) picks a free port. */
|
|
18
|
+
port?: number;
|
|
19
|
+
/** 1-70 chars of `[0-9A-Za-z'+_.-]`; invalid values throw `MediaProxyOptionError`. */
|
|
20
|
+
boundary?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface MjpegServer {
|
|
24
|
+
readonly port: number;
|
|
25
|
+
readonly host: string;
|
|
26
|
+
/** URL clients (browsers, `<img>`, ffplay) can open. */
|
|
27
|
+
readonly url: string;
|
|
28
|
+
readonly clientCount: number;
|
|
29
|
+
/**
|
|
30
|
+
* Settles when the frame source ends (resolves) or fails (rejects with its error), or resolves
|
|
31
|
+
* on {@link close}. Already handled internally, so ignoring it never raises an unhandled rejection.
|
|
32
|
+
*/
|
|
33
|
+
readonly done: Promise<void>;
|
|
34
|
+
close(): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Serves `frames` as an MJPEG `multipart/x-mixed-replace` stream to any number of HTTP clients.
|
|
39
|
+
* Each client keeps only the newest unsent frame: a slow client skips frames (every JPEG is
|
|
40
|
+
* independently decodable) rather than building a backlog or stalling the others. Once the source
|
|
41
|
+
* has ended or failed, new requests get `410 Gone`.
|
|
42
|
+
*/
|
|
43
|
+
export async function serveMjpeg(
|
|
44
|
+
frames: AsyncIterable<JpegFrame>,
|
|
45
|
+
opts: MjpegServerOptions = {},
|
|
46
|
+
): Promise<MjpegServer> {
|
|
47
|
+
const boundary = opts.boundary ?? DEFAULT_MJPEG_BOUNDARY;
|
|
48
|
+
assertValidMjpegBoundary(boundary);
|
|
49
|
+
// Open the source first so a throwing iterator never leaves a listening server behind.
|
|
50
|
+
const iterator = frames[Symbol.asyncIterator]();
|
|
51
|
+
const host = opts.host ?? '127.0.0.1';
|
|
52
|
+
const clients = new Set<Client>();
|
|
53
|
+
let sourceDone = false;
|
|
54
|
+
let closing = false;
|
|
55
|
+
|
|
56
|
+
const server = http.createServer((req, res) => {
|
|
57
|
+
if (req.method !== 'GET') {
|
|
58
|
+
res.writeHead(405).end();
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (sourceDone) {
|
|
62
|
+
res.writeHead(410).end();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
res.writeHead(200, {
|
|
66
|
+
'Content-Type': mjpegContentType(boundary),
|
|
67
|
+
'Cache-Control': 'no-cache, no-store',
|
|
68
|
+
Connection: 'close',
|
|
69
|
+
});
|
|
70
|
+
res.flushHeaders();
|
|
71
|
+
const client = new Client(res, boundary);
|
|
72
|
+
clients.add(client);
|
|
73
|
+
res.on('close', () => clients.delete(client));
|
|
74
|
+
});
|
|
75
|
+
server.on('clientError', (_err, socket) => socket.destroy());
|
|
76
|
+
try {
|
|
77
|
+
await new Promise<void>((resolve, reject) => {
|
|
78
|
+
server.once('error', reject);
|
|
79
|
+
server.listen(opts.port ?? 0, host, () => {
|
|
80
|
+
server.off('error', reject);
|
|
81
|
+
resolve();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
} catch (err) {
|
|
85
|
+
releaseQuietly(iterator);
|
|
86
|
+
throw err;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let signalClosed!: () => void;
|
|
90
|
+
const closed = new Promise<void>((resolve) => (signalClosed = resolve));
|
|
91
|
+
const pump = (async () => {
|
|
92
|
+
try {
|
|
93
|
+
for (;;) {
|
|
94
|
+
const result = await iterator.next();
|
|
95
|
+
if (result.done || closing) {
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
for (const client of clients) {
|
|
99
|
+
client.offer(result.value);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
} finally {
|
|
103
|
+
// Source ended or failed: finish all responses so clients see a clean end.
|
|
104
|
+
sourceDone = true;
|
|
105
|
+
for (const client of clients) {
|
|
106
|
+
client.end();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
})();
|
|
110
|
+
// Either the source settles, or close() was called while it is idle.
|
|
111
|
+
const done = Promise.race([pump, closed]);
|
|
112
|
+
// Surfaced through `done`; these keep an unobserved failure from becoming an unhandled rejection.
|
|
113
|
+
ignoreRejection(pump);
|
|
114
|
+
ignoreRejection(done);
|
|
115
|
+
|
|
116
|
+
const {port} = server.address() as AddressInfo;
|
|
117
|
+
return {
|
|
118
|
+
port,
|
|
119
|
+
host,
|
|
120
|
+
url: `http://${host.includes(':') ? `[${host}]` : host}:${port}/`,
|
|
121
|
+
get clientCount() {
|
|
122
|
+
return clients.size;
|
|
123
|
+
},
|
|
124
|
+
done,
|
|
125
|
+
async close() {
|
|
126
|
+
closing = true;
|
|
127
|
+
sourceDone = true;
|
|
128
|
+
signalClosed();
|
|
129
|
+
// Ask the source to stop; not awaited, as an idle source may not settle until its next item.
|
|
130
|
+
releaseQuietly(iterator);
|
|
131
|
+
for (const client of clients) {
|
|
132
|
+
client.end();
|
|
133
|
+
}
|
|
134
|
+
server.closeAllConnections();
|
|
135
|
+
await new Promise<void>((resolve) => server.close(() => resolve()));
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
class Client {
|
|
141
|
+
private pending: JpegFrame | undefined;
|
|
142
|
+
private writing = false;
|
|
143
|
+
|
|
144
|
+
constructor(
|
|
145
|
+
private readonly res: ServerResponse,
|
|
146
|
+
private readonly boundary: string,
|
|
147
|
+
) {}
|
|
148
|
+
|
|
149
|
+
offer(frame: JpegFrame): void {
|
|
150
|
+
this.pending = frame;
|
|
151
|
+
if (!this.writing) {
|
|
152
|
+
this.flush();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
end(): void {
|
|
157
|
+
this.pending = undefined;
|
|
158
|
+
if (!this.res.writableEnded) {
|
|
159
|
+
this.res.end();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private flush(): void {
|
|
164
|
+
const frame = this.pending;
|
|
165
|
+
if (!frame || this.res.writableEnded || this.res.destroyed) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
this.pending = undefined;
|
|
169
|
+
this.writing = true;
|
|
170
|
+
const [head, body, tail] = formatMjpegPart(frame, this.boundary);
|
|
171
|
+
this.res.cork();
|
|
172
|
+
this.res.write(head);
|
|
173
|
+
this.res.write(body);
|
|
174
|
+
const ok = this.res.write(tail);
|
|
175
|
+
this.res.uncork();
|
|
176
|
+
const done = () => {
|
|
177
|
+
this.writing = false;
|
|
178
|
+
this.flush();
|
|
179
|
+
};
|
|
180
|
+
if (ok) {
|
|
181
|
+
done();
|
|
182
|
+
} else {
|
|
183
|
+
this.res.once('drain', done);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {MediaProxyOptionError} from '../errors.js';
|
|
2
|
+
import type {JpegFrame} from '../types.js';
|
|
3
|
+
|
|
4
|
+
export const DEFAULT_MJPEG_BOUNDARY = 'appium-mjpeg-frame';
|
|
5
|
+
|
|
6
|
+
// Safe in an unquoted `boundary=` parameter (a MIME token) and a valid RFC 2046 delimiter.
|
|
7
|
+
const BOUNDARY_PATTERN = /^[0-9A-Za-z'+_.-]{1,70}$/;
|
|
8
|
+
|
|
9
|
+
/** Throws `MediaProxyOptionError` unless `boundary` is 1-70 chars of `[0-9A-Za-z'+_.-]`. */
|
|
10
|
+
export function assertValidMjpegBoundary(boundary: string): void {
|
|
11
|
+
if (typeof boundary !== 'string' || !BOUNDARY_PATTERN.test(boundary)) {
|
|
12
|
+
throw new MediaProxyOptionError(
|
|
13
|
+
`boundary must be 1-70 characters of [0-9A-Za-z'+_.-], got ${JSON.stringify(boundary)}`,
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** `Content-Type` header value for an MJPEG HTTP response using `boundary`. */
|
|
19
|
+
export function mjpegContentType(boundary: string = DEFAULT_MJPEG_BOUNDARY): string {
|
|
20
|
+
assertValidMjpegBoundary(boundary);
|
|
21
|
+
return `multipart/x-mixed-replace; boundary=${boundary}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Header preceding one JPEG in the multipart body. */
|
|
25
|
+
export function mjpegPartHeader(frame: JpegFrame, boundary: string = DEFAULT_MJPEG_BOUNDARY): Buffer {
|
|
26
|
+
assertValidMjpegBoundary(boundary);
|
|
27
|
+
return Buffer.from(
|
|
28
|
+
`--${boundary}\r\nContent-Type: image/jpeg\r\nContent-Length: ${frame.data.length}\r\n\r\n`,
|
|
29
|
+
'ascii',
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const PART_TRAILER = Buffer.from('\r\n', 'ascii');
|
|
34
|
+
|
|
35
|
+
/** `[header, jpeg, trailer]` for one multipart part; write them in order without concatenating. */
|
|
36
|
+
export function formatMjpegPart(frame: JpegFrame, boundary: string = DEFAULT_MJPEG_BOUNDARY): [Buffer, Buffer, Buffer] {
|
|
37
|
+
return [mjpegPartHeader(frame, boundary), frame.data, PART_TRAILER];
|
|
38
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export {listH264Encoders} from './capabilities.js';
|
|
2
|
+
export {
|
|
3
|
+
MediaProxyEncoderUnavailableError,
|
|
4
|
+
MediaProxyError,
|
|
5
|
+
MediaProxyOptionError,
|
|
6
|
+
MediaProxyProtocolError,
|
|
7
|
+
MediaProxyUnavailableError,
|
|
8
|
+
} from './errors.js';
|
|
9
|
+
export {
|
|
10
|
+
DEFAULT_BITRATE,
|
|
11
|
+
DEFAULT_FPS,
|
|
12
|
+
DEFAULT_QUALITY,
|
|
13
|
+
DEFAULT_SCALE,
|
|
14
|
+
Transcoder,
|
|
15
|
+
createTranscoder,
|
|
16
|
+
transcode,
|
|
17
|
+
} from './transcoder.js';
|
|
18
|
+
export {
|
|
19
|
+
APST_HEADER_SIZE,
|
|
20
|
+
APST_VERSION,
|
|
21
|
+
ApstParser,
|
|
22
|
+
ApstTrack,
|
|
23
|
+
encodeApstEndOfStream,
|
|
24
|
+
encodeApstFrame,
|
|
25
|
+
encodeApstFrameBuffer,
|
|
26
|
+
} from './framing/apst.js';
|
|
27
|
+
export type {ApstFrame} from './framing/apst.js';
|
|
28
|
+
export {DEFAULT_MJPEG_BOUNDARY, formatMjpegPart, mjpegContentType, mjpegPartHeader} from './framing/mjpeg-multipart.js';
|
|
29
|
+
export {serveMjpeg} from './framing/mjpeg-http.js';
|
|
30
|
+
export type {MjpegServer, MjpegServerOptions} from './framing/mjpeg-http.js';
|
|
31
|
+
export type {
|
|
32
|
+
AudioAccessUnit,
|
|
33
|
+
H264Options,
|
|
34
|
+
InputOptions,
|
|
35
|
+
JpegFrame,
|
|
36
|
+
JpegStreamOptions,
|
|
37
|
+
MjpegOptions,
|
|
38
|
+
NalAccessUnit,
|
|
39
|
+
Rotation,
|
|
40
|
+
TranscoderOptions,
|
|
41
|
+
VideoAccessUnit,
|
|
42
|
+
VideoStreamOptions,
|
|
43
|
+
} from './types.js';
|