obs-source.h 8.8 KB
Newer Older
J
jp9000 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/******************************************************************************
    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/>.
******************************************************************************/

18
#pragma once
J
jp9000 已提交
19 20 21 22

#include "util/c99defs.h"
#include "util/darray.h"
#include "util/dstr.h"
23
#include "util/threading.h"
J
jp9000 已提交
24
#include "media-io/media-io.h"
25
#include "media-io/audio-resampler.h"
J
jp9000 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38

/*
 * ===========================================
 *   Sources
 * ===========================================
 *
 *   A source is literally a "source" of audio and/or video.
 *
 *   A module with sources needs to export these functions:
 *       + enum_[type]
 *
 *   Each individual source is then exported by it's name.  For example, a
 * source named "mysource" would have the following exports:
39
 *       + mysource_getname
J
jp9000 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 *       + mysource_create
 *       + mysource_destroy
 *       + mysource_getflags
 *
 *       [and optionally]
 *       + mysource_update
 *       + mysource_config
 *       + mysource_video_tick
 *       + mysource_video_render
 *       + mysource_getparam
 *       + mysource_setparam
 *
 * ===========================================
 *   Primary Exports
 * ===========================================
 *   const bool enum_[type](size_t idx, const char **name);
 *       idx: index of the source.
 *       type: pointer to variable that receives the type of the source
 *       Return value: false when no more available.
 *
 * ===========================================
 *   Source Exports
 * ===========================================
63 64 65 66
 *   const char *[name]_getname(const char *locale);
 *       Returns the full name of the source type (seen by the user).
 *
 * ---------------------------------------------------------
67
 *   void *[name]_create(const char *settings, obs_source_t source);
J
jp9000 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
 *       Creates a source.
 *
 *       settings: Settings of the source.
 *       source: pointer to main source
 *       Return value: Internal source pointer, or NULL if failed.
 *
 * ---------------------------------------------------------
 *   void [name]_destroy(void *data);
 *       Destroys the source.
 *
 * ---------------------------------------------------------
 *   uint32_t [name]_getflags(void *data);
 *       Returns a combination of one of the following values:
 *           + SOURCE_VIDEO: source has video
 *           + SOURCE_AUDIO: source has audio
 *           + SOURCE_ASYNC: video is sent asynchronously via RAM
 *
 * ---------------------------------------------------------
J
jp9000 已提交
86
 *   uint32_t [name]_getwidth(void *data);
J
jp9000 已提交
87 88 89 90
 *       Returns the width of a source, or -1 for maximum width.  If you render
 *       video, this is required.
 *
 * ---------------------------------------------------------
J
jp9000 已提交
91
 *   uint32_t [name]_getheight(void *data);
J
jp9000 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
 *       Returns the height of a source, or -1 for maximum height.  If you
 *       render video, this is required.
 *
 * ===========================================
 *   Optional Source Exports
 * ===========================================
 *   void [name]_config(void *data, void *parent);
 *       Called to configure the source.
 *
 *       parent: Parent window pointer
 *
 * ---------------------------------------------------------
 *   void [name]_video_activate(void *data);
 *       Called when the source is being displayed.
 *
 * ---------------------------------------------------------
 *   void [name]_video_deactivate(void *data);
 *       Called when the source is no longer being displayed.
 *
 * ---------------------------------------------------------
 *   void [name]_video_tick(void *data, float seconds);
 *       Called each video frame with the time taken between the last and
 *       current frame, in seconds.
 *
 * ---------------------------------------------------------
 *   void [name]_video_render(void *data);
 *       Called to render the source.
 *
 * ---------------------------------------------------------
 *   void [name]_getparam(void *data, const char *param, void *buf,
 *                        size_t buf_size);
 *       Gets a source parameter value.  
 *
 *       param: Name of parameter.
 *       Return value: Value of parameter.
 *
 * ---------------------------------------------------------
 *   void [name]_setparam(void *data, const char *param, const void *buf,
 *                        size_t size);
 *       Sets a source parameter value.
 *
 *       param: Name of parameter.
 *       val: Value of parameter to set.
 *
 * ---------------------------------------------------------
137
 *   bool [name]_enum_children(void *data, size_t idx, obs_source_t *child);
J
jp9000 已提交
138 139 140 141 142 143 144
 *       Enumerates child sources, if any.
 *
 *       idx: Child source index.
 *       child: Pointer to variable that receives child source. 
 *       Return value: true if sources remaining, otherwise false.
 *
 * ---------------------------------------------------------
145 146
 *   struct source_frame *[name]_filter_video(void *data,
 *                                     const struct source_frame *frame);
J
jp9000 已提交
147 148 149
 *       Filters audio data.  Used with audio filters.
 *
 *       frame: Video frame data.
150
 *       returns: New video frame data (or NULL if pending)
J
jp9000 已提交
151 152
 *
 * ---------------------------------------------------------
153 154
 *   struct filter_audio [name]_filter_audio(void *data,
 *                                     struct filter_audio *audio);
J
jp9000 已提交
155 156 157
 *       Filters video data.  Used with async video data.
 *
 *       audio: Audio data.
158
 *       reutrns New audio data (or NULL if pending)
J
jp9000 已提交
159 160 161 162 163 164 165 166 167 168
 */

