Skip to content

Commit

Permalink
ST: Detect the mmsghdr automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Feb 26, 2021
1 parent 9852ef4 commit b65f202
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 56 deletions.
33 changes: 8 additions & 25 deletions trunk/3rdparty/st-srs/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -833,9 +833,13 @@ int st_sendmsg(_st_netfd_t *fd, const struct msghdr *msg, int flags, st_utime_t
return n;
}

int st_sendmmsg(st_netfd_t fd, struct st_mmsghdr *msgvec, unsigned int vlen, int flags, st_utime_t timeout)
// The sendmmsg() system call was added in Linux 3.0. Support in glibc was added in version 2.14.
// @see http://man7.org/linux/man-pages/man2/sendmmsg.2.html
#if defined(__linux__) && __GLIBC__>=2 && __GLIBC_MINOR__>=14
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
int st_sendmmsg(st_netfd_t fd, struct mmsghdr *msgvec, unsigned int vlen, int flags, st_utime_t timeout)
{
#if defined(MD_HAVE_SENDMMSG) && defined(_GNU_SOURCE)
int n;
int left;
struct mmsghdr *p;
Expand Down Expand Up @@ -871,30 +875,9 @@ int st_sendmmsg(st_netfd_t fd, struct st_mmsghdr *msgvec, unsigned int vlen, int
return n;
}
return (int)vlen - left;
#else
struct st_mmsghdr *p;
int i, n;

// @see http://man7.org/linux/man-pages/man2/sendmmsg.2.html
for (i = 0; i < (int)vlen; ++i) {
p = msgvec + i;
n = st_sendmsg(fd, &p->msg_hdr, flags, timeout);
if (n < 0) {
// An error is returned only if no datagrams could be sent.
if (i == 0) {
return n;
}
return i + 1;
}

p->msg_len = n;
}

// Returns the number of messages sent from msgvec; if this is less than vlen, the caller can retry with a
// further sendmmsg() call to send the remaining messages.
return vlen;
#endif
}
#endif
#endif


/*
Expand Down
13 changes: 8 additions & 5 deletions trunk/3rdparty/st-srs/public.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ extern int st_sendto(st_netfd_t fd, const void *msg, int len, const struct socka
extern int st_recvmsg(st_netfd_t fd, struct msghdr *msg, int flags, st_utime_t timeout);
extern int st_sendmsg(st_netfd_t fd, const struct msghdr *msg, int flags, st_utime_t timeout);

// The sendmmsg() system call was added in Linux 3.0. Support in glibc was added in version 2.14.
// @see http://man7.org/linux/man-pages/man2/sendmmsg.2.html
#if defined(__linux__) && __GLIBC__>=2 && __GLIBC_MINOR__>=14
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
#define _GNU_SOURCE
#include <sys/socket.h>
struct st_mmsghdr {
struct msghdr msg_hdr; /* Message header */
unsigned int msg_len; /* Number of bytes transmitted */
};
extern int st_sendmmsg(st_netfd_t fd, struct st_mmsghdr *msgvec, unsigned int vlen, int flags, st_utime_t timeout);
extern int st_sendmmsg(st_netfd_t fd, struct mmsghdr *msgvec, unsigned int vlen, int flags, st_utime_t timeout);
#endif
#endif

extern st_netfd_t st_open(const char *path, int oflags, mode_t mode);

Expand Down
8 changes: 0 additions & 8 deletions trunk/auto/depends.sh
Original file line number Diff line number Diff line change
Expand Up @@ -348,14 +348,6 @@ fi
if [[ $SRS_OSX == YES ]]; then
_ST_MAKE=darwin-debug && _ST_EXTRA_CFLAGS="-DMD_HAVE_KQUEUE" && _ST_LD=${SRS_TOOL_CC} && _ST_OBJ="DARWIN_`uname -r`_DBG"
fi
# For UDP sendmmsg, disable it if not suppported.
if [[ $SRS_SENDMMSG == YES ]]; then
echo "Build ST with UDP sendmmsg support."
_ST_EXTRA_CFLAGS="$_ST_EXTRA_CFLAGS -DMD_HAVE_SENDMMSG -D_GNU_SOURCE"
else
echo "Build ST without UDP sendmmsg support."
_ST_EXTRA_CFLAGS="$_ST_EXTRA_CFLAGS -UMD_HAVE_SENDMMSG -U_GNU_SOURCE"
fi
# Whether enable debug stats.
if [[ $SRS_DEBUG_STATS == YES ]]; then
_ST_EXTRA_CFLAGS="$_ST_EXTRA_CFLAGS -DDEBUG_STATS"
Expand Down
10 changes: 0 additions & 10 deletions trunk/auto/options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ SRS_EXTRA_FLAGS=
# Performance optimize.
SRS_NASM=YES
SRS_SRTP_ASM=YES
SRS_SENDMMSG=NO
SRS_DEBUG=NO
SRS_DEBUG_STATS=NO

