Skip to content

Commit 9b475d9

Browse files
authored
Let forward feature support select stream by its name. (#220)
when config source stream name, the forward task will select the stream by this name to forward, when source stream name is missing, it will random select one active stream to forward.
1 parent 11cce62 commit 9b475d9

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

platform/forward.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/ossrs/go-oryx-lib/errors"
2020
ohttp "github.com/ossrs/go-oryx-lib/http"
2121
"github.com/ossrs/go-oryx-lib/logger"
22+
2223
// Use v8 because we use Go 1.16+, while v9 requires Go 1.18+
2324
"github.com/go-redis/redis/v8"
2425
"github.com/google/uuid"
@@ -334,6 +335,8 @@ func (v *ForwardWorker) Start(ctx context.Context) error {
334335
type ForwardConfigure struct {
335336
// The platform name, for example, wx
336337
Platform string `json:"platform"`
338+
// the source stream in oryx.
339+
Stream string `json:"stream"`
337340
// The RTMP server url, for example, rtmp://localhost/live
338341
Server string `json:"server"`
339342
// The RTMP stream and secret, for example, livestream
@@ -347,13 +350,14 @@ type ForwardConfigure struct {
347350
}
348351

349352
func (v *ForwardConfigure) String() string {
350-
return fmt.Sprintf("platform=%v, server=%v, secret=%v, enabled=%v, customed=%v, label=%v",
351-
v.Platform, v.Server, v.Secret, v.Enabled, v.Customed, v.Label,
353+
return fmt.Sprintf("platform=%v, stream=%v, server=%v, secret=%v, enabled=%v, customed=%v, label=%v",
354+
v.Platform, v.Stream, v.Server, v.Secret, v.Enabled, v.Customed, v.Label,
352355
)
353356
}
354357

355358
func (v *ForwardConfigure) Update(u *ForwardConfigure) error {
356359
v.Platform = u.Platform
360+
v.Stream = u.Stream
357361
v.Server = u.Server
358362
v.Secret = u.Secret
359363
v.Label = u.Label
@@ -500,18 +504,28 @@ func (v *ForwardTask) Run(ctx context.Context) error {
500504
ctx = logger.WithContext(ctx)
501505
logger.Tf(ctx, "forward run task %v", v.String())
502506

507+
// select active stream by stream name or random select one when stream name is empty.
503508
selectActiveStream := func() (*SrsStream, error) {
504509
streams, err := rdb.HGetAll(ctx, SRS_STREAM_ACTIVE).Result()
505510
if err != nil {
506511
return nil, errors.Wrapf(err, "hgetall %v", SRS_STREAM_ACTIVE)
507512
}
508513

514+
streamName := v.config.Stream
515+
509516
var best *SrsStream
510517
for _, v := range streams {
511518
var stream SrsStream
512519
if err := json.Unmarshal([]byte(v), &stream); err != nil {
513520
return nil, errors.Wrapf(err, "unmarshal %v", v)
514521
}
522+
if streamName != "" {
523+
if stream.Stream == streamName {
524+
best = &stream
525+
break
526+
}
527+
continue
528+
}
515529

516530
if best == nil {
517531
best = &stream

ui/src/pages/ScenarioForward.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ function ScenarioForwardImpl({defaultActiveKey, defaultSecrets}) {
162162
}, [configs, setConfigs]);
163163

164164
// Update the forward config to server.
165-
const updateSecrets = React.useCallback((e, action, platform, server, secret, enabled, custom, label, onSuccess) => {
165+
const updateSecrets = React.useCallback((e, action, platform, stream, server, secret, enabled, custom, label, onSuccess) => {
166166
e.preventDefault();
167167
if (!server) return alert(t('plat.com.addr'));
168168
if (custom && !label) return alert(t('plat.com.label'));
@@ -171,7 +171,7 @@ function ScenarioForwardImpl({defaultActiveKey, defaultSecrets}) {
171171
setSubmiting(true);
172172

173173
axios.post('/terraform/v1/ffmpeg/forward/secret', {
174-
action, platform, server, secret, enabled: !!enabled, custom: !!custom, label,
174+
action, platform, stream, server, secret, enabled: !!enabled, custom: !!custom, label,
175175
}, {
176176
headers: Token.loadBearerHeader(),
177177
}).then(res => {
@@ -235,6 +235,11 @@ function ScenarioForwardImpl({defaultActiveKey, defaultSecrets}) {
235235
<Form.Text> * {conf.custom ? `(${t('helper.required')})` : `(${t('helper.optional')})`} {t('plat.com.name2')}</Form.Text>
236236
<Form.Control as="input" defaultValue={conf.label} onChange={(e) => updateConfigObject({...conf, label: e.target.value})}/>
237237
</Form.Group>
238+
<Form.Group className="mb-3">
239+
<Form.Label>{t('plat.com.source')}</Form.Label>
240+
{!conf.custom && <Form.Text> * {t('plat.com.source')} check System-{'>'}Streams tab</Form.Text>}
241+
<Form.Control as="input" defaultValue={conf.stream} onChange={(e) => updateConfigObject({...conf, stream: e.target.value})}/>
242+
</Form.Group>
238243
<Form.Group className="mb-3">
239244
<Form.Label>{conf.custom ? t('plat.com.server') : t('plat.com.server2')}</Form.Label>
240245
{!conf.custom && <Form.Text> * {t('plat.com.server3')} <a href={conf?.locale?.link} target='_blank' rel='noreferrer'>{conf?.locale?.link2}</a>, {t('plat.com.server4')}</Form.Text>}
@@ -259,7 +264,7 @@ function ScenarioForwardImpl({defaultActiveKey, defaultSecrets}) {
259264
type="submit"
260265
disabled={submiting}
261266
onClick={(e) => {
262-
updateSecrets(e, 'update', conf.platform, conf.server, conf.secret, !conf.enabled, conf.custom, conf.label, () => {
267+
updateSecrets(e, 'update', conf.platform, conf.stream, conf.server, conf.secret, !conf.enabled, conf.custom, conf.label, () => {
263268
updateConfigObject({...conf, enabled: !conf.enabled});
264269
});
265270
}}

0 commit comments

Comments
 (0)