Skip to content

Commit 1e4e112

Browse files
committed
BasicRTSPOnlySubsession: further refactor
Merged duplicate messaging - separate audio and video where the format of messages is identical, only the path where to send differs.
1 parent b27b494 commit 1e4e112

2 files changed

Lines changed: 55 additions & 122 deletions

File tree

src/rtsp/BasicRTSPOnlySubsession.cpp

Lines changed: 54 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ BasicRTSPOnlySubsession::createNew(UsageEnvironment& env,
6969
rtpPort, params);
7070
}
7171

72+
constexpr enum module_class path_sender_audio[] = { MODULE_CLASS_AUDIO,
73+
MODULE_CLASS_SENDER,
74+
MODULE_CLASS_NONE };
75+
constexpr enum module_class path_sender_video[] = { MODULE_CLASS_SENDER,
76+
MODULE_CLASS_NONE };
77+
7278
BasicRTSPOnlySubsession::BasicRTSPOnlySubsession(UsageEnvironment& env,
7379
Boolean reuseFirstSource, rtsp_types_t avType, int rtpPort,
7480
struct rtsp_server_parameters params) :
@@ -80,6 +86,8 @@ BasicRTSPOnlySubsession::BasicRTSPOnlySubsession(UsageEnvironment& env,
8086
gethostname(fCNAME, sizeof fCNAME);
8187
this->avType = avType;
8288
this->rtpPort = rtpPort;
89+
this->sender_msg_path =
90+
avType == rtsp_type_audio ? path_sender_audio : path_sender_video;
8391
fCNAME[sizeof fCNAME - 1] = '\0';
8492

8593
// print (preliminary) SDP
@@ -201,131 +209,55 @@ void BasicRTSPOnlySubsession::startStream(unsigned /* clientSessionId */,
201209
return;
202210
}
203211

204-
if (avType == rtsp_type_video) {
205-
char pathV[1024];
206-
207-
memset(pathV, 0, sizeof(pathV));
208-
enum module_class path_sender[] = { MODULE_CLASS_SENDER,
209-
MODULE_CLASS_NONE };
210-
append_message_path(pathV, sizeof(pathV), path_sender);
211-
212-
//CHANGE DST PORT
213-
struct msg_sender *msgV1 = (struct msg_sender *) new_message(
214-
sizeof(struct msg_sender));
215-
msgV1->tx_port = ntohs(destination->rtpPort.num());
216-
msgV1->type = SENDER_MSG_CHANGE_PORT;
217-
resp = send_message(rtsp_params.parent, pathV, (struct message *) msgV1);
218-
free_response(resp);
219-
220-
//CHANGE DST ADDRESS
221-
struct msg_sender *msgV2 = (struct msg_sender *) new_message(
222-
sizeof(struct msg_sender));
223-
char host[IN6_MAX_ASCII_LEN + 1];
224-
const int ret =
225-
getnameinfo((struct sockaddr *) &destination->addr,
226-
sizeof destination->addr, host,
227-
sizeof host, nullptr, 0, NI_NUMERICHOST);
228-
assert(ret == 0);
229-
strncpy(msgV2->receiver, host,
230-
sizeof(msgV2->receiver) - 1);
231-
msgV2->type = SENDER_MSG_CHANGE_RECEIVER;
232-
233-
resp = send_message(rtsp_params.parent, pathV, (struct message *) msgV2);
234-
free_response(resp);
235-
}
236-
237-
if (avType == rtsp_type_audio) {
238-
char pathA[1024];
239-
240-
memset(pathA, 0, sizeof(pathA));
241-
enum module_class path_sender[] = { MODULE_CLASS_AUDIO,
242-
MODULE_CLASS_SENDER, MODULE_CLASS_NONE };
243-
append_message_path(pathA, sizeof(pathA), path_sender);
244-
245-
//CHANGE DST PORT
246-
struct msg_sender *msgA1 = (struct msg_sender *) new_message(
247-
sizeof(struct msg_sender));
248-
msgA1->tx_port = ntohs(destination->rtpPort.num());
249-
msgA1->type = SENDER_MSG_CHANGE_PORT;
250-
resp = send_message(rtsp_params.parent, pathA, (struct message *) msgA1);
251-
free_response(resp);
252-
resp = NULL;
253-
254-
//CHANGE DST ADDRESS
255-
struct msg_sender *msgA2 = (struct msg_sender *) new_message(
256-
sizeof(struct msg_sender));
257-
char host[IN6_MAX_ASCII_LEN + 1];
258-
const int ret =
259-
getnameinfo((struct sockaddr *) &destination->addr,
260-
sizeof destination->addr, host,
261-
sizeof host, nullptr, 0, NI_NUMERICHOST);
262-
assert(ret == 0);
263-
strncpy(msgA2->receiver, host,
264-
sizeof(msgA2->receiver) - 1);
265-
msgA2->type = SENDER_MSG_CHANGE_RECEIVER;
266-
267-
resp = send_message(rtsp_params.parent, pathA, (struct message *) msgA2);
268-
free_response(resp);
269-
resp = NULL;
270-
}
212+
char path[1024] = "";
213+
214+
append_message_path(path, sizeof(path), sender_msg_path);
215+
216+
//CHANGE DST PORT
217+
auto *msg1 =
218+
(struct msg_sender *) new_message(sizeof(struct msg_sender));
219+
msg1->tx_port = ntohs(destination->rtpPort.num());
220+
msg1->type = SENDER_MSG_CHANGE_PORT;
221+
resp = send_message(rtsp_params.parent, path, (struct message *) msg1);
222+
free_response(resp);
223+
224+
// CHANGE DST ADDRESS
225+
auto *msg2 =
226+
(struct msg_sender *) new_message(sizeof(struct msg_sender));
227+
char host[IN6_MAX_ASCII_LEN + 1];
228+
const int ret = getnameinfo((struct sockaddr *) &destination->addr,
229+
sizeof destination->addr, host, sizeof host,
230+
nullptr, 0, NI_NUMERICHOST);
231+
assert(ret == 0);
232+
strncpy(msg2->receiver, host, sizeof(msg2->receiver) - 1);
233+
msg2->type = SENDER_MSG_CHANGE_RECEIVER;
234+
235+
resp = send_message(rtsp_params.parent, path, (struct message *) msg2);
236+
free_response(resp);
271237
}
272238

