forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobs-output-delay.c
214 lines (172 loc) · 5.52 KB
/
obs-output-delay.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
/******************************************************************************
Copyright (C) 2015 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 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 <inttypes.h>
#include "obs-internal.h"
static inline bool delay_active(const struct obs_output *output)
{
return os_atomic_load_bool(&output->delay_active);
}
static inline bool delay_capturing(const struct obs_output *output)
{
return os_atomic_load_bool(&output->delay_capturing);
}
static inline void push_packet(struct obs_output *output,
struct encoder_packet *packet, uint64_t t)
{
struct delay_data dd;
dd.msg = DELAY_MSG_PACKET;
dd.ts = t;
obs_encoder_packet_create_instance(&dd.packet, packet);
pthread_mutex_lock(&output->delay_mutex);
circlebuf_push_back(&output->delay_data, &dd, sizeof(dd));
pthread_mutex_unlock(&output->delay_mutex);
}
static inline void process_delay_data(struct obs_output *output,
struct delay_data *dd)
{
switch (dd->msg) {
case DELAY_MSG_PACKET:
if (!delay_active(output) || !delay_capturing(output))
obs_encoder_packet_release(&dd->packet);
else
output->delay_callback(output, &dd->packet);
break;
case DELAY_MSG_START:
obs_output_actual_start(output);
break;
case DELAY_MSG_STOP:
obs_output_actual_stop(output, false, dd->ts);
break;
}
}
void obs_output_cleanup_delay(obs_output_t *output)
{
struct delay_data dd;
while (output->delay_data.size) {
circlebuf_pop_front(&output->delay_data, &dd, sizeof(dd));
if (dd.msg == DELAY_MSG_PACKET) {
obs_encoder_packet_release(&dd.packet);
}
}
output->active_delay_ns = 0;
os_atomic_set_long(&output->delay_restart_refs, 0);
}
static inline bool pop_packet(struct obs_output *output, uint64_t t)
{
uint64_t elapsed_time;
struct delay_data dd;
bool popped = false;
bool preserve;
/* ------------------------------------------------ */
preserve = (output->delay_cur_flags & OBS_OUTPUT_DELAY_PRESERVE) != 0;
pthread_mutex_lock(&output->delay_mutex);
if (output->delay_data.size) {
circlebuf_peek_front(&output->delay_data, &dd, sizeof(dd));
elapsed_time = (t - dd.ts);
if (preserve && output->reconnecting) {
output->active_delay_ns = elapsed_time;
} else if (elapsed_time > output->active_delay_ns) {
circlebuf_pop_front(&output->delay_data, NULL,
sizeof(dd));
popped = true;
}
}
pthread_mutex_unlock(&output->delay_mutex);
/* ------------------------------------------------ */
if (popped)
process_delay_data(output, &dd);
return popped;
}
void process_delay(void *data, struct encoder_packet *packet)
{
struct obs_output *output = data;
uint64_t t = os_gettime_ns();
push_packet(output, packet, t);
while (pop_packet(output, t))
;
}
void obs_output_signal_delay(obs_output_t *output, const char *signal)
{
struct calldata params;
uint8_t stack[128];
calldata_init_fixed(¶ms, stack, sizeof(stack));
calldata_set_ptr(¶ms, "output", output);
calldata_set_int(¶ms, "sec", output->active_delay_ns / 1000000000);
signal_handler_signal(output->context.signals, signal, ¶ms);
}
bool obs_output_delay_start(obs_output_t *output)
{
struct delay_data dd = {
.msg = DELAY_MSG_START,
.ts = os_gettime_ns(),
};
if (!delay_active(output)) {
bool can_begin = obs_output_can_begin_data_capture(output, 0);
if (!can_begin)
return false;
if (!obs_output_initialize_encoders(output, 0))
return false;
}
pthread_mutex_lock(&output->delay_mutex);
circlebuf_push_back(&output->delay_data, &dd, sizeof(dd));
pthread_mutex_unlock(&output->delay_mutex);
os_atomic_inc_long(&output->delay_restart_refs);
if (delay_active(output)) {
do_output_signal(output, "starting");
return true;
}
if (!obs_output_begin_data_capture(output, 0)) {
obs_output_cleanup_delay(output);
return false;
}
return true;
}
void obs_output_delay_stop(obs_output_t *output)
{
struct delay_data dd = {
.msg = DELAY_MSG_STOP,
.ts = os_gettime_ns(),
};
pthread_mutex_lock(&output->delay_mutex);
circlebuf_push_back(&output->delay_data, &dd, sizeof(dd));
pthread_mutex_unlock(&output->delay_mutex);
do_output_signal(output, "stopping");
}
void obs_output_set_delay(obs_output_t *output, uint32_t delay_sec,
uint32_t flags)
{
if (!obs_output_valid(output, "obs_output_set_delay"))
return;
if ((output->info.flags & OBS_OUTPUT_ENCODED) == 0) {
blog(LOG_WARNING,
"Output '%s': Tried to set a delay "
"value on a non-encoded output",
output->context.name);
return;
}
output->delay_sec = delay_sec;
output->delay_flags = flags;
}
uint32_t obs_output_get_delay(const obs_output_t *output)
{
return obs_output_valid(output, "obs_output_set_delay")
? output->delay_sec
: 0;
}
uint32_t obs_output_get_active_delay(const obs_output_t *output)
{
return obs_output_valid(output, "obs_output_set_delay")
? (uint32_t)(output->active_delay_ns / 1000000000ULL)
: 0;
}