forked from moq-dev/moq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.ts
More file actions
60 lines (50 loc) · 1.89 KB
/
Copy pathaudio.ts
File metadata and controls
60 lines (50 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { z } from "zod";
import { ContainerSchema, DEFAULT_CONTAINER } from "./container";
import { u53Schema } from "./integers";
// Backwards compatibility: old track schema
const TrackSchema = z.object({
name: z.string(),
priority: z.number().int().min(0).max(255),
});
// Mirrors AudioDecoderConfig
// https://w3c.github.io/webcodecs/#audio-decoder-config
export const AudioConfigSchema = z.object({
// See: https://w3c.github.io/webcodecs/codec_registry.html
codec: z.string(),
// Container format for timestamp encoding
// Defaults to "legacy" when not specified in catalog (backward compatibility)
container: ContainerSchema.default(DEFAULT_CONTAINER),
// The description is used for some codecs.
// If provided, we can initialize the decoder based on the catalog alone.
// Otherwise, the initialization information is in-band.
description: z.string().optional(), // hex encoded TODO use base64
// The sample rate of the audio in Hz
sampleRate: u53Schema,
// The number of channels in the audio
numberOfChannels: u53Schema,
// The bitrate of the audio in bits per second
// TODO: Support up to Number.MAX_SAFE_INTEGER
bitrate: u53Schema.optional(),
});
export const AudioSchema = z
.object({
// A map of track name to rendition configuration.
// This is not an array so it will work with JSON Merge Patch.
renditions: z.record(z.string(), AudioConfigSchema),
// The priority of the audio track, relative to other tracks in the broadcast.
priority: z.number().int().min(0).max(255),
})
.or(
// Backwards compatibility: transform old {track, config} format to new object format
z
.object({
track: TrackSchema,
config: AudioConfigSchema,
})
.transform((old) => ({
renditions: { [old.track.name]: old.config },
priority: old.track.priority,
})),
);
export type Audio = z.infer<typeof AudioSchema>;
export type AudioConfig = z.infer<typeof AudioConfigSchema>;