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 // write the stream to our writebuffer, and write the buffer to disk if it's full
93 int CEncoder::WriteStream(const void *pBuffer, uint32_t iBytes)
95 if ((WRITEBUFFER_SIZE - m_dwWriteBufferPointer) > iBytes)
97 // writebuffer is big enough to fit data
98 memcpy(m_btWriteBuffer + m_dwWriteBufferPointer, pBuffer, iBytes);
99 m_dwWriteBufferPointer += iBytes;
104 // buffer is not big enough to fit data
105 if (m_dwWriteBufferPointer == 0)
107 // nothing in our buffer, just write the entire pBuffer to disk
108 return FileWrite(pBuffer, iBytes);
111 uint32_t dwBytesRemaining = iBytes - (WRITEBUFFER_SIZE - m_dwWriteBufferPointer);
112 // fill up our write buffer and write it to disk
113 memcpy(m_btWriteBuffer + m_dwWriteBufferPointer, pBuffer, (WRITEBUFFER_SIZE - m_dwWriteBufferPointer));
114 FileWrite(m_btWriteBuffer, WRITEBUFFER_SIZE);
115 m_dwWriteBufferPointer = 0;
117 // pbtRemaining = pBuffer + bytesWritten
118 uint8_t* pbtRemaining = (uint8_t *)pBuffer + (iBytes - dwBytesRemaining);
119 if (dwBytesRemaining > WRITEBUFFER_SIZE)
121 // data is not going to fit in our buffer, just write it to disk
122 if (FileWrite(pbtRemaining, dwBytesRemaining) == -1) return -1;
127 // copy remaining bytes to our currently empty writebuffer
128 memcpy(m_btWriteBuffer, pbtRemaining, dwBytesRemaining);
129 m_dwWriteBufferPointer = dwBytesRemaining;
135 // flush the contents of our writebuffer
136 int CEncoder::FlushStream()
139 if (m_dwWriteBufferPointer == 0) return 0;
141 iResult = FileWrite(m_btWriteBuffer, m_dwWriteBufferPointer);
142 m_dwWriteBufferPointer = 0;