阿里直播SDK,直播推流地址和播流地址生成

刺骨的言语ヽ痛彻心扉 2022-04-17 03:13 795阅读 0赞

最近,公司要搞屏幕远程控制,包含了屏幕直播。一开始公司准备自己搭服务器的,后来 说服务器转码,前端还要插件的,麻烦之类的。然后就变成了用阿里的SDK 一条龙服务。

不过 我自己在前期调研阶段 ,也自己实现了直播推送H.264 到服务器,服务器转发到手机解码播放。延迟1s左右。这个 在我下一篇博客哪里会详细叙说。

阿里直播SDK推流地址生成

首先 来看一个推流地址样式
rtmp://liveDomain/appName/streamName?auth_key=time-rand-0-md5hash
记得注意 我加粗的字体,下面上代码

  1. /** * @param appName 控制台上面的app名称 * @param streamName 流的名称 * @param time 十位数的时间戳 * @param rand 随机数,建议使用UUID(不能包含中划线“-”,例如: 477b3bbc253f467b8def6711128c7bec 格式) * @param key 鉴权key * @param liveDomain 推流域名 * @return 推流的地址 */
  2. public static String CreatePushUrl(String appName, String streamName, String time,String rand, String key, String liveDomain) {
  3. Objects.requireNonNull(appName);
  4. Objects.requireNonNull(streamName);
  5. Objects.requireNonNull(time);
  6. Objects.requireNonNull(key);
  7. Objects.requireNonNull(liveDomain);
  8. String strpush = "/" + appName + "/" + streamName + "-" + time + "-"+rand+"-0-" + key;
  9. String pushurl = "rtmp://video-center.alivecdn.com/" + appName + "/" + streamName + "?vhost=" + liveDomain + "&auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strpush);
  10. return pushurl;
  11. }

阿里直播SDK播流地址生成

  1. /** * @param appName 控制台上面的app名称 * @param streamName 流的名称 * @param time 十位数的时间戳 * @param rand 这是用来标识的 否则同一个时间戳 生成的地址总是相同的 随机数,建议使用UUID(不能包含中划线“-”,例如: 477b3bbc253f467b8def6711128c7bec 格式) * @param key 鉴权key * @param liveDomain 推流域名 * @param templateId 无用 传入null就行 * @return 播放流的地址 默认是flv 也可以更改此代码 */
  2. public static String GetPlayUrl(String appName, String streamName, String time,String rand, String key, String liveDomain, String templateId) {
  3. String strviewrtmp1 = null;
  4. String strviewflv1 = null;
  5. String strviewm3u81 = null;
  6. String rtmpurl1 = null;
  7. String flvurl1 = null;
  8. String m3u8url1 = null;
  9. Objects.requireNonNull(appName);
  10. Objects.requireNonNull(streamName);
  11. Objects.requireNonNull(time);
  12. Objects.requireNonNull(key);
  13. Objects.requireNonNull(liveDomain);
  14. if (templateId == null) {
  15. strviewrtmp1 = "/" + appName + "/" + streamName + "-" + time + "-"+rand+"-0-" + key;
  16. strviewflv1 = "/" + appName + "/" + streamName + ".flv-" + time + "-"+rand+"-0-" + key;
  17. strviewm3u81 = "/" + appName + "/" + streamName + ".m3u8-" + time + "-"+rand+"-0-" + key;
  18. rtmpurl1 = "rtmp://" + liveDomain + "/" + appName + "/" + streamName + "?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewrtmp1);
  19. flvurl1 = "http://" + liveDomain + "/" + appName + "/" + streamName + ".flv?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewflv1);
  20. m3u8url1 = "http://" + liveDomain + "/" + appName + "/" + streamName + ".m3u8?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewm3u81);
  21. } else {
  22. strviewrtmp1 = "/" + appName + "/" + streamName + "_" + templateId + "-" + time + "-"+rand+"-0-" + key;
  23. strviewflv1 = "/" + appName + "/" + streamName + "_" + templateId + ".flv-" + time + "-"+rand+"-0-" + key;
  24. strviewm3u81 = "/" + appName + "/" + streamName + "_" + templateId + ".m3u8-" + time + "-"+rand+"-0-" + key;
  25. rtmpurl1 = "rtmp://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + "?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewrtmp1);
  26. flvurl1 = "http://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + ".flv?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewflv1);
  27. m3u8url1 = "http://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + ".m3u8?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewm3u81);
  28. }
  29. Log.d(">>>>>>>>>>>>>>>>>>", rtmpurl1);
  30. Log.d(">>>>>>>>>>>>>>>>>>", flvurl1);
  31. Log.d(">>>>>>>>>>>>>>>>>>", m3u8url1);
  32. return flvurl1;
  33. }

代码很全面
现在 附上完整代码。

md5加密算法 网上复制来的

  1. public class Md5Utils {
  2. public static String getMD5(String str) {
  3. try {
  4. // 生成一个MD5加密计算摘要
  5. MessageDigest md = MessageDigest.getInstance("MD5");
  6. // 计算md5函数
  7. md.update(str.getBytes());
  8. // digest()最后确定返回md5 hash值,返回值为8为字符串。因为md5 hash值是16位的hex值,实际上就是8位的字符
  9. // BigInteger函数则将8位的字符串转换成16位hex值,用字符串来表示;得到字符串形式的hash值
  10. String md5=new BigInteger(1, md.digest()).toString(16);
  11. //BigInteger会把0省略掉,需补全至32位
  12. return fillMD5(md5);
  13. } catch (Exception e) {
  14. throw new RuntimeException("MD5加密错误:"+e.getMessage(),e);
  15. }
  16. }
  17. private static String fillMD5(String md5){
  18. return md5.length()==32?md5:fillMD5("0"+md5);
  19. }
  20. }

