Skip to content

Commit

Permalink
STAR-1302: enable sending Stream ID
Browse files Browse the repository at this point in the history
  • Loading branch information
Magnus Gustafsson committed May 29, 2024
1 parent 098e373 commit 48d7116
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
20 changes: 17 additions & 3 deletions SRTNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@ bool SRTNet::startClient(const std::string& host,
int mtu,
bool failOnConnectionError,
int32_t peerIdleTimeout,
const std::string& psk) {
return startClient(host, port, "", 0, reorder, latency, overhead, ctx, mtu, failOnConnectionError, peerIdleTimeout, psk);
const std::string& psk,
const std::string& streamId) {
return startClient(host, port, "", 0, reorder, latency, overhead, ctx, mtu, failOnConnectionError, peerIdleTimeout, psk, streamId);
}

// Host can provide an IP or name meaning any IPv4 or IPv6 address or name type www.google.com
Expand All @@ -398,7 +399,8 @@ bool SRTNet::startClient(const std::string& host,
int mtu,
bool failOnConnectionError,
int32_t peerIdleTimeout,
const std::string& psk) {
const std::string& psk,
const std::string& streamId) {
std::lock_guard<std::mutex> lock(mNetMtx);
if (mCurrentMode != Mode::unknown) {
SRT_LOGGER(true, LOGG_ERROR,
Expand All @@ -419,6 +421,7 @@ bool SRTNet::startClient(const std::string& host,
mConfiguration.mMtu = mtu;
mConfiguration.mPeerIdleTimeout = peerIdleTimeout;
mConfiguration.mPsk = psk;
mConfiguration.mStreamId = streamId;

if (!createClientSocket()) {
SRT_LOGGER(true, LOGG_ERROR, "Failed to create caller socket");
Expand Down Expand Up @@ -621,6 +624,17 @@ bool SRTNet::createClientSocket() {
}
}

if (!mConfiguration.mStreamId.empty()) {
result = srt_setsockflag(mContext, SRTO_STREAMID,
mConfiguration.mStreamId.c_str(),
mConfiguration.mStreamId.length());
if (result == SRT_ERROR) {
SRT_LOGGER(true, LOGG_ERROR,
"srt_setsockflag SRTO_STREAMID: " << srt_getlasterror_str());
return false;
}
}

result = srt_setsockflag(mContext, SRTO_PEERIDLETIMEO, &mConfiguration.mPeerIdleTimeout, sizeof(mConfiguration.mPeerIdleTimeout));
if (result == SRT_ERROR) {
SRT_LOGGER(true, LOGG_ERROR, "srt_setsockflag : SRTO_PEERIDLETIMEO" << srt_getlasterror_str());
Expand Down
9 changes: 7 additions & 2 deletions SRTNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class SRTNet {
* @param peerIdleTimeout Optional Connection considered broken if no packet received before this timeout.
* Defaults to 5 seconds.
* @param psk Optional Pre Shared Key (AES-128)
* @param streamId Optional Stream ID
* @return true if configuration was accepted and remote IP port could be resolved, false otherwise. It will also
* return false in case the connection can be made but the provided PSK doesn't match the PSK on the server.
*/
Expand All @@ -134,7 +135,8 @@ class SRTNet {
int mtu,
bool failOnConnectionError,
int32_t peerIdleTimeout = 5000,
const std::string& psk = "");
const std::string& psk = "",
const std::string& streamId = "");

/**
*
Expand All @@ -158,6 +160,7 @@ class SRTNet {
* @param peerIdleTimeout Optional Connection considered broken if no packet received before this timeout.
* Defaults to 5 seconds.
* @param psk Optional Pre Shared Key (AES-128)
* @param streamId Optional Stream ID
* @return true if configuration was accepted and remote IP port could be resolved, false otherwise. It will also
* return false in case the connection can be made but the provided PSK doesn't match the PSK on the server.
*/
Expand All @@ -172,7 +175,8 @@ class SRTNet {
int mtu,
bool failOnConnectionError,
int32_t peerIdleTimeout = 5000,
const std::string& psk = "");
const std::string& psk = "",
const std::string& streamId = "");

/**
*
Expand Down Expand Up @@ -339,6 +343,7 @@ class SRTNet {
int mMtu;
int32_t mPeerIdleTimeout;
std::string mPsk;
std::string mStreamId;
};

/** Internal variables and methods
Expand Down
25 changes: 25 additions & 0 deletions test/TestSrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,3 +692,28 @@ TEST_F(TestSRTFixture, GetLocallyBoundPort) {
true, 5000, kValidPsk));
EXPECT_NE(mClient.getLocallyBoundPort(), 0);
}

TEST_F(TestSRTFixture, StreamId) {
SRTNet server;
SRTNet client;
const std::string sentStreamId = "An example Stream ID";
char receivedStreamId[1024];

auto ctx = std::make_shared<SRTNet::NetworkConnection>();
server.clientConnected = [&](struct sockaddr &sin, SRTSOCKET newSocket,
std::shared_ptr<SRTNet::NetworkConnection> &ctx,
const SRTNet::ConnectionInformation &) {
int size = 1024;
EXPECT_NE(srt_getsockflag(newSocket, SRTO_STREAMID, receivedStreamId, &size), SRT_ERROR);
EXPECT_EQ(size, sentStreamId.size());
return ctx;
};

ASSERT_TRUE(server.startServer("127.0.0.1", 8009, 16, 1000, 100,
SRT_LIVE_MAX_PLSIZE, 5000, kValidPsk, false,
ctx));
ASSERT_TRUE(client.startClient("127.0.0.1", 8009, 16, 1000, 100, ctx,
SRT_LIVE_MAX_PLSIZE, true, 5000, kValidPsk,
sentStreamId));
ASSERT_EQ(sentStreamId, std::string(receivedStreamId));
}

0 comments on commit 48d7116

Please sign in to comment.