Skip to content

Commit

Permalink
Merge branch 'master' into hz_srt_restream_js
Browse files Browse the repository at this point in the history
  • Loading branch information
harryz2000 committed Oct 23, 2024
2 parents 6707e2f + c6413bb commit 1a4c38f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 52 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medooze-rtmp-server",
"version": "1.29.0",
"version": "1.30.0",
"description": "RTMP Server by Medooze",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -35,7 +35,7 @@
"uuid": "^3.3.2"
},
"peerDependencies": {
"medooze-media-server-src": "^3.0.0"
"medooze-media-server-src": "^3.1.2"
},
"devDependencies": {
"@types/node": "^20.8.6",
Expand Down
79 changes: 51 additions & 28 deletions tests/client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const tap = require("tap");
const RTMPServer = require("../index.js");

RTMPServer.enableWarning(false);
RTMPServer.enableLog(false);
RTMPServer.enableDebug(false);
RTMPServer.enableUltraDebug(false);
Expand All @@ -17,23 +18,37 @@ function sleep(ms)
return new Promise(resolve => setTimeout(resolve, ms));
}

function promise()
{
let resolve;
let reject;
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
promise.resolve = resolve;
promise.reject = reject;
return promise;
}


process.on("uncaughtException", onExit);
process.on("SIGINT", onExit);
process.on("SIGTERM", onExit);
process.on("SIGQUIT", onExit);

