video-service.js 5.06 KB
/*
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2016 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 */

/* globals Promise */
define('screens/player/runtime/impl/video-service', [
    'underscore',
    'jquery',
    'screens/player/runtime/admin',
    'screens/player/shared/serviceadmin',
    'screens/player/store/store',
    'screens/player/firmware/preferences/preferences',
    'module'
], function(_, $, Admin, ServiceAdmin, Store, Preferences, mod) {
    'use strict';

    /**
     * Implements the video service
     *
     * @class VideoService
     */
    var VideoService = {

        serviceId: mod.id,

        /**
         * @memberof VideoService
         */
        serviceName: 'com.adobe.aem.screens.videoservice',

        /**
         * Window IO to post messages across iFrames
         * @memberof VideoService
         */
        wio: null,

        /**
         * Activate method.
         * @returns {*|Promise} Optional a promise that resolves (or rejects) when completed.
         */
        activate: function() {
            return Promise.resolve();
        },

        /**
         * Deactivate method.
         */
        deactivate: function() {
            // theoretically, we should unregister the handler. but the service admin doesn't support it yet.
            this.wio = null;
        },

        isNativeVideoAvailable: function() {
                return !!((window.plugins) && (window.plugins.ScreensVideoPlayer));
        },

        /**
         * Initialize messaging across iFrames.
         * @param {WindowIO} wio - Messaging across iFrames
         */
        initMessaging: function(wio) {
            if (!this.wio) {
                this.wio = wio;
                this.wio.on('check-native-video', this.checkNativeVideo.bind(this));
                this.wio.on('native-video-play', this.playVideo.bind(this));
            }
        },

        /**
         * Check whether native playback is enabled.
         * @returns {boolean} true if Native video is enabled, false otherwise
         */
        checkNativeVideo: function() {
            var preferencesService = ServiceAdmin.getService(Preferences.serviceName);
            var config = preferencesService.getPreferences();
            // Check capability and also admin UI preference
            if ((window.plugins) && (window.plugins.ScreensVideoPlayer) && (this.wio) && (config.enableNativeVideo)) {
                console.log('Native Video is available and enabled in preferences.');
                this.wio.postMessage('native-video-enabled');
                return true;
            }

            console.log('Native video not available.');
            return false;
        },

        /**
         * Plays the given video
         * @param {Object} video - The video to play with information from the HTML5 element
         */
        playVideo: function(video) {
            console.log('native-video-play');

            var store = ServiceAdmin.getService(Store.serviceName);
            var adminUIVisible = store.getState()[Admin.NAMESPACE].visible;
            var osdUIVisible = store.getState().osd.visible;

            if ((window.plugins) && (window.plugins.ScreensVideoPlayer) && (adminUIVisible !== 'show') && (osdUIVisible !== 'show')) {
                var options = {
                    successCallback: this.handleVideoCompleted.bind(this),
                    errorCallback: this.handleVideoInterrupted.bind(this),
                    id: video.id
                };
                console.log('Playing ' + video.URL);
                window.plugins.ScreensVideoPlayer.playVideo(video.URL, options);
            }
            else {
                video.message = 'video-close-event';
                this.wio.postMessage('no-native-video', video);
            }
        },

        handleVideoCompleted: function(response) {
            console.log('Video ended');
            if (this.wio) {
                this.wio.postMessage('video-ended', response);
            }
            else {
                console.log('Messaging not initialized');
            }
        },

        handleVideoInterrupted: function(response) {
            console.log('Video interrupted');
            if (this.wio) {
                this.wio.postMessage('no-native-video', response);
            }
            else {
                console.log('Messaging not initialized');
            }
        }
    };

    return VideoService;

});

require([
    'screens/player/runtime/impl/video-service',
    'screens/player/shared/serviceadmin'
], function(Service, ServiceAdmin) {
    'use strict';
    ServiceAdmin.register(Service);
});