阿里直播SDK,直播推流地址和播流地址生成
最近,公司要搞屏幕远程控制,包含了屏幕直播。一开始公司准备自己搭服务器的,后来 说服务器转码,前端还要插件的,麻烦之类的。然后就变成了用阿里的SDK 一条龙服务。
不过 我自己在前期调研阶段 ,也自己实现了直播推送H.264 到服务器,服务器转发到手机解码播放。延迟1s左右。这个 在我下一篇博客哪里会详细叙说。
阿里直播SDK推流地址生成
首先 来看一个推流地址样式
rtmp://liveDomain/appName/streamName?auth_key=time-rand-0-md5hash
记得注意 我加粗的字体,下面上代码
/** * @param appName 控制台上面的app名称 * @param streamName 流的名称 * @param time 十位数的时间戳 * @param rand 随机数,建议使用UUID(不能包含中划线“-”,例如: 477b3bbc253f467b8def6711128c7bec 格式) * @param key 鉴权key * @param liveDomain 推流域名 * @return 推流的地址 */
public static String CreatePushUrl(String appName, String streamName, String time,String rand, String key, String liveDomain) {
Objects.requireNonNull(appName);
Objects.requireNonNull(streamName);
Objects.requireNonNull(time);
Objects.requireNonNull(key);
Objects.requireNonNull(liveDomain);
String strpush = "/" + appName + "/" + streamName + "-" + time + "-"+rand+"-0-" + key;
String pushurl = "rtmp://video-center.alivecdn.com/" + appName + "/" + streamName + "?vhost=" + liveDomain + "&auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strpush);
return pushurl;
}
阿里直播SDK播流地址生成
/** * @param appName 控制台上面的app名称 * @param streamName 流的名称 * @param time 十位数的时间戳 * @param rand 这是用来标识的 否则同一个时间戳 生成的地址总是相同的 随机数,建议使用UUID(不能包含中划线“-”,例如: 477b3bbc253f467b8def6711128c7bec 格式) * @param key 鉴权key * @param liveDomain 推流域名 * @param templateId 无用 传入null就行 * @return 播放流的地址 默认是flv 也可以更改此代码 */
public static String GetPlayUrl(String appName, String streamName, String time,String rand, String key, String liveDomain, String templateId) {
String strviewrtmp1 = null;
String strviewflv1 = null;
String strviewm3u81 = null;
String rtmpurl1 = null;
String flvurl1 = null;
String m3u8url1 = null;
Objects.requireNonNull(appName);
Objects.requireNonNull(streamName);
Objects.requireNonNull(time);
Objects.requireNonNull(key);
Objects.requireNonNull(liveDomain);
if (templateId == null) {
strviewrtmp1 = "/" + appName + "/" + streamName + "-" + time + "-"+rand+"-0-" + key;
strviewflv1 = "/" + appName + "/" + streamName + ".flv-" + time + "-"+rand+"-0-" + key;
strviewm3u81 = "/" + appName + "/" + streamName + ".m3u8-" + time + "-"+rand+"-0-" + key;
rtmpurl1 = "rtmp://" + liveDomain + "/" + appName + "/" + streamName + "?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewrtmp1);
flvurl1 = "http://" + liveDomain + "/" + appName + "/" + streamName + ".flv?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewflv1);
m3u8url1 = "http://" + liveDomain + "/" + appName + "/" + streamName + ".m3u8?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewm3u81);
} else {
strviewrtmp1 = "/" + appName + "/" + streamName + "_" + templateId + "-" + time + "-"+rand+"-0-" + key;
strviewflv1 = "/" + appName + "/" + streamName + "_" + templateId + ".flv-" + time + "-"+rand+"-0-" + key;
strviewm3u81 = "/" + appName + "/" + streamName + "_" + templateId + ".m3u8-" + time + "-"+rand+"-0-" + key;
rtmpurl1 = "rtmp://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + "?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewrtmp1);
flvurl1 = "http://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + ".flv?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewflv1);
m3u8url1 = "http://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + ".m3u8?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewm3u81);
}
Log.d(">>>>>>>>>>>>>>>>>>", rtmpurl1);
Log.d(">>>>>>>>>>>>>>>>>>", flvurl1);
Log.d(">>>>>>>>>>>>>>>>>>", m3u8url1);
return flvurl1;
}
代码很全面
现在 附上完整代码。
md5加密算法 网上复制来的
public class Md5Utils {
public static String getMD5(String str) {
try {
// 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5");
// 计算md5函数
md.update(str.getBytes());
// digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
// BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
String md5=new BigInteger(1, md.digest()).toString(16);
//BigInteger会把0省略掉,需补全至32位
return fillMD5(md5);
} catch (Exception e) {
throw new RuntimeException("MD5加密错误:"+e.getMessage(),e);
}
}
private static String fillMD5(String md5){
return md5.length()==32?md5:fillMD5("0"+md5);
}
}
阿里直播SDK推流播流工具
package com.alivc.live.pusher.demo.utils;
import android.hardware.input.InputManager;
import android.util.Log;
import java.util.Dictionary;
import java.util.Objects;
/** * 创建时间 2018/11/12 * * @author plani */
public class PlayAndPushUtils {
/** * @param appName 控制台上面的app名称 * @param streamName 流的名称 * @param time 十位数的时间戳 * @param rand 随机数,建议使用UUID(不能包含中划线“-”,例如: 477b3bbc253f467b8def6711128c7bec 格式) * @param key 鉴权key * @param liveDomain 推流域名 * @return 推流的地址 */
public static String CreatePushUrl(String appName, String streamName, String time,String rand, String key, String liveDomain) {
Objects.requireNonNull(appName);
Objects.requireNonNull(streamName);
Objects.requireNonNull(time);
Objects.requireNonNull(key);
Objects.requireNonNull(liveDomain);
String strpush = "/" + appName + "/" + streamName + "-" + time + "-"+rand+"-0-" + key;
String pushurl = "rtmp://video-center.alivecdn.com/" + appName + "/" + streamName + "?vhost=" + liveDomain + "&auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strpush);
return pushurl;
}
/** * @param appName 控制台上面的app名称 * @param streamName 流的名称 * @param time 十位数的时间戳 * @param rand 这是用来标识的 否则同一个时间戳 生成的地址总是相同的 随机数,建议使用UUID(不能包含中划线“-”,例如: 477b3bbc253f467b8def6711128c7bec 格式) * @param key 鉴权key * @param liveDomain 推流域名 * @param templateId 无用 传入null就行 * @return 播放流的地址 默认是flv 也可以更改此代码 */
public static String GetPlayUrl(String appName, String streamName, String time,String rand, String key, String liveDomain, String templateId) {
String strviewrtmp1 = null;
String strviewflv1 = null;
String strviewm3u81 = null;
String rtmpurl1 = null;
String flvurl1 = null;
String m3u8url1 = null;
Objects.requireNonNull(appName);
Objects.requireNonNull(streamName);
Objects.requireNonNull(time);
Objects.requireNonNull(key);
Objects.requireNonNull(liveDomain);
if (templateId == null) {
strviewrtmp1 = "/" + appName + "/" + streamName + "-" + time + "-"+rand+"-0-" + key;
strviewflv1 = "/" + appName + "/" + streamName + ".flv-" + time + "-"+rand+"-0-" + key;
strviewm3u81 = "/" + appName + "/" + streamName + ".m3u8-" + time + "-"+rand+"-0-" + key;
rtmpurl1 = "rtmp://" + liveDomain + "/" + appName + "/" + streamName + "?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewrtmp1);
flvurl1 = "http://" + liveDomain + "/" + appName + "/" + streamName + ".flv?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewflv1);
m3u8url1 = "http://" + liveDomain + "/" + appName + "/" + streamName + ".m3u8?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewm3u81);
} else {
strviewrtmp1 = "/" + appName + "/" + streamName + "_" + templateId + "-" + time + "-"+rand+"-0-" + key;
strviewflv1 = "/" + appName + "/" + streamName + "_" + templateId + ".flv-" + time + "-"+rand+"-0-" + key;
strviewm3u81 = "/" + appName + "/" + streamName + "_" + templateId + ".m3u8-" + time + "-"+rand+"-0-" + key;
rtmpurl1 = "rtmp://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + "?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewrtmp1);
flvurl1 = "http://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + ".flv?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewflv1);
m3u8url1 = "http://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + ".m3u8?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewm3u81);
}
Log.d(">>>>>>>>>>>>>>>>>>", rtmpurl1);
Log.d(">>>>>>>>>>>>>>>>>>", flvurl1);
Log.d(">>>>>>>>>>>>>>>>>>", m3u8url1);
return flvurl1;
}
}
如果有不懂的,可以关注我的公众号 “知我饭否” 向我留言。我也会每天更新一些文章,有兴趣的可以扫描下方的二维码。
还没有评论,来说两句吧...