273239
void BasicRTSPOnlySubsession::deleteStream(unsigned /* clientSessionId */,
274240
void*& /* streamToken */) {
275-
if (avType == rtsp_type_video) {
276-
char pathV[1024];
277-
delete destination;
278-
destination = NULL;
279-
memset(pathV, 0, sizeof(pathV));
280-
enum module_class path_sender[] = { MODULE_CLASS_SENDER,
281-
MODULE_CLASS_NONE };
282-
append_message_path(pathV, sizeof(pathV), path_sender);
283-
284-
//CHANGE DST PORT
285-
struct msg_sender *msgV1 = (struct msg_sender *) new_message(
286-
sizeof(struct msg_sender));
287-
msgV1->tx_port = rtsp_params.rtp_port_video;
288-
msgV1->type = SENDER_MSG_CHANGE_PORT;
289-
struct response *resp;
290-
resp = send_message(rtsp_params.parent, pathV, (struct message *) msgV1);
291-
free_response(resp);
292-
293-
//CHANGE DST ADDRESS
294-
struct msg_sender *msgV2 = (struct msg_sender *) new_message(
295-
sizeof(struct msg_sender));
296-
strncpy(msgV2->receiver, "127.0.0.1", sizeof(msgV2->receiver) - 1);
297-
msgV2->type = SENDER_MSG_CHANGE_RECEIVER;
298-
resp = send_message(rtsp_params.parent, pathV, (struct message *) msgV2);
299-
free_response(resp);
300-
}
301-
302-
if (avType == rtsp_type_audio) {
303-
char pathA[1024];
304-
delete destination;
305-
destination = NULL;
306-
memset(pathA, 0, sizeof(pathA));
307-
enum module_class path_sender[] = { MODULE_CLASS_AUDIO,
308-
MODULE_CLASS_SENDER, MODULE_CLASS_NONE };
309-
append_message_path(pathA, sizeof(pathA), path_sender);
310-
311-
//CHANGE DST PORT
312-
struct msg_sender *msgA1 = (struct msg_sender *) new_message(
313-
sizeof(struct msg_sender));
314-
315-
//TODO: GET AUDIO PORT SET (NOT A COMMON CASE WHEN RTSP IS ENABLED: DEFAULT -> vport + 2)
316-
msgA1->tx_port = rtsp_params.rtp_port_audio;
317-
msgA1->type = SENDER_MSG_CHANGE_PORT;
318-
struct response *resp;
319-
resp = send_message(rtsp_params.parent, pathA, (struct message *) msgA1);
320-
free_response(resp);
321-
322-
//CHANGE DST ADDRESS
323-
struct msg_sender *msgA2 = (struct msg_sender *) new_message(
324-
sizeof(struct msg_sender));
325-
strncpy(msgA2->receiver, "127.0.0.1", sizeof(msgA2->receiver) - 1);
326-
msgA2->type = SENDER_MSG_CHANGE_RECEIVER;
327-
resp = send_message(rtsp_params.parent, pathA, (struct message *) msgA2);
328-
free_response(resp);
329-
}
241+
char path[1024] = "";
242+
delete destination;
243+
destination = nullptr;
244+
append_message_path(path, sizeof(path), sender_msg_path);
245+
246+
// CHANGE DST PORT
247+
auto *msg1 =
248+
(struct msg_sender *) new_message(sizeof(struct msg_sender));
249+
msg1->tx_port = rtsp_params.rtp_port_video;
250+
msg1->type = SENDER_MSG_CHANGE_PORT;
251+
struct response *resp =
252+
send_message(rtsp_params.parent, path, (struct message *) msg1);
253+
free_response(resp);
254+
255+
// CHANGE DST ADDRESS
256+
auto *msg2 =
257+
(struct msg_sender *) new_message(sizeof(struct msg_sender));
258+
strncpy(msg2->receiver, "127.0.0.1", sizeof(msg2->receiver) - 1);
259+
msg2->type = SENDER_MSG_CHANGE_RECEIVER;
260+
resp = send_message(rtsp_params.parent, path, (struct message *) msg2);
261+
free_response(resp);
330262
}
331263
/* vi: set noexpandtab: */

src/rtsp/BasicRTSPOnlySubsession.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ private:
151151
char fCNAME[100];
152152
rtsp_types_t avType;
153153
int rtpPort;
154+
const enum module_class *sender_msg_path;
154155
struct rtsp_server_parameters rtsp_params;
155156
};
156157

0 commit comments

Comments
 (0)