1.安装环境(很麻烦,可以选择编译启动)
官方文档快速开始docker配置:
docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 -d\
registry.cn-hangzhou.aliyuncs.com/ossrs/srs:4 ./objs/srs -c conf/docker.conf
因为我们需要修改配置文件,故改造使用挂载方式:
如果你不喜欢挂载,这种让人无语的麻烦操作,可以在容器内安装vim等工具直接修改
-
先创建一个容器
docker run -p 1935:1935 -p 1985:1985 -p 8080:8080 -d \ registry.cn-hangzhou.aliyuncs.com/ossrs/srs:4 ./objs/srs -c conf/docker.conf
-
进入docker内部,查看配置文件位置
docker exec -it 容器名 bash
-
使用docker cp命令将conf复制到外部宿主机上
docker cp 容器名:/usr/local/srs/conf /soft/srs/
-
挂载外部conf目录,运行
docker run -p 1935:1935 -p 1985:1985 -p 8080:8080 -d \ -v /soft/srs/conf:/usr/local/srs/conf \ registry.cn-hangzhou.aliyuncs.com/ossrs/srs:4
2.编写回调接口
使用POST,在官方文档上有callback的请求的详细参数
新建一个springboot工程,创建一个post接口,端口设置为8085(自行设置)
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody
public class CallBackController {
@PostMapping("/authenticat")
public int authenticat(){
int temp=(int) (Math.random() * 2);
System.out.println("有新的连接"+temp);
return temp;
}
}
将项目打包成jar,直接部署在测试服务器上
java -jar xxx.jar
3.配置SRS
注意,在上面我们运行时,使用的是docker.conf,所以我们需要在此文件中进行配置的修改
Q:不知道是不是作者的疏忽还是什么,如果你进入conf目录,你会发现conf下除了许多许多的配置文件外,还包含着一个conf文件夹,该文件夹下包含着同样的配置文件,我们在启动中使用的外层的conf文件。
打开docker.conf,修改配置
注意:本人测试使用localhost或者127.0.0.1无法访问接口,使用公网ip即可进行接口调用(还需要在防火墙打开对应端口,本人测试如果不打开端口仍无法调用)如果该问题有较好解答请赐教
#上面省略
vhost __defaultVhost__ {
#上面省略
http_hooks {
enabled on;
#推流时,需要回调该接口
on_publish http://公网ip:8085/authenticat;
#on_unpublish http://127.0.0.1:8085/api/v1/streams http://localhost:8085/api/v1/streams;
#on_play http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions;
#on_stop http://127.0.0.1:8085/api/v1/sessions http://localhost:8085/api/v1/sessions;
#on_dvr http://127.0.0.1:8085/api/v1/dvrs http://localhost:8085/api/v1/dvrs;
#on_hls http://127.0.0.1:8085/api/v1/hls http://localhost:8085/api/v1/hls;
#on_hls_notify http://127.0.0.1:8085/api/v1/hls/[app]/[stream]/[ts_url][param];
}
}
4.测试
-
重启SRS
docker restart xxx
-
打开网页控制台:ip:8080
-
连接SRS,端口号:1985
-
使用OBS推流
-
查看springboot控制台和obs推流情况
OBS在Callback验证不通过时会显示端口连接
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.12.RELEASE) 2022-12-21 21:28:53.160 INFO 6850 --- [ main] com.zko0.SrsCallBackDemoApplication : Starting SrsCallBackDemoApplication v0.0.1-SNAPSHOT on VM-24-4-centos with PID 6850 (/SrsCallBack/SrsCallBackDemo-0.0.1-SNAPSHOT.jar started by root in /SrsCallBack) 2022-12-21 21:28:53.164 INFO 6850 --- [ main] com.zko0.SrsCallBackDemoApplication : No active profile set, falling back to default profiles: default 2022-12-21 21:28:54.836 INFO 6850 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8085 (http) 2022-12-21 21:28:54.853 INFO 6850 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-12-21 21:28:54.853 INFO 6850 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.46] 2022-12-21 21:28:54.945 INFO 6850 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-12-21 21:28:54.945 INFO 6850 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1615 ms 2022-12-21 21:28:55.216 INFO 6850 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2022-12-21 21:28:55.507 INFO 6850 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8085 (http) with context path '' 2022-12-21 21:28:55.524 INFO 6850 --- [ main] com.zko0.SrsCallBackDemoApplication : Started SrsCallBackDemoApplication in 3.035 seconds (JVM running for 3.642) 2022-12-21 21:29:05.543 INFO 6850 --- [nio-8085-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2022-12-21 21:29:05.543 INFO 6850 --- [nio-8085-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2022-12-21 21:29:05.551 INFO 6850 --- [nio-8085-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms 有新的连接0 有新的连接0 有新的连接0 有新的连接0 有新的连接1