2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
22 #include "filesystem/File.h"
23 #include "utils/log.h"
28 m_dwWriteBufferPointer = 0;
31 m_iInBitsPerSample = 0;
39 bool CEncoder::Init(const char* strFile, int iInChannels, int iInRate, int iInBits)
41 if (strFile == NULL) return false;
43 m_dwWriteBufferPointer = 0;
46 m_iInChannels = iInChannels;
47 m_iInSampleRate = iInRate;
48 m_iInBitsPerSample = iInBits;
50 if (!FileCreate(strFile))
52 CLog::Log(LOGERROR, "Error: Cannot open file: %s", strFile);
58 bool CEncoder::FileCreate(const char* filename)
62 m_file = new XFILE::CFile;
64 return m_file->OpenForWrite(filename, true);
68 bool CEncoder::FileClose()
79 // return total bytes written, or -1 on error
80 int CEncoder::FileWrite(const void *pBuffer, uint32_t iBytes)
85 uint32_t dwBytesWritten = m_file->Write(pBuffer, iBytes);
89 return dwBytesWritten;
92 int64_t CEncoder::FileSeek(int64_t iFilePosition, int iWhence)
97 return m_file->Seek(iFilePosition, iWhence);
100 // write the stream to our writebuffer, and write the buffer to disk if it's full
101 int CEncoder::WriteStream(const void *pBuffer, uint32_t iBytes)
103 if ((WRITEBUFFER_SIZE - m_dwWriteBufferPointer) > iBytes)
105 // writebuffer is big enough to fit data
106 memcpy(m_btWriteBuffer + m_dwWriteBufferPointer, pBuffer, iBytes);
107 m_dwWriteBufferPointer += iBytes;
112 // buffer is not big enough to fit data
113 if (m_dwWriteBufferPointer == 0)
115 // nothing in our buffer, just write the entire pBuffer to disk
116 return FileWrite(pBuffer, iBytes);
119 uint32_t dwBytesRemaining = iBytes - (WRITEBUFFER_SIZE - m_dwWriteBufferPointer);
120 // fill up our write buffer and write it to disk
121 memcpy(m_btWriteBuffer + m_dwWriteBufferPointer, pBuffer, (WRITEBUFFER_SIZE - m_dwWriteBufferPointer));
122 FileWrite(m_btWriteBuffer, WRITEBUFFER_SIZE);
123 m_dwWriteBufferPointer = 0;
125 // pbtRemaining = pBuffer + bytesWritten
126 uint8_t* pbtRemaining = (uint8_t *)pBuffer + (iBytes - dwBytesRemaining);
127 if (dwBytesRemaining > WRITEBUFFER_SIZE)
129 // data is not going to fit in our buffer, just write it to disk
130 if (FileWrite(pbtRemaining, dwBytesRemaining) == -1) return -1;
135 // copy remaining bytes to our currently empty writebuffer
136 memcpy(m_btWriteBuffer, pbtRemaining, dwBytesRemaining);
137 m_dwWriteBufferPointer = dwBytesRemaining;
143 // flush the contents of our writebuffer
144 int CEncoder::FlushStream()
147 if (m_dwWriteBufferPointer == 0) return 0;
149 iResult = FileWrite(m_btWriteBuffer, m_dwWriteBufferPointer);
150 m_dwWriteBufferPointer = 0;