Recently, our company had a project that required integration with Tencent Cloud Live. Since the stream push URL for Tencent Cloud Live is generated on the client side, don't count on retrieving it via an API call.

Unfortunately, the Tencent Cloud website only provides code examples in Java and PHP, with no C# example available:

I really couldn't figure out where the txTime parameter in their Java code example was coming from. After trying various approaches, I finally deduced that it represents the total number of seconds since 1970.

The rest involved calculating the MD5 hash, concatenating strings, and converting to hexadecimal. These are all straightforward tasks, and writing them in C# wasn't particularly difficult.

After some tinkering, I finally got it working.

using System;
using System.Security.Cryptography;
using System.Text;

namespace TencentCloud
{
    public class Program
    {
        // Any value is fine.
        public const string StreamAppName = "live";
        // Any value is fine.
        public const string StreamName = "mystreamname";
        // Get it from Tencent cloud.
        public const string Key = "yourkeyaaaaaaaaaaaaaaaaaa";
        // Get it from Tencent cloud.
        public const string Domain = "74371.livepush.myqcloud.com";
        public const string PlayDomain = "play.bopodastaging.top";

        public static void Main(string[] args)
        {
            // Stream
            var safeUrl = GetSafeUrl(
                key: Key,
                streamName: StreamName,
                txTime: DateTime.Now + TimeSpan.FromDays(1));

            var streamUrl = $"rtmp://{Domain}/{StreamAppName}/";
            var streamKey = $"{StreamName}?{safeUrl}";

            Console.WriteLine(streamUrl);
            Console.WriteLine(streamKey);

            // Play
            Console.WriteLine($"rtmp://{PlayDomain}/{StreamAppName}/{StreamName}");
            Console.WriteLine($"http://{PlayDomain}/{StreamAppName}/{StreamName}.flv");
            Console.WriteLine($"http://{PlayDomain}/{StreamAppName}/{StreamName}.m3u8");
        }

        private static string GetSafeUrl(string key, string streamName, DateTime txTime)
        {
            var textTime = ConvertToUnixTimestamp(txTime).ToString("X").ToUpper();

            var input = $"{key}{streamName}{textTime}";
            var txSecret = GetMD5(input);

            return $"txSecret={txSecret}&txTime={textTime}";
        }

        public static string GetMD5(string sourceString)
        {
            var data = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(sourceString));
            var sBuilder = new StringBuilder();
            foreach (var c in data)
            {
                sBuilder.Append(c.ToString("x2"));
            }
            return sBuilder.ToString();
        }

        private static int ConvertToUnixTimestamp(DateTime date)
        {
            var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var diff = date.ToUniversalTime() - origin;
            return (int)Math.Floor(diff.TotalSeconds);
        }
    }
}

Runtime effect: