xref: /haiku/src/add-ons/media/media-add-ons/mixer/MixerCore.h (revision 2141d2fe3a5df2f55f3590f67660573b50d1d1d3)
1 /*
2  * Copyright 2003-2010 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Marcus Overhagen
7  */
8 #ifndef _MIXER_CORE_H
9 #define _MIXER_CORE_H
10 
11 
12 #include "MixerSettings.h"
13 
14 #include <Buffer.h>
15 #include <Locker.h>
16 #include <TimeSource.h>
17 
18 
19 class AudioMixer;
20 class BBufferGroup;
21 class MixerInput;
22 class MixerOutput;
23 class Resampler;
24 
25 
26 // The number of "enum media_multi_channels" types from MediaDefs.h
27 // XXX should be 18, but limited to 12 here
28 #define MAX_CHANNEL_TYPES	12
29 // XXX using a dedicated mono channel, this should be channel type 31
30 //     but for now we redefine type 12
31 #define B_CHANNEL_MONO		B_CHANNEL_TOP_CENTER
32 
33 
34 class MixerCore {
35 public:
36 								MixerCore(AudioMixer* node);
37 	virtual						~MixerCore();
38 
39 			MixerSettings*		Settings();
40 			void				UpdateResamplingAlgorithm();
41 
42 	// To avoid calling Settings()->AttenuateOutput() for every outgoing
43 	// buffer, this setting is cached in fOutputGain and must be set by
44 	// the audio mixer node using SetOutputAttenuation()
45 			void				SetOutputAttenuation(float gain);
46 			MixerInput*			AddInput(const media_input& input);
47 			MixerOutput*		AddOutput(const media_output& output);
48 
49 			bool				RemoveInput(int32 inputID);
50 			bool				RemoveOutput();
51 			int32				CreateInputID();
52 
53 	// index = 0 to count-1, NOT inputID
54 			MixerInput*			Input(int index);
55 			MixerOutput*		Output();
56 
57 			bool				Lock();
58 			bool				LockWithTimeout(bigtime_t timeout);
59 			bool				IsLocked() const;
60 			void				Unlock();
61 
62 			void				BufferReceived(BBuffer* buffer,
63 									bigtime_t lateness);
64 
65 			void				InputFormatChanged(int32 inputID,
66 									const media_multi_audio_format& format);
67 			void				OutputFormatChanged(
68 									const media_multi_audio_format& format);
69 
70 			void				SetOutputBufferGroup(BBufferGroup* group);
71 			void				SetTimingInfo(BTimeSource* timeSource,
72 									bigtime_t downstreamLatency);
73 			void				EnableOutput(bool enabled);
74 
75 			bool				Start();
76 			bool				Stop();
77 
78 			uint32				OutputChannelCount();
79 
80 private:
81 			void				_StartMixThread();
82 			void				_StopMixThread();
83 
84 			void				_UpdateResamplers(
85 									const media_multi_audio_format& format);
86 			void				_ApplyOutputFormat();
87 	static	int32				_MixThreadEntry(void* arg);
88 			void				_MixThread();
89 
90 private:
91 			BLocker*			fLocker;
92 			BList*				fInputs;
93 			MixerOutput*		fOutput;
94 			int32				fNextInputID;
95 			bool				fRunning;
96 									// true = the mix thread is running
97 
98 			bool				fStarted;
99 									// true = mix thread should be
100 									// started of it is not running
101 
102 			bool				fOutputEnabled;
103 									// true = mix thread should be
104 									// started of it is not running
105 
106 			Resampler**			fResampler; // array
107 			float*				fMixBuffer;
108 			int32				fMixBufferFrameRate;
109 			int32				fMixBufferFrameCount;
110 			int32				fMixBufferChannelCount;
111 			int32*				fMixBufferChannelTypes; //array
112 			bool				fDoubleRateMixing;
113 			bigtime_t			fDownstreamLatency;
114 			MixerSettings*		fSettings;
115 			AudioMixer*			fNode;
116 			BBufferGroup*		fBufferGroup;
117 			BTimeSource*		fTimeSource;
118 			thread_id			fMixThread;
119 			sem_id				fMixThreadWaitSem;
120 			float				fOutputGain;
121 
122 	friend class MixerInput;
123 		// NOTE: debug only
124 };
125 
126 
127 inline bool
128 MixerCore::Lock()
129 {
130 	return fLocker->Lock();
131 }
132 
133 
134 inline bool
135 MixerCore::LockWithTimeout(bigtime_t timeout)
136 {
137 	return fLocker->LockWithTimeout(timeout) == B_OK;
138 }
139 
140 
141 inline bool
142 MixerCore::IsLocked() const
143 {
144 	return fLocker->IsLocked();
145 }
146 
147 
148 inline void
149 MixerCore::Unlock()
150 {
151 	fLocker->Unlock();
152 }
153 
154 
155 #endif // _MIXER_CORE_H
156