00001
00006 #include <cassert>
00007 #include "mp3stream.h"
00008 #include "except.h"
00009
00010 #ifdef DEBUG
00011 #include <iostream>
00012 #endif
00013
00014 MP3Stream::MP3Stream(Bitstream &bs, int type) : m_stream(bs)
00015 {
00016 m_type = type;
00017 m_ff = m_f = new MP3Frame(this);
00018 m_cutbytes = 0;
00019 }
00020
00021 MP3Stream::~MP3Stream()
00022 {
00023 while(m_ff)
00024 m_ff = m_ff->Destroy();
00025 }
00026
00027 MP3Frame *MP3Stream::First() const
00028 {
00029 return m_f;
00030 }
00031
00032 void MP3Stream::SetFirst(MP3Frame *frame)
00033 {
00034 assert(frame);
00035 m_f = frame;
00036 }
00037
00038 Bitstream &MP3Stream::GetBitstream() const
00039 {
00040 return m_stream;
00041 }
00042
00043 void MP3Stream::WriteData(Bitstream &bs, int numbytes, int startpos)
00044 {
00045 int cutstart, size, writesize;
00046
00047 while(numbytes > 0 && m_f)
00048 {
00049 if(!m_cutbytes)
00050 m_f->SetDataBegin(startpos);
00051
00052 size = m_f->DataSize();
00053
00054 cutstart = m_cutbytes ? (size - m_cutbytes) : 0;
00055
00056
00057 if(size - cutstart > numbytes )
00058 m_cutbytes = size - cutstart - numbytes;
00059 else
00060 m_cutbytes = 0;
00061
00062
00063 writesize = size - cutstart - m_cutbytes;
00064 assert(writesize <= numbytes);
00065 assert(writesize >= 0);
00066
00067 if(writesize > 0)
00068 m_f->WriteData(bs, m_ff, cutstart, m_cutbytes);
00069
00070 numbytes -= writesize;
00071 startpos += writesize;
00072
00073 try {
00074 if(!m_cutbytes)
00075 m_f = m_f->Next();
00076 }
00077 catch(eEOF e)
00078 {
00079 m_f = 0;
00080 }
00081 }
00082
00083 if(numbytes > 0)
00084 {
00085 #ifdef DEBUG
00086 std::cout << "append " << numbytes << " zeroes" << std::endl;
00087 #endif
00088
00089 bs.WriteNullBytes(numbytes);
00090 }
00091 }
00092
00093 void MP3Stream::SeekTo(MP3Frame *frame)
00094 {
00095
00096 while(m_ff != frame && !m_ff->isLocked())
00097 {
00098 if(m_f == m_ff) m_f = frame;
00099 m_ff = m_ff->Destroy();
00100 }
00101 }
00102
00103 int MP3Stream::WriteMode() const
00104 {
00105 return m_type;
00106 }