obs-source.c 8.3 KB
Newer Older
J
jp9000 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/******************************************************************************
    Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "obs.h"
#include "obs-data.h"

bool get_source_info(void *module, const char *module_name,
		const char *source_name, struct source_info *info)
{
	info->create = load_module_subfunc(module, module_name,
			source_name,"create", true);
	info->destroy = load_module_subfunc(module, module_name,
			source_name, "destroy", true);
	info->get_output_flags = load_module_subfunc(module, module_name,
			source_name, "get_output_flags", true);

	if (!info->create || !info->destroy || !info->get_output_flags)
		return false;

	info->config = load_module_subfunc(module, module_name,
			source_name, "config", false);
	info->activate = load_module_subfunc(module, module_name,
			source_name, "activate", false);
	info->deactivate = load_module_subfunc(module, module_name,
			source_name, "deactivate", false);
	info->video_tick = load_module_subfunc(module, module_name,
			source_name, "video_tick", false);
	info->video_render = load_module_subfunc(module, module_name,
			source_name, "video_render", false);
	info->getwidth = load_module_subfunc(module, module_name,
			source_name, "getwidth", false);
	info->getheight = load_module_subfunc(module, module_name,
			source_name, "getheight", false);

	info->getparam = load_module_subfunc(module, module_name,
			source_name, "getparam", false);
	info->setparam = load_module_subfunc(module, module_name,
			source_name, "setparam", false);
	info->enum_children = load_module_subfunc(module, module_name,
			source_name, "enum_children", false);

	info->filter_video = load_module_subfunc(module, module_name,
			source_name, "filter_video", false);
	info->filter_audio = load_module_subfunc(module, module_name,
			source_name, "filter_audio", false);

	info->name = source_name;
	return true;
}

65 66
static inline const struct source_info *find_source(struct darray *list,
		const char *name)
J
jp9000 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79
{
	size_t i;
	struct source_info *array = list->array;

	for (i = 0; i < list->num; i++) {
		struct source_info *info = array+i;
		if (strcmp(info->name, name) == 0)
			return info;
	}

	return NULL;
}

80
void obs_source_init(struct obs_source *source)
J
jp9000 已提交
81 82 83 84 85 86 87 88 89
{
	source->filter_target    = NULL;
	source->rendering_filter = false;

	dstr_init(&source->settings);
	da_init(source->filters);
	da_push_back(obs->sources, &source);
}

90
obs_source_t obs_source_create(enum source_type type, const char *name,
J
jp9000 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
		const char *settings)
{
	const struct source_info *info = NULL;
	struct darray *list = NULL;
	struct obs_source *source;

	switch (type) {
	case SOURCE_INPUT:      list = &obs->input_types.da; break;
	case SOURCE_FILTER:     list = &obs->filter_types.da; break;
	case SOURCE_TRANSITION: list = &obs->transition_types.da; break;
	default:
		return NULL;
	}

105
	info = find_source(list, name);
J
jp9000 已提交
106 107 108 109 110 111 112 113 114 115 116 117
	if (!info) {
		blog(LOG_WARNING, "Source '%s' not found", type);
		return NULL;
	}

	source = bmalloc(sizeof(struct obs_source));
	source->data = info->create(settings, source);
	if (!source->data) {
		bfree(source);
		return NULL;
	}

118
	obs_source_init(source);
J
jp9000 已提交
119 120 121 122 123
	dstr_copy(&source->settings, settings);
	memcpy(&source->callbacks, info, sizeof(struct source_info));
	return source;
}

124
void obs_source_destroy(obs_source_t source)
J
jp9000 已提交
125 126 127
{
	if (source) {
		da_free(source->filters);
128
		da_erase_item(obs->sources, &source);
J
jp9000 已提交
129 130 131 132 133 134 135

		source->callbacks.destroy(source->data);
		dstr_free(&source->settings);
		bfree(source);
	}
}

136
uint32_t obs_source_get_output_flags(obs_source_t source)
J
jp9000 已提交
137 138 139 140
{
	return source->callbacks.get_output_flags(source->data);
}

141
bool obs_source_hasconfig(obs_source_t source)
J
jp9000 已提交
142 143 144 145
{
	return source->callbacks.config != NULL;
}

146
void obs_source_config(obs_source_t source, void *parent)
J
jp9000 已提交
147 148 149 150 151
{
	if (source->callbacks.config)
		source->callbacks.config(source->data, parent);
}

152
void obs_source_activate(obs_source_t source)
J
jp9000 已提交
153 154 155 156 157
{
	if (source->callbacks.activate)
		source->callbacks.activate(source->data);
}

158
void obs_source_deactivate(obs_source_t source)
J
jp9000 已提交
159 160 161 162 163
{
	if (source->callbacks.deactivate)
		source->callbacks.deactivate(source->data);
}

164
void obs_source_video_tick(obs_source_t source, float seconds)
J
jp9000 已提交
165 166 167 168 169
{
	if (source->callbacks.video_tick)
		source->callbacks.video_tick(source->data, seconds);
}

170
void obs_source_video_render(obs_source_t source)
J
jp9000 已提交
171 172 173 174
{
	if (source->callbacks.video_render) {
		if (source->filters.num && !source->rendering_filter) {
			source->rendering_filter = true;
175
			obs_source_video_render(source->filters.array[0]);
J
jp9000 已提交
176 177 178 179 180 181 182
			source->rendering_filter = false;
		} else {
			source->callbacks.video_render(source->data);
		}
	}
}

183
int obs_source_getwidth(obs_source_t source)
J
jp9000 已提交
184 185 186 187 188 189
{
	if (source->callbacks.getwidth)
		return source->callbacks.getwidth(source->data);
	return 0;
}

190
int obs_source_getheight(obs_source_t source)
J
jp9000 已提交
191 192 193 194 195 196
{
	if (source->callbacks.getheight)
		return source->callbacks.getheight(source->data);
	return 0;
}

197
size_t obs_source_getparam(obs_source_t source, const char *param, void *buf,
J
jp9000 已提交
198 199 200 201 202 203 204 205
		size_t buf_size)
{
	if (source->callbacks.getparam)
		return source->callbacks.getparam(source->data, param, buf,
				buf_size);
	return 0;
}

206 207
void obs_source_setparam(obs_source_t source, const char *param,
		const void *data, size_t size)
J
jp9000 已提交
208 209 210 211 212
{
	if (source->callbacks.setparam)
		source->callbacks.setparam(source->data, param, data, size);
}

213 214
bool obs_source_enum_children(obs_source_t source, size_t idx,
		obs_source_t *child)
J
jp9000 已提交
215 216 217 218 219 220
{
	if (source->callbacks.enum_children)
		return source->callbacks.enum_children(source, idx, child);
	return false;
}

221
obs_source_t obs_filter_gettarget(obs_source_t filter)
J
jp9000 已提交
222 223 224 225
{
	return filter->filter_target;
}

226
void obs_source_filter_add(obs_source_t source, obs_source_t filter)
J
jp9000 已提交
227 228 229 230 231 232 233 234
{
	if (da_find(source->filters, &filter, 0) != -1) {
		blog(LOG_WARNING, "Tried to add a filter that was already "
		                  "present on the source");
		return;
	}

	if (source->filters.num) {
235
		obs_source_t *back = da_end(source->filters);
J
jp9000 已提交
236 237 238 239 240 241 242
		(*back)->filter_target = filter;
	}

	da_push_back(source->filters, &filter);
	filter->filter_target = source;
}

243
void obs_source_filter_remove(obs_source_t source, obs_source_t filter)
J
jp9000 已提交
244 245 246 247 248 249
{
	size_t idx = da_find(source->filters, &filter, 0);
	if (idx == -1)
		return;

	if (idx > 0) {
250
		obs_source_t prev = source->filters.array[idx-1];
J
jp9000 已提交
251 252 253 254 255 256 257
		prev->filter_target = filter->filter_target;
	}

	da_erase(source->filters, idx);
	filter->filter_target = NULL;
}

258
void obs_source_filter_setorder(obs_source_t source, obs_source_t filter,
J
jp9000 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		enum order_movement movement)
{
	size_t idx = da_find(source->filters, &filter, 0);
	size_t i;
	if (idx == -1)
		return;

	if (movement == ORDER_MOVE_UP) {
		if (idx == source->filters.num-1)
			return;
		da_move_item(source->filters, idx, idx+1);

	} else if (movement == ORDER_MOVE_DOWN) {
		if (idx == 0)
			return;
		da_move_item(source->filters, idx, idx-1);

	} else if (movement == ORDER_MOVE_TOP) {
		if (idx == source->filters.num-1)
			return;
		da_move_item(source->filters, idx, source->filters.num-1);

	} else if (movement == ORDER_MOVE_BOTTOM) {
		if (idx == 0)
			return;
		da_move_item(source->filters, idx, 0);
	}

	/* reorder filter targets */
	for (i = 0; i < source->filters.num; i++) {
289
		obs_source_t next_filter = (i == source->filters.num-1) ?
J
jp9000 已提交
290 291 292 293 294
			source : source->filters.array[idx+1];
		source->filters.array[i]->filter_target = next_filter;
	}
}

295
const char *obs_source_get_settings(obs_source_t source)
J
jp9000 已提交
296 297 298 299
{
	return source->settings.array;
}

300
void obs_source_save_settings(obs_source_t source, const char *settings)
J
jp9000 已提交
301 302 303 304
{
	dstr_copy(&source->settings, settings);
}

305
void obs_source_output_video(obs_source_t source, struct video_frame *frame)
J
jp9000 已提交
306 307 308 309
{
	/* TODO */
}

310
void obs_source_output_audio(obs_source_t source, struct audio_data *audio)
J
jp9000 已提交
311 312 313
{
	/* TODO */
}