Skip to content

Instantly share code, notes, and snippets.

@krzemienski
Last active September 3, 2020 04:11
Show Gist options
  • Save krzemienski/47c79f0a5f94b9e5e7789742144b67a0 to your computer and use it in GitHub Desktop.
Save krzemienski/47c79f0a5f94b9e5e7789742144b67a0 to your computer and use it in GitHub Desktop.
Lambda edge logic to determine what dash manifest
'use strict';
const https = require('https');
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const origin = event.Records[0].cf.request.origin;
const uri = request.uri;
const headers = request.headers;
console.log('Request: ', request);
console.log('Requested URI: ', uri);
console.log('Requested origin HEADERS: ', headers);
if (headers['x-supported-codecs-list'] && headers['x-supported-codecs-list'].length > 0 && headers['x-supported-codecs-list'][0].value.includes("hevc")) {
var masterUri = request.uri;
const cache_host = 'https://s3via.cloudfront.net';
const cacheUrl = cache_host + masterUri.replace('master.mpd', 'master-hevc.mpd');
https.get(cacheUrl, (res) => {
if (res.statusCode === 200) {
request.uri = masterUri.replace('master.mpd', 'master-hevc.mpd');
console.log(`Request uri set to "${request.uri}"`);
callback(null, request);
} else {
callback(null, request);
}
}).on("error", (err) => {
console.log("Error: " + err.message);
callback(null, request);
});
} else {
var masterUri = request.uri;
const cache_host = 'https://s3via.cloudfront.net';
const cacheUrl = cache_host + masterUri.replace('master.mpd', 'master-avc-hevc.mpd');
https.get(cacheUrl, (res) => {
if (res.statusCode === 200) {
request.uri = masterUri.replace('master.mpd', 'master-avc-hevc.mpd');
console.log(`Request uri set to "${request.uri}"`);
callback(null, request);
} else {
callback(null, request);
}
}).on("error", (err) => {
console.log("Error: " + err.message);
callback(null, request);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment