-
Notifications
You must be signed in to change notification settings - Fork 56
/
rtsp_output_helper.cpp
230 lines (202 loc) · 6.12 KB
/
rtsp_output_helper.cpp
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
#include <vector>
#include <cstdio>
#include <obs-module.h>
#include <obs-frontend-api.h>
#include <util/config-file.h>
#include <util/dstr.h>
#include "rtsp_output_helper.h"
#include "helper.h"
using namespace std;
RtspOutputHelper::RtspOutputHelper(const string outputName)
: RtspOutputHelper(obs_get_output_by_name(outputName.c_str()))
{
}
RtspOutputHelper::RtspOutputHelper(obs_output_t *obsOutput)
: obsOutput(obsOutput)
{
}
RtspOutputHelper::~RtspOutputHelper()
{
obs_output_release(obsOutput);
obs_encoder_release(videoEncoder);
for (const auto audioEncoder : audioEncoders)
obs_encoder_release(audioEncoder);
}
RtspOutputHelper *RtspOutputHelper::CreateRtspOutput(obs_data_t *settings,
obs_data_t *hotkey)
{
const auto rtspOutput = new RtspOutputHelper(
obs_output_create("rtsp_output", obs_module_text("RtspOutput"),
settings, hotkey));
rtspOutput->SignalConnect(
"pre_start", OnPreStartSignal, rtspOutput);
return rtspOutput;
}
void RtspOutputHelper::UpdateSettings(obs_data_t *settings) const
{
obs_output_update(obsOutput, settings);
}
obs_data_t *RtspOutputHelper::GetSettings() const
{
return obs_output_get_settings(obsOutput);
}
void RtspOutputHelper::UpdateEncoder()
{
GetBaseConfig();
CreateVideoEncoder();
CreateAudioEncoder();
}
bool RtspOutputHelper::Start() const
{
return obs_output_start(obsOutput);
}
void RtspOutputHelper::Stop() const
{
obs_output_stop(obsOutput);
}
string RtspOutputHelper::GetLastError() const
{
return obs_output_get_last_error(obsOutput);
}
obs_data_t *RtspOutputHelper::HotkeysSave() const
{
return obs_hotkeys_save_output(obsOutput);
}
void RtspOutputHelper::SignalConnect(const char *signal,
signal_callback_t callback, void *data) const
{
const auto handler = obs_output_get_signal_handler(obsOutput);
signal_handler_connect(handler, signal, callback, data);
}
void RtspOutputHelper::SignalDisconnect(const char *signal,
signal_callback_t callback, void *data) const
{
const auto handler = obs_output_get_signal_handler(obsOutput);
signal_handler_disconnect(handler, signal, callback, data);
}
string RtspOutputHelper::GetOutputName() const
{
return string(obs_output_get_name(obsOutput));
}
uint64_t RtspOutputHelper::GetTotalBytes() const
{
return obs_output_get_total_bytes(obsOutput);
}
int RtspOutputHelper::GetTotalFrames() const
{
return obs_output_get_total_frames(obsOutput);
}
int RtspOutputHelper::GetFramesDropped() const
{
return obs_output_get_frames_dropped(obsOutput);
}
bool RtspOutputHelper::IsActive() const
{
return obs_output_active(obsOutput);
}
void RtspOutputHelper::CreateVideoEncoder()
{
obs_encoder_t *encoder;
if (outputSettings.adv_out)
//encoder = obs_get_encoder_by_name("streaming_h264"); //OBS 27.2.4 Or Older
encoder = obs_get_encoder_by_name("advanced_video_stream");
else
//encoder = obs_get_encoder_by_name("simple_h264_stream"); //OBS 27.2.4 Or Older
encoder = obs_get_encoder_by_name("simple_video_stream");
obs_encoder_release(videoEncoder);
videoEncoder = obs_video_encoder_create(
obs_encoder_get_id(encoder), "rtsp_output_video",
obs_encoder_get_settings(encoder), nullptr);
obs_encoder_release(encoder);
if (outputSettings.adv_out)
obs_encoder_set_scaled_size(videoEncoder,
outputSettings.rescale_cx,
outputSettings.rescale_cy);
obs_encoder_set_video(videoEncoder, obs_get_video());
{
auto video = obs_output_video(obsOutput);
if (video == nullptr)
video = obs_get_video();
obs_encoder_set_video(videoEncoder, video);
}
obs_output_set_video_encoder(obsOutput, videoEncoder);
}
void RtspOutputHelper::CreateAudioEncoder()
{
obs_encoder_t *encoder;
if (outputSettings.adv_out) {
/*if ((encoder = obs_get_encoder_by_name("adv_stream_aac")) ==
nullptr)
encoder = obs_get_encoder_by_name(
"avc_aac_stream");*/ //OBS 26.0.2 Or Older
/*if ((encoder = obs_get_encoder_by_name("adv_stream_audio")) ==
nullptr) //OBS 30.0.0 Or Older
encoder = obs_get_encoder_by_name("adv_stream_aac");*/
encoder = obs_get_encoder_by_name("adv_stream_audio");
}
else
encoder = obs_get_encoder_by_name("simple_aac");
for (const auto audioEncoder : audioEncoders)
obs_encoder_release(audioEncoder);
audioEncoders.clear();
const auto config = rtsp_properties_open_config();
auto tracks =
static_cast<uint8_t>(config_get_uint(config, CONFIG_SECTIION, "AudioTracks"));
{
auto outputData = GetSettings();
if (tracks == 0) {
obs_data_set_bool(outputData, "output_audio", false);
tracks = 0x1;
} else obs_data_set_bool(outputData, "output_audio", true);
UpdateSettings(outputData);
obs_data_release(outputData);
}
auto trackIndex = 0;
for (auto idx = 0; idx < OBS_OUTPUT_MULTI_TRACK; idx++) {
if ((tracks & (1 << idx)) == 0) continue;
auto audioEncoder = obs_audio_encoder_create(
obs_encoder_get_id(encoder),
string("rtsp_output_audio_track")
.append(to_string(idx + 1))
.c_str(),
obs_encoder_get_settings(encoder), idx, nullptr);
{
auto audio = obs_output_audio(obsOutput);
if (audio == nullptr)
audio = obs_get_audio();
obs_encoder_set_audio(audioEncoder, audio);
}
audioEncoders.push_back(audioEncoder);
obs_output_set_audio_encoder(obsOutput, audioEncoder,
trackIndex++);
}
config_close(config);
obs_encoder_release(encoder);
}
void RtspOutputHelper::GetBaseConfig()
{
config_t *basicConfig = obs_frontend_get_profile_config();
const char *mode = config_get_string(basicConfig, "Output", "Mode");
outputSettings.adv_out = astrcmpi(mode, "Advanced") == 0;
outputSettings.rescale_cx = 0;
outputSettings.rescale_cy = 0;
if (outputSettings.adv_out) {
const bool rescale =
config_get_bool(basicConfig, "AdvOut", "Rescale");
if (const char *rescaleRes =
config_get_string(basicConfig, "AdvOut", "RescaleRes"); rescale && rescaleRes && *rescaleRes) {
if (sscanf(rescaleRes, "%ux%u",
&outputSettings.rescale_cx,
&outputSettings.rescale_cy) != 2) {
outputSettings.rescale_cx = 0;
outputSettings.rescale_cy = 0;
}
}
}
}
void RtspOutputHelper::OnPreStartSignal(void *data, calldata_t *cd)
{
UNUSED_PARAMETER(cd);
const auto helper = static_cast<RtspOutputHelper *>(data);
helper->UpdateEncoder();
}