<?php
$object = "1612602178709.mp4";
$expires = time() + 10 * 60;
$resourceKey = "https://d22jaj1x4zpoxb.cloudfront.net/{$object}";

$expires = time() + 5 * 60; //Expire the link in 5 minutes
$RTMP_url = getsignedurl("file.mp4", $expires);

$pseudo_url = getsignedurl($resourceKey, $expires);

function getsignedurl($resource, $expires)
{
    // key pair generated for cloudfront
    $keyPairId = 'APKAIE2MMGXVN6OAZUTQ';
    $json = '{"Statement":[{"Resource":"' . $resource . '","Condition":{"DateLessThan":{"AWS:EpochTime":' . $expires . '}}}]}';
    // read cloudfront private key pair
    $privateKey = dirname(__FILE__) . '/private/pk-APKAIE2MMGXVN6OAZUTQ.pem';
    $fp = fopen($privateKey, 'r');
    $priv_key = fread($fp, 8192);
    fclose($fp);
    // create the private key
    $key = openssl_get_privatekey($priv_key);
    // sign the policy with the private key, this may not work properly on windows.
    // depending on your php version you might have to use
    // openssl_sign($json, $signed_policy, $key, OPENSSL_ALGO_SHA1)
    openssl_sign($json, $signed_policy, $key);
    openssl_free_key($key);
    // create url safe signed policy
    $base64_signed_policy = base64_encode($signed_policy);
    $signature = str_replace(array('+', '=', '/'), array('-', '_', '~'), $base64_signed_policy);
    // construct the url
    $url = $resource . '?Expires=' . $expires . '&Signature=' . $signature . '&Key-Pair-Id=' . $keyPairId;
    return $url;
}


?>

<body>
<script src="http://cdn.jwplayer.com/players/UVQWMA4o-kGWxh33Q.js"></script>
    <div id=”example_video”></div>
    <script>
        var RTMP_url = '<?php echo $RTMP_url; ?>';
        var pseudo_url = '<?php echo $pseudo_url; ?>';

        jwplayer('example_video').setup({
            playlist: [{
                image: "/img/video.png",
                sources: [{
                        file: "rtmp://d22jaj1x4zpoxb.cloudfront.net/cfx/st/mp4:" + RTMP_url,
                        height: 569,
                        width: 940
                    },
                    {
                        file: pseudo_url,
                        height: 569,
                        width: 940
                    }
                ],
                title: "Play movie"
            }],
            width: "940",
            height: "569",
            primary: "flash"
        });
    </script>
    
</body>