腾讯云直播生成 推流地址 拉流地址


/***
 * 腾讯云 直播
 * 
 *
 */
public class TencentYunVedioUtil {

    public static final String bizid = "54852";
    //推流防盗链的key
    public static final String push_key = "cbdaq2bbfqewrb6a446213ba5db84786e";
    //拉流防盗链的key
    public static final String play_key = "u36VZisasfD1THL21C232LIxT997m9x2e";
    //推流地址 例子  bizid:8888      直播码:8888_test_123    防盗链签名  txSecret 过期时间  txTime
    public static final String pushUrl ="rtmp://54852.livepush.myqcloud.com/live/";
    
    //拉流地址 例子 rtmp  只需要将push 改成play即可
    public static final String playUrl_rmpt ="rtmp://live.test.xx.cn/live/";
    public static final String playUrl_flv ="rtmp://live.test.xx.cn/live/";
    public static final String playUrl_hls ="rtmp://live.test.xx.cn/live/";
    
    
    
    
    /**
     * 生成防盗链签名
     * @param key :防盗链key
     * @param stream_id :直播码(或称作流ID)
     * @param txTime :过期时间 16进制的unix时间戳
     * @return
     */
    public static String genSign(String key,String stream_id,String txTime) {
        return     MD5Encode.getMD5Str(key+stream_id+txTime);
    }
    
    /**
     * 生成直播码
     *        直播码 也叫房间号,推荐用随机数字或者用户ID,注意一个合法的直播码需要拼接 BIZID 前缀。
     * @param bizid 
     * @param userId 用户id
     * @return
     */
    public static String genLiveCode(String bizid,String userId) {
        return bizid+"_"+userId;
    }
    
    
    
    /**
     * 将传入的时间转换为 16进制
     * @param date
     * @return
     */
    public static String to16Hex(Date date) {
        Long ab = date.getTime()/1000;
        String a = Long.toHexString(ab);
        return a.toUpperCase();
    }
    
    /**
     * 将当前时间加1天
     */
    public static Date addOneDay() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 1);
        Date date = cal.getTime();
        return date;
    }
    /**
     * 校验是否在线
     * @param urls 拉流地址
     * @return
     */
    public static boolean isPush(String urls) {
        try {
            URL url = new URL(urls);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(1000);
            conn.setReadTimeout(1000);
            conn.setRequestMethod("GET");
            conn.connect();
            return conn.getResponseCode() == 200;
        } catch (Exception e) {
        }
        return false;
    }
    
    
    
    
    /**
     * 生成推流全路径地址
     * @param pushUrl 推流地址 头部
     * @param stream_id 直播码  调用该方法genLiveCode() 生成  
     * @param push_key 推流鉴权
     * @return
     */
    public static String createPushUrl(String pushUrlHead,String stream_id,String push_key) {
        //过期时间 16进制的unix时间戳
        String txTime = to16Hex(addOneDay());
        StringBuffer sb = new StringBuffer();
        sb.append(pushUrlHead)
            .append(stream_id)
            .append("?")
            .append("txSecret=")
            .append(genSign(push_key, stream_id,txTime))
            .append("&")
            .append("txTime=")
            .append(txTime);
        return sb.toString();
    }
    
    

    /**
     * 生成拉流全路径地址
     * @param playUrlHead  拉流的头地址
     * @param stream_id  直播码  调用该方法genLiveCode() 生成  
     * @param play_key  拉流鉴权
     * @param end 如 .flv结尾
     * @return
     */
    public static String createPlayUrl(String playUrlHead,String stream_id
            ,String play_key,String end) {
        //过期时间 16进制的unix时间戳
        String txTime = to16Hex(addOneDay());
        StringBuffer sb = new StringBuffer();
        sb.append(playUrlHead)
        .append(stream_id)
        .append("?")
        .append("txSecret=")
        .append(genSign(play_key, stream_id,txTime))
        .append("&")
        .append("txTime=")
        .append(txTime)
        .append(end);
        return sb.toString();
    }
    
    
    
    
    
    
    
    public static void main(String[] args) {

        //生成推流地址
        System.out.println(createPushUrl(pushUrl,"test",push_key));

       //生成拉流地址
        System.out.println(createPlayUrl(playUrl_rmpt,"test",play_key,""));
    }
    
    
}

 

 

 

 

 

 

 


public class MD5Encode {

    private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
    
    public static String getMD5Str(String str) {
        String resStr = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            resStr = byteArrayToHexString(md.digest(str.getBytes()));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return resStr;
    }
    
    /**
     * 转换字节数组为16进制字串
     * 
     * @param b
     *            字节数组
     * @return 16进制字串
     */

    private static String byteArrayToHexString(byte[] b) {
        StringBuffer resSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resSb.append(byteToHexString(b[i]));
        }
        return resSb.toString();
    }

    

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n = 256 + n;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }
    
}

你可能感兴趣的:(腾讯云直播生成 推流地址 拉流地址)