TencentPushController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package me.zhengjie.application.admin.controller;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  7. import lombok.RequiredArgsConstructor;
  8. import me.zhengjie.annotation.rest.AnonymousPostMapping;
  9. import me.zhengjie.base.util.DownloadUtils;
  10. import me.zhengjie.dao.mybatis.entity.OrderRoomIdEntity;
  11. import me.zhengjie.dao.mybatis.entity.PushDataEntity;
  12. import me.zhengjie.dao.mybatis.mapper.OrderRoomIdMapper;
  13. import me.zhengjie.dao.mybatis.mapper.PushDataMapper;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.stereotype.Controller;
  17. import org.springframework.web.bind.annotation.RequestBody;
  18. import org.springframework.web.bind.annotation.ResponseBody;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. @Controller
  23. @RequiredArgsConstructor
  24. public class TencentPushController {
  25. private static final Logger log = LoggerFactory.getLogger(TencentPushController.class);
  26. private final PushDataMapper pushDataMapper;
  27. private final OrderRoomIdMapper orderRoomIdMapper;
  28. @AnonymousPostMapping("/tencentpush")
  29. @ResponseBody
  30. public Map<String, Object> tencentpush(@RequestBody Map<String, String> json) throws Exception {
  31. String channelId = json.get("channel_id");
  32. Map<String, String> map = split(json.get("stream_param"));
  33. if (channelId.contains("share")) {
  34. // 转成json形式
  35. String fileId = (String) json.get("file_id");
  36. PushDataEntity pushData = new PushDataEntity();
  37. pushData.setFileId(fileId);
  38. pushData.setPushType("pc");
  39. pushData.setTypeName(json.get("appname"));
  40. pushData.setData(JSON.toJSONString(json));
  41. pushData.setPath(json.get("video_url"));
  42. pushDataMapper.insert(pushData);
  43. log.info("pc端的推送信息:" + JSON.toJSON(json));
  44. // 文件fileId
  45. String roomId = map.get("groupid");
  46. QueryWrapper<OrderRoomIdEntity> orderRoomQuery = new QueryWrapper<>();
  47. orderRoomQuery.eq("roomId", roomId);
  48. orderRoomQuery.orderByDesc("create_time");
  49. List<OrderRoomIdEntity> orderRoomId = orderRoomIdMapper.selectList(orderRoomQuery);
  50. // 这里通过订单查询相关信息
  51. if (orderRoomId != null && orderRoomId.size() > 0) {
  52. OrderRoomIdEntity orderRoomIdEntity = orderRoomId.get(0);
  53. String filePath = orderRoomIdEntity.getBusinessNo();
  54. String fileName = filePath + "/PC" + fileId + ".mp4";
  55. DownloadUtils.downloadFile(json.get("video_url"), fileName);
  56. OrderRoomIdEntity update = new OrderRoomIdEntity();
  57. update.setPcFileId(fileId);
  58. update.setPcVideoUrl(fileName);
  59. update.setId(orderRoomIdEntity.getId());
  60. orderRoomIdMapper.updateById(update);
  61. }
  62. }
  63. if (channelId.contains("aux")) {
  64. // 转成json形式
  65. String fileId = (String) json.get("file_id");
  66. PushDataEntity pushData = new PushDataEntity();
  67. pushData.setFileId(fileId);
  68. pushData.setPushType("pc");
  69. pushData.setTypeName(json.get("appname"));
  70. pushData.setData(JSON.toJSONString(json));
  71. pushData.setPath(json.get("video_url"));
  72. pushDataMapper.insert(pushData);
  73. log.info("app的视频推送:" + JSON.toJSON(json));
  74. // 文件fileId
  75. String roomId = map.get("groupid");
  76. QueryWrapper<OrderRoomIdEntity> orderRoomQuery = new QueryWrapper<>();
  77. orderRoomQuery.eq("roomId", roomId);
  78. orderRoomQuery.orderByDesc("create_time");
  79. List<OrderRoomIdEntity> orderRoomId = orderRoomIdMapper.selectList(orderRoomQuery);
  80. // 这里通过订单查询相关信息
  81. if (orderRoomId != null && orderRoomId.size() > 0) {
  82. OrderRoomIdEntity orderRoomIdEntity = orderRoomId.get(0);
  83. String filePath = orderRoomIdEntity.getBusinessNo();
  84. String fileName = filePath + "/APP" + fileId + ".mp4";
  85. DownloadUtils.downloadFile(json.get("video_url"), fileName);
  86. OrderRoomIdEntity update = new OrderRoomIdEntity();
  87. update.setAppFileId(fileId);
  88. update.setAppVideoUrl(fileName);
  89. update.setId(orderRoomIdEntity.getId());
  90. orderRoomIdMapper.updateById(update);
  91. }
  92. }
  93. Map<String, Object> result = new HashMap<>();
  94. result.put("success", true);
  95. result.put("code", 200);
  96. return result;
  97. }
  98. // @AnonymousPostMapping("/apppush")
  99. @ResponseBody
  100. public Map<String, Object> apppush(@RequestBody JSONObject json) throws Exception {
  101. Object data = JSON.toJSON(json);
  102. log.info("app的视频推送start######################################################################################");
  103. log.info("app的视频推送:" + data);
  104. log.info("app的视频推送end########################################################################################");
  105. // pushData.set
  106. String eventType = json.getString("EventType");
  107. if (eventType != null && eventType.equalsIgnoreCase("ProcedureStateChanged")) {
  108. JSONObject procedureState = json.getJSONObject("ProcedureStateChangeEvent");
  109. if (procedureState != null) {
  110. JSONArray mediaProcess = procedureState.getJSONArray("MediaProcessResultSet");
  111. for (int i = 0; i < mediaProcess.size(); i++) {
  112. JSONObject map = mediaProcess.getJSONObject(i);
  113. if (map.get("Type") != null && map.getString("Type").equalsIgnoreCase("Transcode")) {
  114. JSONObject videoUrl = map.getJSONObject("TranscodeTask").getJSONObject("Output");
  115. // 通过id查询房间号码
  116. String fileId = procedureState.getString("FileId");
  117. QueryWrapper<PushDataEntity> query = new QueryWrapper<>();
  118. query.eq("file_id", fileId);
  119. query.eq("push_type", "app");
  120. query.eq("type_name", "live");
  121. List<PushDataEntity> file = pushDataMapper.selectList(query);
  122. if (file != null && file.size() > 0) {
  123. String pushData = file.get(0).getData();
  124. JSONObject jsonObj = JSONObject.parseObject(pushData);
  125. String streamParam = jsonObj.getString("stream_param");
  126. Map<String, String> param = split(streamParam);
  127. String roomId = param.get("roomId");
  128. OrderRoomIdEntity orderRoomId = new OrderRoomIdEntity();
  129. orderRoomId.setAppFileId(fileId);
  130. String filePath = orderRoomId.getBusinessNo() + "/";
  131. String fileName = filePath + "/APP" + fileId + ".mp4";
  132. DownloadUtils.downloadFile(videoUrl.get("Url").toString(), fileName);
  133. orderRoomId.setAppVideoUrl(fileName);
  134. //
  135. UpdateWrapper<OrderRoomIdEntity> updateQw = new UpdateWrapper<>();
  136. updateQw.eq("roomId", roomId);
  137. orderRoomIdMapper.update(orderRoomId, updateQw);
  138. PushDataEntity pushDataEntity = new PushDataEntity();
  139. pushDataEntity.setFileId(fileId);
  140. pushDataEntity.setPushType("app");
  141. pushDataEntity.setData(JSON.toJSONString(json));
  142. pushDataEntity.setPath(videoUrl.get("Url").toString());
  143. pushDataEntity.setTypeName("ProcedureStateChangeEvent");
  144. pushDataMapper.insert(pushDataEntity);
  145. } else {
  146. Map<String, Object> result = new HashMap<>();
  147. result.put("success", false);
  148. result.put("code", 100);
  149. return result;
  150. }
  151. }
  152. }
  153. }
  154. } else if (eventType != null && eventType.equalsIgnoreCase("NewFileUpload")) {
  155. // FileUploadEvent
  156. JSONObject jsonObject = json.getJSONObject("FileUploadEvent");
  157. String fileId = jsonObject.getString("FileId");
  158. JSONObject mediaBasicInfo = jsonObject.getJSONObject("MediaBasicInfo");
  159. String path = mediaBasicInfo.getString("MediaUrl");
  160. // 这里判断
  161. String name = mediaBasicInfo.getString("Name");
  162. if (name.contains("aux")) {
  163. JSONObject sourceInfo = mediaBasicInfo.getJSONObject("SourceInfo");
  164. String sourceContext = sourceInfo.getString("SourceContext");
  165. Map<String, String> map = split(sourceContext);
  166. String roomId = map.get("groupid");
  167. QueryWrapper<OrderRoomIdEntity> orderRoomQuery = new QueryWrapper<>();
  168. orderRoomQuery.eq("roomId", roomId);
  169. orderRoomQuery.orderByDesc("create_time");
  170. List<OrderRoomIdEntity> orderRoomId = orderRoomIdMapper.selectList(orderRoomQuery);
  171. if (orderRoomId != null && orderRoomId.size() > 0) {
  172. OrderRoomIdEntity orderRoomIdEntity = orderRoomId.get(0);
  173. String filePath = orderRoomIdEntity.getBusinessNo() + "/";
  174. String fileName = filePath + "/APP" + fileId + ".mp4";
  175. DownloadUtils.downloadFile(path, fileName);
  176. OrderRoomIdEntity update = new OrderRoomIdEntity();
  177. update.setPcFileId(fileId);
  178. update.setAppVideoUrl(fileName);
  179. update.setId(orderRoomIdEntity.getId());
  180. orderRoomIdMapper.updateById(update);
  181. }
  182. }
  183. PushDataEntity PushDataEntity = new PushDataEntity();
  184. PushDataEntity.setFileId(fileId);
  185. PushDataEntity.setTypeName(eventType);
  186. PushDataEntity.setPushType("app");
  187. PushDataEntity.setData(JSON.toJSONString(json));
  188. PushDataEntity.setPath(path);
  189. PushDataEntity.setTypeName(eventType);
  190. pushDataMapper.insert(PushDataEntity);
  191. // 保存推送过来的视频
  192. } else {
  193. // 处理"appname": "live",
  194. String live = json.getString("appname");
  195. if (live.equalsIgnoreCase("live")) {
  196. PushDataEntity pushDataEntity = new PushDataEntity();
  197. pushDataEntity.setFileId(json.getString("file_id"));
  198. pushDataEntity.setPushType("app");
  199. pushDataEntity.setData(JSON.toJSONString(json));
  200. pushDataEntity.setPath(json.getString("video_url"));
  201. pushDataEntity.setTypeName(json.getString("appname"));
  202. pushDataMapper.insert(pushDataEntity);
  203. } else {
  204. PushDataEntity pushDataEntity = new PushDataEntity();
  205. pushDataEntity.setFileId("");
  206. pushDataEntity.setPushType("app");
  207. pushDataEntity.setData(JSON.toJSONString(json));
  208. pushDataEntity.setPath("");
  209. pushDataMapper.insert(pushDataEntity);
  210. }
  211. }
  212. Map<String, Object> result = new HashMap<>();
  213. result.put("success", true);
  214. result.put("code", 200);
  215. return result;
  216. }
  217. public static void main(String[] args) {
  218. Map<String, String> map = new HashMap<String, String>();
  219. map.put("EventType", "ProcedureStateChanged");
  220. map.put("FileUploadEvent", null);
  221. System.out.println(JSON.toJSON(map));
  222. }
  223. /**
  224. * @param urlparam 带分隔的url参数
  225. * @return
  226. */
  227. public static Map<String, String> split(String urlparam) {
  228. Map<String, String> map = new HashMap<String, String>();
  229. String[] param = urlparam.split("&");
  230. for (String keyvalue : param) {
  231. String[] pair = keyvalue.split("=");
  232. if (pair.length == 2) {
  233. map.put(pair[0], pair[1]);
  234. }
  235. }
  236. return map;
  237. }
  238. @AnonymousPostMapping(value = "/uploadToken", produces = "text/plain")
  239. @ResponseBody
  240. public String uploadToken(String auth_token) throws Exception {
  241. return "ok";
  242. }
  243. }