利用srs进行视频流转发

框图如下

 

 docker-compose.yaml如下

 rtmp2rtc.conf的配置如下

 就增加了

#配置如下
forward {
         enabled on;#开启转发
         backend http://192.168.0.131:6789/api/v1/forward; #有视频流数据后会调用这个接口
     }
#回调的参数如下
Received payload: 
{
    action=on_forward, 
    server_id=vid-k29ms28, 
    service_id=f00k5l8v, 
    client_id=l3ymi987, 
    ip=172.18.0.1, 
    vhost=__defaultVhost__, 
    app=live/livestream, 
    tcUrl=rtmp://192.168.0.131/live/livestream, 
    stream=4TADL21001004X-165-0-7, 
    param=
}

预期返回srs的返回值是

{
    "data": {
        "urls": [
            "rtmp://xx.xx.xx.xx:xxxx/xx/xx"
        ]
    },
    "code": 0
}

java代码如下




import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.http.ResponseEntity;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/forward")
public class SrsForwardController {

    @PostMapping
    public ResponseEntity<Map<String, Object>> handleForward(@RequestBody Map<String, Object> payload) {
        // 打印接收到的回调数据
        System.out.println("Received payload: " + payload);

        // 提取stream参数并生成SN码
        String stream = (String) payload.get("stream");
//        String snCode = stream.split("-")[0];
        // 将stream中的第一个'-'替换为'_'
        String snCode = stream.replaceFirst("-", "_");
        // 构建目标RTMP地址
        String destinationUrl = "rtmp://xx.xx.xx.xx:xxxx/xxx/xx";
        System.out.println("destinationUrl="+destinationUrl);
        // 返回响应给SRS,指定目标转发地址
        Map<String, Object> response = Map.of(
                "code", 0,
                "data", Map.of("urls", new String[] { destinationUrl })
        );
        System.out.println(ResponseEntity.ok(response));
        return ResponseEntity.ok(response);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y_Hungry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值