@mediabunny/server 1.63.0 → 1.64.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.
@@ -50,7 +50,7 @@ var MediabunnyServer = (() => {
50
50
  registerMediabunnyServer: () => registerMediabunnyServer,
51
51
  toAvFrame: () => toAvFrame
52
52
  });
53
- var import_mediabunny7 = __require("mediabunny");
53
+ var import_mediabunny8 = __require("mediabunny");
54
54
  var NodeAv8 = __toESM(__require("node-av"));
55
55
 
56
56
  // packages/server/src/video-decoder.ts
@@ -73,7 +73,8 @@ var MediabunnyServer = (() => {
73
73
  flac: NodeAv.AV_CODEC_ID_FLAC,
74
74
  ac3: NodeAv.AV_CODEC_ID_AC3,
75
75
  eac3: NodeAv.AV_CODEC_ID_EAC3,
76
- dts: NodeAv.AV_CODEC_ID_DTS
76
+ dts: NodeAv.AV_CODEC_ID_DTS,
77
+ truehd: NodeAv.AV_CODEC_ID_TRUEHD
77
78
  };
78
79
  var cachedHardwareContext = void 0;
79
80
  var getHardwareContext = () => {
@@ -402,6 +403,24 @@ var MediabunnyServer = (() => {
402
403
  return { nbChannels: numChannels, order: NodeAv.AV_CHANNEL_ORDER_UNSPEC, mask: 0n };
403
404
  }
404
405
  };
406
+ var getTrueHdChannelLayout = (numChannels) => {
407
+ switch (numChannels) {
408
+ case 1:
409
+ return NodeAv.AV_CHANNEL_LAYOUT_MONO;
410
+ case 2:
411
+ return NodeAv.AV_CHANNEL_LAYOUT_STEREO;
412
+ case 3:
413
+ return NodeAv.AV_CHANNEL_LAYOUT_SURROUND;
414
+ case 4:
415
+ return NodeAv.AV_CHANNEL_LAYOUT_4POINT0;
416
+ case 5:
417
+ return NodeAv.AV_CHANNEL_LAYOUT_5POINT0;
418
+ case 6:
419
+ return NodeAv.AV_CHANNEL_LAYOUT_5POINT1;
420
+ default:
421
+ return { nbChannels: numChannels, order: NodeAv.AV_CHANNEL_ORDER_UNSPEC, mask: 0n };
422
+ }
423
+ };
405
424
 
406
425
  // src/logging.ts
407
426
  var _Logging = class _Logging {
@@ -699,6 +718,14 @@ var MediabunnyServer = (() => {
699
718
  throw new Error(`Unexpected value: ${x}`);
700
719
  };
701
720
  var SECOND_TO_MICROSECOND_FACTOR = 1e6 * (1 + Number.EPSILON);
721
+ var isWebKitCache = null;
722
+ var isWebKit = () => {
723
+ if (isWebKitCache !== null) {
724
+ return isWebKitCache;
725
+ }
726
+ return isWebKitCache = !!(typeof navigator !== "undefined" && // eslint-disable-next-line @typescript-eslint/no-deprecated
727
+ (navigator.vendor?.match(/apple/i) || /AppleWebKit/.test(navigator.userAgent) && !/Chrome/.test(navigator.userAgent) || /\b(iPad|iPhone|iPod)\b/.test(navigator.userAgent)));
728
+ };
702
729
  var NativePromiseConstructor = (async () => {
703
730
  })().constructor;
704
731
  var simplifyRational = (rational) => {
@@ -1343,7 +1370,8 @@ var MediabunnyServer = (() => {
1343
1370
  "flac",
1344
1371
  "ac3",
1345
1372
  "eac3",
1346
- "dts"
1373
+ "dts",
1374
+ "truehd"
1347
1375
  ];
1348
1376
  var AUDIO_CODECS = [
1349
1377
  ...NON_PCM_AUDIO_CODECS,
@@ -1585,6 +1613,11 @@ var MediabunnyServer = (() => {
1585
1613
  };
1586
1614
  var VALID_VIDEO_CODEC_STRING_PREFIXES = ["avc1", "avc3", "hev1", "hvc1", "vp8", "vp09", "av01", ...PRORES_FOURCCS];
1587
1615
 
1616
+ // shared/truehd-misc.ts
1617
+ var TRUEHD_MAJOR_SYNC_WORD = 4168249274;
1618
+ var TRUEHD_SAMPLE_RATES = [44100, 48e3, 88200, 96e3, 176400, 192e3];
1619
+ var TRUEHD_CHANNEL_COUNTS = [1, 2, 3, 4, 5, 6];
1620
+
1588
1621
  // src/codec-data.ts
1589
1622
  var iterateNalUnitsInAnnexB = function* (packetData) {
1590
1623
  let i = 0;
@@ -2832,6 +2865,14 @@ var MediabunnyServer = (() => {
2832
2865
  var av1CodecInfoHasColorInfo = (info) => {
2833
2866
  return info.colourPrimaries !== 2 || info.transferCharacteristics !== 2 || info.matrixCoefficients !== 2;
2834
2867
  };
2868
+ var determineAudioPacketType = (codec, packetData) => {
2869
+ if (codec === "truehd") {
2870
+ const syncWord = packetData.byteLength >= 8 ? toDataView(packetData).getUint32(4) : null;
2871
+ return syncWord === TRUEHD_MAJOR_SYNC_WORD ? "key" : "delta";
2872
+ } else {
2873
+ return "key";
2874
+ }
2875
+ };
2835
2876
  var AC3_FRAME_SIZES = [
2836
2877
  // frmsizecod, [48kHz, 44.1kHz, 32kHz] in bytes
2837
2878
  64 * 2,
@@ -3564,7 +3605,7 @@ var MediabunnyServer = (() => {
3564
3605
  }
3565
3606
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
3566
3607
  static supports(codec, config) {
3567
- return codec === "aac" || codec === "opus" || codec === "mp3" || codec === "vorbis" || codec === "flac" || codec === "ac3" || codec === "eac3" || codec === "dts";
3608
+ return codec === "aac" || codec === "opus" || codec === "mp3" || codec === "vorbis" || codec === "flac" || codec === "ac3" || codec === "eac3" || codec === "dts" || codec === "truehd";
3568
3609
  }
3569
3610
  async init() {
3570
3611
  this.frame = new NodeAv6.Frame();
@@ -3677,7 +3718,7 @@ var MediabunnyServer = (() => {
3677
3718
  }
3678
3719
  static supports(codec, config) {
3679
3720
  const { numberOfChannels, sampleRate } = config;
3680
- return codec === "aac" && numberOfChannels >= 1 && numberOfChannels <= 48 && AAC_SAMPLE_RATES.includes(sampleRate) || codec === "opus" && numberOfChannels >= 1 && numberOfChannels <= 255 && OPUS_SAMPLE_RATES.includes(sampleRate) || codec === "mp3" && numberOfChannels >= 1 && numberOfChannels <= 2 && MP3_SAMPLE_RATES.includes(sampleRate) || codec === "vorbis" && numberOfChannels >= 1 && numberOfChannels <= 255 && sampleRate <= 2e5 || codec === "flac" && numberOfChannels >= 1 && numberOfChannels <= 8 && sampleRate <= 655350 || codec === "ac3" && numberOfChannels >= 1 && numberOfChannels <= 6 && AC3_SAMPLE_RATES.includes(sampleRate) || codec === "eac3" && numberOfChannels >= 1 && numberOfChannels <= 16 && AC3_SAMPLE_RATES.includes(sampleRate) || codec === "dts" && DTS_CHANNEL_COUNTS.includes(numberOfChannels) && DTS_SAMPLE_RATES.includes(sampleRate) && dtsBitrateFits(resolveBitrate(config, codec), sampleRate, numberOfChannels);
3721
+ return codec === "aac" && numberOfChannels >= 1 && numberOfChannels <= 48 && AAC_SAMPLE_RATES.includes(sampleRate) || codec === "opus" && numberOfChannels >= 1 && numberOfChannels <= 255 && OPUS_SAMPLE_RATES.includes(sampleRate) || codec === "mp3" && numberOfChannels >= 1 && numberOfChannels <= 2 && MP3_SAMPLE_RATES.includes(sampleRate) || codec === "vorbis" && numberOfChannels >= 1 && numberOfChannels <= 255 && sampleRate <= 2e5 || codec === "flac" && numberOfChannels >= 1 && numberOfChannels <= 8 && sampleRate <= 655350 || codec === "ac3" && numberOfChannels >= 1 && numberOfChannels <= 6 && AC3_SAMPLE_RATES.includes(sampleRate) || codec === "eac3" && numberOfChannels >= 1 && numberOfChannels <= 16 && AC3_SAMPLE_RATES.includes(sampleRate) || codec === "dts" && DTS_CHANNEL_COUNTS.includes(numberOfChannels) && DTS_SAMPLE_RATES.includes(sampleRate) && dtsBitrateFits(resolveBitrate(config, codec), sampleRate, numberOfChannels) || codec === "truehd" && TRUEHD_CHANNEL_COUNTS.includes(numberOfChannels) && TRUEHD_SAMPLE_RATES.includes(sampleRate);
3681
3722
  }
3682
3723
  async init() {
3683
3724
  this.frame = new NodeAv7.Frame();
@@ -3698,11 +3739,19 @@ var MediabunnyServer = (() => {
3698
3739
  const codecContext = new NodeAv7.CodecContext();
3699
3740
  codecContext.allocContext3(this.avCodec);
3700
3741
  let sampleFormat = NodeAv7.AV_SAMPLE_FMT_FLTP;
3701
- if (this.avCodec.sampleFormats && !this.avCodec.sampleFormats.includes(NodeAv7.AV_SAMPLE_FMT_FLTP)) {
3742
+ if (this.codec === "truehd") {
3743
+ sampleFormat = NodeAv7.AV_SAMPLE_FMT_S32P;
3744
+ } else if (this.avCodec.sampleFormats && !this.avCodec.sampleFormats.includes(NodeAv7.AV_SAMPLE_FMT_FLTP)) {
3702
3745
  sampleFormat = this.avCodec.sampleFormats[0];
3703
3746
  }
3704
3747
  codecContext.sampleRate = this.config.sampleRate;
3705
- codecContext.channelLayout = this.codec === "dts" ? getDtsChannelLayout(this.config.numberOfChannels) : getChannelLayout(this.config.numberOfChannels);
3748
+ if (this.codec === "dts") {
3749
+ codecContext.channelLayout = getDtsChannelLayout(this.config.numberOfChannels);
3750
+ } else if (this.codec === "truehd") {
3751
+ codecContext.channelLayout = getTrueHdChannelLayout(this.config.numberOfChannels);
3752
+ } else {
3753
+ codecContext.channelLayout = getChannelLayout(this.config.numberOfChannels);
3754
+ }
3706
3755
  codecContext.codecType = NodeAv7.AVMEDIA_TYPE_AUDIO;
3707
3756
  codecContext.codecId = CODEC_TO_CODEC_ID[this.codec];
3708
3757
  codecContext.sampleFormat = sampleFormat;
@@ -3712,7 +3761,7 @@ var MediabunnyServer = (() => {
3712
3761
  codecContext.rcMinRate = codecContext.bitRate;
3713
3762
  codecContext.rcMaxRate = codecContext.bitRate;
3714
3763
  }
3715
- const options = this.codec === "dts" ? NodeAv7.Dictionary.fromObject({ strict: -2 }) : null;
3764
+ const options = this.codec === "dts" || this.codec === "truehd" ? NodeAv7.Dictionary.fromObject({ strict: -2 }) : null;
3716
3765
  const ret = await codecContext.open2(this.avCodec, options);
3717
3766
  NodeAv7.FFmpegError.throwIfError(ret, "Open codec context");
3718
3767
  this.codecContext = codecContext;
@@ -3875,7 +3924,7 @@ var MediabunnyServer = (() => {
3875
3924
  }
3876
3925
  const packet = new import_mediabunny6.EncodedPacket(
3877
3926
  data,
3878
- "key",
3927
+ determineAudioPacketType(this.codec, data),
3879
3928
  timestamp,
3880
3929
  duration
3881
3930
  );
@@ -3926,16 +3975,204 @@ var MediabunnyServer = (() => {
3926
3975
  return config.bitrate ?? new import_mediabunny6.Quality("medium")._toAudioBitrate(codec) ?? 0;
3927
3976
  };
3928
3977
 
3978
+ // packages/prores/src/index.ts
3979
+ var import_mediabunny7 = __require("mediabunny");
3980
+ var import_turbores = __require("turbores");
3981
+ var PRORES_LOADED_SYMBOL = Symbol.for("@mediabunny/prores loaded");
3982
+ if (globalThis[PRORES_LOADED_SYMBOL]) {
3983
+ import_mediabunny7.Logging._error(
3984
+ "[WARNING]\n@mediabunny/prores was loaded twice. This will likely cause the decoder not to work correctly. Check if multiple dependencies are importing different versions of @mediabunny/prores, or if something is being bundled incorrectly."
3985
+ );
3986
+ }
3987
+ globalThis[PRORES_LOADED_SYMBOL] = true;
3988
+ var _ProresDecoder = class _ProresDecoder extends import_mediabunny7.CustomVideoDecoder {
3989
+ constructor() {
3990
+ super(...arguments);
3991
+ this.decoder = null;
3992
+ this.framePool = [];
3993
+ }
3994
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
3995
+ static supports(codec, config) {
3996
+ return codec === "prores";
3997
+ }
3998
+ /** @internal */
3999
+ static _determineSupportedVideoFrameFormats() {
4000
+ const result = [];
4001
+ const data = new Uint8Array(32);
4002
+ for (const format of import_turbores.PIXEL_FORMATS) {
4003
+ try {
4004
+ const frame = new VideoFrame(data, {
4005
+ format,
4006
+ codedWidth: 2,
4007
+ codedHeight: 2,
4008
+ timestamp: 0,
4009
+ duration: 0
4010
+ });
4011
+ frame.close();
4012
+ result.push(format);
4013
+ } catch {
4014
+ }
4015
+ }
4016
+ return result;
4017
+ }
4018
+ async init() {
4019
+ if (typeof VideoFrame !== "undefined") {
4020
+ _ProresDecoder._supportedVideoFrameFormats ??= _ProresDecoder._determineSupportedVideoFrameFormats();
4021
+ }
4022
+ const decoder = await import_turbores.Decoder.create({
4023
+ proresFourCc: this.config.codec,
4024
+ useSharedMemory: import_turbores.Decoder.canUseSharedMemory(),
4025
+ allowedOutputFormats: _ProresDecoder._supportedVideoFrameFormats ?? void 0
4026
+ });
4027
+ if (decoder instanceof Error) {
4028
+ throw decoder;
4029
+ }
4030
+ this.decoder = decoder;
4031
+ }
4032
+ async decode(packet) {
4033
+ assert(this.decoder);
4034
+ while (this.decoder.desiredSize <= 0) {
4035
+ await this.decoder.dequeued;
4036
+ }
4037
+ void this.runDecode(packet).catch((error) => this.onError(error));
4038
+ }
4039
+ async runDecode(packet) {
4040
+ assert(this.decoder);
4041
+ let frame;
4042
+ if (this.framePool.length > 0) {
4043
+ frame = this.framePool.shift();
4044
+ } else {
4045
+ frame = new import_turbores.Frame();
4046
+ }
4047
+ const result = await this.decoder.decode(packet.data, frame);
4048
+ this.framePool.push(frame);
4049
+ if (result instanceof Error) {
4050
+ throw result;
4051
+ }
4052
+ if (result.visibleHeight < result.codedHeight && isWebKit()) {
4053
+ this.trimCodedHeightToVisibleHeight(result);
4054
+ }
4055
+ const colorSpaceInit = {
4056
+ primaries: result.colorPrimariesString,
4057
+ matrix: result.colorMatrixString,
4058
+ transfer: result.colorTransferString,
4059
+ fullRange: result.colorRangeFull
4060
+ };
4061
+ let displayWidth = result.visibleWidth;
4062
+ let displayHeight = result.visibleHeight;
4063
+ if (result.pixelAspectRatio.num > result.pixelAspectRatio.den) {
4064
+ displayWidth = Math.round(
4065
+ result.visibleWidth * result.pixelAspectRatio.num / result.pixelAspectRatio.den
4066
+ );
4067
+ } else {
4068
+ displayHeight = Math.round(
4069
+ result.visibleHeight * result.pixelAspectRatio.den / result.pixelAspectRatio.num
4070
+ );
4071
+ }
4072
+ let sample;
4073
+ if (typeof VideoFrame !== "undefined") {
4074
+ const frame2 = new VideoFrame(result.frameData, {
4075
+ format: result.pixelFormat,
4076
+ codedWidth: result.codedWidth,
4077
+ codedHeight: result.codedHeight,
4078
+ displayWidth,
4079
+ displayHeight,
4080
+ visibleRect: {
4081
+ x: 0,
4082
+ y: 0,
4083
+ width: result.visibleWidth,
4084
+ height: result.visibleHeight
4085
+ },
4086
+ timestamp: packet.microsecondTimestamp,
4087
+ duration: packet.microsecondDuration,
4088
+ colorSpace: colorSpaceInit
4089
+ });
4090
+ sample = new import_mediabunny7.VideoSample(frame2, {
4091
+ timestamp: packet.timestamp,
4092
+ duration: packet.duration
4093
+ });
4094
+ } else {
4095
+ sample = new import_mediabunny7.VideoSample(result.frameData, {
4096
+ format: result.pixelFormat,
4097
+ codedWidth: result.codedWidth,
4098
+ codedHeight: result.codedHeight,
4099
+ displayWidth,
4100
+ displayHeight,
4101
+ visibleRect: {
4102
+ left: 0,
4103
+ top: 0,
4104
+ width: result.visibleWidth,
4105
+ height: result.visibleHeight
4106
+ },
4107
+ timestamp: packet.timestamp,
4108
+ duration: packet.duration,
4109
+ colorSpace: colorSpaceInit
4110
+ });
4111
+ }
4112
+ this.onSample(sample);
4113
+ }
4114
+ trimCodedHeightToVisibleHeight(result) {
4115
+ const bytesPerSample = result.pixelFormat.includes("P") ? 2 : 1;
4116
+ const subWidth = result.pixelFormat.includes("444") ? 1 : 2;
4117
+ const subHeight = result.pixelFormat.includes("420") ? 2 : 1;
4118
+ const chromaCodedWidth = result.codedWidth / subWidth;
4119
+ const chromaCodedHeight = result.codedHeight / subHeight;
4120
+ const chromaVisibleHeight = Math.ceil(result.visibleHeight / subHeight);
4121
+ const lumaCodedPixels = result.codedWidth * result.codedHeight;
4122
+ const lumaVisiblePixels = result.codedWidth * result.visibleHeight;
4123
+ const chromaCodedPixels = chromaCodedWidth * chromaCodedHeight;
4124
+ const chromaVisiblePixels = chromaCodedWidth * chromaVisibleHeight;
4125
+ result.frameData.set(
4126
+ result.frameData.subarray(
4127
+ bytesPerSample * lumaCodedPixels,
4128
+ bytesPerSample * (lumaCodedPixels + chromaCodedPixels)
4129
+ ),
4130
+ bytesPerSample * lumaVisiblePixels
4131
+ );
4132
+ result.frameData.set(
4133
+ result.frameData.subarray(
4134
+ bytesPerSample * (lumaCodedPixels + chromaCodedPixels),
4135
+ bytesPerSample * (lumaCodedPixels + 2 * chromaCodedPixels)
4136
+ ),
4137
+ bytesPerSample * (lumaVisiblePixels + chromaVisiblePixels)
4138
+ );
4139
+ result.codedHeight = result.visibleHeight;
4140
+ }
4141
+ async flush() {
4142
+ assert(this.decoder);
4143
+ while (this.decoder.decodeQueueSize > 0) {
4144
+ await this.decoder.dequeued;
4145
+ }
4146
+ }
4147
+ async close() {
4148
+ assert(this.decoder);
4149
+ await this.decoder.close();
4150
+ for (const frame of this.framePool) {
4151
+ frame.clear();
4152
+ }
4153
+ }
4154
+ };
4155
+ /** @internal */
4156
+ _ProresDecoder._supportedVideoFrameFormats = null;
4157
+ var ProresDecoder = _ProresDecoder;
4158
+ var registered = false;
4159
+ var registerProresDecoder = () => {
4160
+ if (registered) {
4161
+ return;
4162
+ }
4163
+ registered = true;
4164
+ (0, import_mediabunny7.registerDecoder)(ProresDecoder);
4165
+ };
4166
+
3929
4167
  // packages/server/src/index.ts
3930
- var import_prores = __require("@mediabunny/prores");
3931
4168
  var SERVER_LOADED_SYMBOL = Symbol.for("@mediabunny/server loaded");
3932
4169
  if (globalThis[SERVER_LOADED_SYMBOL]) {
3933
- import_mediabunny7.Logging._error(
4170
+ import_mediabunny8.Logging._error(
3934
4171
  "[WARNING]\n@mediabunny/server was loaded twice. This will likely cause the package not to work correctly. Check if multiple dependencies are importing different versions of @mediabunny/server, or if something is being bundled incorrectly."
3935
4172
  );
3936
4173
  }
3937
4174
  globalThis[SERVER_LOADED_SYMBOL] = true;
3938
- var registered = false;
4175
+ var registered2 = false;
3939
4176
  var _serverOptions = {};
3940
4177
  var registerMediabunnyServer = (options = {}) => {
3941
4178
  if (typeof options !== "object" || !options) {
@@ -3946,27 +4183,27 @@ var MediabunnyServer = (() => {
3946
4183
  "options.hardwareContext, when provided, must be a NodeAv.HardwareContext, a function, or null."
3947
4184
  );
3948
4185
  }
3949
- if (registered) {
4186
+ if (registered2) {
3950
4187
  return;
3951
4188
  }
3952
- registered = true;
4189
+ registered2 = true;
3953
4190
  _serverOptions = options;
3954
4191
  NodeAv8.Log.setLevel(NodeAv8.AV_LOG_ERROR);
3955
- (0, import_mediabunny7.registerDecoder)(NodeAvVideoDecoder);
3956
- (0, import_prores.registerProresDecoder)();
3957
- (0, import_mediabunny7.registerEncoder)(NodeAvVideoEncoder);
3958
- (0, import_mediabunny7.registerDecoder)(NodeAvAudioDecoder);
3959
- (0, import_mediabunny7.registerEncoder)(NodeAvAudioEncoder);
3960
- (0, import_mediabunny7.registerVideoSampleTransformer)(transformVideoSample);
4192
+ (0, import_mediabunny8.registerDecoder)(NodeAvVideoDecoder);
4193
+ registerProresDecoder();
4194
+ (0, import_mediabunny8.registerEncoder)(NodeAvVideoEncoder);
4195
+ (0, import_mediabunny8.registerDecoder)(NodeAvAudioDecoder);
4196
+ (0, import_mediabunny8.registerEncoder)(NodeAvAudioEncoder);
4197
+ (0, import_mediabunny8.registerVideoSampleTransformer)(transformVideoSample);
3961
4198
  };
3962
4199
  var toAvFrame = async (sample, frame) => {
3963
- if (!(sample instanceof import_mediabunny7.VideoSample) && !(sample instanceof import_mediabunny7.AudioSample)) {
4200
+ if (!(sample instanceof import_mediabunny8.VideoSample) && !(sample instanceof import_mediabunny8.AudioSample)) {
3964
4201
  throw new TypeError("sample must be a VideoSample or an AudioSample.");
3965
4202
  }
3966
4203
  if (!(frame instanceof NodeAv8.Frame)) {
3967
4204
  throw new TypeError("frame must be a NodeAv.Frame.");
3968
4205
  }
3969
- if (sample instanceof import_mediabunny7.VideoSample) {
4206
+ if (sample instanceof import_mediabunny8.VideoSample) {
3970
4207
  if (sample._data instanceof AvFrameVideoSampleResource) {
3971
4208
  frame.unref();
3972
4209
  frame.ref(sample._data.frame);