Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support autoRetry for dynamic push sessions #551

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ relay: {
## Dynamic push
When the local server receives a publish request.
Automatically push the stream to the edge server.
Use autoRetry flag to prevent relay end from edge disconnection when publisher is still alive.

```
relay: {
Expand All @@ -662,6 +663,7 @@ relay: {
app: 'live',
mode: 'push',
edge: 'rtmp://192.168.0.10',
autoRetry: true
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ relay: {

### 动态推流
当本地服务器收到一个发布请求,自动将这个流推送到边缘服务器。
设置 autoRetry 为 true ,当与边缘服务器断连时,如果请求发布者依然活跃,动态推流会自动重试。

```
relay: {
Expand All @@ -556,6 +557,7 @@ relay: {
app: 'live',
mode: 'push',
edge: 'rtmp://192.168.0.10',
autoRetry: true
}
]
}
Expand Down
16 changes: 16 additions & 0 deletions src/node_relay_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ const fs = require('fs');
const querystring = require('querystring');
const _ = require('lodash');

const DYNAMIC_PUSH_AUTO_RETRY_TIME_MS = 5000;

class NodeRelayServer {
constructor(config) {
this.config = config;
this.staticCycle = null;
this.staticSessions = new Map();
this.dynamicSessions = new Map();
this.dynamicPushRetryHandlers = new Map();;
}

async run() {
Expand Down Expand Up @@ -206,7 +209,20 @@ class NodeRelayServer {
let session = new NodeRelaySession(conf);
session.id = id;
session.on('end', (id) => {
const handler = this.dynamicPushRetryHandlers.get(id);
if (handler) clearTimeout(handler);
this.dynamicSessions.delete(id);
this.dynamicPushRetryHandlers.delete(id);
});
session.on('retry', (id) => {
Logger.log('[relay dynamic push] [retry] start after' + DYNAMIC_PUSH_AUTO_RETRY_TIME_MS + 'id=' + id, inPath, 'to', ouPath);
const handler = setTimeout(
() => {
session.run();
Logger.log('[relay dynamic push] [retry] start, id=' + id, inPath, 'to', conf.ouPath);
},
DYNAMIC_PUSH_AUTO_RETRY_TIME_MS);
this.dynamicPushRetryHandlers.set(id, handler);
});
this.dynamicSessions.set(id, session);
session.run();
Expand Down
11 changes: 10 additions & 1 deletion src/node_relay_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//
const Logger = require('./node_core_logger');
const NodeCoreUtils = require('./node_core_utils');
const context = require('./node_core_ctx');

const EventEmitter = require('events');
const { spawn } = require('child_process');
Expand Down Expand Up @@ -52,7 +53,15 @@ class NodeRelaySession extends EventEmitter {

this.ffmpeg_exec.on('close', (code) => {
Logger.log('[relay end] id=' + this.id, 'code=' + code);
this.emit('end', this.id);
const publisherAlive = context.sessions.get(this.id)?.isPublishing;
if (this.conf.autoRetry) {
Logger.log('[relay end - detect autoRetry] publisher if alive: ' + !!publisherAlive);
if (publisherAlive && this.conf.mode === 'push') {
this.emit('retry', this.id, this.conf.inPath, this.conf.ouPath);
}
} else {
this.emit('end', this.id);
}
});
}

Expand Down