MediaService.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.zj.service;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import javax.servlet.http.HttpServletResponse;
  7. import org.springframework.stereotype.Service;
  8. import org.springframework.web.socket.BinaryMessage;
  9. import org.springframework.web.socket.WebSocketSession;
  10. import com.zj.thread.ProcessThread;
  11. import cn.hutool.core.thread.ThreadUtil;
  12. /**
  13. * 媒体服务
  14. * @author ZJ
  15. *
  16. */
  17. @Service
  18. public class MediaService {
  19. public static Map<String, ProcessThread> map = new HashMap<String, ProcessThread>();
  20. /**
  21. *
  22. * @param url 源地址
  23. * @param id 源地址唯一标识(表示同一个媒体)
  24. */
  25. public void playForHttp(String input, String id, HttpServletResponse response) {
  26. ProcessThread processThread = map.get(id);
  27. //新增的媒体需要进行推流初始化
  28. if (processThread == null) {
  29. processThread = new ProcessThread(input);
  30. //初始化推拉流
  31. map.put(id, processThread);
  32. ThreadUtil.execute(processThread);
  33. }
  34. //创建客户端的输出流
  35. ByteArrayOutputStream byteArrayOutputStream = processThread.addClient();
  36. //发送头部
  37. sendHeaderForHttp(response, processThread);
  38. //发送数据
  39. sendAVDataForHttp(response, byteArrayOutputStream);
  40. }
  41. public void playForWs(String input, String id, WebSocketSession session) {
  42. ProcessThread processThread = map.get(id);
  43. //新增的媒体需要进行推流初始化
  44. if (processThread == null) {
  45. processThread = new ProcessThread(input);
  46. //初始化推拉流
  47. map.put(id, processThread);
  48. ThreadUtil.execute(processThread);
  49. }
  50. //创建客户端的输出流
  51. ByteArrayOutputStream byteArrayOutputStream = processThread.addClient();
  52. //发送头部
  53. sendHeaderForWs(session, processThread);
  54. //发送数据
  55. sendAVDataForWs(session, byteArrayOutputStream);
  56. }
  57. /**
  58. * 发送FLV header
  59. * @param response
  60. * @param stream
  61. */
  62. private void sendHeaderForHttp(HttpServletResponse response, ProcessThread processThread) {
  63. try {
  64. //最多等三分钟,如果没有header则认为没取到视频,发送header后续要优化
  65. for (int i = 0; i < 1200; i++) {
  66. if (processThread.getHeader() != null) {
  67. response.getOutputStream().write(processThread.getHeader());
  68. break;
  69. }
  70. Thread.sleep(100);
  71. }
  72. /*
  73. * 这里后续还要修改,如果没获取到header怎么和前段交互,后端线程怎么处理
  74. */
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. } catch (InterruptedException e) {
  78. }
  79. }
  80. /**
  81. * 发送视频数据
  82. * @param response
  83. * @param outData
  84. */
  85. private void sendAVDataForHttp(HttpServletResponse response, ByteArrayOutputStream outData) {
  86. try {
  87. while (true) {
  88. if (outData.size() > 0) {
  89. response.getOutputStream().write(outData.toByteArray());
  90. outData.reset();
  91. } else {
  92. Thread.sleep(100);
  93. }
  94. }
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. } catch (InterruptedException e) {
  98. }
  99. }
  100. /**
  101. * 发送FLV header
  102. * @param response
  103. * @param stream
  104. */
  105. private void sendHeaderForWs(WebSocketSession session, ProcessThread processThread) {
  106. try {
  107. //最多等三分钟,如果没有header则认为没取到视频,发送header后续要优化
  108. for (int i = 0; i < 1200; i++) {
  109. if (processThread.getHeader() != null) {
  110. session.sendMessage(new BinaryMessage(processThread.getHeader()));
  111. break;
  112. }
  113. Thread.sleep(100);
  114. }
  115. /*
  116. * 这里后续还要修改,如果没获取到header怎么和前段交互,后端线程怎么处理
  117. */
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. } catch (InterruptedException e) {
  121. }
  122. }
  123. /**
  124. * 发送视频数据
  125. * @param response
  126. * @param outData
  127. */
  128. private void sendAVDataForWs(WebSocketSession session, ByteArrayOutputStream outData) {
  129. try {
  130. while (true) {
  131. if (outData.size() > 0) {
  132. session.sendMessage(new BinaryMessage(outData.toByteArray()));
  133. outData.reset();
  134. } else {
  135. Thread.sleep(100);
  136. }
  137. }
  138. } catch (IOException e) {
  139. e.printStackTrace();
  140. } catch (InterruptedException e) {
  141. }
  142. }
  143. }