using System; using System.Collections.Generic; using System.Text; using System.Data; using WeiSha.Common; using Song.Entities; using WeiSha.Data; using Song.ServiceInterfaces; using System.Data.Common; using pili_sdk.pili; using pili_sdk; namespace Song.ServiceImpls { public class LiveCom : ILive { //设置项的前缀 string prefix = "Qiniuyun_"; /// /// 初始化相关参数 /// public void Initialization() { pili_sdk.Pili.Initialization(this.GetAccessKey, this.GetSecretKey, this.GetLiveSpace, "v1"); } public bool Test(string accesskey, string secretkey, string hubname) { return Pili.API().Test(accesskey, secretkey, hubname, "v1"); } #region 设置 /// /// 设置密钥 /// /// /// public void SetupKey(string accessKey, string secretKey) { if(!string.IsNullOrWhiteSpace(accessKey) && accessKey.Trim()!="") Business.Do().Save(prefix + "AccessKey", accessKey, false); if (!string.IsNullOrWhiteSpace(secretKey) && secretKey.Trim() != "") Business.Do().Save(prefix + "SecretKey", secretKey, false); } /// /// 设置直播空间的名称 /// /// public void SetupLiveSpace(string pace) { if (!string.IsNullOrWhiteSpace(pace) && pace.Trim() != "") Business.Do().Save(prefix + "pace", pace, false); } /// /// 设置推流地址 /// /// public void SetupPublish(string domain) { if (!string.IsNullOrWhiteSpace(domain)) Business.Do().Save(prefix + "Publish", domain, false); } /// /// 设置直播域名 /// /// /// /// public void SetupLive(string rtmp, string hls, string hdl) { if (!string.IsNullOrWhiteSpace(rtmp)) Business.Do().Save(prefix + "RTMP", rtmp, false); if (!string.IsNullOrWhiteSpace(hls)) Business.Do().Save(prefix + "HLS", hls, false); if (!string.IsNullOrWhiteSpace(hdl)) Business.Do().Save(prefix + "HDL", hdl, false); } /// /// 设置封面CDN域名 /// /// public void SetupSnapshot(string domain) { if (!string.IsNullOrWhiteSpace(domain)) Business.Do().Save(prefix + "Snapshot", domain, false); } /// /// 设置点播域名 /// /// public void SetupVod(string domain) { if (!string.IsNullOrWhiteSpace(domain)) Business.Do().Save(prefix + "Vod", domain, false); } /// /// 设置协议,是http还是https /// /// public void SetupProtocol(string protocol) { if (!string.IsNullOrWhiteSpace(protocol)) Business.Do().Save(prefix + "Protocol", protocol, false); } #endregion #region 获取参数 /// /// 直播平台的密钥 /// public string GetAccessKey { get { return Business.Do().GetValue(prefix + "AccessKey"); } } /// /// 直播平台的密钥 /// public string GetSecretKey { get { return Business.Do().GetValue(prefix + "SecretKey"); } } /// /// 直播空间名称 /// public string GetLiveSpace { get { return Business.Do().GetValue(prefix + "pace"); } } /// /// rtmp播放域 /// public string GetRTMP { get { return Business.Do().GetValue(prefix + "RTMP"); } } /// /// hls播放域名 /// public string GetHLS { get { return Business.Do().GetValue(prefix + "HLS"); } } /// /// hdl播放域名 /// public string GetHDL { get { return Business.Do().GetValue(prefix + "HDL"); } } /// /// 访问协议,http或https /// public string GetProtocol { get { string protocol= Business.Do().GetValue(prefix + "Protocol"); return string.IsNullOrWhiteSpace(protocol) ? "http" : protocol; } } /// /// 推流的地址 /// /// 直播流的名称 public string GetPublish(string streamname) { pili_sdk.pili.Stream stream = Pili.API().GetForTitle(streamname); if (stream == null) return string.Empty; string url = string.Format("https://{0}/{1}/{2}",stream.PublishRtmpHost,stream.HubName,stream.Title); return url; } /// /// 直播时实截图的域名 /// public string GetSnapshot { get { return Business.Do().GetValue(prefix + "Snapshot"); } } /// /// 点播的域名 /// public string GetVod { get { return Business.Do().GetValue(prefix + "Vod"); } } #endregion #region 管理直播流 /// /// 创建直播流 /// /// public pili_sdk.pili.Stream StreamCreat(string name) { Stream stream = null; try { stream = Pili.API().Create(name); return stream; } catch (Exception e) { throw e; } } public pili_sdk.pili.Stream StreamCreat() { return this.StreamCreat(string.Empty); } /// /// 直播流列表 /// /// public pili_sdk.pili.StreamList StreamList(string prefix, long count) { return StreamList(prefix, null, count); } /// /// 直播流列表 /// /// 直播流名称前缀 /// 是否正在直播中 /// 取几条记录 /// public pili_sdk.pili.StreamList StreamList(string prefix, bool? living, long count) { string marker = null; // optional long limit = count; // optional string titlePrefix = prefix; // optional try { StreamList streamList = Pili.API().List(marker, limit, titlePrefix, living); IList list = streamList.Streams; foreach (Stream s in list) { // access the stream } return streamList; } catch (Exception ex) { throw ex; } } /// /// 获取直播流 /// /// /// public pili_sdk.pili.Stream StreamGet(string name) { return Pili.API().GetForTitle(name); } /// /// 删除直播流 /// /// /// public bool StreamDelete(string name) { Stream stream = this.StreamGet(name); if (stream == null) return false; // try { string res = Pili.API().Delete(stream); if ("No Content".Equals(res)) { return true; } return false; } catch (Exception ex) { throw ex; } } #endregion } }