forked from iamscottxu/obs-rtspserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtsp_output_helper.cpp
200 lines (173 loc) · 5.31 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
#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);
}
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_output_video(obsOutput));
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
encoder = obs_get_encoder_by_name("adv_stream_aac");
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 trackIndex = 0;
for (auto idx = 0; idx < OBS_OUTPUT_MULTI_TRACK; idx++) {
if (!config_get_bool(config, CONFIG_SECTIION,
string("AudioTrack")
.append(to_string(idx + 1))
.c_str()))
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);
obs_encoder_set_audio(audioEncoder,
obs_output_audio(obsOutput));
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();
}