tap.test("Server", async function (suite)
{

await suite.test("publish+unpublish", async function (test)
{
test.plan(7);
test.plan(8);

let incomingStream,outgoingStream;
//Create server and app
const app = RTMPServer.createApplication();
const rtmp = RTMPServer.createServer();

const serverConnected = promise();
app.on("connect", (client) =>
{
//Add publish listener
Expand All @@ -53,6 +68,7 @@ tap.test("Server", async function (suite)
test.ok(incomingStream, "Got incoming stream");
//Started
stream.sendStatus(transId, RTMPServer.NetStream.Publish.Start);
serverConnected.resolve();
}
})
});
Expand All @@ -67,6 +83,7 @@ tap.test("Server", async function (suite)
//Create client connection
const connection = RTMPServer.createClientConnection();

const connected = promise();
connection.on("connected",async ()=>{

test.pass("client connected");
Expand All @@ -77,32 +94,35 @@ tap.test("Server", async function (suite)

outgoingStream.on("cmd", (stream, name, cmd)=>{
//Got publishing command
test.same(cmd[1].code, RTMPServer.NetStream.Publish.Start.code)
test.same(cmd[1].code, RTMPServer.NetStream.Publish.Start.code, "Client got publishing cmd")
//Attach streams
outgoingStream.attachTo(incomingStream);
connected.resolve();
});
});


connection.on("disconnected",(conn, errorCode)=>{
test.fail("Expected stopped not disconnected event");
});

const stopped = promise();
connection.on("stopped",(conn)=>{
test.pass("client connection stopped");
stopped.resolve();
});


//Connect
connection.connect("127.0.0.1", 1936, "test");

//Wait 1 seconds
await sleep(1000);
await connected;
await serverConnected;

//Check we have stats
test.ok(connection.getStats());

connection.on("disconnected",(conn, errorCode)=>{
test.equal(errorCode, RTMPServer.NetConnectionErrorCode.NoError);
});
test.ok(connection.getStats(), "Client connection got stats");

//Stop
connection.stop();

//Wait 1 seconds
await sleep(1000);
await stopped;

//Stop server
rtmp.stop();
Expand All @@ -116,16 +136,17 @@ tap.test("Server", async function (suite)
//Create client connection
const connection = RTMPServer.createClientConnection();

const disconnected = promise();
connection.on("disconnected", (conn, errorCode)=>{
test.equal(errorCode, RTMPServer.NetConnectionErrorCode.GetSockOptError);
test.equal(errorCode, RTMPServer.NetConnectionErrorCode.PollError);
disconnected.resolve();
});

//Connect. Note the connect wouldn't fail immediately.
let errorCode = connection.connect("127.0.0.1", 1937, "test");
let errorCode = connection.connect("127.0.0.1", 193734, "test");
test.equal(errorCode, RTMPServer.NetConnectionErrorCode.NoError);

//Wait 1 seconds
await sleep(1000);
await disconnected;

test.end();
});
Expand All @@ -144,7 +165,7 @@ tap.test("Server", async function (suite)

await suite.test("peerclosed", async function (test)
{
test.plan(2);
test.plan(3);

//Create server and app
const app = RTMPServer.createApplication();
Expand All @@ -153,6 +174,7 @@ tap.test("Server", async function (suite)
app.on("connect", (client) =>
{
// Close the connection
test.pass("Server rejecting client");
client.reject();
});

Expand All @@ -163,23 +185,24 @@ tap.test("Server", async function (suite)
//Create client connection
const connection = RTMPServer.createClientConnection();

//Connect
let errorCode = connection.connect("127.0.0.1", 1936, "test");
test.equal(errorCode, RTMPServer.NetConnectionErrorCode.NoError);

const disconnected = promise();
connection.on("disconnected", (conn, errorCode)=>{
test.equal(errorCode, RTMPServer.NetConnectionErrorCode.PeerClosed);
test.equal(errorCode, RTMPServer.NetConnectionErrorCode.PeerClosed, "Client expects peer closed when server rejected");
disconnected.resolve();
});

//Wait 1 seconds
await sleep(1000);
//Connect
let errorCode = connection.connect("127.0.0.1", 1936, "test");
test.equal(errorCode, RTMPServer.NetConnectionErrorCode.NoError, "Expect initial TCP connection to be successful");

await disconnected;

//Stop server
rtmp.stop();

test.end();
});

suite.end();

}).then(() =>
Expand Down
54 changes: 32 additions & 22 deletions tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function ffmpeg(args)
}


RTMPServer.enableWarning(false);
RTMPServer.enableLog(false);
RTMPServer.enableDebug(false);
RTMPServer.enableUltraDebug(false);
Expand All @@ -44,6 +45,19 @@ function sleep(ms)
return new Promise(resolve => setTimeout(resolve, ms));
}

function promise()
{
let resolve;
let reject;
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
promise.resolve = resolve;
promise.reject = reject;
return promise;
}

function ffmpeg(args)
{
let child;
Expand Down Expand Up @@ -86,7 +100,7 @@ function args(stream, token)
tap.test("Server", async function (suite)
{

await suite.test("publish+unpublish", async function (test)
await suite.test("start+stop", async function (test)
{
//Create server and app
const app = RTMPServer.createApplication();
Expand All @@ -110,6 +124,7 @@ tap.test("Server", async function (suite)
const app = RTMPServer.createApplication();
const rtmp = RTMPServer.createServer();

let connected = promise();
app.on("connect", (client) =>
{
//Add publish listener
Expand All @@ -119,6 +134,7 @@ tap.test("Server", async function (suite)
const incomingStream = stream.createIncomingStreamBridge();
//Send to server
test.ok(incomingStream);
connected.resolve();
});
//Accept client connection by default
client.accept();
Expand All @@ -130,11 +146,8 @@ tap.test("Server", async function (suite)

//Start publishing rtmp
const pub = ffmpeg(args("nane","token"));

test.ok(pub);

//Wait 5 seconds
await sleep(1000);
await connected;

//Stop
await pub.stop();
Expand All @@ -159,7 +172,7 @@ tap.test("Server", async function (suite)
//Reject
client.reject();
//Worked
test.pass("RTMPconnection rejected sucessfully")
test.pass("RTMPconnection rejected sucessfully");
});

//Start rtmp server
Expand All @@ -173,10 +186,7 @@ tap.test("Server", async function (suite)
test.ok(pub);

//Should be rejected
pub.catch((e)=>test.pass("ffmpeg disconnected"));

//Wait 5 seconds
await sleep(1000);
await pub.catch((e)=>test.pass("ffmpeg disconnected"));

//Stop server
rtmp.stop();
Expand All @@ -193,6 +203,7 @@ tap.test("Server", async function (suite)
const app = RTMPServer.createApplication();
const rtmp = RTMPServer.createServer();

let connected = promise();
app.on("connect", (client) =>
{
//Add publish listener
Expand All @@ -202,6 +213,7 @@ tap.test("Server", async function (suite)
const incomingStream = stream.createIncomingStreamBridge(100,200);
//Send to server
test.ok(incomingStream);
connected.resolve();
});
//Accept client connection by default
client.accept();
Expand All @@ -215,9 +227,7 @@ tap.test("Server", async function (suite)
const pub = ffmpeg(args("nane","token"));

test.ok(pub);

//Wait 5 seconds
await sleep(1000);
await connected;

//Stop
await pub.stop();
Expand All @@ -237,6 +247,7 @@ tap.test("Server", async function (suite)
const app = RTMPServer.createApplication();
const rtmp = RTMPServer.createServer();

let connected = promise();
app.on("connect", (client) =>
{
//Add publish listener
Expand All @@ -250,6 +261,7 @@ tap.test("Server", async function (suite)
incomingStreamTrack.setTargetBitrateHint(1000);
//No error
test.pass();
connected.resolve();
});
//Accept client connection by default
client.accept();
Expand All @@ -263,9 +275,7 @@ tap.test("Server", async function (suite)
const pub = ffmpeg(args("nane","token"));

test.ok(pub);

//Wait 5 seconds
await sleep(1000);
await connected;

//Stop
await pub.stop();
Expand All @@ -285,6 +295,7 @@ tap.test("Server", async function (suite)
const app = RTMPServer.createApplication();
const rtmp = RTMPServer.createServer();

let connected = promise();
app.on("connect", (client) =>
{
//Add publish listener
Expand All @@ -301,6 +312,7 @@ tap.test("Server", async function (suite)
//Rtt should be the same
test.same(rtt,stats.audio[""].rtt,"rtt is the same for audio");
test.same(rtt,stats.video[""].rtt,"rtt is the same dor video");
connected.resolve();
});
//Accept client connection by default
client.accept();
Expand All @@ -314,9 +326,7 @@ tap.test("Server", async function (suite)
const pub = ffmpeg(args("nane","token"));

test.ok(pub);

//Wait 5 seconds
await sleep(1000);
await connected;

//Stop
await pub.stop();
Expand All @@ -336,6 +346,7 @@ tap.test("Server", async function (suite)
const app = RTMPServer.createApplication();
const rtmp = RTMPServer.createServer();

let connected = promise();
app.on("connect", (client) =>
{
//Add publish listener
Expand All @@ -352,6 +363,7 @@ tap.test("Server", async function (suite)
//Rtt should be the same
test.same(rtt,stats.audio[""].rtt,"rtt is the same for audio");
test.same(rtt,stats.video[""].rtt,"rtt is the same dor video");
connected.resolve();
});
//Accept client connection by default
client.accept();
Expand All @@ -365,9 +377,7 @@ tap.test("Server", async function (suite)
const pub = ffmpeg(args("nane","token"));

test.ok(pub);

//Wait 5 seconds
await sleep(1000);
await connected;

//Stop
await pub.stop();
Expand Down

0 comments on commit 1a4c38f

Please sign in to comment.