視頻點播RTMP推流直播流媒體服務二次開發集成接口

LiveQing流媒體服務器軟件,包含雲平臺服務,雲直播服務等,支持與LiveGBS的對接,支持設備RTMP推流,視頻文件轉碼等功能java

集成二次開發

二次開發中,方式是在本身業務系統後端登陸接口中,調用流媒體的登陸接口,獲取所需的sid或是token後端

1. 封閉內網使用

在業務使用,提供視頻分發能力時,且不會對外公開接口端口10080(默認端口),能夠直接將接口鑑權關閉,具體服務器登陸 http://localhost:10080 默認用戶名/密碼 admin/admin, 在 基礎配置 頁面,【接口鑑權】開關。服務器

2. 業務系統對接(兩種方式)

2.1 cookie方式

注: HttpOnly = true 客戶端API(例如JavaScript)沒法訪問僅限http的cookie。 此限制經過跨站點腳本(XSS)消除了cookie被盜的威脅。cookie

  1. 在後端業務代碼中對接,如Java/PHP/Node.js 等app

  2. 調用LiveDSS登陸接口,接口調用成功後會在請求Headers的cookie中寫入sid加密

  3. 取出cookie裏的sidurl

  4. 其它接口調用時在請求頭cookies中傳遞sid.net

  5. Content-Type:application/x-www-form-urlencoded3d

  6. 接口請求路徑示例:http://localhost:10080/logincode

代碼示例:Java

2.1.1 獲取sid

import java.io.DataOutputStream;
  import java.net.HttpURLConnection;
  import java.net.URL;
  import java.util.Iterator;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;

  public class GetLoginSid {

  public static void main(String[] args) throws Exception {
        URL url = new URL("http://dss.liveqing.com:10080/login");
        //發起POST請求,並傳遞username,password參數(須要md5加密)
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");     
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        DataOutputStream out = new DataOutputStream(conn.getOutputStream()); 
        String content = "username=admin&password=21232f297a57a5a743894a0e4a801fc3";
        out.writeBytes(content);
        out.flush(); 
        out.close();
        Map<String, List<String>> headerFields = conn.getHeaderFields();
        Set<String> headerFieldsSet = headerFields.keySet();
        Iterator<String> hearerFieldsIter = headerFieldsSet.iterator();

        while (hearerFieldsIter.hasNext()) {
            String headerFieldKey = hearerFieldsIter.next();

            if ("Set-Cookie".equalsIgnoreCase(headerFieldKey)) {
                List<String> headerFieldValue = headerFields.get(headerFieldKey);
                for (String headerValue : headerFieldValue) {
                    String[] fields = headerValue.split(";\\s*");
                    for (int j = 0; j < fields.length; j++) {
                         if (fields[j].indexOf('=') > 0) {
                            String[] f = fields[j].split("=");
                            if ("Expires".equalsIgnoreCase(f[0])) {                       
                                System.out.println("Expires:" + f[1]);
                            }
                            else if ("Max-Age".equalsIgnoreCase(f[0])) {                 
                                System.out.println("Max-Age:" + f[1]);
                            }else if ("sid".equalsIgnoreCase(f[0])) {  //獲取sid   
                                System.out.println("sid:" + f[1]);
                            }
                       }
                    }
                }
          }
        }
    }
  }

運行以下

2.1.2 攜帶sid調用其它接口

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class RequestOtherAPI {

public static void main(String[] args) throws Exception {
        URL url = new URL("http://dss.liveqing.com:10080/live/list");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");     
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
      //這裏傳遞上一步得到sid
        conn.setRequestProperty("Cookie","sid=s%3Ark-TEuVtm.WnWoXuDY%2FldJuEc64I6TXjd0Fq1eqByEd4ng1UwNb2I;");
            DataOutputStream out = new DataOutputStream(conn.getOutputStream()); 
            String content = "start=0&limit=10";
            out.writeBytes(content);
            out.flush(); 
            out.close();
            conn.connect();
            StringBuffer sbf = new StringBuffer();
            InputStream is = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            System.out.println(sbf.toString());
    }
  }

運行以下

2.2 token方式

  1. 調用登陸接口獲取token,調用時傳遞 onlytoken=true
    如: http://dss.liveqing.com:10080/login?username=admin&password=21232f29757a5a&onlytoken=true

  2. Content-Type:application/x-www-form-urlencoded

  3. 其它接口調用時傳遞附加token入參

代碼示例:Java

2.2.1 獲取token

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetLoginToken {

public static void main(String[] args) throws Exception {
        URL url = new URL("http://localhost:10080/login");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");     
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
        DataOutputStream out = new DataOutputStream(conn.getOutputStream()); 
            String content = "username=admin&password=21232f297a57a5a743894a0e4a801fc3";
            out.writeBytes(content);
            out.flush(); 
            out.close();
            conn.connect();
            StringBuffer sbf = new StringBuffer();
            InputStream is = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            System.out.println(sbf.toString());
    }
  }

運行以下

2.2.2 攜帶token調用其它接口

其餘接口調用時,附加token入參

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class RequestOtherAPIByToken {
public static void main(String[] args) throws Exception {
        URL url = new URL("http://localhost:10080/live/list");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");     
        conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            DataOutputStream out = new DataOutputStream(conn.getOutputStream()); 
            String content = "start=0&limit=10&token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1Mzc3NzExNTAsInB3IjoiMjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzMiLCJ0bSI6MTUzNzY4NDc1MCwidW4iOiJhZG1pbiJ9.b1U-R-_HVKV9reWRD50327B1ztUqs3gowUGi_lDzlmU";
            out.writeBytes(content);
            out.flush(); 
            out.close();
            conn.connect();
            StringBuffer sbf = new StringBuffer();
            InputStream is = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
               sbf.append("\r\n");
            }
            reader.close();
            System.out.println(sbf.toString());
    }
}

運行以下

相關文章
相關標籤/搜索