阿里直播SDK推流播流工具

  1. package com.alivc.live.pusher.demo.utils;
  2. import android.hardware.input.InputManager;
  3. import android.util.Log;
  4. import java.util.Dictionary;
  5. import java.util.Objects;
  6. /** * 创建时间 2018/11/12 * * @author plani */
  7. public class PlayAndPushUtils {
  8. /** * @param appName 控制台上面的app名称 * @param streamName 流的名称 * @param time 十位数的时间戳 * @param rand 随机数,建议使用UUID(不能包含中划线“-”,例如: 477b3bbc253f467b8def6711128c7bec 格式) * @param key 鉴权key * @param liveDomain 推流域名 * @return 推流的地址 */
  9. public static String CreatePushUrl(String appName, String streamName, String time,String rand, String key, String liveDomain) {
  10. Objects.requireNonNull(appName);
  11. Objects.requireNonNull(streamName);
  12. Objects.requireNonNull(time);
  13. Objects.requireNonNull(key);
  14. Objects.requireNonNull(liveDomain);
  15. String strpush = "/" + appName + "/" + streamName + "-" + time + "-"+rand+"-0-" + key;
  16. String pushurl = "rtmp://video-center.alivecdn.com/" + appName + "/" + streamName + "?vhost=" + liveDomain + "&auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strpush);
  17. return pushurl;
  18. }
  19. /** * @param appName 控制台上面的app名称 * @param streamName 流的名称 * @param time 十位数的时间戳 * @param rand 这是用来标识的 否则同一个时间戳 生成的地址总是相同的 随机数,建议使用UUID(不能包含中划线“-”,例如: 477b3bbc253f467b8def6711128c7bec 格式) * @param key 鉴权key * @param liveDomain 推流域名 * @param templateId 无用 传入null就行 * @return 播放流的地址 默认是flv 也可以更改此代码 */
  20. public static String GetPlayUrl(String appName, String streamName, String time,String rand, String key, String liveDomain, String templateId) {
  21. String strviewrtmp1 = null;
  22. String strviewflv1 = null;
  23. String strviewm3u81 = null;
  24. String rtmpurl1 = null;
  25. String flvurl1 = null;
  26. String m3u8url1 = null;
  27. Objects.requireNonNull(appName);
  28. Objects.requireNonNull(streamName);
  29. Objects.requireNonNull(time);
  30. Objects.requireNonNull(key);
  31. Objects.requireNonNull(liveDomain);
  32. if (templateId == null) {
  33. strviewrtmp1 = "/" + appName + "/" + streamName + "-" + time + "-"+rand+"-0-" + key;
  34. strviewflv1 = "/" + appName + "/" + streamName + ".flv-" + time + "-"+rand+"-0-" + key;
  35. strviewm3u81 = "/" + appName + "/" + streamName + ".m3u8-" + time + "-"+rand+"-0-" + key;
  36. rtmpurl1 = "rtmp://" + liveDomain + "/" + appName + "/" + streamName + "?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewrtmp1);
  37. flvurl1 = "http://" + liveDomain + "/" + appName + "/" + streamName + ".flv?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewflv1);
  38. m3u8url1 = "http://" + liveDomain + "/" + appName + "/" + streamName + ".m3u8?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewm3u81);
  39. } else {
  40. strviewrtmp1 = "/" + appName + "/" + streamName + "_" + templateId + "-" + time + "-"+rand+"-0-" + key;
  41. strviewflv1 = "/" + appName + "/" + streamName + "_" + templateId + ".flv-" + time + "-"+rand+"-0-" + key;
  42. strviewm3u81 = "/" + appName + "/" + streamName + "_" + templateId + ".m3u8-" + time + "-"+rand+"-0-" + key;
  43. rtmpurl1 = "rtmp://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + "?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewrtmp1);
  44. flvurl1 = "http://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + ".flv?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewflv1);
  45. m3u8url1 = "http://" + liveDomain + "/" + appName + "/" + streamName + "_" + templateId + ".m3u8?auth_key=" + time + "-"+rand+"-0-" + Md5Utils.getMD5(strviewm3u81);
  46. }
  47. Log.d(">>>>>>>>>>>>>>>>>>", rtmpurl1);
  48. Log.d(">>>>>>>>>>>>>>>>>>", flvurl1);
  49. Log.d(">>>>>>>>>>>>>>>>>>", m3u8url1);
  50. return flvurl1;
  51. }
  52. }

如果有不懂的,可以关注我的公众号 “知我饭否” 向我留言。我也会每天更新一些文章,有兴趣的可以扫描下方的二维码

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,795人围观)

还没有评论,来说两句吧...

相关阅读

    相关 直播与拉简概

    推流:将直播内容推送至服务器的过程 拉流:为服务器已有直播内容,用指定地址进行拉取的过程 ![在这里插入图片描述][14676bc156ca43ed832d1672e6bb

    相关 Swift 直播项目(、拉

    直播项目中关键的是推流和拉流,对于推流推流拉流大厂当然可以自己开发,但是像我一样在公司独立开发的,是没有那个时间和精力去独立开发一套推流拉流的(不是没时间和精力,是因为没那个能