summaryrefslogtreecommitdiff
path: root/lib/mediaselector.php
blob: 2adaf717d8fa729d72ace6bbb6ba74e8470e1056 (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
<?php

require_once __DIR__ . '/sounds-auth.php';

const MEDIASELECTOR_BASE = 'https://open.live.bbc.co.uk/mediaselector/6/select';

function mediaselector_lookup(array $options, ?string $token = null): stdClass {
    // ['version' => '3.0', 'cvid' => 'urn:bbc:pips:pid:...']
    $url = MEDIASELECTOR_BASE;
    $options['format'] = 'json';
    foreach ($options as $k => $v) {
        $url .= "/$k/$v";
    }
    $ctx = stream_context_get_default();
    if (isset($token)) {
        stream_context_set_option($ctx, 'http',
                                  'header', "Authorization: Bearer $token");
    }
    return json_decode(file_get_contents($url, context: $ctx));
}

function mediaselector_findHls(string $vpid, ?string $token = null): ?string {
    $resp = mediaselector_lookup([
        'version' => '3.0',
        'proto' => 'https',
        'transferformat' => 'hls',
        'mediaset' => 'pc',
        'cvid' => "urn:bbc:pips:pid:$vpid",
    ], $token);
    if (isset($resp->result) && $resp->result == 'selectionunavailable') {
        return null;
    }
    return $resp->media[0]->connection[0]->href;
}

function mediaselector_findLiveHls(string $service): ?string {
    $jwt = sounds_getLiveBearerToken($service);
    return mediaselector_findHls($service, $jwt);
}