1 /*************************************************************************/
2 /*  audio_driver_wasapi.h                                                */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 
31 #ifndef AUDIO_DRIVER_WASAPI_H
32 #define AUDIO_DRIVER_WASAPI_H
33 
34 #ifdef WASAPI_ENABLED
35 
36 #include "core/os/mutex.h"
37 #include "core/os/thread.h"
38 #include "servers/audio_server.h"
39 
40 #include <audioclient.h>
41 #include <mmdeviceapi.h>
42 #include <windows.h>
43 
44 class AudioDriverWASAPI : public AudioDriver {
45 
46 	class AudioDeviceWASAPI {
47 	public:
48 		IAudioClient *audio_client;
49 		IAudioRenderClient *render_client;
50 		IAudioCaptureClient *capture_client;
51 		bool active;
52 
53 		WORD format_tag;
54 		WORD bits_per_sample;
55 		unsigned int channels;
56 		unsigned int frame_size;
57 
58 		String device_name;
59 		String new_device;
60 
AudioDeviceWASAPI()61 		AudioDeviceWASAPI() :
62 				audio_client(NULL),
63 				render_client(NULL),
64 				capture_client(NULL),
65 				active(false),
66 				format_tag(0),
67 				bits_per_sample(0),
68 				channels(0),
69 				frame_size(0),
70 				device_name("Default"),
71 				new_device("Default") {
72 		}
73 	};
74 
75 	AudioDeviceWASAPI audio_input;
76 	AudioDeviceWASAPI audio_output;
77 
78 	Mutex *mutex;
79 	Thread *thread;
80 
81 	Vector<int32_t> samples_in;
82 
83 	unsigned int channels;
84 	int mix_rate;
85 	int buffer_frames;
86 
87 	bool thread_exited;
88 	mutable bool exit_thread;
89 
90 	static _FORCE_INLINE_ void write_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i, int32_t sample);
91 	static _FORCE_INLINE_ int32_t read_sample(WORD format_tag, int bits_per_sample, BYTE *buffer, int i);
92 	static void thread_func(void *p_udata);
93 
94 	Error init_render_device(bool reinit = false);
95 	Error init_capture_device(bool reinit = false);
96 
97 	Error finish_render_device();
98 	Error finish_capture_device();
99 
100 	Error audio_device_init(AudioDeviceWASAPI *p_device, bool p_capture, bool reinit);
101 	Error audio_device_finish(AudioDeviceWASAPI *p_device);
102 	Array audio_device_get_list(bool p_capture);
103 
104 public:
get_name()105 	virtual const char *get_name() const {
106 		return "WASAPI";
107 	}
108 
109 	virtual Error init();
110 	virtual void start();
111 	virtual int get_mix_rate() const;
112 	virtual SpeakerMode get_speaker_mode() const;
113 	virtual Array get_device_list();
114 	virtual String get_device();
115 	virtual void set_device(String device);
116 	virtual void lock();
117 	virtual void unlock();
118 	virtual void finish();
119 
120 	virtual Error capture_start();
121 	virtual Error capture_stop();
122 	virtual Array capture_get_device_list();
123 	virtual void capture_set_device(const String &p_name);
124 	virtual String capture_get_device();
125 
126 	AudioDriverWASAPI();
127 };
128 
129 #endif // AUDIO_DRIVER_WASAPI_H
130 #endif
131