/*
 * SmoothIndexReader.cpp
 *****************************************************************************
 * Copyright (C) 2015 - VideoLAN and VLC authors
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "SmoothIndexReader.hpp"
#include "../../adaptive/playlist/BaseRepresentation.h"
#include "../../adaptive/playlist/BaseAdaptationSet.h"
#include "../../adaptive/playlist/SegmentTemplate.h"
#include "../../adaptive/playlist/SegmentTimeline.h"
#include "../../adaptive/playlist/BasePlaylist.hpp"

using namespace adaptive::mp4;
using namespace smooth::mp4;

SmoothIndexReader::SmoothIndexReader(vlc_object_t *obj)
    : AtomsReader(obj)
{
}

bool SmoothIndexReader::parseIndex(block_t *p_block, BaseRepresentation *rep, uint64_t sequence)
{
    if(!rep || !parseBlock(p_block))
        return false;

    /* Fixup moof sequence as they do not monotonically increase
       nor provide a way to have moof time on implicit discontinuity */
    const MP4_Box_t *mfhd_box = MP4_BoxGet( rootbox, "moof/mfhd" );
    if( mfhd_box )
        SetDWBE( &p_block->p_buffer[mfhd_box->i_pos + 8 + 4], sequence );

    /* Do track ID fixup, must match track #1 */
    const MP4_Box_t *tfhd_box = MP4_BoxGet( rootbox, "moof/traf/tfhd" );
    if ( tfhd_box )
        SetDWBE( &p_block->p_buffer[tfhd_box->i_pos + 8 + 4], 0x01 );

    if(!rep->getPlaylist()->isLive())
        return true;

    const MP4_Box_t *uuid_box = MP4_BoxGet( rootbox, "moof/traf/uuid" );
    while( uuid_box && uuid_box->i_type == ATOM_uuid )
    {
        if ( !CmpUUID( &uuid_box->i_uuid, &TfrfBoxUUID ) )
            break;
        uuid_box = uuid_box->p_next;
    }

    if(!uuid_box)
        return false;

    SegmentTimeline *timelineadd = new (std::nothrow) SegmentTimeline(nullptr);
    if (timelineadd)
    {
        const MP4_Box_data_tfrf_t *p_tfrfdata = uuid_box->data.p_tfrf;
        for ( uint8_t i=0; i<p_tfrfdata->i_fragment_count; i++ )
        {
            uint64_t dur = p_tfrfdata->p_tfrf_data_fields[i].i_fragment_duration;
            uint64_t stime = p_tfrfdata->p_tfrf_data_fields[i].i_fragment_abs_time;
            timelineadd->addElement(i+1, dur, 0, stime);
        }

        rep->inheritSegmentTemplate()->
             inheritSegmentTimeline()->
             updateWith(*timelineadd);
        delete timelineadd;

#ifndef NDEBUG
        msg_Dbg(rep->getPlaylist()->getVLCObject(), "Updated timeline from tfrf");
        rep->debug(rep->getPlaylist()->getVLCObject(), 0);
#endif
    }

    return true;
}
