根据萤石开放平台进行视频直播

xiaoxiao2021-02-28  14

第一步application.properties的配置:

#萤石个人用户编码 AppKey=3f29a131b62742949d7d841b7242060f #个人密文 Secret=920418c120bbffe4798b3e8364b564e0

第二步HTTP链接工具类,需要导入httpclient依赖包:

import com.front.FrontApplication; import org.apache.commons.lang.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.*; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; /** *http链接工具类 * urltool * * @author * @create 2018-02-23 10:19 **/ @Controller public class HttpClient { protected final static Logger logger = LoggerFactory.getLogger(HttpClient.class); private RequestConfig requestConfig; private final static String ENCODING_DEFAULT = "UTF-8"; private static HttpClient httpClient = new HttpClient(); /** * 默认无参构造器 */ private HttpClient() { requestConfig = RequestConfig.custom().setSocketTimeout(15000) .setConnectTimeout(15000).build();// 设置请求和传输超时时间 } public static HttpClient singleInstacne() { return httpClient; } /** * @param url * @param * @return * @throws ClientProtocolException * @throws IOException */ public String sendWithPut(String url,String stringJson,Map<String,String> headers) throws ClientProtocolException, IOException { HttpPut httpPut= new HttpPut(url); httpPut.setConfig(requestConfig); CloseableHttpResponse response = null; String ret = null; CloseableHttpClient httpclient = null; //设置header httpPut.setHeader("Content-type", "application/json"); if (headers != null && headers.size() > 0) { for (Map.Entry<String, String> entry : headers.entrySet()) { httpPut.setHeader(entry.getKey(),entry.getValue()); } } //组织请求参数 StringEntity stringEntity = new StringEntity(stringJson,"utf-8"); httpPut.setEntity(stringEntity); try { httpclient = HttpClients.createDefault(); response = httpclient.execute(httpPut); HttpEntity entity = response.getEntity(); ret = EntityUtils.toString(entity, ENCODING_DEFAULT); } catch (ClientProtocolException e) { logger.warn("Http client ClientProtocolException : " + e.getMessage()); e.printStackTrace(); throw e; } catch (IOException e) { logger.warn("Http client IOException : " + e.getMessage()); e.printStackTrace(); throw e; } finally { if (response != null) { response.close(); } if (httpclient != null) { httpclient.close(); } } //log.debug("receipt is : " + ret); return ret; } /** * @param url * @param * @return * @throws ClientProtocolException * @throws IOException */ public String sendWithDelete(String url, String message) throws ClientProtocolException, IOException { if (StringUtils.isNotBlank(message)) { url = url + "?" + message; } HttpDelete httpDelete= new HttpDelete(url); httpDelete.setConfig(requestConfig); CloseableHttpResponse response = null; String ret = null; CloseableHttpClient httpclient = null; try { httpclient = HttpClients.createDefault(); response = httpclient.execute(httpDelete); HttpEntity entity = response.getEntity(); ret = EntityUtils.toString(entity, ENCODING_DEFAULT); } catch (ClientProtocolException e) { logger.warn("Http client ClientProtocolException : " + e.getMessage()); e.printStackTrace(); throw e; } catch (IOException e) { logger.warn("Http client IOException : " + e.getMessage()); e.printStackTrace(); throw e; } finally { if (response != null) { response.close(); } if (httpclient != null) { httpclient.close(); } } //log.debug("receipt is : " + ret); return ret; } /** * @param url * @param json * @return * @throws ClientProtocolException * @throws IOException */ public String sendWithPost(String url, String json ) throws ClientProtocolException, IOException { String APPLICATION_JSON = "application/json"; String CONTENT_TYPE_TEXT_JSON = "text/json"; HttpPost httpPost = new HttpPost(url); httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON); httpPost.setConfig(requestConfig); CloseableHttpResponse response = null; String ret = null; CloseableHttpClient httpclient = null; try { httpclient = HttpClients.createDefault(); StringEntity se = new StringEntity(json,"utf-8"); se.setContentType(CONTENT_TYPE_TEXT_JSON); se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON)); httpPost.setEntity(se); response = httpclient.execute(httpPost); HttpEntity entity = response.getEntity(); ret = EntityUtils.toString(entity, ENCODING_DEFAULT); } catch (ClientProtocolException e) { logger.warn("Http client ClientProtocolException : " + e.getMessage()); e.printStackTrace(); throw e; } catch (IOException e) { logger.warn("Http client IOException : " + e.getMessage()); e.printStackTrace(); throw e; } finally { if (response != null) { response.close(); } if (httpclient != null) { httpclient.close(); } } logger.debug("receipt is : " + ret); return ret; } /** * @Title: postForm * @Description: post封装from * @param url * @param params * @return */ private HttpPost postForm(String url, Map<String, String> params) { HttpPost httpost = new HttpPost(url); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); Set<String> keySet = params.keySet(); for (String key : keySet) { nvps.add(new BasicNameValuePair(key, params.get(key))); } try { httpost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); } catch (UnsupportedEncodingException e) { logger.warn("Http client UnsupportedEncodingException : " + e.getMessage()); e.printStackTrace(); } return httpost; } /** * @param url * @param message * @return * @throws ClientProtocolException * @throws IOException */ public String sendWithGet(String url, String message) throws ClientProtocolException, IOException { if (StringUtils.isNotBlank(message)) { url = url + "?" + message; } System.out.println(url); HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); CloseableHttpResponse response = null; String ret = null; CloseableHttpClient httpclient = null; try { httpclient = HttpClients.createDefault(); response = httpclient.execute(httpGet); HttpEntity entity = response.getEntity(); ret = EntityUtils.toString(entity, ENCODING_DEFAULT); } catch (ClientProtocolException e) { logger.warn("Http client ClientProtocolException : " + e.getMessage()); e.printStackTrace(); throw e; } catch (IOException e) { logger.warn("Http client IOException : " + e.getMessage()); e.printStackTrace(); throw e; } finally { if (response != null) { response.close(); } if (httpclient != null) { httpclient.close(); } } return ret; } }

第三步获得令牌工具类:

import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import java.io.IOException; /** * 获得用户令牌的工具类 * * @author * @data 2018-4-2 14:18:58 * */ @Controller public class AccessTokenUtils { private static final Logger log = org.slf4j.LoggerFactory.getLogger(AccessTokenUtils.class); /** * 获得个人ID */ @Value("${AppKey}") private String AppKey; /** * 获得个人密文 */ @Value("${Secret}") private String Secret; /** * 获取令牌接口 */ private String openUrl="https://open.ys7.com/api/lapp/token/get"; /** * 获得用户个人token * @return */ public String getAccessToken(){ String jsonData = "{\"appKey\":" + AppKey + "&\"appSecret\":"+Secret+"}"; HttpClient httpClient = HttpClient.singleInstacne(); String message= null; try { message= httpClient.sendWithPost(openUrl+"?appKey="+AppKey+"&appSecret="+Secret,jsonData); if(StringUtils.isNotBlank(message)){ //将返回去的json转成对象 JSONObject jsonObject = JSONObject.fromObject(message); if(jsonObject.containsKey("code")){ String code = jsonObject.getString("code"); int i = Integer.parseInt(code); String error = ErrorManage.getError(i); log.info("获取令牌响应的结果是:"+error); } if(jsonObject.containsKey("data")){ String data = jsonObject.getString("data"); JSONObject jsonObject1 = JSONObject.fromObject(data); if(jsonObject1.containsKey("accessToken")){ String accessToken = jsonObject1.getString("accessToken"); return accessToken; } } } } catch (IOException e) { e.printStackTrace(); } return null; } }

第四步请求获得用户设备信息:

import com.front.entity.Video; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * 查询用户设备信息工具类 * * @author * @date 2018-4-4 16:08:42 */ @Controller public class DeviceDataUtils { private static final Logger log= LoggerFactory.getLogger(DeviceDataUtils.class); /** * 获取用户令牌 */ @Autowired private AccessTokenUtils accessTokenUtils; /** * 获取设备信息列表 * @return */ public List<Video> getDevices() { //查询设备列表请求接口 String url="https://open.ys7.com/api/lapp/device/list"; //获得用户令牌 String accessToken = accessTokenUtils.getAccessToken(); //起始页 int pageStart=0; //每页显示数 int pageSize=50; //创建设备集合 List<Video> list=new ArrayList<>(); //创建链接对象 HttpClient client= HttpClient.singleInstacne(); String message=null; try { String jsonData = "{\"accessToken\":" + accessToken + "&\"pageStart\":"+pageStart+"&\"pageSize\":"+pageSize+"}"; message = client.sendWithPost(url + "?accessToken=" + accessToken + "&pageStart=" + pageStart + "&pageSize=" + pageSize,jsonData); if(StringUtils.isNotBlank(message)){ JSONObject jsonObject = JSONObject.fromObject(message); if(jsonObject.containsKey("data")){ String data = jsonObject.getString("data"); JSONArray jsonArray = JSONArray.fromObject(data); for (Object o : jsonArray) { //创建设备对象 Video video =new Video(); JSONObject jsonObject1 = JSONObject.fromObject(o); //设置设备序列号 video.setDeviceSerial(jsonObject1.getString("deviceSerial")); //设置设备名 video.setDeviceName(jsonObject1.getString("deviceName")); //设置设备类型 video.setDeviceType(jsonObject1.getString("deviceType")); //设置设备版本号 video.setDeviceVersion(jsonObject1.getString("deviceVersion")); list.add(video); } String code = ErrorManage.getError(Integer.parseInt(jsonObject.getString("code"))); log.info("查询设备分页响应状态:"+code); } } } catch (IOException e) { e.printStackTrace(); } return list; } }

第五步获得直播地址:

import com.front.entity.Video; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * 用户视频直播列表工具类 */ @Controller public class DeviceLiveBroadcast { private static final Logger log= LoggerFactory.getLogger(DeviceLiveBroadcast.class); /** * 获得用户令牌 */ @Autowired private AccessTokenUtils accessTokenUtils; /** * 查询用户下直播设备列表 * @return */ public List<Video> getLiveBroadcast() { //查询播放设备列表接口地址 String url="https://open.ys7.com/api/lapp/live/video/list"; //获得用户令牌 String accessToken = accessTokenUtils.getAccessToken(); //起始页 int pageStart=0; //每页显示数 int pageSize=50; //创建设备集合 List<Video> list=new ArrayList<>(); //创建链接对象 HttpClient client= HttpClient.singleInstacne(); String message=null; String jsonData = "{\"accessToken\":" + accessToken + "&\"pageStart\":"+pageStart+"&\"pageSize\":"+pageSize+"}"; try { message=client.sendWithPost(url+"?accessToken=" + accessToken + "&pageStart=" + pageStart + "&pageSize=" + pageSize,jsonData); if(StringUtils.isNotBlank(message)){ JSONObject jsonObject = JSONObject.fromObject(message); if(jsonObject.containsKey("data")){ String data = jsonObject.getString("data"); JSONArray jsonArray = JSONArray.fromObject(data); for (Object o : jsonArray) { //创建设备对象 Video video =new Video(); JSONObject jsonObject1 = JSONObject.fromObject(o); //设置设备序列号 video.setDeviceSerial(jsonObject1.getString("deviceSerial")); //设置设备通道 video.setChannelNo(Integer.parseInt(jsonObject1.getString("channelNo"))); //设置设备hls协议标准播放地址 video.setHls(jsonObject1.getString("liveAddress")); //设置设备hls协议高清播放地址 video.setHlsHd(jsonObject1.getString("hdAddress")); //设置设备rtmp协议标准播放地址 video.setRtmp(jsonObject1.getString("rtmp")); //设置设备rtmp高清播放地址 video.setRtmpHd(jsonObject1.getString("rtmpHd")); list.add(video); } String code = ErrorManage.getError(Integer.parseInt(jsonObject.getString("code"))); log.info("查询设备播放列表分页响应状态:"+code); } } } catch (IOException e) { e.printStackTrace(); } return list; } }

第六步设备实体类:

import com.fasterxml.jackson.annotation.JsonProperty; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; /** * 视频实体类 * * @author * @date 2018-4-3 10:13:20 */ @Document(collection = "Video") public class Video { /** * 设备ID */ @Id private String id; /** * 设备所属组ID */ @Field("groupId") @JsonProperty("groupId") private String groupId; /** * 设备序列号 */ @Field("deviceSerial") @JsonProperty("deviceSerial") private String deviceSerial; /** * 设备名称 */ @Field("deviceName") @JsonProperty("deviceName") private String deviceName; /** * 设备类型 */ @Field("deviceType") @JsonProperty("deviceType") private String deviceType; /** * 设备版本号 */ @Field("deviceVersion") @JsonProperty("deviceVersion") private String deviceVersion; /** * 设备通道号 默认是1 */ @Field("channelNo") @JsonProperty("channelNo") private Integer channelNo; /** * hls协议标准直播地址 */ @Field("hls") @JsonProperty("hls") private String hls; /** * hls协议高清直播地址 */ @Field("hlsHd") @JsonProperty("hlsHd") private String hlsHd; /** * rtmp协议标准播放地址 */ @Field("rtmp") @JsonProperty("rtmp") private String rtmp; /** * rtmp协议高清直播地址 */ @Field("rtmpHd") @JsonProperty("rtmpHd") private String rtmpHd; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getGroupId() { return groupId; } public void setGroupId(String groupId) { this.groupId = groupId; } public String getHls() { return hls; } public void setHls(String hls) { this.hls = hls; } public String getHlsHd() { return hlsHd; } public void setHlsHd(String hlsHd) { this.hlsHd = hlsHd; } public String getRtmp() { return rtmp; } public void setRtmp(String rtmp) { this.rtmp = rtmp; } public String getRtmpHd() { return rtmpHd; } public void setRtmpHd(String rtmpHd) { this.rtmpHd = rtmpHd; } public Integer getChannelNo() { return channelNo; } public void setChannelNo(Integer channelNo) { this.channelNo = channelNo; } public String getDeviceSerial() { return deviceSerial; } public void setDeviceSerial(String deviceSerial) { this.deviceSerial = deviceSerial; } public String getDeviceName() { return deviceName; } public void setDeviceName(String deviceName) { this.deviceName = deviceName; } public String getDeviceType() { return deviceType; } public void setDeviceType(String deviceType) { this.deviceType = deviceType; } public String getDeviceVersion() { return deviceVersion; } public void setDeviceVersion(String deviceVersion) { this.deviceVersion = deviceVersion; } }
转载请注明原文地址: https://www.6miu.com/read-2800072.html

最新回复(0)