summaryrefslogtreecommitdiff
path: root/src/config/rtcjs.ts
blob: f9e8a679bc3ab7e37849ef08ac6ba2560dee6a0e (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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));
}