1 // Copyright 2005-2019 The Mumble Developers. All rights reserved.
2 // Use of this source code is governed by a BSD-style license
3 // that can be found in the LICENSE file at the root of the
4 // Mumble source tree or at <https://www.mumble.info/LICENSE>.
5 
6 /* Copyright (C) 2005-2011, Thorvald Natvig <thorvald@natvig.com>
7    Copyright (C) 2007, Stefan Gehn <mETz AT gehn DOT net>
8 
9    All rights reserved.
10 
11    Redistribution and use in source and binary forms, with or without
12    modification, are permitted provided that the following conditions
13    are met:
14 
15    - Redistributions of source code must retain the above copyright notice,
16      this list of conditions and the following disclaimer.
17    - Redistributions in binary form must reproduce the above copyright notice,
18      this list of conditions and the following disclaimer in the documentation
19      and/or other materials provided with the distribution.
20    - Neither the name of the Mumble Developers nor the names of its
21      contributors may be used to endorse or promote products derived from this
22      software without specific prior written permission.
23 
24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
28    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 
37 #ifndef MUMBLE_MUMBLE_PAAUDIO_H_
38 #define MUMBLE_MUMBLE_PAAUDIO_H_
39 
40 #include "AudioInput.h"
41 #include "AudioOutput.h"
42 
43 #include <portaudio.h>
44 
45 class PortAudioSystem;
46 typedef boost::shared_ptr<PortAudioSystem> PortAudioSystemPtr;
47 typedef boost::weak_ptr<PortAudioSystem> WeakPortAudioSystemPtr;
48 
49 /**
50  * Small wrapper around some critical PortAudio functions.
51  *
52  * Basically this ensures that the PA lib is initialized and terminated properly and that
53  * several threads can open/close streams as they like.
54  */
55 class PortAudioSystem : public QObject {
56 	private:
57 		Q_OBJECT
58 		Q_DISABLE_COPY(PortAudioSystem)
59 	protected:
60 		//! Mutex around PA stream creation/deletion
61 		static QMutex qmStream;
62 	public:
63 		static bool initStream(PaStream **stream, PaDeviceIndex devIndex, int frameSize, int *chans, bool isInput);
64 		static bool terminateStream(PaStream *stream);
65 
66 		static bool startStream(PaStream *stream);
67 		static bool stopStream(PaStream *stream);
68 
69 		static const QList<audioDevice> enumerateDevices(bool input, PaDeviceIndex match = -1);
70 };
71 
72 
73 class PortAudioInput : public AudioInput {
74 	private:
75 		Q_OBJECT
76 		Q_DISABLE_COPY(PortAudioInput)
77 	public:
78 		PortAudioInput();
79 		~PortAudioInput() Q_DECL_OVERRIDE;
80 		void run() Q_DECL_OVERRIDE;
81 };
82 
83 
84 class PortAudioOutput : public AudioOutput {
85 	private:
86 		Q_OBJECT
87 		Q_DISABLE_COPY(PortAudioOutput)
88 	public:
89 		PortAudioOutput();
90 		~PortAudioOutput() Q_DECL_OVERRIDE;
91 		void run() Q_DECL_OVERRIDE;
92 };
93 
94 #else
95 class PortAudioSystem;
96 class PortAudioInput;
97 class PortAudioOutput;
98 #endif
99