/*
 * Copyright (C) 2011 Google Inc. All rights reserved.
 * Copyright (C) 2011, 2012, 2015 Ericsson AB. All rights reserved.
 * Copyright (C) 2013-2015 Apple Inc. All rights reserved.
 * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "MediaStream.h"

#if ENABLE(MEDIA_STREAM)

#include "Event.h"
#include "ExceptionCode.h"
#include "RealtimeMediaSourceCenter.h"
#include "MediaStreamRegistry.h"
#include "RealtimeMediaSource.h"
#include "MediaStreamTrackEvent.h"
#include "KURL.h"
#include <wtf/NeverDestroyed.h>

namespace WebCore {

static Vector<MediaStream*>& mediaStreams()
{
    static NeverDestroyed<Vector<MediaStream*>> streams;
    return streams;
}

static void registerMediaStream(MediaStream* stream)
{
    mediaStreams().append(stream);
}

static void unRegisterMediaStream(MediaStream* stream)
{
    Vector<MediaStream*>& allStreams = mediaStreams();
    size_t pos = allStreams.find(stream);
    if (pos != notFound)
        allStreams.remove(pos);
}

MediaStream* MediaStream::lookUp(const MediaStreamPrivate& privateStream)
{
    Vector<MediaStream*>& allStreams = mediaStreams();
    for(int i = 0; i < allStreams.size(); ++i) {
    	MediaStream* stream = allStreams[i];
        if (&stream->privateStream() == &privateStream)
            return stream;
    }

    return 0;
}

static URLRegistry* s_registry;

void MediaStream::setRegistry(URLRegistry& registry)
{
    ASSERT(!s_registry);
    s_registry = &registry;
}

MediaStream* MediaStream::lookUp(const KURL& url)
{
    if (!s_registry)
        return 0;

    return static_cast<MediaStream*>(s_registry->lookup(url.string()));
}

PassRefPtr<MediaStream> MediaStream::create(ScriptExecutionContext* context)
{
    return MediaStream::create(context);
}

PassRefPtr<MediaStream> MediaStream::create(ScriptExecutionContext* context, MediaStream& stream)
{
    return MediaStream::create(context, MediaStreamPrivate::create(Vector<RefPtr<MediaStreamTrackPrivate>>()));
}

PassRefPtr<MediaStream> MediaStream::create(ScriptExecutionContext* context, const MediaStreamTrackVector& tracks)
{
    return adoptRef(new MediaStream(context, tracks));
}

PassRefPtr<MediaStream> MediaStream::create(ScriptExecutionContext* context, Ref<MediaStreamPrivate>&& streamPrivate)
{
    return adoptRef(new MediaStream(context, WTFMove(streamPrivate)));
}

static inline MediaStreamTrackPrivateVector createTrackPrivateVector(const MediaStreamTrackVector& tracks)
{
    MediaStreamTrackPrivateVector trackPrivates;
    trackPrivates.reserveCapacity(tracks.size());
    for (auto& track : tracks)
        trackPrivates.append(&track->privateTrack());
    return trackPrivates;
}

MediaStream::MediaStream(ScriptExecutionContext* context, const MediaStreamTrackVector& tracks)
    : ContextDestructionObserver(context)
	, m_private(MediaStreamPrivate::create(createTrackPrivateVector(tracks)))
    , m_activityEventTimer(this, &MediaStream::activityEventTimerFired)
{
    // This constructor preserves MediaStreamTrack instances and must be used by calls originating
    // from the JavaScript MediaStream constructor.

    for(int i = 0; i < tracks.size(); ++i) {
    	const RefPtr<MediaStreamTrack>& track = tracks[i];
        track->addObserver(this);
        m_trackSet.add(track->id(), track);
    }

    m_isActive = m_private->active();
    m_private->addObserver(*this);
    registerMediaStream(this);
}

MediaStream::MediaStream(ScriptExecutionContext* context, Ref<MediaStreamPrivate>&& streamPrivate)
    : ContextDestructionObserver(context)
	, m_private(WTFMove(streamPrivate))
    , m_isActive(m_private->active())
    , m_activityEventTimer(this, &MediaStream::activityEventTimerFired)
{
    m_private->addObserver(*this);
    registerMediaStream(this);

    Vector<RefPtr<MediaStreamTrackPrivate>> tracks = m_private->tracks();
    for(int i = 0; i < tracks.size(); ++i) {
    	RefPtr<MediaStreamTrackPrivate>& trackPrivate = tracks[i];
        RefPtr<MediaStreamTrack> track = MediaStreamTrack::create(*context, *trackPrivate);
        track->addObserver(this);
        m_trackSet.add(track->id(), track.release());
    }
}

MediaStream::~MediaStream()
{
    unRegisterMediaStream(this);
    m_private->removeObserver(*this);
    for(auto it = m_trackSet.begin(); it != m_trackSet.end(); ++it)
    	it->second->removeObserver(this);
}

PassRefPtr<MediaStream> MediaStream::clone()
{
	MediaStreamTrackVector clonedTracks;
    clonedTracks.reserveInitialCapacity(m_trackSet.size());

    for(auto it = m_trackSet.begin(); it != m_trackSet.end(); ++it)
        clonedTracks.uncheckedAppend(it->second->clone());

    return MediaStream::create(scriptExecutionContext(), clonedTracks);
}

void MediaStream::addTrack(MediaStreamTrack& track)
{
    if (!internalAddTrack(track, StreamModifier::DomAPI))
        return;

    for(int i = 0; i < m_observers.size(); ++i)
    	m_observers[i]->didAddOrRemoveTrack();
}

void MediaStream::removeTrack(MediaStreamTrack& track)
{
    if (!internalRemoveTrack(track, StreamModifier::DomAPI))
        return;

    for(int i = 0; i < m_observers.size(); ++i)
    	m_observers[i]->didAddOrRemoveTrack();
}

MediaStreamTrack* MediaStream::getTrackById(String id)
{
    auto it = m_trackSet.find(id);
    if (it != m_trackSet.end())
        return it->second.get();

    return 0;
}

MediaStreamTrackVector MediaStream::getAudioTracks() const
{
    return trackVectorForType(RealtimeMediaSource::Type::Audio);
}

MediaStreamTrackVector MediaStream::getVideoTracks() const
{
    return trackVectorForType(RealtimeMediaSource::Type::Video);
}

MediaStreamTrackVector MediaStream::getTracks() const
{
	MediaStreamTrackVector tracks;
    tracks.reserveCapacity(m_trackSet.size());
    copyValuesToVector(m_trackSet, tracks);

    return tracks;
}

void MediaStream::contextDestroyed()
{
    ContextDestructionObserver::contextDestroyed();
}

void MediaStream::trackDidEnd()
{
    m_private->updateActiveState(MediaStreamPrivate::NotifyClientOption::Notify);
}

void MediaStream::activeStatusChanged()
{
    // Schedule the active state change and event dispatch since this callback may be called
    // synchronously from the DOM API (e.g. as a result of addTrack()).
    scheduleActiveStateChange();
}

void MediaStream::didAddTrack(MediaStreamTrackPrivate& trackPrivate)
{
    ScriptExecutionContext* context = scriptExecutionContext();
    if (!context)
        return;

    if (!getTrackById(trackPrivate.id()))
    	internalAddTrack(*MediaStreamTrack::create(*context, trackPrivate), StreamModifier::Platform);
}

void MediaStream::didRemoveTrack(MediaStreamTrackPrivate& trackPrivate)
{
    if(RefPtr<MediaStreamTrack> track = getTrackById(trackPrivate.id()))
    	internalRemoveTrack(*track, StreamModifier::Platform);
}

void MediaStream::addTrackFromPlatform(Ref<MediaStreamTrack>&& track)
{
    m_private->addTrack(&track->privateTrack(), MediaStreamPrivate::NotifyClientOption::Notify);
    internalAddTrack(track.get(), StreamModifier::Platform);
}

bool MediaStream::internalAddTrack(MediaStreamTrack& track, StreamModifier streamModifier)
{
    if (getTrackById(track.id()))
        return false;

    m_trackSet.add(track.id(), &track);
    track.addObserver(this);

    if (streamModifier == StreamModifier::DomAPI)
        m_private->addTrack(&track.privateTrack(), MediaStreamPrivate::NotifyClientOption::DontNotify);
    else
        dispatchEvent(MediaStreamTrackEvent::create(eventNames().addtrackEvent, false, false, &track));

    return true;
}

bool MediaStream::internalRemoveTrack(MediaStreamTrack& track, StreamModifier streamModifier)
{
    if (!m_trackSet.remove(track.id()))
        return false;

    track.removeObserver(this);

    if (streamModifier == StreamModifier::DomAPI)
        m_private->removeTrack(track.privateTrack(), MediaStreamPrivate::NotifyClientOption::DontNotify);
    else
        dispatchEvent(MediaStreamTrackEvent::create(eventNames().removetrackEvent, false, false, &track));

    return true;
}

void MediaStream::scheduleActiveStateChange()
{
    bool active = false;
    for(auto it = m_trackSet.begin(); it != m_trackSet.end(); ++it) {
    	auto& track = it->second;
        if (!track->ended()) {
            active = true;
            break;
        }
    }
    if (m_isActive == active)
        return;

    m_isActive = active;

    const AtomicString& eventName = m_isActive ? eventNames().inactiveEvent : eventNames().activeEvent;
    m_scheduledActivityEvents.append(Event::create(eventName, false, false));

    if (!m_activityEventTimer.isActive())
        m_activityEventTimer.startOneShot(0);
}

void MediaStream::activityEventTimerFired()
{
    Vector<RefPtr<Event>> events;
    events.swap(m_scheduledActivityEvents);

    for(int i = 0; i < events.size(); ++i)
        dispatchEvent(events[i].release());

    events.clear();
}

EventTargetInterface MediaStream::eventTargetInterface() const
{
    return MediaStreamEventTargetInterfaceType;
}

ScriptExecutionContext* MediaStream::scriptExecutionContext() const
{
    return ContextDestructionObserver::scriptExecutionContext();
}

URLRegistry& MediaStream::registry() const
{
    return MediaStreamRegistry::registry();
}

MediaStreamTrackVector MediaStream::trackVectorForType(RealtimeMediaSource::Type filterType) const
{
	MediaStreamTrackVector tracks;
    for(auto it = m_trackSet.begin(); it != m_trackSet.end(); ++it) {
    	const RefPtr<MediaStreamTrack>& track = it->second;
        if (track->source()->type() == filterType)
            tracks.append(track);
    }

    return tracks;
}

void MediaStream::addObserver(MediaStream::Observer* observer)
{
    if (m_observers.find(observer) == notFound)
        m_observers.append(observer);
}

void MediaStream::removeObserver(MediaStream::Observer* observer)
{
    size_t pos = m_observers.find(observer);
    if (pos != notFound)
        m_observers.remove(pos);
}

} // namespace WebCore

#endif // ENABLE(MEDIA_STREAM)
