Source: models/streams/premium.js

define('application/models/streams/premium', [
    'application/models/stream',
    'rofl/lib/utils',
    'rofl/media/source',
    'rofl/devices/mediaplayer/mediaplayer',
    'antie/runtimecontext',
    'rofl/media/drm/item/playready'
], function (
    Base,
    Utils,
    MediaSource,
    MediaPlayer,
    RuntimeContext,
    PlayReadyDRMItem
) {
    'use strict';

    var device = RuntimeContext.getDevice(),
        capitalizeFirstLetter = function (text) {
            return text.charAt(0).toUpperCase() + text.slice(1);
        },
        model = device.getModel(),
        brand = device.getBrand(),
        ENDPOINTS = {
            LIVE: 'https://www.kpncdn.eu/smarttv/smooth/{{contentId}}',
            VOD: 'https://www.kpncdn.eu/smarttv/premium/{{contentId}}_{{startTime}}_{{endTime}}',
            RESTART: 'https://www.kpncdn.eu/smarttv/smooth/{{contentId}}_{{startTime}}'
        };

    if (model && brand && model === '2016' && brand === 'samsung') {
        ENDPOINTS = {
            LIVE: 'https://www.kpncdn.eu/smarttv/premium/{{contentId}}',
            VOD: 'https://www.kpncdn.eu/smarttv/premium/{{contentId}}_{{startTime}}_{{endTime}}',
            RESTART: 'https://www.kpncdn.eu/smarttv/premium/{{contentId}}_{{startTime}}'
        };
    }

    return Base.extend({

        /**
         * Resolves the endpoint.
         *
         * @param {Object} params - The parameters.
         * @param {string} params.type - The stream type. LIVE or VOD.
         * @param {string} params.contentId - The content id.
         * @param {boolean} [params.stop] - True if it should stop the stream. Optional.
         * @returns {string} - The endpoint.
         */
        resolveEndpoint: function (params) {
            var type,
                endpoint;

            if (!params || !params.type || !params.contentId) {
                throw 'Missing type or content id.';
            }

            type = params.type;

            endpoint = ENDPOINTS[type];

            params.deviceId = device.getUdid();
            this._type = type;

            return Utils.formatTemplate(endpoint, params);
        },

        /**
         * Validates the response.
         *
         * @param {Object} response - The response.
         * @returns {boolean} - True if valid.
         */
        validateResponse: function (response) {
            return response.resultCode === '0'
                && Utils.hasNested(response, 'resultObj', 'sources');
        },

        /**
         * Transforms the response.
         *
         * @param {Object} response - The response.
         * @returns {Object} - The media source.
         */
        transformFrom: function (response) {
            var data = Utils.getNested(response, 'resultObj', 'sources'),
                streamUrl = data.src,
                deviceBrand = device.getBrand(),
                streamType = this._type === 'VOD' ? MediaPlayer.TYPE.VIDEO : MediaPlayer.TYPE.LIVE_VIDEO,
                mediaSource,
                drmItem = new PlayReadyDRMItem(),
                licenseOverride;

            if (model && brand && model === '2016' && brand === 'samsung') {
                mediaSource = new MediaSource(streamUrl, streamType, MediaSource.MIME_TYPES.mpd);
            } else {

                switch (this._type) {
                    case 'VOD':
                        mediaSource = new MediaSource(streamUrl, streamType, MediaSource.MIME_TYPES.mpd);
                        break;
                    case 'RESTART':
                    case 'LIVE':
                        mediaSource = new MediaSource(streamUrl, streamType, MediaSource.MIME_TYPES.ism);
                        break;
                }
            }

            mediaSource.setStartBitRate(2500000);

            licenseOverride = this._getLicenseOverrideUrl(data);

            if (licenseOverride) {
                drmItem.setLicenseOverrideUri(licenseOverride);
            }

            if (deviceBrand === 'hbbtv' || deviceBrand === 'lg') {
                drmItem.setEmbeddedLicense(true);
            }

            drmItem.setProtectionHeaderUTF16(true);
            mediaSource.setDrmItem(drmItem);

            return mediaSource;
        },

        /**
         * Returns the license override url.
         *
         * @param {Object} data - The data.
         * @returns {string|null} - The license override url or null.
         * @private
         */
        _getLicenseOverrideUrl: function (data) {
            var contentProtection = Utils.getNested(data, 'contentProtection'),
                url = Utils.getNested(contentProtection, 'playready', 'licenseAcquisitionURL');

            if (!url || !contentProtection) {
                return null;
            }

            Utils.each(contentProtection, function (value, key) {
                var object = {};

                if (key !== 'playready') {
                    object[capitalizeFirstLetter(key)] = value;

                    url = Utils.addToQueryStringOfURL(url, object);
                }
            });

            return url;
        }
    });
});