1 /******************************************************************************
2  Copyright (C) 2019 by Hugh Bailey ("Jim") <jim@obsproject.com>
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  ******************************************************************************/
17 
18 #include "obs-browser-source.hpp"
19 #if CHROME_VERSION_BUILD < 4103 && CHROME_VERSION_BUILD >= 3683
EnumAudioStreams(obs_source_enum_proc_t cb,void * param)20 void BrowserSource::EnumAudioStreams(obs_source_enum_proc_t cb, void *param)
21 {
22 	std::lock_guard<std::mutex> lock(audio_sources_mutex);
23 	for (obs_source_t *audio_source : audio_sources) {
24 		cb(source, audio_source, param);
25 	}
26 }
27 
mix_audio(float * __restrict p_out,const float * __restrict p_in,size_t pos,size_t count)28 static inline void mix_audio(float *__restrict p_out,
29 			     const float *__restrict p_in, size_t pos,
30 			     size_t count)
31 {
32 	float *__restrict out = p_out;
33 	const float *__restrict in = p_in + pos;
34 	const float *__restrict end = in + count;
35 
36 	while (in < end)
37 		*out++ += *in++;
38 }
39 
AudioMix(uint64_t * ts_out,struct audio_output_data * audio_output,size_t channels,size_t sample_rate)40 bool BrowserSource::AudioMix(uint64_t *ts_out,
41 			     struct audio_output_data *audio_output,
42 			     size_t channels, size_t sample_rate)
43 {
44 	uint64_t timestamp = 0;
45 	struct obs_source_audio_mix child_audio;
46 
47 	std::lock_guard<std::mutex> lock(audio_sources_mutex);
48 	for (obs_source_t *s : audio_sources) {
49 		if (!obs_source_audio_pending(s)) {
50 			uint64_t source_ts = obs_source_get_audio_timestamp(s);
51 
52 			if (source_ts && (!timestamp || source_ts < timestamp))
53 				timestamp = source_ts;
54 		}
55 	}
56 
57 	if (!timestamp)
58 		return false;
59 
60 	for (obs_source_t *s : audio_sources) {
61 		uint64_t source_ts;
62 		size_t pos, count;
63 
64 		if (obs_source_audio_pending(s)) {
65 			continue;
66 		}
67 
68 		source_ts = obs_source_get_audio_timestamp(s);
69 		if (!source_ts) {
70 			continue;
71 		}
72 
73 		pos = (size_t)ns_to_audio_frames(sample_rate,
74 						 source_ts - timestamp);
75 		count = AUDIO_OUTPUT_FRAMES - pos;
76 
77 		obs_source_get_audio_mix(s, &child_audio);
78 		for (size_t ch = 0; ch < channels; ch++) {
79 			float *out = audio_output->data[ch];
80 			float *in = child_audio.output[0].data[ch];
81 
82 			mix_audio(out, in, pos, count);
83 		}
84 	}
85 
86 	*ts_out = timestamp;
87 	return true;
88 }
89 #endif
90