Skip to content

Commit 770c44a

Browse files
committed
rtsp server: support for sending H.265
1 parent b7601d2 commit 770c44a

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

src/transmit.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,74 @@ void tx_send_h264(struct tx *tx, struct video_frame *frame,
11771177
}
11781178
}
11791179

1180+
void tx_send_h265(struct tx *tx, struct video_frame *frame,
1181+
struct rtp *rtp_session) {
1182+
assert(frame->tile_count == 1);
1183+
assert(!frame->fragment);
1184+
const uint32_t ts = get_std_video_local_mediatime();
1185+
struct tile *tile = &frame->tiles[0];
1186+
1187+
const char pt = PT_DynRTP_Type96;
1188+
// assume IPv6 (we don't know a priori)
1189+
unsigned maxPacketSize = tx->mtu - get_tx_hdr_len(true);
1190+
1191+
const unsigned char *endptr = nullptr;
1192+
const unsigned char *nal = (uint8_t *) tile->data;
1193+
const unsigned char *const end = nal + tile->data_len;
1194+
1195+
while ((nal = rtpenc_get_next_nal(nal, end - nal, &endptr))) {
1196+
unsigned int nalsize = endptr - nal;
1197+
1198+
if (nalsize <= maxPacketSize) { // single NALU packet
1199+
char *payload = const_cast<char *>(
1200+
reinterpret_cast<const char *>(nal));
1201+
if (rtp_send_data_hdr(rtp_session, ts, pt, 1 /* m */, 0,
1202+
nullptr, (char *) nullptr, 0,
1203+
payload, (int) nalsize, nullptr,
1204+
0, 0) < 0) {
1205+
error_msg("There was a problem sending the RTP "
1206+
"packet\n");
1207+
}
1208+
} else { // fragment
1209+
uint8_t hdr[3];
1210+
hdr[0] = (nal[0] & 0x81) | NAL_RTP_HEVC_FU << 1;
1211+
hdr[1] = nal[1];
1212+
hdr[2] =
1213+
1 << 7 | (nal[0] >> 1 & 0x3F); // s=1,e=1+FU type
1214+
nal += 2;
1215+
nalsize -= 2;
1216+
while (nalsize > 0) {
1217+
unsigned size = nalsize;
1218+
int m = 0;
1219+
if (nalsize <= maxPacketSize) { // last packet
1220+
m = 1;
1221+
hdr[2] &= 0x40; // set end bit
1222+
} else {
1223+
size = maxPacketSize;
1224+
}
1225+
char *payload = const_cast<char *>(
1226+
reinterpret_cast<const char *>(nal));
1227+
if (rtp_send_data_hdr(
1228+
rtp_session, ts, pt, m, 0, nullptr,
1229+
(char *) hdr, sizeof hdr,
1230+
(char *) payload, (int) size, nullptr,
1231+
0, 0) < 0) {
1232+
error_msg("There was a problem sending "
1233+
"the RTP "
1234+
"packet\n");
1235+
}
1236+
hdr[2] &= 0x7F; // clear start bit
1237+
nal += size;
1238+
nalsize -= size;
1239+
}
1240+
}
1241+
}
1242+
1243+
if (endptr != end) {
1244+
error_msg("No NAL found!\n");
1245+
}
1246+
}
1247+
11801248
void tx_send_jpeg(struct tx *tx, struct video_frame *frame,
11811249
struct rtp *rtp_session) {
11821250
uint32_t ts = 0;

src/transmit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ void format_video_header(struct video_frame *frame, int tile_idx, in
8080
uint32_t *hdr);
8181

8282
void tx_send_h264(struct tx *tx_session, struct video_frame *frame, struct rtp *rtp_session);
83+
void tx_send_h265(struct tx *tx_session, struct video_frame *frame, struct rtp *rtp_session);
8384
void tx_send_jpeg(struct tx *tx_session, struct video_frame *frame, struct rtp *rtp_session);
8485

8586
/**

src/utils/sdp.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ get_video_rtp_pt_rtpmap(codec_t codec, char rtpmapLine[STR_LEN])
312312
rtpmap_codec = "JPEG";
313313
break;
314314
case H264:
315+
case H265:
315316
pt = PT_DynRTP_Type96;
316-
rtpmap_codec = "H264";
317+
rtpmap_codec = codec == H264 ? "H264" : "H265";
317318
break;
318319
default:
319320
return -2;

src/video_rxtx/h264_rtp.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,19 @@ void
8787
h264_rtp_video_rxtx::configure_rtsp_server_video()
8888
{
8989
assert((rtsp_params.avType & rtsp_type_video) != 0);
90-
if (rtsp_params.video_codec == H264) {
90+
switch (rtsp_params.video_codec) {
91+
case H264:
9192
tx_send_std = tx_send_h264;
92-
} else if (rtsp_params.video_codec == JPEG) {
93+
break;
94+
case H265:
95+
tx_send_std = tx_send_h265;
96+
break;
97+
case JPEG:
9398
tx_send_std = tx_send_jpeg;
94-
} else {
99+
break;
100+
default:
95101
MSG(ERROR,
96-
"codecs other than H.264 and JPEG currently not "
102+
"codecs other than H.264/H.265 and JPEG currently not "
97103
"supported, got %s\n",
98104
get_codec_name(rtsp_params.video_codec));
99105
return;

0 commit comments

Comments
 (0)