MediaInfo.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package com.genersoft.iot.vmp.media.bean;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.genersoft.iot.vmp.media.zlm.dto.hook.OnStreamChangedHookParam;
  5. import com.genersoft.iot.vmp.utils.MediaServerUtils;
  6. import io.swagger.v3.oas.annotations.media.Schema;
  7. import lombok.Data;
  8. import java.util.List;
  9. import java.util.Map;
  10. /**
  11. * 视频信息
  12. */
  13. @Data
  14. @Schema(description = "视频信息")
  15. public class MediaInfo {
  16. @Schema(description = "应用名")
  17. private String app;
  18. @Schema(description = "流ID")
  19. private String stream;
  20. @Schema(description = "流媒体节点")
  21. private MediaServer mediaServer;
  22. @Schema(description = "协议")
  23. private String schema;
  24. @Schema(description = "观看人数")
  25. private Integer readerCount;
  26. @Schema(description = "视频编码类型")
  27. private String videoCodec;
  28. @Schema(description = "视频宽度")
  29. private Integer width;
  30. @Schema(description = "视频高度")
  31. private Integer height;
  32. @Schema(description = "FPS")
  33. private Integer fps;
  34. @Schema(description = "丢包率")
  35. private Integer loss;
  36. @Schema(description = "音频编码类型")
  37. private String audioCodec;
  38. @Schema(description = "音频通道数")
  39. private Integer audioChannels;
  40. @Schema(description = "音频采样率")
  41. private Integer audioSampleRate;
  42. @Schema(description = "音频采样率")
  43. private Long duration;
  44. @Schema(description = "在线")
  45. private Boolean online;
  46. @Schema(description = "unknown = 0,rtmp_push=1,rtsp_push=2,rtp_push=3,pull=4,ffmpeg_pull=5,mp4_vod=6,device_chn=7")
  47. private Integer originType;
  48. @Schema(description = "产生流的源流地址")
  49. private String originUrl;
  50. @Schema(description = "存活时间,单位秒")
  51. private Long aliveSecond;
  52. @Schema(description = "数据产生速度,单位byte/s")
  53. private Long bytesSpeed;
  54. @Schema(description = "鉴权参数")
  55. private String callId;
  56. @Schema(description = "额外参数")
  57. private Map<String, String> paramMap;
  58. @Schema(description = "服务ID")
  59. private String serverId;
  60. public static MediaInfo getInstance(JSONObject jsonObject, MediaServer mediaServer, String serverId) {
  61. MediaInfo mediaInfo = new MediaInfo();
  62. mediaInfo.setMediaServer(mediaServer);
  63. mediaInfo.setServerId(serverId);
  64. String app = jsonObject.getString("app");
  65. mediaInfo.setApp(app);
  66. String stream = jsonObject.getString("stream");
  67. mediaInfo.setStream(stream);
  68. String schema = jsonObject.getString("schema");
  69. mediaInfo.setSchema(schema);
  70. Integer totalReaderCount = jsonObject.getInteger("totalReaderCount");
  71. Boolean online = jsonObject.getBoolean("online");
  72. Integer originType = jsonObject.getInteger("originType");
  73. String originUrl = jsonObject.getString("originUrl");
  74. Long aliveSecond = jsonObject.getLong("aliveSecond");
  75. String params = jsonObject.getString("params");
  76. Long bytesSpeed = jsonObject.getLong("bytesSpeed");
  77. if (totalReaderCount != null) {
  78. mediaInfo.setReaderCount(totalReaderCount);
  79. }
  80. if (online != null) {
  81. mediaInfo.setOnline(online);
  82. }
  83. if (originType != null) {
  84. mediaInfo.setOriginType(originType);
  85. }
  86. if (originUrl != null) {
  87. mediaInfo.setOriginUrl(originUrl);
  88. }
  89. if (aliveSecond != null) {
  90. mediaInfo.setAliveSecond(aliveSecond);
  91. }
  92. if (bytesSpeed != null) {
  93. mediaInfo.setBytesSpeed(bytesSpeed);
  94. }
  95. if (params != null) {
  96. mediaInfo.setParamMap(MediaServerUtils.urlParamToMap(params));
  97. if(mediaInfo.getCallId() == null) {
  98. mediaInfo.setCallId(mediaInfo.getParamMap().get("callId"));
  99. }
  100. }
  101. JSONArray jsonArray = jsonObject.getJSONArray("tracks");
  102. if (jsonArray.isEmpty()) {
  103. return null;
  104. }
  105. for (int i = 0; i < jsonArray.size(); i++) {
  106. JSONObject trackJson = jsonArray.getJSONObject(i);
  107. Integer channels = trackJson.getInteger("channels");
  108. Integer codecId = trackJson.getInteger("codec_id");
  109. Integer codecType = trackJson.getInteger("codec_type");
  110. Integer sampleRate = trackJson.getInteger("sample_rate");
  111. Integer height = trackJson.getInteger("height");
  112. Integer width = trackJson.getInteger("width");
  113. Integer fps = trackJson.getInteger("fps");
  114. Integer loss = trackJson.getInteger("loss");
  115. Integer frames = trackJson.getInteger("frames");
  116. Long keyFrames = trackJson.getLongValue("key_frames");
  117. Integer gop_interval_ms = trackJson.getInteger("gop_interval_ms");
  118. Long gop_size = trackJson.getLongValue("gop_size");
  119. Long duration = trackJson.getLongValue("duration");
  120. if (channels != null) {
  121. mediaInfo.setAudioChannels(channels);
  122. }
  123. if (sampleRate != null) {
  124. mediaInfo.setAudioSampleRate(sampleRate);
  125. }
  126. if (height != null) {
  127. mediaInfo.setHeight(height);
  128. }
  129. if (width != null) {
  130. mediaInfo.setWidth(width);
  131. }
  132. if (fps != null) {
  133. mediaInfo.setFps(fps);
  134. }
  135. if (loss != null) {
  136. mediaInfo.setLoss(loss);
  137. }
  138. if (duration > 0L) {
  139. mediaInfo.setDuration(duration);
  140. }
  141. if (codecId != null) {
  142. switch (codecId) {
  143. case 0:
  144. mediaInfo.setVideoCodec("H264");
  145. break;
  146. case 1:
  147. mediaInfo.setVideoCodec("H265");
  148. break;
  149. case 2:
  150. mediaInfo.setAudioCodec("AAC");
  151. break;
  152. case 3:
  153. mediaInfo.setAudioCodec("G711A");
  154. break;
  155. case 4:
  156. mediaInfo.setAudioCodec("G711U");
  157. break;
  158. }
  159. }
  160. }
  161. return mediaInfo;
  162. }
  163. public static MediaInfo getInstance(OnStreamChangedHookParam param, MediaServer mediaServer, String serverId) {
  164. MediaInfo mediaInfo = new MediaInfo();
  165. mediaInfo.setApp(param.getApp());
  166. mediaInfo.setStream(param.getStream());
  167. mediaInfo.setSchema(param.getSchema());
  168. mediaInfo.setMediaServer(mediaServer);
  169. mediaInfo.setReaderCount(param.getTotalReaderCount());
  170. mediaInfo.setOnline(param.isRegist());
  171. mediaInfo.setOriginType(param.getOriginType());
  172. mediaInfo.setOriginUrl(param.getOriginUrl());
  173. mediaInfo.setAliveSecond(param.getAliveSecond());
  174. mediaInfo.setBytesSpeed(param.getBytesSpeed());
  175. mediaInfo.setParamMap(param.getParamMap());
  176. if(mediaInfo.getCallId() == null) {
  177. mediaInfo.setCallId(param.getParamMap().get("callId"));
  178. }
  179. mediaInfo.setServerId(serverId);
  180. List<OnStreamChangedHookParam.MediaTrack> tracks = param.getTracks();
  181. if (tracks == null || tracks.isEmpty()) {
  182. return mediaInfo;
  183. }
  184. for (OnStreamChangedHookParam.MediaTrack mediaTrack : tracks) {
  185. switch (mediaTrack.getCodec_id()) {
  186. case 0:
  187. mediaInfo.setVideoCodec("H264");
  188. break;
  189. case 1:
  190. mediaInfo.setVideoCodec("H265");
  191. break;
  192. case 2:
  193. mediaInfo.setAudioCodec("AAC");
  194. break;
  195. case 3:
  196. mediaInfo.setAudioCodec("G711A");
  197. break;
  198. case 4:
  199. mediaInfo.setAudioCodec("G711U");
  200. break;
  201. }
  202. if (mediaTrack.getSample_rate() > 0) {
  203. mediaInfo.setAudioSampleRate(mediaTrack.getSample_rate());
  204. }
  205. if (mediaTrack.getChannels() > 0) {
  206. mediaInfo.setAudioChannels(mediaTrack.getChannels());
  207. }
  208. if (mediaTrack.getHeight() > 0) {
  209. mediaInfo.setHeight(mediaTrack.getHeight());
  210. }
  211. if (mediaTrack.getWidth() > 0) {
  212. mediaInfo.setWidth(mediaTrack.getWidth());
  213. }
  214. }
  215. return mediaInfo;
  216. }
  217. }