/ src / config / rtcjs.ts
rtcjs.ts
  1  import { platform } from '@amp/web-apps-utils';
  2  import { HLSJS_CDN, HLSJS_VERSION } from './hlsjs';
  3  
  4  declare global {
  5      interface Window {
  6          rtc?: any;
  7      }
  8  }
  9  
 10  export type ReportingOptions = {
 11      storeBagURL: string;
 12      clientName: string;
 13      serviceName: string;
 14      applicationName: string;
 15      applicationVersion: string;
 16      browserName: string;
 17      browserMajorVersion: string;
 18      browserMinorVersion: string;
 19      osName: string;
 20      osVersion: string;
 21  };
 22  
 23  /**
 24   * Generate a URL for loading HLS.js.
 25   */
 26  export function generateRTCJSURL(version?: string): URL {
 27      // FIXME: Add a local storage override for the HLS.js version
 28      version = version ?? HLSJS_VERSION;
 29  
 30      return new URL(`${HLSJS_CDN}/${version}/rtc.js/rtc.js`);
 31  }
 32  
 33  export function getRTCNamespace() {
 34      if (window.rtc === undefined) {
 35          throw new Error('Unable to load RTC library');
 36      }
 37  
 38      return window.rtc;
 39  }
 40  
 41  export function getReportingOptions(): ReportingOptions {
 42      // FIXME: Add correct information for RTC reporting for Web App Store
 43      return {
 44          storeBagURL:
 45              'https://mediaservices.cdn-apple.com/store_bags/hlsjs/aasw/v1/rtc_storebag.json',
 46  
 47          // Application
 48          clientName: 'AASW',
 49          serviceName: 'com.apple.apps.external',
 50          applicationName: 'AppleAppStoreVWeb',
 51          applicationVersion: 'WebAppStore/1.0.0',
 52  
 53          // Browser
 54          browserName: platform.clientName() ?? '',
 55          browserMajorVersion: platform.majorVersion()?.toString() ?? '0',
 56          browserMinorVersion: platform.minorVersion()?.toString() ?? '0',
 57  
 58          // Operating System
 59          osName: platform.osName() ?? '',
 60          osVersion: platform.osName() ?? '',
 61      } as const;
 62  }
 63  
 64  /**
 65   * Generate the configuration used for an `RTCReportingAgent`.
 66   *
 67   * @see {@link makeReportingAgent}
 68   */
 69  export function generateReportingConfig(rtc: any) {
 70      rtc = rtc ?? getRTCNamespace();
 71      const options = getReportingOptions();
 72      const key = rtc.RTCReportingAgentConfigKeys;
 73  
 74      return {
 75          [key.Sender]: 'HLSJS',
 76          [key.ClientName]: options.clientName,
 77          [key.ServiceName]: options.serviceName,
 78          [key.ApplicationName]: options.applicationName,
 79          [key.DeviceName]: options.osVersion,
 80          [key.ReportingStoreBag]: new rtc.RTCStorebag.RTCReportingStoreBag(
 81              options.storeBagURL,
 82              options.clientName,
 83              options.serviceName,
 84              options.applicationName,
 85              options.browserName,
 86              { iTunesAppVersion: options.applicationVersion },
 87          ),
 88  
 89          // Fake out these fields
 90          model: options.browserName,
 91          firmwareVersion: `${options.browserMajorVersion}.${options.browserMinorVersion}`,
 92      };
 93  }
 94  
 95  /**
 96   * Create an `RTCReportingAgent` with default configuration from `generateReportingConfig`.
 97   *
 98   * The reporting agent can be used with HLS.js playback to enable RTC reporting.
 99   */
100  export function makeReportingAgent(rtc: any): any {
101      rtc = rtc ?? getRTCNamespace();
102      return new rtc.RTCReportingAgent(generateReportingConfig(rtc));
103  }