EmbeddedChannelManager.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.IO;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using WingRtmpService.ChannelManagers.Messages;
  5. using WingRtmpService.ChannelManagers.Netease.Helper;
  6. using WingServerCommon.Config;
  7. using WingServerCommon.Config.Parameters;
  8. using WingServerCommon.Log;
  9. namespace WingRtmpService.ChannelManagers.Embedded
  10. {
  11. class EmbeddedChannelManager : IChannelManager
  12. {
  13. private string _url = "";
  14. public EmbeddedChannelManager()
  15. {
  16. _url = EnvironmentConfigs.Rtmp.RTMPPushUrl;
  17. }
  18. private string GetPushUrl(string streamName)
  19. {
  20. var rtmpPrefix = _url.Replace("http://", "").Replace("https://", "");
  21. return $"rtmp://{rtmpPrefix.TrimEnd('/')}/{streamName}";
  22. }
  23. private string GetPullUrl(string streamName)
  24. {
  25. var rtmpPrefix = _url.Replace("http://", "").Replace("https://", "");
  26. return $"rtmp://{rtmpPrefix.TrimEnd('/')}/{streamName}";
  27. }
  28. private string GetHlsPullUrl(string streamName)
  29. {
  30. return $"{_url.TrimEnd('/')}/{streamName}/play.m3u8";
  31. }
  32. private string GetHttpPullUrl(string streamName)
  33. {
  34. return $"{_url.TrimEnd('/')}/{streamName}.flv";
  35. }
  36. public ICreateChannelResult CreateChannel(ChannelParameter channel)
  37. {
  38. if (channel.Parameter.ContainsKey(ChannelConstant.CreateChannelKey))
  39. {
  40. var streamName = channel.Parameter[ChannelConstant.CreateChannelKey];
  41. var channelInfo = new ChannelInfo
  42. {
  43. Cid = channel.Parameter[ChannelConstant.CreateChannelKey],
  44. PushUrl = GetPushUrl(streamName),
  45. RtmpPullUrl = GetPullUrl(streamName),
  46. HlsPullUrl = GetHlsPullUrl(streamName),
  47. HttpPullUrl = GetHttpPullUrl(streamName),
  48. };
  49. Logger.WriteLineInfo($"RtmpService EmbeddedChannelManager CreateChannel, streamName:{streamName}");
  50. return new CreateChannelResult
  51. {
  52. Code = (int)SendResultStatus.Success,
  53. ChannelInfo = channelInfo
  54. };
  55. }
  56. Logger.WriteLineError("RtmpService EmbeddedChannelManager CreateChannel failed, Create channel parameter is empty.");
  57. throw new InvalidDataException();
  58. }
  59. public Task<IDeleteChannelResult> DeleteChannelAsync(ChannelParameter channel)
  60. {
  61. return Task.FromResult<IDeleteChannelResult>(new DeleteChannelResult { Code = (int)SendResultStatus.Success });
  62. }
  63. }
  64. }