Expand Down Expand Up @@ -177,7 +176,6 @@ Performance: @see https://blog.csdn.net/win_lin/article/details/5
--nasm=on|off Whether build FFMPEG for RTC with nasm. Default: $(value2switch $SRS_NASM)
--srtp-nasm=on|off Whether build SRTP with ASM(openssl-asm), requires RTC and openssl-1.0.*. Default: $(value2switch $SRS_SRTP_ASM)
--sendmmsg=on|off Whether enable UDP sendmmsg. Default: $(value2switch $SRS_SENDMMSG). @see http://man7.org/linux/man-pages/man2/sendmmsg.2.html
Toolchain options: @see https://github.com/ossrs/srs/issues/1547#issuecomment-576078411
--static=on|off Whether add '-static' to link options. Default: $(value2switch $SRS_STATIC)
Expand Down Expand Up @@ -230,8 +228,6 @@ function parse_user_option() {
--x86-64) SRS_X86_X64=YES ;;
--osx) SRS_OSX=YES ;;

--sendmmsg) if [[ $value == off ]]; then SRS_SENDMMSG=NO; else SRS_SENDMMSG=YES; fi ;;

--without-srtp-nasm) SRS_SRTP_ASM=NO ;;
--with-srtp-nasm) SRS_SRTP_ASM=YES ;;
--srtp-nasm) if [[ $value == off ]]; then SRS_SRTP_ASM=NO; else SRS_SRTP_ASM=YES; fi ;;
Expand Down Expand Up @@ -523,11 +519,6 @@ function apply_detail_options() {
echo "Use openssl-1.0 for SRTP-ASM."
SRS_SSL_1_0=YES
fi

if [[ $SRS_OSX == YES && $SRS_SENDMMSG == YES ]]; then
echo "Disable sendmmsg for OSX"
SRS_SENDMMSG=NO
fi
}
apply_detail_options

Expand Down Expand Up @@ -562,7 +553,6 @@ function regenerate_options() {
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --ffmpeg-fit=$(value2switch $SRS_FFMPEG_FIT)"
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --nasm=$(value2switch $SRS_NASM)"
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --srtp-nasm=$(value2switch $SRS_SRTP_ASM)"
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --sendmmsg=$(value2switch $SRS_SENDMMSG)"
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --clean=$(value2switch $SRS_CLEAN)"
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --gperf=$(value2switch $SRS_GPERF)"
SRS_AUTO_CONFIGURE="${SRS_AUTO_CONFIGURE} --gmc=$(value2switch $SRS_GPERF_MC)"
Expand Down
11 changes: 9 additions & 2 deletions trunk/src/protocol/srs_service_st.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,10 +446,17 @@ int srs_sendmsg(srs_netfd_t stfd, const struct msghdr *msg, int flags, srs_utime
return st_sendmsg((st_netfd_t)stfd, msg, flags, (st_utime_t)timeout);
}

int srs_sendmmsg(srs_netfd_t stfd, struct srs_mmsghdr *msgvec, unsigned int vlen, int flags, srs_utime_t timeout)
// The sendmmsg() system call was added in Linux 3.0. Support in glibc was added in version 2.14.
// @see http://man7.org/linux/man-pages/man2/sendmmsg.2.html
#if defined(__linux__) && __GLIBC__>=2 && __GLIBC_MINOR__>=14
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
int srs_sendmmsg(srs_netfd_t stfd, struct mmsghdr *msgvec, unsigned int vlen, int flags, srs_utime_t timeout)
{
return st_sendmmsg((st_netfd_t)stfd, (struct st_mmsghdr*)msgvec, vlen, flags, (st_utime_t)timeout);
return st_sendmmsg((st_netfd_t)stfd, msgvec, vlen, flags, (st_utime_t)timeout);
}
#endif
#endif

srs_netfd_t srs_accept(srs_netfd_t stfd, struct sockaddr *addr, int *addrlen, srs_utime_t timeout)
{
Expand Down
13 changes: 7 additions & 6 deletions trunk/src/protocol/srs_service_st.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ extern int srs_sendto(srs_netfd_t stfd, void *buf, int len, const struct sockadd
extern int srs_recvmsg(srs_netfd_t stfd, struct msghdr *msg, int flags, srs_utime_t timeout);
extern int srs_sendmsg(srs_netfd_t stfd, const struct msghdr *msg, int flags, srs_utime_t timeout);

// The sendmmsg() system call was added in Linux 3.0. Support in glibc was added in version 2.14.
// @see http://man7.org/linux/man-pages/man2/sendmmsg.2.html
#include <sys/socket.h>
struct srs_mmsghdr {
struct msghdr msg_hdr; /* Message header */
unsigned int msg_len; /* Number of bytes transmitted */
};
extern int srs_sendmmsg(srs_netfd_t stfd, struct srs_mmsghdr *msgvec, unsigned int vlen, int flags, srs_utime_t timeout);
#if defined(__linux__) && __GLIBC__>=2 && __GLIBC_MINOR__>=14
#include <linux/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,0,0)
extern int srs_sendmmsg(srs_netfd_t stfd, struct mmsghdr *msgvec, unsigned int vlen, int flags, srs_utime_t timeout);
#endif
#endif

extern srs_netfd_t srs_accept(srs_netfd_t stfd, struct sockaddr *addr, int *addrlen, srs_utime_t timeout);

Expand Down

1 comment on commit b65f202

@winlinvip
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refine sendmmsg for ossrs/state-threads#12

Please sign in to comment.