Sync with trunk r58740.
[reactos.git] / dll / directx / dmusic / buffer.c
1 /*
2 * IDirectMusicBuffer Implementation
3 *
4 * Copyright (C) 2003-2004 Rok Mandeljc
5 * Copyright (C) 2012 Christian Costa
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "dmusic_private.h"
23 //#include "initguid.h"
24 #include "dmksctrl.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
27
28 static inline IDirectMusicBufferImpl *impl_from_IDirectMusicBuffer(IDirectMusicBuffer *iface)
29 {
30 return CONTAINING_RECORD(iface, IDirectMusicBufferImpl, IDirectMusicBuffer_iface);
31 }
32
33 /* IDirectMusicBufferImpl IUnknown part: */
34 static HRESULT WINAPI IDirectMusicBufferImpl_QueryInterface(LPDIRECTMUSICBUFFER iface, REFIID riid, LPVOID *ret_iface)
35 {
36 TRACE("(%p)->(%s, %p)\n", iface, debugstr_dmguid(riid), ret_iface);
37
38 if (IsEqualIID(riid, &IID_IUnknown) ||
39 IsEqualIID(riid, &IID_IDirectMusicBuffer))
40 {
41 IDirectMusicBuffer_AddRef(iface);
42 *ret_iface = iface;
43 return S_OK;
44 }
45
46 *ret_iface = NULL;
47
48 WARN("(%p)->(%s, %p): not found\n", iface, debugstr_dmguid(riid), ret_iface);
49
50 return E_NOINTERFACE;
51 }
52
53 static ULONG WINAPI IDirectMusicBufferImpl_AddRef(LPDIRECTMUSICBUFFER iface)
54 {
55 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
56 ULONG ref = InterlockedIncrement(&This->ref);
57
58 TRACE("(%p)->(): new ref = %u\n", iface, ref);
59
60 DMUSIC_LockModule();
61
62 return ref;
63 }
64
65 static ULONG WINAPI IDirectMusicBufferImpl_Release(LPDIRECTMUSICBUFFER iface)
66 {
67 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
68 ULONG ref = InterlockedDecrement(&This->ref);
69
70 TRACE("(%p)->(): new ref = %u\n", iface, ref);
71
72 if (!ref) {
73 HeapFree(GetProcessHeap(), 0, This->data);
74 HeapFree(GetProcessHeap(), 0, This);
75 }
76
77 DMUSIC_UnlockModule();
78
79 return ref;
80 }
81
82 /* IDirectMusicBufferImpl IDirectMusicBuffer part: */
83 static HRESULT WINAPI IDirectMusicBufferImpl_Flush(LPDIRECTMUSICBUFFER iface)
84 {
85 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
86
87 TRACE("(%p)->()\n", iface);
88
89 This->write_pos = 0;
90
91 return S_OK;
92 }
93
94 static HRESULT WINAPI IDirectMusicBufferImpl_TotalTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prtTime)
95 {
96 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
97
98 FIXME("(%p, %p): stub\n", This, prtTime);
99
100 return S_OK;
101 }
102
103 static HRESULT WINAPI IDirectMusicBufferImpl_PackStructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME ref_time, DWORD channel_group, DWORD channel_message)
104 {
105 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
106 DWORD new_write_pos = This->write_pos + sizeof(DMUS_EVENTHEADER) + sizeof(DWORD);
107 DMUS_EVENTHEADER header;
108
109 TRACE("(%p)->(0x%s, %u, 0x%x)\n", iface, wine_dbgstr_longlong(ref_time), channel_group, channel_message);
110
111 if (new_write_pos > This->size)
112 return DMUS_E_BUFFER_FULL;
113
114 /* Channel_message 0xZZYYXX (3 bytes) is a midi message where XX = status byte, YY = byte 1 and ZZ = byte 2 */
115
116 if (!(channel_message & 0x80))
117 {
118 /* Status byte MSB is always set */
119 return DMUS_E_INVALID_EVENT;
120 }
121
122 if (!This->write_pos)
123 This->start_time = ref_time;
124
125 header.cbEvent = 3; /* Midi message takes 4 bytes space but only 3 are relevant */
126 header.dwChannelGroup = channel_group;
127 header.rtDelta = ref_time - This->start_time;
128 header.dwFlags = DMUS_EVENT_STRUCTURED;
129
130 memcpy(This->data + This->write_pos, &header, sizeof(header));
131 *(DWORD*)(This->data + This->write_pos + sizeof(header)) = channel_message;
132 This->write_pos = new_write_pos;
133
134 return S_OK;
135 }
136
137 static HRESULT WINAPI IDirectMusicBufferImpl_PackUnstructured(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME rt, DWORD dwChannelGroup, DWORD cb, LPBYTE lpb)
138 {
139 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
140
141 FIXME("(%p, 0x%s, %d, %d, %p): stub\n", This, wine_dbgstr_longlong(rt), dwChannelGroup, cb, lpb);
142
143 return S_OK;
144 }
145
146 static HRESULT WINAPI IDirectMusicBufferImpl_ResetReadPtr(LPDIRECTMUSICBUFFER iface)
147 {
148 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
149
150 FIXME("(%p): stub\n", This);
151
152 return S_OK;
153 }
154
155 static HRESULT WINAPI IDirectMusicBufferImpl_GetNextEvent(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME prt, LPDWORD pdwChannelGroup, LPDWORD pdwLength, LPBYTE* ppData)
156 {
157 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
158
159 FIXME("(%p, %p, %p, %p, %p): stub\n", This, prt, pdwChannelGroup, pdwLength, ppData);
160
161 return S_OK;
162 }
163
164 static HRESULT WINAPI IDirectMusicBufferImpl_GetRawBufferPtr(LPDIRECTMUSICBUFFER iface, LPBYTE* data)
165 {
166 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
167
168 TRACE("(%p)->(%p)\n", iface, data);
169
170 if (!data)
171 return E_POINTER;
172
173 *data = This->data;
174
175 return S_OK;
176 }
177
178 static HRESULT WINAPI IDirectMusicBufferImpl_GetStartTime(LPDIRECTMUSICBUFFER iface, LPREFERENCE_TIME ref_time)
179 {
180 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
181
182 TRACE("(%p)->(%p)\n", iface, ref_time);
183
184 if (!ref_time)
185 return E_POINTER;
186 if (!This->write_pos)
187 return DMUS_E_BUFFER_EMPTY;
188
189 *ref_time = This->start_time;
190
191 return S_OK;
192 }
193
194 static HRESULT WINAPI IDirectMusicBufferImpl_GetUsedBytes(LPDIRECTMUSICBUFFER iface, LPDWORD used_bytes)
195 {
196 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
197
198 TRACE("(%p)->(%p)\n", iface, used_bytes);
199
200 if (!used_bytes)
201 return E_POINTER;
202
203 *used_bytes = This->write_pos;
204
205 return S_OK;
206 }
207
208 static HRESULT WINAPI IDirectMusicBufferImpl_GetMaxBytes(LPDIRECTMUSICBUFFER iface, LPDWORD max_bytes)
209 {
210 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
211
212 TRACE("(%p)->(%p)\n", iface, max_bytes);
213
214 if (!max_bytes)
215 return E_POINTER;
216
217 *max_bytes = This->size;
218
219 return S_OK;
220 }
221
222 static HRESULT WINAPI IDirectMusicBufferImpl_GetBufferFormat(LPDIRECTMUSICBUFFER iface, LPGUID format)
223 {
224 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
225
226 TRACE("(%p)->(%p)\n", iface, format);
227
228 if (!format)
229 return E_POINTER;
230
231 *format = This->format;
232 return S_OK;
233 }
234
235 static HRESULT WINAPI IDirectMusicBufferImpl_SetStartTime(LPDIRECTMUSICBUFFER iface, REFERENCE_TIME ref_time)
236 {
237 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
238
239 TRACE("(%p)->(0x%s)\n", This, wine_dbgstr_longlong(ref_time));
240
241 This->start_time = ref_time;
242
243 return S_OK;
244 }
245
246 static HRESULT WINAPI IDirectMusicBufferImpl_SetUsedBytes(LPDIRECTMUSICBUFFER iface, DWORD used_bytes)
247 {
248 IDirectMusicBufferImpl *This = impl_from_IDirectMusicBuffer(iface);
249
250 TRACE("(%p)->(%u)\n", iface, used_bytes);
251
252 if (used_bytes > This->size)
253 return DMUS_E_BUFFER_FULL;
254
255 This->write_pos = used_bytes;
256
257 return S_OK;
258 }
259
260 static const IDirectMusicBufferVtbl DirectMusicBuffer_Vtbl = {
261 IDirectMusicBufferImpl_QueryInterface,
262 IDirectMusicBufferImpl_AddRef,
263 IDirectMusicBufferImpl_Release,
264 IDirectMusicBufferImpl_Flush,
265 IDirectMusicBufferImpl_TotalTime,
266 IDirectMusicBufferImpl_PackStructured,
267 IDirectMusicBufferImpl_PackUnstructured,
268 IDirectMusicBufferImpl_ResetReadPtr,
269 IDirectMusicBufferImpl_GetNextEvent,
270 IDirectMusicBufferImpl_GetRawBufferPtr,
271 IDirectMusicBufferImpl_GetStartTime,
272 IDirectMusicBufferImpl_GetUsedBytes,
273 IDirectMusicBufferImpl_GetMaxBytes,
274 IDirectMusicBufferImpl_GetBufferFormat,
275 IDirectMusicBufferImpl_SetStartTime,
276 IDirectMusicBufferImpl_SetUsedBytes
277 };
278
279 HRESULT DMUSIC_CreateDirectMusicBufferImpl(LPDMUS_BUFFERDESC desc, LPVOID* ret_iface)
280 {
281 IDirectMusicBufferImpl* dmbuffer;
282 HRESULT hr;
283
284 TRACE("(%p, %p)\n", desc, ret_iface);
285
286 *ret_iface = NULL;
287
288 dmbuffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicBufferImpl));
289 if (!dmbuffer)
290 return E_OUTOFMEMORY;
291
292 dmbuffer->IDirectMusicBuffer_iface.lpVtbl = &DirectMusicBuffer_Vtbl;
293 dmbuffer->ref = 0; /* Will be inited by QueryInterface */
294
295 if (IsEqualGUID(&desc->guidBufferFormat, &GUID_NULL))
296 dmbuffer->format = KSDATAFORMAT_SUBTYPE_MIDI;
297 else
298 dmbuffer->format = desc->guidBufferFormat;
299 dmbuffer->size = (desc->cbBuffer + 3) & ~3; /* Buffer size must be multiple of 4 bytes */
300
301 dmbuffer->data = HeapAlloc(GetProcessHeap(), 0, dmbuffer->size);
302 if (!dmbuffer->data) {
303 HeapFree(GetProcessHeap(), 0, dmbuffer);
304 return E_OUTOFMEMORY;
305 }
306
307 hr = IDirectMusicBufferImpl_QueryInterface((LPDIRECTMUSICBUFFER)dmbuffer, &IID_IDirectMusicBuffer, ret_iface);
308 if (FAILED(hr))
309 {
310 HeapFree(GetProcessHeap(), 0, dmbuffer->data);
311 HeapFree(GetProcessHeap(), 0, dmbuffer);
312 }
313
314 return hr;
315 }