/**
 * Copyright 2015 CANAL+ Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { MediaSource_ } from "../../../compat";
import { resetMediaSource } from "../../../core/init/utils/create_media_source";
import { AudioVideoSegmentBuffer } from "../../../core/segment_buffers/implementations";
import log from "../../../log";
import createCancellablePromise from "../../../utils/create_cancellable_promise";
import isNonEmptyString from "../../../utils/is_non_empty_string";
/**
 * Open the media source and create the `AudioVideoSegmentBuffer`.
 * @param {HTMLVideoElement} videoElement
 * @param {string} codec
 * @param {Object} cleanUpSignal
 * @returns {Promise.<Object>}
 */
export default function prepareSourceBuffer(videoElement, codec, cleanUpSignal) {
    return createCancellablePromise(cleanUpSignal, function (resolve, reject) {
        if (MediaSource_ == null) {
            throw new Error("No MediaSource Object was found in the current browser.");
        }
        // make sure the media has been correctly reset
        var oldSrc = isNonEmptyString(videoElement.src) ? videoElement.src :
            null;
        resetMediaSource(videoElement, null, oldSrc);
        log.info("Init: Creating MediaSource");
        var mediaSource = new MediaSource_();
        var objectURL = URL.createObjectURL(mediaSource);
        log.info("Init: Attaching MediaSource URL to the media element", objectURL);
        videoElement.src = objectURL;
        cleanUpSignal.register(function () {
            resetMediaSource(videoElement, mediaSource, objectURL);
        });
        mediaSource.addEventListener("sourceopen", onSourceOpen);
        mediaSource.addEventListener("webkitsourceopen", onSourceOpen);
        return function () {
            mediaSource.removeEventListener("sourceopen", onSourceOpen);
            mediaSource.removeEventListener("webkitsourceopen", onSourceOpen);
        };
        function onSourceOpen() {
            try {
                mediaSource.removeEventListener("sourceopen", onSourceOpen);
                mediaSource.removeEventListener("webkitsourceopen", onSourceOpen);
                resolve(new AudioVideoSegmentBuffer("video", codec, mediaSource));
            }
            catch (err) {
                reject(err);
            }
        }
    });
}
