forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obs.c
429 lines (335 loc) · 9.33 KB
/
obs.c
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/******************************************************************************
Copyright (C) 2013 by Hugh Bailey <[email protected]>
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 2 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"
#include "obs-module.h"
struct obs_subsystem *obs = NULL;
extern char *find_libobs_data_file(const char *file);
static inline void make_gs_init_data(struct gs_init_data *gid,
struct obs_video_info *ovi)
{
memcpy(&gid->window, &ovi->window, sizeof(struct gs_window));
gid->cx = ovi->window_width;
gid->cy = ovi->window_height;
gid->num_backbuffers = 2;
gid->format = GS_RGBA;
gid->zsformat = GS_ZS_NONE;
gid->adapter = ovi->adapter;
}
static inline void make_video_info(struct video_info *vi,
struct obs_video_info *ovi)
{
vi->name = "video";
vi->type = ovi->output_format;
vi->fps_num = ovi->fps_num;
vi->fps_den = ovi->fps_den;
vi->width = ovi->output_width;
vi->height = ovi->output_height;
}
static bool obs_init_textures(struct obs_video_info *ovi)
{
struct obs_video *video = &obs->video;
bool success = true;
size_t i;
for (i = 0; i < NUM_TEXTURES; i++) {
video->copy_surfaces[i] = gs_create_stagesurface(
ovi->output_width, ovi->output_height,
GS_RGBA);
if (!video->copy_surfaces[i])
return false;
video->render_textures[i] = gs_create_texture(
ovi->base_width, ovi->base_height,
GS_RGBA, 1, NULL, GS_RENDERTARGET);
if (!video->render_textures[i])
return false;
video->output_textures[i] = gs_create_texture(
ovi->output_width, ovi->output_height,
GS_RGBA, 1, NULL, GS_RENDERTARGET);
if (!video->output_textures[i])
return false;
}
return true;
}
static bool obs_init_graphics(struct obs_video_info *ovi)
{
struct obs_video *video = &obs->video;
struct gs_init_data graphics_data;
bool success = true;
int errorcode;
make_gs_init_data(&graphics_data, ovi);
video->output_width = ovi->output_width;
video->output_height = ovi->output_height;
errorcode = gs_create(&video->graphics, ovi->graphics_module,
&graphics_data);
if (errorcode != GS_SUCCESS) {
if (errorcode == GS_ERROR_MODULENOTFOUND)
blog(LOG_ERROR, "Could not find graphics module '%s'",
ovi->graphics_module);
return false;
}
gs_entercontext(video->graphics);
if (!obs_init_textures(ovi))
success = false;
if (success) {
char *filename = find_libobs_data_file("default.effect");
video->default_effect = gs_create_effect_from_file(filename,
NULL);
bfree(filename);
if (!video->default_effect)
success = false;
}
gs_leavecontext();
return success;
}
static bool obs_init_video(struct obs_video_info *ovi)
{
struct obs_video *video = &obs->video;
struct video_info vi;
int errorcode;
make_video_info(&vi, ovi);
errorcode = video_output_open(&video->video, obs->media, &vi);
if (errorcode != VIDEO_OUTPUT_SUCCESS) {
if (errorcode == VIDEO_OUTPUT_INVALIDPARAM)
blog(LOG_ERROR, "Invalid video parameters specified");
else
blog(LOG_ERROR, "Could not open video output");
return false;
}
errorcode = pthread_create(&video->video_thread, NULL,
obs_video_thread, obs);
if (errorcode != 0)
return false;
video->thread_initialized = true;
return true;
}
static void obs_free_video()
{
struct obs_video *video = &obs->video;
if (video->video) {
void *thread_retval;
video_output_stop(video->video);
if (video->thread_initialized) {
pthread_join(video->video_thread, &thread_retval);
video->thread_initialized = false;
}
video_output_close(video->video);
video->video = NULL;
}
}
static void obs_free_graphics()
{
struct obs_video *video = &obs->video;
size_t i;
if (video->graphics) {
int cur_texture = video->cur_texture;
gs_entercontext(video->graphics);
if (video->copy_mapped)
stagesurface_unmap(video->copy_surfaces[cur_texture]);
for (i = 0; i < NUM_TEXTURES; i++) {
stagesurface_destroy(video->copy_surfaces[i]);
texture_destroy(video->render_textures[i]);
texture_destroy(video->output_textures[i]);
video->copy_surfaces[i] = NULL;
video->render_textures[i] = NULL;
video->output_textures[i] = NULL;
}
effect_destroy(video->default_effect);
video->default_effect = NULL;
gs_leavecontext();
gs_destroy(video->graphics);
video->graphics = NULL;
video->cur_texture = 0;
}
}
static bool obs_init_audio(struct audio_info *ai)
{
struct obs_audio *audio = &obs->audio;
int errorcode;
/* TODO: sound subsystem */
errorcode = audio_output_open(&audio->audio, obs->media, ai);
if (errorcode == AUDIO_OUTPUT_SUCCESS)
return true;
else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
blog(LOG_ERROR, "Invalid audio parameters specified");
else
blog(LOG_ERROR, "Could not open audio output");
return false;
}
static void obs_free_audio(void)
{
struct obs_audio *audio = &obs->audio;
if (audio->audio)
audio_output_close(audio->audio);
memset(audio, 0, sizeof(struct obs_audio));
}
static bool obs_init_data(void)
{
struct obs_data *data = &obs->data;
pthread_mutex_init_value(&obs->data.displays_mutex);
if (pthread_mutex_init(&data->sources_mutex, NULL) != 0)
return false;
if (pthread_mutex_init(&data->displays_mutex, NULL) != 0)
return false;
return true;
}
static void obs_free_data(void)
{
struct obs_data *data = &obs->data;
uint32_t i;
for (i = 0; i < MAX_CHANNELS; i++)
obs_set_output_source(i, NULL);
while (data->displays.num)
obs_display_destroy(data->displays.array[0]);
pthread_mutex_lock(&obs->data.sources_mutex);
for (i = 0; i < data->sources.num; i++)
obs_source_release(data->sources.array[i]);
da_free(data->sources);
pthread_mutex_unlock(&obs->data.sources_mutex);
}
static bool obs_init(void)
{
obs = bmalloc(sizeof(struct obs_subsystem));
memset(obs, 0, sizeof(struct obs_subsystem));
obs_init_data();
obs->media = media_open();
if (!obs->media)
return false;
return true;
}
bool obs_startup()
{
bool success;
if (obs) {
blog(LOG_ERROR, "Tried to call obs_startup more than once");
return false;
}
success = obs_init();
if (!success)
obs_shutdown();
return success;
}
void obs_shutdown(void)
{
size_t i;
if (!obs)
return;
da_free(obs->input_types);
da_free(obs->filter_types);
da_free(obs->transition_types);
da_free(obs->output_types);
da_free(obs->service_types);
obs_free_data();
obs_free_video();
obs_free_graphics();
obs_free_audio();
media_close(obs->media);
for (i = 0; i < obs->modules.num; i++)
free_module(obs->modules.array+i);
da_free(obs->modules);
bfree(obs);
obs = NULL;
}
bool obs_reset_video(struct obs_video_info *ovi)
{
struct obs_video *video = &obs->video;
obs_free_video();
if (!ovi) {
obs_free_graphics();
return true;
}
if (!video->graphics && !obs_init_graphics(ovi))
return false;
return obs_init_video(ovi);
}
bool obs_reset_audio(struct audio_info *ai)
{
/*obs_free_audio();
if(!ai)
return true;
return obs_init_audio(ai);*/
/* TODO */
return true;
}
bool obs_enum_input_types(size_t idx, const char **name)
{
if (idx >= obs->input_types.num)
return false;
*name = obs->input_types.array[idx].name;
return true;
}
bool obs_enum_filter_types(size_t idx, const char **name)
{
if (idx >= obs->filter_types.num)
return false;
*name = obs->filter_types.array[idx].name;
return true;
}
bool obs_enum_transition_types(size_t idx, const char **name)
{
if (idx >= obs->transition_types.num)
return false;
*name = obs->transition_types.array[idx].name;
return true;
}
bool obs_enum_output_types(size_t idx, const char **name)
{
if (idx >= obs->output_types.num)
return false;
*name = obs->output_types.array[idx].name;
return true;
}
graphics_t obs_graphics(void)
{
return obs->video.graphics;
}
media_t obs_media(void)
{
return obs->media;
}
bool obs_add_source(obs_source_t source)
{
pthread_mutex_lock(&obs->data.sources_mutex);
da_push_back(obs->data.sources, &source);
obs_source_addref(source);
pthread_mutex_unlock(&obs->data.sources_mutex);
return true;
}
obs_source_t obs_get_output_source(uint32_t channel)
{
struct obs_source *source;
assert(channel < MAX_CHANNELS);
source = obs->data.channels[channel];
obs_source_addref(source);
return source;
}
void obs_set_output_source(uint32_t channel, obs_source_t source)
{
struct obs_source *prev_source;
assert(channel < MAX_CHANNELS);
prev_source = obs->data.channels[channel];
obs->data.channels[channel] = source;
if (source)
obs_source_addref(source);
if (prev_source)
obs_source_release(prev_source);
}
void obs_enum_sources(ENUM_SOURCES_PROC enum_proc, void *param)
{
struct obs_data *data = &obs->data;
size_t i;
pthread_mutex_lock(&data->sources_mutex);
for (i = 0; i < data->sources.num; i++)
enum_proc(data->sources.array[i], param);
pthread_mutex_unlock(&data->sources_mutex);
}