betajs/betajs-media

View on GitHub
src/video_player/player_support.js

Summary

Maintainability
C
1 day
Test Coverage
Scoped.define("module:Player.Support", [
    "base:Promise",
    "base:Objs",
    "browser:Info"
], function(Promise, Objs, Info) {
    return {

        VIDEO: [0, 0, 0, 28, 102, 116, 121, 112, 105, 115, 111, 109, 0, 0, 2, 0, 105, 115, 111, 109, 105, 115, 111, 50, 109, 112, 52, 49, 0, 0, 0, 8, 102, 114, 101, 101, 0, 0, 2, 239, 109, 100, 97, 116, 33, 16, 5, 32, 164, 27, 255, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 101, 100, 116, 115, 0, 0, 0, 28, 101, 108, 115, 116, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 47, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 100, 109, 100, 105, 97, 0, 0, 0, 32, 109, 100, 104, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 172, 68, 0, 0, 8, 0, 85, 196, 0, 0, 0, 0, 0, 45, 104, 100, 108, 114, 0, 0, 0, 0, 0, 0, 0, 0, 115, 111, 117, 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 83, 111, 117, 110, 100, 72, 97, 110, 100, 108, 101, 114, 0, 0, 0, 1, 15, 109, 105, 110, 102, 0, 0, 0, 16, 115, 109, 104, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 100, 105, 110, 102, 0, 0, 0, 28, 100, 114, 101, 102, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 12, 117, 114, 108, 32, 0, 0, 0, 1, 0, 0, 0, 211, 115, 116, 98, 108, 0, 0, 0, 103, 115, 116, 115, 100, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 87, 109, 112, 52, 97, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 16, 0, 0, 0, 0, 172, 68, 0, 0, 0, 0, 0, 51, 101, 115, 100, 115, 0, 0, 0, 0, 3, 128, 128, 128, 34, 0, 2, 0, 4, 128, 128, 128, 20, 64, 21, 0, 0, 0, 0, 1, 244, 0, 0, 1, 243, 249, 5, 128, 128, 128, 2, 18, 16, 6, 128, 128, 128, 1, 2, 0, 0, 0, 24, 115, 116, 116, 115, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 0, 28, 115, 116, 115, 99, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 28, 115, 116, 115, 122, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 115, 0, 0, 1, 116, 0, 0, 0, 20, 115, 116, 99, 111, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 44, 0, 0, 0, 98, 117, 100, 116, 97, 0, 0, 0, 90, 109, 101, 116, 97, 0, 0, 0, 0, 0, 0, 0, 33, 104, 100, 108, 114, 0, 0, 0, 0, 0, 0, 0, 0, 109, 100, 105, 114, 97, 112, 112, 108, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 45, 105, 108, 115, 116, 0, 0, 0, 37, 169, 116, 111, 111, 0, 0, 0, 29, 100, 97, 116, 97, 0, 0, 0, 1, 0, 0, 0, 0, 76, 97, 118, 102, 53, 54, 46, 52, 48, 46, 49, 48, 49],
        AUDIO: [255, 227, 24, 196, 0, 0, 0, 3, 72, 1, 64, 0, 0, 4, 132, 16, 31, 227, 192, 225, 76, 255, 67, 12, 255, 221, 27, 255, 228, 97, 73, 63, 255, 195, 131, 69, 192, 232, 223, 255, 255, 207, 102, 239, 255, 255, 255, 101, 158, 206, 70, 20, 59, 255, 254, 95, 70, 149, 66, 4, 16, 128, 0, 2, 2, 32, 240, 138, 255, 36, 106, 183, 255, 227, 24, 196, 59, 11, 34, 62, 80, 49, 135, 40, 0, 253, 29, 191, 209, 200, 141, 71, 7, 255, 252, 152, 74, 15, 130, 33, 185, 6, 63, 255, 252, 195, 70, 203, 86, 53, 15, 255, 255, 247, 103, 76, 121, 64, 32, 47, 255, 34, 227, 194, 209, 138, 76, 65, 77, 69, 51, 46, 57, 55, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 255, 227, 24, 196, 73, 13, 153, 210, 100, 81, 135, 56, 0, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170, 170],

        /**
         * @param options
         * @returns {*}
         */
        canAutoplayVideo: function(options) {
            options = this._setupDefaultValues(options);
            var blob = new Blob([new Uint8Array(this.VIDEO)], {
                type: 'video/mp4'
            });
            return this._startPlayback(options, function() {
                return {
                    element: document.createElement('video'),
                    source: URL.createObjectURL(blob)
                };
            });
        },

        /**
         * @param options
         * @returns {*}
         */
        canAutoplayAudio: function(options) {
            options = this._setupDefaultValues(options);
            var blob = new Blob([new Uint8Array(this.AUDIO)], {
                type: 'audio/mpeg'
            });
            return this._startPlayback(options, function() {
                return {
                    element: document.createElement('audio'),
                    source: URL.createObjectURL(blob)
                };
            });
        },

        resolutionToLabel: function(width, height) {
            if (height < 300)
                return "SD";
            if (height < 400)
                return "360p";
            if (height < 500)
                return "480p";
            return "HD";
        },

        elementFileInfo: function(elementType, elementEvent, elementAttrs, file) {
            try {
                var element = document.createElement(elementType);
                Objs.iter(elementAttrs, function(value, key) {
                    element[key] = value;
                });
                var promise = Promise.create();
                var failed = false;
                var timer = setTimeout(function() {
                    failed = true;
                    if (element.error != undefined) {
                        promise.asyncError(element.error);
                    } else {
                        promise.asyncError("Timeout");
                    }
                }, 1000);
                element[elementEvent] = function() {
                    if (failed)
                        return;
                    clearTimeout(timer);
                    promise.asyncSuccess(element);
                };
                element.src = (window.URL || window.webkitURL).createObjectURL(file);
                return promise;
            } catch (e) {
                return Promise.error(e);
            }
        },

        videoFileInfo: function(file) {
            return this.elementFileInfo("video", "onloadeddata", {
                volume: 0,
                muted: true,
                preload: true
            }, file).mapSuccess(function(video) {
                return {
                    width: video.videoWidth,
                    height: video.videoHeight,
                    duration: video.duration
                };
            }).mapError(function(error) {
                return error;
            });
        },

        audioFileInfo: function(file) {
            return this.elementFileInfo("audio", "onloadeddata", {
                volume: 0,
                muted: true,
                preload: true
            }, file).mapSuccess(function(audio) {
                return {
                    duration: audio.duration
                };
            });
        },

        imageFileInfo: function(file) {
            return this.elementFileInfo("img", "onload", {}, file).mapSuccess(function(image) {
                return {
                    width: image.width,
                    height: image.height
                };
            });
        },

        _setupDefaultValues: function(options) {
            // options will override default values
            return Objs.tree_extend({
                muted: false,
                timeout: null,
                playsinline: false
            }, options);
        },

        _startPlayback: function(options, elementCallback) {
            var playResult, timeoutId, sendOutput;
            var muted = options.muted,
                timeout = options.timeout,
                playsinline = options.playsinline;

            var _elementCallback = elementCallback(),
                element = _elementCallback.element,
                source = _elementCallback.source;

            var promise = Promise.create();

            if (Info.isMobile() && Info.isiOS() && !playsinline) {
                return promise.asyncSuccess({
                    result: false,
                    error: "iOS mobile should be used only with playsinline"
                });
            }

            element.muted = muted;
            if (muted === true) {
                element.setAttribute('muted', 'muted');
            }
            // indicates that the video is to be played "inline",
            // that is within the element's playback area.
            if (playsinline === true) {
                element.setAttribute('playsinline', 'playsinline');
            }

            element.src = source;

            playResult = element.play();

            if (timeout) {
                timeoutId = setTimeout(function() {
                    sendOutput(false, new Error('Timeout ' + timeout + ' ms has been reached'));
                }, timeout);
            }

            // promise.asyncSuccess();
            // promise.asyncError('Could not start recording');

            sendOutput = function sendOutput(result) {
                var error = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

                // Clean up to avoid MediaElementLeak
                element.remove();
                element.srcObject = null;

                if (typeof timeoutId !== "undefined") clearTimeout(timeoutId);
                promise.asyncSuccess({
                    result: result,
                    error: error
                });
            };

            if (playResult !== undefined) {
                playResult.then(function() {
                    return sendOutput(true);
                    /*jshint es5: true */
                })['catch'](function(playError) {
                    return sendOutput(false, playError);
                });
            } else {
                // If it's an old player where promise is not supported, then autoplay allowed
                sendOutput(true);
            }

            return promise;
        }

    };
});