1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
|
#include "./item.h"
#include <strsafe.h>
#define DECODEBUFFER_SIZE 4096*4
DWORD BurnerItem::zeroMem[] = {0x00,};
BurnerItem::BurnerItem(void)
{
fullname = NULL;
title = NULL;
length = 0;
sizeBytes = 0;
sizeSectors = 0;
preGap = 0;
fposition = 0;
fhandle = NULL;
heap = GetProcessHeap();
ZeroMemory(ISRC, 12);
itemStatus = BURNERITEM_READY;
percentCompleted = 0;
errorCode = BURNERITEM_SUCCESS;
if (zeroMem[0] == 0x00) FillMemory(zeroMem, ZEROMEM_SIZE, 0xFFFFFFFF);
}
BurnerItem::~BurnerItem(void)
{
Destroy();
}
HRESULT BurnerItem::Create(const wchar_t* fullname, const wchar_t *title, int length)
{
if (this->fullname)
{
errorCode = BURNERITEM_ALREADYCREATED;
return errorCode;
}
this->fullname = DuplicateString(heap, fullname, lstrlenW(fullname));
this->title = DuplicateString(heap, title, lstrlenW(title));
this->length = length;
itemStatus = BURNERITEM_READY;
errorCode = BURNERITEM_SUCCESS;
return errorCode;
}
void BurnerItem::Destroy(void)
{
if (fullname)
{
HeapFree(heap, NULL, fullname);
fullname = NULL;
}
if (title)
{
HeapFree(heap, NULL, title);
title = NULL;
}
length = 0;
sizeBytes = 0;
sizeSectors = 0;
preGap = 0;
fposition = 0;
fhandle = NULL;
ZeroMemory(ISRC, 12);
}
HRESULT BurnerItem::Decode(BurnManager *manager, void *fileHandle, BURNERITEMCALLBACK notifyCB, void *userparam)
{
itemStatus = BURNERITEM_DECODING;
if (notifyCB) notifyCB(this, userparam, BURNERITEM_DECODESTARTING, BURNERITEM_SUCCESS);
if (!fullname)
{
errorCode = BURNERITEM_BADFILENAME;
itemStatus = BURNERITEM_DECODED;
if (notifyCB) notifyCB(this, userparam, BURNERITEM_DECODEFINISHED, errorCode);
return errorCode;
}
ifc_audiostream *stream = manager->CreateDecoder(fullname);
if (!stream)
{
errorCode = BURNERITEM_UNABLEOPENFILE;
itemStatus = BURNERITEM_DECODED;
if (notifyCB) notifyCB(this, userparam, BURNERITEM_DECODEFINISHED, errorCode);
return errorCode;
}
fposition = 0;
LONG fposLow(0), fposHigh(0);
fposLow = SetFilePointer(fileHandle, 0, &fposHigh, FILE_CURRENT);
fposition = (((__int64)fposHigh) << 32) | fposLow;
DWORD decoded = 0;
sizeBytes = 0;
sizeSectors = 0;
double percent = 0.0;
int iopercent = -1;
percentCompleted = 0;
errorCode = BURNERITEM_DECODEPROGRESS;
do
{
unsigned __int8 buffer[DECODEBUFFER_SIZE] = {0};
int decode_killswitch=0, decode_error;
decoded = (DWORD)stream->ReadAudio(buffer, DECODEBUFFER_SIZE, &decode_killswitch, &decode_error);
if (decoded > 0)
{
DWORD written = 0;
if (!WriteFile(fileHandle, buffer, decoded, &written, NULL))
{
errorCode = BURNERITEM_WRITEERROR;
break;
}
sizeBytes += decoded;
double step = ((double)decoded) / (((double)length) * 176.4f)*100.0f;
percent += step;
percentCompleted = (int)percent;
if (iopercent != percentCompleted)
{
iopercent = percentCompleted;
if (notifyCB)
{
if (BURNERITEM_CONTINUE != notifyCB(this, userparam, BURNERITEM_DECODEPROGRESS, percentCompleted))
{
errorCode = BURNERITEM_DECODECANCELING;
break;
}
}
}
}
} while(decoded);
if (stream) manager->CloseDecoder(stream);
itemStatus = BURNERITEM_DECODED;
DWORD notifyCode = BURNERITEM_DECODEFINISHED;
switch(errorCode)
{
case BURNERITEM_DECODECANCELING:
errorCode = BURNERITEM_ABORTED;
itemStatus = BURNERITEM_ABORTED;
break;
case BURNERITEM_DECODEPROGRESS:
sizeSectors = (DWORD)(sizeBytes/2352 + ((sizeBytes%2352) ? 1 : 0));
errorCode = BURNERITEM_SUCCESS;
break;
}
if (notifyCB) notifyCB(this, userparam, notifyCode, errorCode);
return errorCode;
}
HRESULT BurnerItem::AddStream(obj_primo *primoSDK, void *fileHandle)
{
fhandle = fileHandle;
needSetFilePos = TRUE;
streamedSize = 0;
errorCode = primoSDK->AddAudioStream(StreamFiller, this, 0, GetSizeInSectors());
return errorCode;
}
void BurnerItem::SetPreGap(unsigned int preGap)
{
this->preGap = preGap;
}
void BurnerItem::SetISRC(unsigned __int8 *ISRC)
{
CopyMemory(this->ISRC, ISRC, 12);
}
int BurnerItem::GetStatus(DWORD *retCode)
{
if(retCode)
{
switch(itemStatus)
{
case BURNERITEM_DECODING:
case BURNERITEM_BURNING:
*retCode = percentCompleted;
break;
default:
*retCode = errorCode;
}
}
return itemStatus;
}
DWORD BurnerItem::StreamFiller(PBYTE pBuffer, DWORD dwBytesRequested, PDWORD pdwBytesWritten, PVOID pContext)
{
BurnerItem *item = (BurnerItem*) pContext;
if (item->needSetFilePos)
{
LONG fposLow = (LONG) item->fposition;
LONG fposHigh = (LONG) (item->fposition >> 32);
SetFilePointer(item->fhandle, fposLow, &fposHigh, FILE_BEGIN);
item->streamedSize = 0;
item->needSetFilePos = FALSE;
}
DWORD needToRead;
item->streamedSize += dwBytesRequested;
// check if we need to fill with zero
if (item->streamedSize > item->sizeBytes)
{
needToRead = (DWORD)min(0, ((long)item->sizeBytes) - long(item->streamedSize - dwBytesRequested));
}
else
{
needToRead = dwBytesRequested;
}
// read from file
if (needToRead)
{
*pdwBytesWritten = 0;
if (!ReadFile(item->fhandle, pBuffer, needToRead, pdwBytesWritten, NULL))
{
DWORD le = GetLastError();
item->itemStatus = BURNERITEM_BURNED;
item->errorCode = BURNERITEM_READSTREAMERROR;
}
//else
//{
// LONG fposLow(0), fposHigh(0);
// fposLow = SetFilePointer(item->fhandle, 0, &fposHigh, FILE_CURRENT);
// SetFilePointer(item->fhandle, -((LONG)*pdwBytesWritten), NULL, FILE_CURRENT);
// DWORD written(0);
// long needToZerro(*pdwBytesWritten)l
// while(needToZerro > 0)
// {
// DWORD write = min(ZEROMEM_SIZE*sizeof(DWORD), needToZerro);
// if (!WriteFile(item->fhandle, &zeroMem, write, &written, NULL)) break;
// needToZerro -= written;
// }
// SetFilePointer(item->fhandle, fposLow, &fposHigh, FILE_BEGIN);
//}
}
else
{
*pdwBytesWritten = 0;
}
return (BURNERITEM_SUCCESS == item->errorCode) ? PRIMOSDK_OK : PRIMOSDK_ERROR;
}
wchar_t* BurnerItem::DuplicateString(void *heap, const wchar_t *source, unsigned int cchSource)
{
wchar_t *dest = (wchar_t*) HeapAlloc(heap, NULL, (cchSource + 1) * sizeof(wchar_t));
CopyMemory(dest, source, cchSource * sizeof(wchar_t));
dest[cchSource] = 0x0000;
return dest;
}
|