#ifndef AVIFILE_ITERATOR_H
#define AVIFILE_ITERATOR_H

#include <default.h>
#include <vector>
#include "asffmt.h"

struct chunk_info
{
    unsigned char keyframe :1;
    int packet_id; // anyone has file with >2^31 packets?
    unsigned int fragment_id;
    unsigned int object_start_time;
    unsigned int object_length;
};

class ASFStreamSeekInfo: public std::vector<chunk_info>
{
public:
    static const framepos_t ERR = ~0U;

    framepos_t prevKeyFrame(framepos_t kf = ERR) const;
    framepos_t nextKeyFrame(framepos_t kf = ERR) const;
    framepos_t nearestKeyFrame(framepos_t kf = ERR) const;
    framepos_t find(unsigned int time) const;
};

/**
 This logic is needed to properly handle reading from networked streams.
 Each stream owns an 'iterator' that can be used to access packets.
 If the stream is local, it allows iterators for different streams
 to point at completely independent packets ( and seek independently ).
 If it isn't, seek() on one iterator invalidates the other, and when
 seek() is called on invalidated iterator, it's automatically
 reposition at the place of last seek.
**/

// Interface
class Iterator
{
protected:
    int m_iRefcount;
public:
    virtual ~Iterator() {}
    virtual const ASFStreamSeekInfo* getSeekInfo()		=0;
    virtual void addRef() { m_iRefcount++; }
    virtual void release() { m_iRefcount--; if(!m_iRefcount) delete this; }
    virtual HRESULT seek(double timestamp, chunk_info* pch)	=0;
    virtual HRESULT seek(int64_t timestamp, chunk_info* pch)	=0;
    virtual packet getPacket(bool& success)			=0;
    virtual void next()						=0;
    virtual void undoPosition()					=0;
    virtual void undo()						=0;
    virtual void forgetUndo()					=0;
};

#endif // AVIFILE_ITERATOR_H
