package com.ruoyi.media.controller; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import com.ruoyi.common.annotation.Anonymous; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.page.TableDataInfo; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.uuid.IdUtils; import com.ruoyi.media.domain.StreamInfo; import com.ruoyi.media.domain.Vtdu; import com.ruoyi.media.service.IMediaService; import com.ruoyi.media.service.IVtduService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; import java.util.stream.Collectors; /** * @Description: * @ClassName: controller * @Author: 刘苏义 * @Date: 2023年07月13日9:26 * @Version: 1.0 **/ @RestController @Api(tags = "流媒体接口") @RequestMapping("/media/stream") @Anonymous public class MediaController extends BaseController { @Resource private IMediaService mediaService; @Resource private IVtduService vtduService; /** * 增加通道 */ @PostMapping() @ApiOperation("增加通道") @PreAuthorize("@ss.hasPermi('media:stream:add')") @ApiOperationSupport(includeParameters = {"streamInfo.name", "streamInfo.rtspSource", "streamInfo.mode", "streamInfo.isCode"}, order = 1) public AjaxResult addPath(@RequestBody StreamInfo streamInfo) { if (StringUtils.isEmpty(streamInfo.getName())) { return AjaxResult.error("通道名称不能为空"); } if (StringUtils.isEmpty(streamInfo.getRtspSource())) { return AjaxResult.error("rtsp地址不能为空"); } String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource(), streamInfo.getMode(), streamInfo.getIsCode()); Vtdu vtdu = new Vtdu(); vtdu.setName(streamInfo.getName()); vtdu.setRtspUrl(streamInfo.getRtspSource()); vtdu.setIsCode(streamInfo.getIsCode()); vtdu.setCodeType(streamInfo.getMode()); vtduService.insertVtdu(vtdu); return AjaxResult.success(rtsp); } /** * 修改通道 */ @ApiOperation("修改通道") @ApiOperationSupport(includeParameters = {"streamInfo.name", "streamInfo.rtspSource", "streamInfo.mode", "streamInfo.isCode"}, order = 2) @PreAuthorize("@ss.hasPermi('media:stream:edit')") @PutMapping public AjaxResult edit(@RequestBody StreamInfo streamInfo) { mediaService.removePath(new String[]{streamInfo.getName()}); vtduService.deleteVtduByName(streamInfo.getName()); String rtsp = mediaService.addPath(streamInfo.getName(), streamInfo.getRtspSource(), streamInfo.getMode(), streamInfo.getIsCode()); Vtdu vtdu = new Vtdu(); vtdu.setName(streamInfo.getName()); vtdu.setRtspUrl(streamInfo.getRtspSource()); vtdu.setIsCode(streamInfo.getIsCode()); vtdu.setCodeType(streamInfo.getMode()); vtduService.insertVtdu(vtdu); return AjaxResult.success(rtsp); } /** * 移除通道 */ @DeleteMapping("/path/{names}") @PreAuthorize("@ss.hasPermi('media:stream:remove')") @ApiOperation("移除通道") @ApiOperationSupport(order = 3) public AjaxResult removePath(@PathVariable String[] names) { mediaService.removePath(names); vtduService.deleteVtduByNames(names); return AjaxResult.success(); } /** * 移除拉流 */ @ApiOperation("移除拉流") @ApiOperationSupport(order =4 ) @PreAuthorize("@ss.hasPermi('media:stream:remove')") @DeleteMapping("/{id}") public AjaxResult removePullStreamSession(@PathVariable String sessionId) { List pullStreamList = mediaService.getPullStreamList(); StreamInfo streamInfo = pullStreamList.stream() .filter(object -> object.getId().equals(sessionId)) .collect(Collectors.toList()).get(0); switch (streamInfo.getSessionType()) { case "rtsp": return AjaxResult.success(mediaService.kickRtspSession(sessionId)); case "webrtc": return AjaxResult.success(mediaService.kickWebrtcSession(sessionId)); case "rtmp": return AjaxResult.success(mediaService.kickRtmpSession(sessionId)); } return AjaxResult.error(); } /** * 获取通道详细信息 */ @ApiOperation("获取通道详细信息") @ApiOperationSupport(order =4 ) @GetMapping(value = "/{name}") public AjaxResult getInfo(@PathVariable("name") String name) { return success(mediaService.getPathInfo(name)); } /** * 获取当前通道列表 */ @GetMapping("/path/list") @ApiOperation("获取当前通道列表") @ApiOperationSupport(order = 5) public TableDataInfo getPaths() { startPage(); return getDataTable(mediaService.paths()); } /** * 按ID查询拉流详情 */ @GetMapping("/getRtspSessionById") @ApiOperation("按ID查询拉流详情") public AjaxResult getRtspSessionById(String sessionId) { List pullStreamList = mediaService.getPullStreamList(); StreamInfo streamInfo = pullStreamList.stream() .filter(object -> object.getId().equals(sessionId)) .collect(Collectors.toList()).get(0); switch (streamInfo.getSessionType()) { case "rtsp": return AjaxResult.success(mediaService.getRtspSessionById(sessionId)); case "rtmp": return AjaxResult.success(mediaService.getRtmpSessionById(sessionId)); case "webrtc": return AjaxResult.success(mediaService.getWebrtcSessionById(sessionId)); } return AjaxResult.error(); } /** * 获取推流列表 */ @PreAuthorize("@ss.hasPermi('media:stream:list')") @GetMapping("/pushList") @ApiOperation("获取推流列表") @ApiOperationSupport(order = 6) public TableDataInfo getPushStreamList() { startPage(); return getDataTable(mediaService.getPushStreamList()); } /** * 获取拉流列表 */ @PreAuthorize("@ss.hasPermi('media:stream:list')") @GetMapping("/pullList") @ApiOperation("获取拉流列表") @ApiOperationSupport(order = 7) public TableDataInfo getPullStreamList() { startPage(); return getDataTable(mediaService.getPullStreamList()); } }