/** @file MOMStream.cpp
 *
 * Copyright (C) Espial Limited 2014 - Company confidential - All rights reserved.
 *
 */

#include "config.h"

#if ENABLE(RMAI)

#include "MOMStream.h"
#include "MOMPlayer.h"

#include "UtilsMOM.h"
#include "MOMException.h"
#include "EventNames.h"

namespace WebCore {

    PassRefPtr<MOMStream> MOMStream::create(ScriptExecutionContext* context, mom_stream stream)
{
    return adoptRef(new MOMStream(context, stream));
}

MOMStream::MOMStream(ScriptExecutionContext* context, mom_stream stream)
    : ActiveDOMObject(context),
      m_stream(mom_stream_keep(stream))
{
}

MOMStream::~MOMStream()
{
    mom_stream_dispose(m_stream);
}

EventTargetInterface MOMStream::eventTargetInterface() const
{
    return MOMStreamEventTargetInterfaceType;
}

void MOMStream::addMetaData( const String & key, const String & value )
{
    layers_string lkey = UtilsMOM::LayersStringFromString(key);
    layers_string lvalue = UtilsMOM::LayersStringFromString(value);
    mom_stream_add_meta_data(m_stream, lkey, lvalue);
    layers_string_dispose(lkey);
    layers_string_dispose(lvalue);
}

void MOMStream::connect( MOMPlayer* player, int mode, int offset, ExceptionCode& ec )
{
    if ( mode >= mom_stream_connect_mode__MAX )
    {
        MOMException::setFromMOMError(mom_err_INVALID, ec);
        return;
    }

    mom_error err = mom_player_connect_stream(player->player(), stream(), static_cast<mom_stream_connect_mode>(mode), offset);
    MOMException::setFromMOMError(err, ec);
}

void MOMStream::connect( MOMPlayer* player, int mode, ExceptionCode& ec )
{
	connect(player, mode, 0, ec);
}

void MOMStream::connect( MOMPlayer* player, ExceptionCode& ec )
{
	connect(player, 0, 0, ec);
}

} // namespace WebCore

#endif // ENABLE(RMAI)
