summaryrefslogtreecommitdiff
path: root/src/config/rtcjs.ts
diff options
context:
space:
mode:
authorrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
committerrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
commitbce557cc2dc767628bed6aac87301a1be7c5431b (patch)
treeb51a051228d01fe3306cd7626d4a96768aadb944 /src/config/rtcjs.ts
init commit
Diffstat (limited to 'src/config/rtcjs.ts')
-rw-r--r--src/config/rtcjs.ts103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/config/rtcjs.ts b/src/config/rtcjs.ts
new file mode 100644
index 0000000..f9e8a67
--- /dev/null
+++ b/src/config/rtcjs.ts
@@ -0,0 +1,103 @@
+import { platform } from '@amp/web-apps-utils';
+import { HLSJS_CDN, HLSJS_VERSION } from './hlsjs';
+
+declare global {
+ interface Window {
+ rtc?: any;
+ }
+}
+
+export type ReportingOptions = {
+ storeBagURL: string;
+ clientName: string;
+ serviceName: string;
+ applicationName: string;
+ applicationVersion: string;
+ browserName: string;
+ browserMajorVersion: string;
+ browserMinorVersion: string;
+ osName: string;
+ osVersion: string;
+};
+
+/**
+ * Generate a URL for loading HLS.js.
+ */
+export function generateRTCJSURL(version?: string): URL {
+ // FIXME: Add a local storage override for the HLS.js version
+ version = version ?? HLSJS_VERSION;
+
+ return new URL(`${HLSJS_CDN}/${version}/rtc.js/rtc.js`);
+}
+
+export function getRTCNamespace() {
+ if (window.rtc === undefined) {
+ throw new Error('Unable to load RTC library');
+ }
+
+ return window.rtc;
+}
+
+export function getReportingOptions(): ReportingOptions {
+ // FIXME: Add correct information for RTC reporting for Web App Store
+ return {
+ storeBagURL:
+ 'https://mediaservices.cdn-apple.com/store_bags/hlsjs/aasw/v1/rtc_storebag.json',
+
+ // Application
+ clientName: 'AASW',
+ serviceName: 'com.apple.apps.external',
+ applicationName: 'AppleAppStoreVWeb',
+ applicationVersion: 'WebAppStore/1.0.0',
+
+ // Browser
+ browserName: platform.clientName() ?? '',
+ browserMajorVersion: platform.majorVersion()?.toString() ?? '0',
+ browserMinorVersion: platform.minorVersion()?.toString() ?? '0',
+
+ // Operating System
+ osName: platform.osName() ?? '',
+ osVersion: platform.osName() ?? '',
+ } as const;
+}
+
+/**
+ * Generate the configuration used for an `RTCReportingAgent`.
+ *
+ * @see {@link makeReportingAgent}
+ */
+export function generateReportingConfig(rtc: any) {
+ rtc = rtc ?? getRTCNamespace();
+ const options = getReportingOptions();
+ const key = rtc.RTCReportingAgentConfigKeys;
+
+ return {
+ [key.Sender]: 'HLSJS',
+ [key.ClientName]: options.clientName,
+ [key.ServiceName]: options.serviceName,
+ [key.ApplicationName]: options.applicationName,
+ [key.DeviceName]: options.osVersion,
+ [key.ReportingStoreBag]: new rtc.RTCStorebag.RTCReportingStoreBag(
+ options.storeBagURL,
+ options.clientName,
+ options.serviceName,
+ options.applicationName,
+ options.browserName,
+ { iTunesAppVersion: options.applicationVersion },
+ ),
+
+ // Fake out these fields
+ model: options.browserName,
+ firmwareVersion: `${options.browserMajorVersion}.${options.browserMinorVersion}`,
+ };
+}
+
+/**
+ * Create an `RTCReportingAgent` with default configuration from `generateReportingConfig`.
+ *
+ * The reporting agent can be used with HLS.js playback to enable RTC reporting.
+ */
+export function makeReportingAgent(rtc: any): any {
+ rtc = rtc ?? getRTCNamespace();
+ return new rtc.RTCReportingAgent(generateReportingConfig(rtc));
+}