Open Broadcaster Software
Free, open source software for live streaming and recording
audio-io.h
Go to the documentation of this file.
1 /******************************************************************************
2  Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.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 #pragma once
19 
20 #include "media-io-defs.h"
21 #include "../util/c99defs.h"
22 #include "../util/util_uint128.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define MAX_AUDIO_MIXES 6
29 #define MAX_AUDIO_CHANNELS 8
30 #define AUDIO_OUTPUT_FRAMES 1024
31 
32 #define TOTAL_AUDIO_SIZE \
33  (MAX_AUDIO_MIXES * MAX_AUDIO_CHANNELS * \
34  AUDIO_OUTPUT_FRAMES * sizeof(float))
35 
36 /*
37  * Base audio output component. Use this to create an audio output track
38  * for the media.
39  */
40 
41 struct audio_output;
42 typedef struct audio_output audio_t;
43 
46 
51 
56 };
57 
76 };
77 
78 struct audio_data {
82 };
83 
86 };
87 
88 typedef bool (*audio_input_callback_t)(void *param,
89  uint64_t start_ts, uint64_t end_ts, uint64_t *new_ts,
90  uint32_t active_mixers, struct audio_output_data *mixes);
91 
93  const char *name;
94 
96  enum audio_format format;
97  enum speaker_layout speakers;
98 
100  void *input_param;
101 };
102 
105  enum audio_format format;
106  enum speaker_layout speakers;
107 };
108 
109 static inline uint32_t get_audio_channels(enum speaker_layout speakers)
110 {
111  switch (speakers) {
112  case SPEAKERS_MONO: return 1;
113  case SPEAKERS_STEREO: return 2;
114  case SPEAKERS_2POINT1: return 3;
115  case SPEAKERS_4POINT0: return 4;
116  case SPEAKERS_4POINT1: return 5;
117  case SPEAKERS_5POINT1: return 6;
118  case SPEAKERS_7POINT1: return 8;
119  case SPEAKERS_UNKNOWN: return 0;
120  }
121 
122  return 0;
123 }
124 
125 static inline size_t get_audio_bytes_per_channel(enum audio_format format)
126 {
127  switch (format) {
128  case AUDIO_FORMAT_U8BIT:
130  return 1;
131 
132  case AUDIO_FORMAT_16BIT:
134  return 2;
135 
136  case AUDIO_FORMAT_FLOAT:
138  case AUDIO_FORMAT_32BIT:
140  return 4;
141 
143  return 0;
144  }
145 
146  return 0;
147 }
148 
149 static inline bool is_audio_planar(enum audio_format format)
150 {
151  switch (format) {
152  case AUDIO_FORMAT_U8BIT:
153  case AUDIO_FORMAT_16BIT:
154  case AUDIO_FORMAT_32BIT:
155  case AUDIO_FORMAT_FLOAT:
156  return false;
157 
162  return true;
163 
165  return false;
166  }
167 
168  return false;
169 }
170 
171 static inline size_t get_audio_planes(enum audio_format format,
172  enum speaker_layout speakers)
173 {
174  return (is_audio_planar(format) ? get_audio_channels(speakers) : 1);
175 }
176 
177 static inline size_t get_audio_size(enum audio_format format,
178  enum speaker_layout speakers, uint32_t frames)
179 {
180  bool planar = is_audio_planar(format);
181 
182  return (planar ? 1 : get_audio_channels(speakers)) *
183  get_audio_bytes_per_channel(format) *
184  frames;
185 }
186 
187 static inline uint64_t audio_frames_to_ns(size_t sample_rate,
188  uint64_t frames)
189 {
190  util_uint128_t val;
191  val = util_mul64_64(frames, 1000000000ULL);
192  val = util_div128_32(val, (uint32_t)sample_rate);
193  return val.low;
194 }
195 
196 static inline uint64_t ns_to_audio_frames(size_t sample_rate,
197  uint64_t frames)
198 {
199  util_uint128_t val;
200  val = util_mul64_64(frames, sample_rate);
201  val = util_div128_32(val, 1000000000);
202  return val.low;
203 }
204 
205 #define AUDIO_OUTPUT_SUCCESS 0
206 #define AUDIO_OUTPUT_INVALIDPARAM -1
207 #define AUDIO_OUTPUT_FAIL -2
208 
209 EXPORT int audio_output_open(audio_t **audio, struct audio_output_info *info);
210 EXPORT void audio_output_close(audio_t *audio);
211 
212 typedef void (*audio_output_callback_t)(void *param, size_t mix_idx,
213  struct audio_data *data);
214 
215 EXPORT bool audio_output_connect(audio_t *video, size_t mix_idx,
216  const struct audio_convert_info *conversion,
217  audio_output_callback_t callback, void *param);
218 EXPORT void audio_output_disconnect(audio_t *video, size_t mix_idx,
219  audio_output_callback_t callback, void *param);
220 
221 EXPORT bool audio_output_active(const audio_t *audio);
222 
223 EXPORT size_t audio_output_get_block_size(const audio_t *audio);
224 EXPORT size_t audio_output_get_planes(const audio_t *audio);
225 EXPORT size_t audio_output_get_channels(const audio_t *audio);
228  const audio_t *audio);
229 
230 
231 #ifdef __cplusplus
232 }
233 #endif
Definition: util_uint128.h:19
speaker_layout
Definition: audio-io.h:67
EXPORT bool audio_output_active(const audio_t *audio)
Definition: audio-io.h:92
unsigned uint32_t
Definition: vc_stdint.h:31
#define MAX_AUDIO_CHANNELS
Definition: audio-io.h:29
audio_format
Definition: audio-io.h:44
Definition: audio-io.h:49
audio_input_callback_t input_callback
Definition: audio-io.h:99
Definition: audio-io.h:54
Definition: audio-io.h:70
Definition: audio-io.h:84
unsigned __int64 uint64_t
Definition: vc_stdint.h:33
EXPORT size_t audio_output_get_planes(const audio_t *audio)
Definition: audio-io.h:103
unsigned char uint8_t
Definition: vc_stdint.h:27
EXPORT void audio_output_close(audio_t *audio)
EXPORT bool audio_output_connect(audio_t *video, size_t mix_idx, const struct audio_convert_info *conversion, audio_output_callback_t callback, void *param)
Definition: audio-io.h:55
bool(* audio_input_callback_t)(void *param, uint64_t start_ts, uint64_t end_ts, uint64_t *new_ts, uint32_t active_mixers, struct audio_output_data *mixes)
Definition: audio-io.h:88
Definition: audio-io.h:53
Definition: audio-io.h:75
#define EXPORT
Definition: c99defs.h:49
EXPORT const struct audio_output_info * audio_output_get_info(const audio_t *audio)
uint32_t frames
Definition: audio-io.h:80
EXPORT uint32_t audio_output_get_sample_rate(const audio_t *audio)
uint32_t samples_per_sec
Definition: audio-io.h:95
Definition: audio-io.h:74
uint32_t samples_per_sec
Definition: audio-io.h:104
Definition: audio-io.h:72
Definition: audio-io.h:73
Definition: audio-io.h:48
Definition: audio-io.h:78
Definition: audio-io.h:68
Definition: audio-io.h:47
Definition: audio-io.h:50
EXPORT size_t audio_output_get_block_size(const audio_t *audio)
EXPORT void audio_output_disconnect(audio_t *video, size_t mix_idx, audio_output_callback_t callback, void *param)
Definition: audio-io.h:52
uint64_t timestamp
Definition: audio-io.h:81
void(* audio_output_callback_t)(void *param, size_t mix_idx, struct audio_data *data)
Definition: audio-io.h:212
void * input_param
Definition: audio-io.h:100
#define MAX_AV_PLANES
Definition: media-io-defs.h:20
EXPORT size_t audio_output_get_channels(const audio_t *audio)
uint64_t low
Definition: util_uint128.h:23
Definition: audio-io.h:69
Definition: audio-io.h:45
EXPORT int audio_output_open(audio_t **audio, struct audio_output_info *info)
#define bool
Definition: vc_stdbool.h:5
const char * name
Definition: audio-io.h:93
struct audio_output audio_t
Definition: audio-io.h:42
Definition: audio-io.h:71
uint8_t * data[MAX_AV_PLANES]
Definition: audio-io.h:79