| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- package me.zhengjie.application.admin.controller;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
- import lombok.RequiredArgsConstructor;
- import me.zhengjie.annotation.rest.AnonymousPostMapping;
- import me.zhengjie.base.util.DownloadUtils;
- import me.zhengjie.dao.mybatis.entity.OrderRoomIdEntity;
- import me.zhengjie.dao.mybatis.entity.PushDataEntity;
- import me.zhengjie.dao.mybatis.mapper.OrderRoomIdMapper;
- import me.zhengjie.dao.mybatis.mapper.PushDataMapper;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.ResponseBody;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Controller
- @RequiredArgsConstructor
- public class TencentPushController {
- private static final Logger log = LoggerFactory.getLogger(TencentPushController.class);
- private final PushDataMapper pushDataMapper;
- private final OrderRoomIdMapper orderRoomIdMapper;
- @AnonymousPostMapping("/tencentpush")
- @ResponseBody
- public Map<String, Object> tencentpush(@RequestBody Map<String, String> json) throws Exception {
- String channelId = json.get("channel_id");
- Map<String, String> map = split(json.get("stream_param"));
- if (channelId.contains("share")) {
- // 转成json形式
- String fileId = (String) json.get("file_id");
- PushDataEntity pushData = new PushDataEntity();
- pushData.setFileId(fileId);
- pushData.setPushType("pc");
- pushData.setTypeName(json.get("appname"));
- pushData.setData(JSON.toJSONString(json));
- pushData.setPath(json.get("video_url"));
- pushDataMapper.insert(pushData);
- log.info("pc端的推送信息:" + JSON.toJSON(json));
- // 文件fileId
- String roomId = map.get("groupid");
- QueryWrapper<OrderRoomIdEntity> orderRoomQuery = new QueryWrapper<>();
- orderRoomQuery.eq("roomId", roomId);
- orderRoomQuery.orderByDesc("create_time");
- List<OrderRoomIdEntity> orderRoomId = orderRoomIdMapper.selectList(orderRoomQuery);
- // 这里通过订单查询相关信息
- if (orderRoomId != null && orderRoomId.size() > 0) {
- OrderRoomIdEntity orderRoomIdEntity = orderRoomId.get(0);
- String filePath = orderRoomIdEntity.getBusinessNo();
- String fileName = filePath + "/PC" + fileId + ".mp4";
- DownloadUtils.downloadFile(json.get("video_url"), fileName);
- OrderRoomIdEntity update = new OrderRoomIdEntity();
- update.setPcFileId(fileId);
- update.setPcVideoUrl(fileName);
- update.setId(orderRoomIdEntity.getId());
- orderRoomIdMapper.updateById(update);
- }
- }
- if (channelId.contains("aux")) {
- // 转成json形式
- String fileId = (String) json.get("file_id");
- PushDataEntity pushData = new PushDataEntity();
- pushData.setFileId(fileId);
- pushData.setPushType("pc");
- pushData.setTypeName(json.get("appname"));
- pushData.setData(JSON.toJSONString(json));
- pushData.setPath(json.get("video_url"));
- pushDataMapper.insert(pushData);
- log.info("app的视频推送:" + JSON.toJSON(json));
- // 文件fileId
- String roomId = map.get("groupid");
- QueryWrapper<OrderRoomIdEntity> orderRoomQuery = new QueryWrapper<>();
- orderRoomQuery.eq("roomId", roomId);
- orderRoomQuery.orderByDesc("create_time");
- List<OrderRoomIdEntity> orderRoomId = orderRoomIdMapper.selectList(orderRoomQuery);
- // 这里通过订单查询相关信息
- if (orderRoomId != null && orderRoomId.size() > 0) {
- OrderRoomIdEntity orderRoomIdEntity = orderRoomId.get(0);
- String filePath = orderRoomIdEntity.getBusinessNo();
- String fileName = filePath + "/APP" + fileId + ".mp4";
- DownloadUtils.downloadFile(json.get("video_url"), fileName);
- OrderRoomIdEntity update = new OrderRoomIdEntity();
- update.setAppFileId(fileId);
- update.setAppVideoUrl(fileName);
- update.setId(orderRoomIdEntity.getId());
- orderRoomIdMapper.updateById(update);
- }
- }
- Map<String, Object> result = new HashMap<>();
- result.put("success", true);
- result.put("code", 200);
- return result;
- }
- // @AnonymousPostMapping("/apppush")
- @ResponseBody
- public Map<String, Object> apppush(@RequestBody JSONObject json) throws Exception {
- Object data = JSON.toJSON(json);
- log.info("app的视频推送start######################################################################################");
- log.info("app的视频推送:" + data);
- log.info("app的视频推送end########################################################################################");
- // pushData.set
- String eventType = json.getString("EventType");
- if (eventType != null && eventType.equalsIgnoreCase("ProcedureStateChanged")) {
- JSONObject procedureState = json.getJSONObject("ProcedureStateChangeEvent");
- if (procedureState != null) {
- JSONArray mediaProcess = procedureState.getJSONArray("MediaProcessResultSet");
- for (int i = 0; i < mediaProcess.size(); i++) {
- JSONObject map = mediaProcess.getJSONObject(i);
- if (map.get("Type") != null && map.getString("Type").equalsIgnoreCase("Transcode")) {
- JSONObject videoUrl = map.getJSONObject("TranscodeTask").getJSONObject("Output");
- // 通过id查询房间号码
- String fileId = procedureState.getString("FileId");
- QueryWrapper<PushDataEntity> query = new QueryWrapper<>();
- query.eq("file_id", fileId);
- query.eq("push_type", "app");
- query.eq("type_name", "live");
- List<PushDataEntity> file = pushDataMapper.selectList(query);
- if (file != null && file.size() > 0) {
- String pushData = file.get(0).getData();
- JSONObject jsonObj = JSONObject.parseObject(pushData);
- String streamParam = jsonObj.getString("stream_param");
- Map<String, String> param = split(streamParam);
- String roomId = param.get("roomId");
- OrderRoomIdEntity orderRoomId = new OrderRoomIdEntity();
- orderRoomId.setAppFileId(fileId);
- String filePath = orderRoomId.getBusinessNo() + "/";
- String fileName = filePath + "/APP" + fileId + ".mp4";
- DownloadUtils.downloadFile(videoUrl.get("Url").toString(), fileName);
- orderRoomId.setAppVideoUrl(fileName);
- //
- UpdateWrapper<OrderRoomIdEntity> updateQw = new UpdateWrapper<>();
- updateQw.eq("roomId", roomId);
- orderRoomIdMapper.update(orderRoomId, updateQw);
- PushDataEntity pushDataEntity = new PushDataEntity();
- pushDataEntity.setFileId(fileId);
- pushDataEntity.setPushType("app");
- pushDataEntity.setData(JSON.toJSONString(json));
- pushDataEntity.setPath(videoUrl.get("Url").toString());
- pushDataEntity.setTypeName("ProcedureStateChangeEvent");
- pushDataMapper.insert(pushDataEntity);
- } else {
- Map<String, Object> result = new HashMap<>();
- result.put("success", false);
- result.put("code", 100);
- return result;
- }
- }
- }
- }
- } else if (eventType != null && eventType.equalsIgnoreCase("NewFileUpload")) {
- // FileUploadEvent
- JSONObject jsonObject = json.getJSONObject("FileUploadEvent");
- String fileId = jsonObject.getString("FileId");
- JSONObject mediaBasicInfo = jsonObject.getJSONObject("MediaBasicInfo");
- String path = mediaBasicInfo.getString("MediaUrl");
- // 这里判断
- String name = mediaBasicInfo.getString("Name");
- if (name.contains("aux")) {
- JSONObject sourceInfo = mediaBasicInfo.getJSONObject("SourceInfo");
- String sourceContext = sourceInfo.getString("SourceContext");
- Map<String, String> map = split(sourceContext);
- String roomId = map.get("groupid");
- QueryWrapper<OrderRoomIdEntity> orderRoomQuery = new QueryWrapper<>();
- orderRoomQuery.eq("roomId", roomId);
- orderRoomQuery.orderByDesc("create_time");
- List<OrderRoomIdEntity> orderRoomId = orderRoomIdMapper.selectList(orderRoomQuery);
- if (orderRoomId != null && orderRoomId.size() > 0) {
- OrderRoomIdEntity orderRoomIdEntity = orderRoomId.get(0);
- String filePath = orderRoomIdEntity.getBusinessNo() + "/";
- String fileName = filePath + "/APP" + fileId + ".mp4";
- DownloadUtils.downloadFile(path, fileName);
- OrderRoomIdEntity update = new OrderRoomIdEntity();
- update.setPcFileId(fileId);
- update.setAppVideoUrl(fileName);
- update.setId(orderRoomIdEntity.getId());
- orderRoomIdMapper.updateById(update);
- }
- }
- PushDataEntity PushDataEntity = new PushDataEntity();
- PushDataEntity.setFileId(fileId);
- PushDataEntity.setTypeName(eventType);
- PushDataEntity.setPushType("app");
- PushDataEntity.setData(JSON.toJSONString(json));
- PushDataEntity.setPath(path);
- PushDataEntity.setTypeName(eventType);
- pushDataMapper.insert(PushDataEntity);
- // 保存推送过来的视频
- } else {
- // 处理"appname": "live",
- String live = json.getString("appname");
- if (live.equalsIgnoreCase("live")) {
- PushDataEntity pushDataEntity = new PushDataEntity();
- pushDataEntity.setFileId(json.getString("file_id"));
- pushDataEntity.setPushType("app");
- pushDataEntity.setData(JSON.toJSONString(json));
- pushDataEntity.setPath(json.getString("video_url"));
- pushDataEntity.setTypeName(json.getString("appname"));
- pushDataMapper.insert(pushDataEntity);
- } else {
- PushDataEntity pushDataEntity = new PushDataEntity();
- pushDataEntity.setFileId("");
- pushDataEntity.setPushType("app");
- pushDataEntity.setData(JSON.toJSONString(json));
- pushDataEntity.setPath("");
- pushDataMapper.insert(pushDataEntity);
- }
- }
- Map<String, Object> result = new HashMap<>();
- result.put("success", true);
- result.put("code", 200);
- return result;
- }
- public static void main(String[] args) {
- Map<String, String> map = new HashMap<String, String>();
- map.put("EventType", "ProcedureStateChanged");
- map.put("FileUploadEvent", null);
- System.out.println(JSON.toJSON(map));
- }
- /**
- * @param urlparam 带分隔的url参数
- * @return
- */
- public static Map<String, String> split(String urlparam) {
- Map<String, String> map = new HashMap<String, String>();
- String[] param = urlparam.split("&");
- for (String keyvalue : param) {
- String[] pair = keyvalue.split("=");
- if (pair.length == 2) {
- map.put(pair[0], pair[1]);
- }
- }
- return map;
- }
- @AnonymousPostMapping(value = "/uploadToken", produces = "text/plain")
- @ResponseBody
- public String uploadToken(String auth_token) throws Exception {
- return "ok";
- }
- }
|