]> git.sesse.net Git - vlc/blob - modules/audio_output/winstore.c
988cadaed8f70075db5f232ddf9844542c3eb5e8
[vlc] / modules / audio_output / winstore.c
1 /*****************************************************************************
2  * mmdevice.c : Windows Multimedia Device API audio output plugin for VLC
3  *****************************************************************************
4  * Copyright (C) 2012 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #define INITGUID
26 #define COBJMACROS
27
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <audiopolicy.h>
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_aout.h>
35 #include <vlc_modules.h>
36 #include "audio_output/mmdevice.h"
37
38 DEFINE_GUID (GUID_VLC_AUD_OUT, 0x4533f59d, 0x59ee, 0x00c6,
39    0xad, 0xb2, 0xc6, 0x8b, 0x50, 0x1a, 0x66, 0x55);
40
41 static void EnterMTA(void)
42 {
43     HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
44     if (unlikely(FAILED(hr)))
45         abort();
46 }
47
48 static void LeaveMTA(void)
49 {
50     CoUninitialize();
51 }
52
53 struct aout_sys_t
54 {
55     aout_stream_t *stream; /**< Underlying audio output stream */
56     module_t *module;
57     IAudioClient *client;
58 };
59
60 static int TimeGet(audio_output_t *aout, mtime_t *restrict delay)
61 {
62     aout_sys_t *sys = aout->sys;
63     HRESULT hr;
64
65     EnterMTA();
66     hr = aout_stream_TimeGet(sys->stream, delay);
67     LeaveMTA();
68
69     return SUCCEEDED(hr) ? 0 : -1;
70 }
71
72 static void Play(audio_output_t *aout, block_t *block)
73 {
74     aout_sys_t *sys = aout->sys;
75
76     EnterMTA();
77     aout_stream_Play(sys->stream, block);
78     LeaveMTA();
79 }
80
81 static void Pause(audio_output_t *aout, bool paused, mtime_t date)
82 {
83     aout_sys_t *sys = aout->sys;
84
85     EnterMTA();
86     aout_stream_Pause(sys->stream, paused);
87     LeaveMTA();
88
89     (void) date;
90 }
91
92 static void Flush(audio_output_t *aout, bool wait)
93 {
94     aout_sys_t *sys = aout->sys;
95
96     EnterMTA();
97     aout_stream_Flush(sys->stream, wait);
98     LeaveMTA();
99 }
100
101 static HRESULT ActivateDevice(void *opaque, REFIID iid, PROPVARIANT *actparms,
102                               void **restrict pv)
103 {
104     (void) iid; (void) actparms;
105     IAudioClient* client = (IAudioClient*)opaque;
106     IAudioClient_AddRef(client);
107     *pv = opaque;
108
109     return S_OK;
110 }
111
112 static int aout_stream_Start(void *func, va_list ap)
113 {
114     aout_stream_start_t start = func;
115     aout_stream_t *s = va_arg(ap, aout_stream_t *);
116     audio_sample_format_t *fmt = va_arg(ap, audio_sample_format_t *);
117     HRESULT *hr = va_arg(ap, HRESULT *);
118
119     *hr = start(s, fmt, &GUID_VLC_AUD_OUT);
120     return SUCCEEDED(*hr) ? VLC_SUCCESS : VLC_EGENERIC;
121 }
122
123 static void aout_stream_Stop(void *func, va_list ap)
124 {
125     aout_stream_stop_t stop = func;
126     aout_stream_t *s = va_arg(ap, aout_stream_t *);
127
128     stop(s);
129 }
130
131 static int Start(audio_output_t *aout, audio_sample_format_t *restrict fmt)
132 {
133     aout_sys_t *sys = aout->sys;
134     HRESULT hr;
135
136     aout_stream_t *s = vlc_object_create(aout, sizeof (*s));
137     if (unlikely(s == NULL))
138         return -1;
139
140     s->owner.device = sys->client;
141     s->owner.activate = ActivateDevice;
142
143     EnterMTA();
144     sys->module = vlc_module_load(s, "aout stream", NULL, false,
145                                   aout_stream_Start, s, fmt, &hr);
146     LeaveMTA();
147
148     if (sys->module == NULL)
149     {
150         vlc_object_release(s);
151         return -1;
152     }
153
154     assert (sys->stream == NULL);
155     sys->stream = s;
156     return 0;
157 }
158
159 static void Stop(audio_output_t *aout)
160 {
161     aout_sys_t *sys = aout->sys;
162
163     assert (sys->stream != NULL);
164
165     EnterMTA();
166     vlc_module_unload(sys->module, aout_stream_Stop, sys->stream);
167     LeaveMTA();
168
169     vlc_object_release(sys->stream);
170     sys->stream = NULL;
171 }
172
173 static int Open(vlc_object_t *obj)
174 {
175     audio_output_t *aout = (audio_output_t *)obj;
176
177     IAudioClient* client = var_InheritInteger(aout, "winstore-audioclient");
178     if (client == NULL)
179         return VLC_EGENERIC;
180
181     aout_sys_t *sys = malloc(sizeof (*sys));
182     if (unlikely(sys == NULL))
183         return VLC_ENOMEM;
184
185     aout->sys = sys;
186     sys->stream = NULL;
187     sys->client = client;
188     aout->start = Start;
189     aout->stop = Stop;
190     aout->time_get = TimeGet;
191     aout->play = Play;
192     aout->pause = Pause;
193     aout->flush = Flush;
194     return VLC_SUCCESS;
195 }
196
197 static void Close(vlc_object_t *obj)
198 {
199     audio_output_t *aout = (audio_output_t *)obj;
200     aout_sys_t *sys = aout->sys;
201
202     free(sys);
203 }
204
205 vlc_module_begin()
206     set_shortname("winstore")
207     set_description(N_("Windows Store audio output"))
208     set_capability("audio output", 150)
209     /* Pointer to the activated AudioClient* */
210     add_integer("winstore-audioclient", 0x0, NULL, NULL, true);
211     set_category(CAT_AUDIO)
212     set_subcategory(SUBCAT_AUDIO_AOUT)
213     add_shortcut("wasapi")
214     set_callbacks(Open, Close)
215 vlc_module_end()