struct obs_source;

struct source_info {
	const char *name;

	/* ----------------------------------------------------------------- */
	/* required implementations */

169 170
	const char *(*getname)(const char *locale);

171
	void *(*create)(const char *settings, obs_source_t source);
J
jp9000 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185
	void (*destroy)(void *data);

	uint32_t (*get_output_flags)(void *data);

	/* ----------------------------------------------------------------- */
	/* optional implementations */

	bool (*config)(void *data, void *parent);

	void (*activate)(void *data);
	void (*deactivate)(void *data);

	void (*video_tick)(void *data, float seconds);
	void (*video_render)(void *data);
J
jp9000 已提交
186 187
	uint32_t (*getwidth)(void *data);
	uint32_t (*getheight)(void *data);
J
jp9000 已提交
188 189 190 191 192 193

	size_t (*getparam)(void *data, const char *param, void *data_out,
			size_t buf_size);
	void (*setparam)(void *data, const char *param, const void *data_in,
			size_t size);

194
	bool (*enum_children)(void *data, size_t idx, obs_source_t *child);
J
jp9000 已提交
195

196 197
	struct source_frame *(*filter_video)(void *data,
			const struct source_frame *frame);
198 199
	struct filtered_audio *(*filter_audio)(void *data,
			struct filtered_audio *audio);
J
jp9000 已提交
200 201
};

202 203 204 205 206
struct audiobuf {
	void     *data;
	uint32_t frames;
	uint64_t timestamp;
};
J
jp9000 已提交
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221
static inline void audiobuf_free(struct audiobuf *buf)
{
	bfree(buf->data);
}

struct obs_source {
	void                         *data; /* source-specific data */
	struct source_info           callbacks;
	struct dstr                  settings;
	bool                         valid;

	/* async video and audio */
	bool                         timing_set;
	uint64_t                     timing_adjust;
222 223
	uint64_t                     last_frame_timestamp;
	uint64_t                     last_sys_timestamp;
224 225
	texture_t                    output_texture;

226 227 228
	bool                         audio_failed;
	struct resample_info         sample_info;
	audio_resampler_t            resampler;
229
	audio_line_t                 audio_line;
230
	DARRAY(struct audiobuf)      audio_wait_buffer;
231 232 233
	DARRAY(struct source_frame*) video_frames;
	pthread_mutex_t              audio_mutex;
	pthread_mutex_t              video_mutex;
234 235
	struct filtered_audio        audio_data;
	size_t                       audio_storage_size;
236 237 238 239 240 241 242

	/* filters */
	struct obs_source            *filter_parent;
	struct obs_source            *filter_target;
	DARRAY(struct obs_source*)   filters;
	pthread_mutex_t              filter_mutex;
	bool                         rendering_filter;
J
jp9000 已提交
243 244 245 246 247
};

extern bool get_source_info(void *module, const char *module_name,
		const char *source_name, struct source_info *info);

248 249
extern bool obs_source_init(struct obs_source *source, const char *settings,
		const struct source_info *info);
J
jp9000 已提交
250

251 252 253
extern void obs_source_activate(obs_source_t source);
extern void obs_source_deactivate(obs_source_t source);
extern void obs_source_video_tick(obs_source_t source, float seconds);