xref: /reactos/dll/directx/wine/amstream/audiodata.c (revision 7401c74a)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * Implementation of IAudioData Interface
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * Copyright 2012 Christian Costa
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck  * License as published by the Free Software Foundation; either
9c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14c2c66affSColin Finck  * Lesser General Public License for more details.
15c2c66affSColin Finck  *
16c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck  */
20c2c66affSColin Finck 
21*7401c74aSAmine Khaldi #include "wine/debug.h"
22*7401c74aSAmine Khaldi 
23*7401c74aSAmine Khaldi #define COBJMACROS
24*7401c74aSAmine Khaldi 
25*7401c74aSAmine Khaldi #include "winbase.h"
26c2c66affSColin Finck #include "amstream_private.h"
27c2c66affSColin Finck 
28*7401c74aSAmine Khaldi WINE_DEFAULT_DEBUG_CHANNEL(amstream);
29*7401c74aSAmine Khaldi 
30c2c66affSColin Finck typedef struct {
31c2c66affSColin Finck     IAudioData IAudioData_iface;
32c2c66affSColin Finck     LONG ref;
33c2c66affSColin Finck     DWORD size;
34c2c66affSColin Finck     BYTE *data;
35c2c66affSColin Finck     BOOL data_owned;
36c2c66affSColin Finck     DWORD actual_data;
37c2c66affSColin Finck     WAVEFORMATEX wave_format;
38c2c66affSColin Finck } AMAudioDataImpl;
39c2c66affSColin Finck 
impl_from_IAudioData(IAudioData * iface)40c2c66affSColin Finck static inline AMAudioDataImpl *impl_from_IAudioData(IAudioData *iface)
41c2c66affSColin Finck {
42c2c66affSColin Finck     return CONTAINING_RECORD(iface, AMAudioDataImpl, IAudioData_iface);
43c2c66affSColin Finck }
44c2c66affSColin Finck 
45c2c66affSColin Finck /*** IUnknown methods ***/
IAudioDataImpl_QueryInterface(IAudioData * iface,REFIID riid,void ** ret_iface)46c2c66affSColin Finck static HRESULT WINAPI IAudioDataImpl_QueryInterface(IAudioData *iface, REFIID riid, void **ret_iface)
47c2c66affSColin Finck {
48c2c66affSColin Finck     TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ret_iface);
49c2c66affSColin Finck 
50c2c66affSColin Finck     if (IsEqualGUID(riid, &IID_IUnknown) ||
51c2c66affSColin Finck         IsEqualGUID(riid, &IID_IAudioData))
52c2c66affSColin Finck     {
53c2c66affSColin Finck         IAudioData_AddRef(iface);
54c2c66affSColin Finck         *ret_iface = iface;
55c2c66affSColin Finck         return S_OK;
56c2c66affSColin Finck     }
57c2c66affSColin Finck 
58c2c66affSColin Finck     ERR("(%p)->(%s,%p),not found\n", iface, debugstr_guid(riid), ret_iface);
59c2c66affSColin Finck     return E_NOINTERFACE;
60c2c66affSColin Finck }
61c2c66affSColin Finck 
IAudioDataImpl_AddRef(IAudioData * iface)62c2c66affSColin Finck static ULONG WINAPI IAudioDataImpl_AddRef(IAudioData* iface)
63c2c66affSColin Finck {
64c2c66affSColin Finck     AMAudioDataImpl *This = impl_from_IAudioData(iface);
65c2c66affSColin Finck     ULONG ref = InterlockedIncrement(&This->ref);
66c2c66affSColin Finck 
67c2c66affSColin Finck     TRACE("(%p)->(): new ref = %u\n", iface, This->ref);
68c2c66affSColin Finck 
69c2c66affSColin Finck     return ref;
70c2c66affSColin Finck }
71c2c66affSColin Finck 
IAudioDataImpl_Release(IAudioData * iface)72c2c66affSColin Finck static ULONG WINAPI IAudioDataImpl_Release(IAudioData* iface)
73c2c66affSColin Finck {
74c2c66affSColin Finck     AMAudioDataImpl *This = impl_from_IAudioData(iface);
75c2c66affSColin Finck     ULONG ref = InterlockedDecrement(&This->ref);
76c2c66affSColin Finck 
77c2c66affSColin Finck     TRACE("(%p)->(): new ref = %u\n", iface, This->ref);
78c2c66affSColin Finck 
79c2c66affSColin Finck     if (!ref)
80c2c66affSColin Finck     {
81c2c66affSColin Finck         if (This->data_owned)
82c2c66affSColin Finck         {
83c2c66affSColin Finck             CoTaskMemFree(This->data);
84c2c66affSColin Finck         }
85c2c66affSColin Finck 
86c2c66affSColin Finck         HeapFree(GetProcessHeap(), 0, This);
87c2c66affSColin Finck     }
88c2c66affSColin Finck 
89c2c66affSColin Finck     return ref;
90c2c66affSColin Finck }
91c2c66affSColin Finck 
92c2c66affSColin Finck /*** IMemoryData methods ***/
IAudioDataImpl_SetBuffer(IAudioData * iface,DWORD size,BYTE * data,DWORD flags)93c2c66affSColin Finck static HRESULT WINAPI IAudioDataImpl_SetBuffer(IAudioData* iface, DWORD size, BYTE *data, DWORD flags)
94c2c66affSColin Finck {
95c2c66affSColin Finck     AMAudioDataImpl *This = impl_from_IAudioData(iface);
96c2c66affSColin Finck 
97c2c66affSColin Finck     TRACE("(%p)->(%u,%p,%x)\n", iface, size, data, flags);
98c2c66affSColin Finck 
99c2c66affSColin Finck     if (!size)
100c2c66affSColin Finck     {
101c2c66affSColin Finck         return E_INVALIDARG;
102c2c66affSColin Finck     }
103c2c66affSColin Finck 
104c2c66affSColin Finck     if (This->data_owned)
105c2c66affSColin Finck     {
106c2c66affSColin Finck         CoTaskMemFree(This->data);
107c2c66affSColin Finck         This->data_owned = FALSE;
108c2c66affSColin Finck     }
109c2c66affSColin Finck 
110c2c66affSColin Finck     This->size = size;
111c2c66affSColin Finck     This->data = data;
112c2c66affSColin Finck 
113c2c66affSColin Finck     if (!This->data)
114c2c66affSColin Finck     {
115c2c66affSColin Finck         This->data = CoTaskMemAlloc(This->size);
116c2c66affSColin Finck         This->data_owned = TRUE;
117c2c66affSColin Finck         if (!This->data)
118c2c66affSColin Finck         {
119c2c66affSColin Finck             return E_OUTOFMEMORY;
120c2c66affSColin Finck         }
121c2c66affSColin Finck     }
122c2c66affSColin Finck 
123c2c66affSColin Finck     return S_OK;
124c2c66affSColin Finck }
125c2c66affSColin Finck 
IAudioDataImpl_GetInfo(IAudioData * iface,DWORD * length,BYTE ** data,DWORD * actual_data)126c2c66affSColin Finck static HRESULT WINAPI IAudioDataImpl_GetInfo(IAudioData* iface, DWORD *length, BYTE **data, DWORD *actual_data)
127c2c66affSColin Finck {
128c2c66affSColin Finck     AMAudioDataImpl *This = impl_from_IAudioData(iface);
129c2c66affSColin Finck 
130c2c66affSColin Finck     TRACE("(%p)->(%p,%p,%p)\n", iface, length, data, actual_data);
131c2c66affSColin Finck 
132c2c66affSColin Finck     if (!This->data)
133c2c66affSColin Finck     {
134c2c66affSColin Finck         return MS_E_NOTINIT;
135c2c66affSColin Finck     }
136c2c66affSColin Finck 
137c2c66affSColin Finck     if (length)
138c2c66affSColin Finck     {
139c2c66affSColin Finck         *length = This->size;
140c2c66affSColin Finck     }
141c2c66affSColin Finck     if (data)
142c2c66affSColin Finck     {
143c2c66affSColin Finck         *data = This->data;
144c2c66affSColin Finck     }
145c2c66affSColin Finck     if (actual_data)
146c2c66affSColin Finck     {
147c2c66affSColin Finck         *actual_data = This->actual_data;
148c2c66affSColin Finck     }
149c2c66affSColin Finck 
150c2c66affSColin Finck     return S_OK;
151c2c66affSColin Finck }
152c2c66affSColin Finck 
IAudioDataImpl_SetActual(IAudioData * iface,DWORD data_valid)153c2c66affSColin Finck static HRESULT WINAPI IAudioDataImpl_SetActual(IAudioData* iface, DWORD data_valid)
154c2c66affSColin Finck {
155c2c66affSColin Finck     AMAudioDataImpl *This = impl_from_IAudioData(iface);
156c2c66affSColin Finck 
157c2c66affSColin Finck     TRACE("(%p)->(%u)\n", iface, data_valid);
158c2c66affSColin Finck 
159c2c66affSColin Finck     if (data_valid > This->size)
160c2c66affSColin Finck     {
161c2c66affSColin Finck         return E_INVALIDARG;
162c2c66affSColin Finck     }
163c2c66affSColin Finck 
164c2c66affSColin Finck     This->actual_data = data_valid;
165c2c66affSColin Finck 
166c2c66affSColin Finck     return S_OK;
167c2c66affSColin Finck }
168c2c66affSColin Finck 
169c2c66affSColin Finck /*** IAudioData methods ***/
IAudioDataImpl_GetFormat(IAudioData * iface,WAVEFORMATEX * wave_format_current)170c2c66affSColin Finck static HRESULT WINAPI IAudioDataImpl_GetFormat(IAudioData* iface, WAVEFORMATEX *wave_format_current)
171c2c66affSColin Finck {
172c2c66affSColin Finck     AMAudioDataImpl *This = impl_from_IAudioData(iface);
173c2c66affSColin Finck 
174c2c66affSColin Finck     TRACE("(%p)->(%p)\n", iface, wave_format_current);
175c2c66affSColin Finck 
176c2c66affSColin Finck     if (!wave_format_current)
177c2c66affSColin Finck     {
178c2c66affSColin Finck         return E_POINTER;
179c2c66affSColin Finck     }
180c2c66affSColin Finck 
181c2c66affSColin Finck     *wave_format_current = This->wave_format;
182c2c66affSColin Finck 
183c2c66affSColin Finck     return S_OK;
184c2c66affSColin Finck }
185c2c66affSColin Finck 
IAudioDataImpl_SetFormat(IAudioData * iface,const WAVEFORMATEX * wave_format)186c2c66affSColin Finck static HRESULT WINAPI IAudioDataImpl_SetFormat(IAudioData* iface, const WAVEFORMATEX *wave_format)
187c2c66affSColin Finck {
188c2c66affSColin Finck     AMAudioDataImpl *This = impl_from_IAudioData(iface);
189c2c66affSColin Finck 
190c2c66affSColin Finck     TRACE("(%p)->(%p)\n", iface, wave_format);
191c2c66affSColin Finck 
192c2c66affSColin Finck     if (!wave_format)
193c2c66affSColin Finck     {
194c2c66affSColin Finck         return E_POINTER;
195c2c66affSColin Finck     }
196c2c66affSColin Finck 
197c2c66affSColin Finck     if (WAVE_FORMAT_PCM != wave_format->wFormatTag)
198c2c66affSColin Finck     {
199c2c66affSColin Finck         return E_INVALIDARG;
200c2c66affSColin Finck     }
201c2c66affSColin Finck 
202c2c66affSColin Finck     This->wave_format = *wave_format;
203c2c66affSColin Finck 
204c2c66affSColin Finck     return S_OK;
205c2c66affSColin Finck }
206c2c66affSColin Finck 
207c2c66affSColin Finck static const struct IAudioDataVtbl AudioData_Vtbl =
208c2c66affSColin Finck {
209c2c66affSColin Finck     /*** IUnknown methods ***/
210c2c66affSColin Finck     IAudioDataImpl_QueryInterface,
211c2c66affSColin Finck     IAudioDataImpl_AddRef,
212c2c66affSColin Finck     IAudioDataImpl_Release,
213c2c66affSColin Finck     /*** IMemoryData methods ***/
214c2c66affSColin Finck     IAudioDataImpl_SetBuffer,
215c2c66affSColin Finck     IAudioDataImpl_GetInfo,
216c2c66affSColin Finck     IAudioDataImpl_SetActual,
217c2c66affSColin Finck     /*** IAudioData methods ***/
218c2c66affSColin Finck     IAudioDataImpl_GetFormat,
219c2c66affSColin Finck     IAudioDataImpl_SetFormat
220c2c66affSColin Finck };
221c2c66affSColin Finck 
AMAudioData_create(IUnknown * pUnkOuter,LPVOID * ppObj)222c2c66affSColin Finck HRESULT AMAudioData_create(IUnknown *pUnkOuter, LPVOID *ppObj)
223c2c66affSColin Finck {
224c2c66affSColin Finck     AMAudioDataImpl *object;
225c2c66affSColin Finck 
226c2c66affSColin Finck     TRACE("(%p,%p)\n", pUnkOuter, ppObj);
227c2c66affSColin Finck 
228c2c66affSColin Finck     if (pUnkOuter)
229c2c66affSColin Finck         return CLASS_E_NOAGGREGATION;
230c2c66affSColin Finck 
231c2c66affSColin Finck     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AMAudioDataImpl));
232c2c66affSColin Finck     if (!object)
233c2c66affSColin Finck         return E_OUTOFMEMORY;
234c2c66affSColin Finck 
235c2c66affSColin Finck     object->IAudioData_iface.lpVtbl = &AudioData_Vtbl;
236c2c66affSColin Finck     object->ref = 1;
237c2c66affSColin Finck 
238c2c66affSColin Finck     object->wave_format.wFormatTag = WAVE_FORMAT_PCM;
239c2c66affSColin Finck     object->wave_format.nChannels = 1;
240c2c66affSColin Finck     object->wave_format.nSamplesPerSec = 11025;
241c2c66affSColin Finck     object->wave_format.wBitsPerSample = 16;
242c2c66affSColin Finck     object->wave_format.nBlockAlign = object->wave_format.wBitsPerSample * object->wave_format.nChannels / 8;
243c2c66affSColin Finck     object->wave_format.nAvgBytesPerSec = object->wave_format.nBlockAlign * object->wave_format.nSamplesPerSec;
244c2c66affSColin Finck 
245c2c66affSColin Finck     *ppObj = &object->IAudioData_iface;
246c2c66affSColin Finck 
247c2c66affSColin Finck     return S_OK;
248c2c66affSColin Finck }
249