Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加RTC和实时音视频手动转录 #214

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ _ReSharper*/
/src/packages
/packages
/bin
/src/.vs/Qiniu/v16/Server/sqlite3
6 changes: 6 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ExpandedNodes": [
""
],
"PreviewInSolutionExplorer": false
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
4 changes: 2 additions & 2 deletions src/Qiniu.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2002
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.156
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Qiniu", "Qiniu\Qiniu.csproj", "{2F5B0328-DE8B-4B53-A500-3077E340A51B}"
EndProject
Expand Down
79 changes: 79 additions & 0 deletions src/Qiniu/Pili/PiliManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Qiniu.Http;
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qiniu.Pili
{
/// <summary>
/// 直播云服务端
/// </summary>
public class PiliManager
{
private const string PILI_API_HOST = "http://pili.qiniuapi.com";
private Auth _auth;
private HttpManager _httpManager;
private readonly string _hub;
private readonly string _encodedStreamTitle;

public PiliManager(Mac mac, string hub, string streamTitle)
{
_auth = new Auth(mac);
_httpManager = new HttpManager();
_hub = hub;
_encodedStreamTitle = Base64.UrlSafeBase64Encode(streamTitle);
}

private string saveAsEntry()
{
return string.Format("{0}/v2/hubs/{1}/streams/{2}/saveas", PILI_API_HOST, _hub, _encodedStreamTitle);
}

/// <summary>
/// 录制直播回放
/// </summary>
/// <param name="fname">保存的文件名</param>
/// <param name="start">要保存的直播的起始时间</param>
/// <param name="end">要保存的直播的结束时间</param>
/// <param name="format">保存的文件格式</param>
/// <param name="pipeline">数据处理的私有队列</param>
/// <param name="notify">保存成功回调通知地址</param>
/// <param name="expireDays">更改ts文件的过期时间</param>
/// <returns></returns>
public SaveAsResult SaveAs(string fname = "", long start = 0, long end = 0, string format = "", string pipeline = "", string notify = "", int expireDays = 0)
{
SaveAsRequest request = new SaveAsRequest(fname, start, end, format, pipeline, notify, expireDays);

SaveAsResult result = new SaveAsResult();

try
{
string url = saveAsEntry();
string body = request.ToJsonStr();
string token = _auth.CreateStreamManageToken(url, body);

HttpResult hr = _httpManager.PostJson(url, body, token);
result.Shadow(hr);
}
catch (Exception ex)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("[{0}] [saveas] Error: ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff"));
Exception e = ex;
while (e != null)
{
sb.Append(e.Message + " ");
e = e.InnerException;
}
sb.AppendLine();

result.RefCode = (int)HttpCode.INVALID_ARGUMENT;
result.RefText += sb.ToString();
}

return result;
}
}
}
36 changes: 36 additions & 0 deletions src/Qiniu/Pili/SaveAsInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qiniu.Pili
{
/// <summary>
/// 录制直播回放-消息内容结果
/// </summary>
public class SaveAsInfo
{
/// <summary>
/// 代码 含义 说明
/// 200 success 成功(OK)
/// 612 stream not found
/// 619 no data 没有直播数据
/// </summary>
public int Code { get; set; }

/// <summary>
/// 错误消息(状态码非OK时)
/// </summary>
public string Error { get; set; }

/// <summary>
/// 保存后在存储空间里的文件名
/// </summary>
public string FName { get; set; }

/// <summary>
/// 持久化异步处理任务ID,异步模式才会返回该字段,可以通过该字段查询转码进度
/// </summary>
public string PersistentID { get; set; }
}
}
94 changes: 94 additions & 0 deletions src/Qiniu/Pili/SaveAsRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qiniu.Pili
{
/// <summary>
/// 录制直播回放-请求
/// </summary>
public class SaveAsRequest
{
/// <summary>
/// 保存的文件名,不指定系统会随机生成
/// </summary>
[JsonProperty("fname")]
public string FileName { get; set; }
/// <summary>
/// 整数,Unix 时间戳,要保存的直播的起始时间,不指定或 0 值表示从第一次直播开始
/// </summary>
[JsonProperty("start")]
public long StartTimestamp { get; set; }
/// <summary>
/// 整数,Unix 时间戳,要保存的直播的结束时间,不指定或 0 值表示当前时间
/// </summary>
[JsonProperty("end")]
public long EndTimestamp { get; set; }
/// <summary>
/// 保存的文件格式,默认为m3u8,如果指定其他格式,则保存动作为异步模式。详细信息可以参考 转码 的api
/// </summary>
[JsonProperty("format")]
public string Format { get; set; }
/// <summary>
/// 异步模式时,数据处理的私有队列,不指定则使用公共队列
/// </summary>
[JsonProperty("pipeline")]
public string Pipeline { get; set; }
/// <summary>
/// 异步模式时,保存成功回调通知地址,不指定则不通知
/// </summary>
[JsonProperty("notify")]
public string Notify { get; set; }
/// <summary>
/// 更改ts文件的过期时间,默认为永久保存。-1 表示不更改ts文件的生命周期,正值表示修改ts文件的生命周期为expireDays
/// </summary>
[JsonProperty("expireDays")]
public int ExpireDays { get; set; }

/// <summary>
/// 初始化(所有成员为空,需要后续赋值)
/// </summary>
public SaveAsRequest()
{
FileName = "";
StartTimestamp = 0;
EndTimestamp = 0;
Format = "";
Pipeline = "";
Notify = "";
ExpireDays = 0;
}

/// <summary>
/// 初始化所有成员
/// </summary>
/// <param name="fname">保存的文件名</param>
/// <param name="start">要保存的直播的起始时间</param>
/// <param name="end">要保存的直播的结束时间</param>
/// <param name="format">保存的文件格式</param>
/// <param name="pipeline">数据处理的私有队列</param>
/// <param name="notify">保存成功回调通知地址</param>
/// <param name="expireDays">更改ts文件的过期时间</param>
public SaveAsRequest(string fname, long start, long end, string format, string pipeline, string notify, int expireDays)
{
FileName = fname;
StartTimestamp = start;
EndTimestamp = end;
Format = format;
Pipeline = pipeline;
Notify = notify;
ExpireDays = expireDays;
}

/// <summary>
/// 转换到JSON字符串
/// </summary>
/// <returns>请求内容的JSON字符串</returns>
public string ToJsonStr()
{
return JsonConvert.SerializeObject(this);
}
}
}
90 changes: 90 additions & 0 deletions src/Qiniu/Pili/SaveAsResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Newtonsoft.Json;
using Qiniu.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qiniu.Pili
{
/// <summary>
/// 录制直播回放-结果
/// </summary>
public class SaveAsResult : HttpResult
{
/// <summary>
/// 获取带宽信息
/// </summary>
public SaveAsInfo Result
{
get
{
SaveAsInfo info = null;
if ((Code == (int)HttpCode.OK) && (!string.IsNullOrEmpty(Text)))
{
info = JsonConvert.DeserializeObject<SaveAsInfo>(Text);
}
return info;
}
}

/// <summary>
/// 转换为易读字符串格式
/// </summary>
/// <returns>便于打印和阅读的字符串</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();

sb.AppendFormat("code:{0}\n", Code);

sb.AppendLine();

if (Result != null)
{
sb.AppendLine("result:");
sb.AppendFormat("code:{0}\n", Result.Code);
if (!string.IsNullOrEmpty(Result.Error))
{
sb.AppendFormat("error:{0}\n", Result.Error);
}
if (!string.IsNullOrEmpty(Result.FName))
{
sb.AppendFormat("fname:{0}\n", Result.FName);
}
if (!string.IsNullOrEmpty(Result.PersistentID))
{
sb.AppendFormat("persistentID:{0}\n", Result.PersistentID);
}
}
else
{
if (!string.IsNullOrEmpty(Text))
{
sb.AppendLine("text:");
sb.AppendLine(Text);
}
}
sb.AppendLine();

sb.AppendFormat("ref-code:{0}\n", RefCode);

if (!string.IsNullOrEmpty(RefText))
{
sb.AppendLine("ref-text:");
sb.AppendLine(RefText);
}

if (RefInfo != null)
{
sb.AppendFormat("ref-info:\n");
foreach (var d in RefInfo)
{
sb.AppendLine(string.Format("{0}:{1}", d.Key, d.Value));
}
}

return sb.ToString();
}
}
}
7 changes: 7 additions & 0 deletions src/Qiniu/Qiniu.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Pili\PiliManager.cs" />
<Compile Include="Pili\SaveAsInfo.cs" />
<Compile Include="Pili\SaveAsRequest.cs" />
<Compile Include="Pili\SaveAsResult.cs" />
<Compile Include="QiniuCSharpSDK.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RTC\RoomTokenRequest.cs" />
<Compile Include="RTC\RTCManager.cs" />
<Compile Include="Storage\Config.cs" />
<Compile Include="Storage\FetchInfo.cs" />
<Compile Include="Storage\FetchResult.cs" />
Expand Down Expand Up @@ -156,6 +162,7 @@
<None Include="packages.config" />
<None Include="QiniuCSharpSDK.snk" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
Expand Down
37 changes: 37 additions & 0 deletions src/Qiniu/RTC/RTCManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qiniu.RTC
{
/// <summary>
/// 实时音视频服务端
/// </summary>
public class RTCManager
{
private const string RTC_API_HOST = "http://rtc.qiniuapi.com";
private Auth _auth;

public RTCManager(Mac mac)
{
_auth = new Auth(mac);
}

/// <summary>
/// RoomToken签发服务
/// </summary>
/// <param name="appId">房间所属账号的AppID</param>
/// <param name="roomName">房间名称</param>
/// <param name="userId">请求加入房间的用户ID</param>
/// <param name="expireAt">鉴权的有效时间</param>
/// <param name="permission">该用户的房间管理权限</param>
/// <returns>RoomToken</returns>
public string GetRoomToken(string appId, string roomName, string userId, long expireAt, string permission)
{
RoomTokenRequest request = new RoomTokenRequest(appId, roomName, userId, expireAt, permission);
return _auth.CreateUploadToken(request.ToJsonStr());
}
}
}
Loading