public void HandleRequest(string url, HybridDSP.Net.HTTP.HTTPServerRequest request, HybridDSP.Net.HTTP.HTTPServerResponse response) { lock (sync) { if (Thread.CurrentThread.Name != null) Thread.CurrentThread.Name = "RTMPProxy"; RTMP rtmp = null; try { NameValueCollection paramsHash = System.Web.HttpUtility.ParseQueryString(new Uri(url).Query); Logger.Log("RTMP Request Parameters:"); foreach (var param in paramsHash.AllKeys) Logger.Log(string.Format("{0}={1}", param, paramsHash[param])); Link link = new Link(); if (paramsHash["rtmpurl"] != null) link = Link.FromRtmpUrl(new Uri(paramsHash["rtmpurl"])); if (paramsHash["app"] != null) link.app = paramsHash["app"]; if (paramsHash["tcUrl"] != null) link.tcUrl = paramsHash["tcUrl"]; if (paramsHash["hostname"] != null) link.hostname = paramsHash["hostname"]; if (paramsHash["port"] != null) link.port = int.Parse(paramsHash["port"]); if (paramsHash["playpath"] != null) link.playpath = paramsHash["playpath"]; if (paramsHash["subscribepath"] != null) link.subscribepath = paramsHash["subscribepath"]; if (paramsHash["pageurl"] != null) link.pageUrl = paramsHash["pageurl"]; if (paramsHash["swfurl"] != null) link.swfUrl = paramsHash["swfurl"]; if (paramsHash["swfsize"] != null) link.SWFSize = int.Parse(paramsHash["swfsize"]); if (paramsHash["swfhash"] != null) link.SWFHash = Link.ArrayFromHexString(paramsHash["swfhash"]); if (paramsHash["swfVfy"] != null) { link.swfUrl = paramsHash["swfVfy"]; link.swfVerify = true; } if (paramsHash["live"] != null) bool.TryParse(paramsHash["live"], out link.bLiveStream); if (paramsHash["auth"] != null) link.auth = paramsHash["auth"]; if (paramsHash["token"] != null) link.token = paramsHash["token"]; if (paramsHash["conn"] != null) link.extras = Link.ParseAMF(paramsHash["conn"]); if (link.tcUrl != null && link.tcUrl.ToLower().StartsWith("rtmpe")) link.protocol = Protocol.RTMPE; if (link.swfVerify) link.GetSwf(); rtmp = new RTMP() { Link = link }; ConnectAndGetStream(rtmp, request, response, ref invalidHeader); } finally { if (rtmp != null) rtmp.Close(); } Logger.Log("Request finished."); } }
public static Link FromRtmpUrl(Uri url) { Link link = new Link(); try { link.protocol = (Protocol)Enum.Parse(typeof(Protocol), url.Scheme.ToUpper()); } catch { Logger.Log(string.Format("Error parsing protocol from url ({0}), setting default: RTMP", url.Scheme)); link.protocol = Protocol.RTMP; } link.hostname = url.Host; link.port = url.Port > 0 ? url.Port : 1935; /* parse application * * rtmp://host[:port]/app[/appinstance][/...] * application = app[/appinstance] */ string parsePlayPathFrom = ""; if (url.PathAndQuery.Contains("slist=")) { /* whatever it is, the '?' and slist= means we need to use everything as app and parse playpath from slist= */ link.app = url.PathAndQuery.Substring(1); parsePlayPathFrom = url.PathAndQuery.Substring(url.PathAndQuery.IndexOf("slist=")); } else if (url.PathAndQuery.StartsWith("/ondemand/")) { /* app = ondemand/foobar, only pass app=ondemand */ link.app = "ondemand"; parsePlayPathFrom = url.PathAndQuery.Substring(9); } else { /* app!=ondemand, so app is app[/appinstance] */ int slash2Index = url.PathAndQuery.IndexOf('/', 1); int slash3Index = slash2Index >= 0 ? url.PathAndQuery.IndexOf('/', slash2Index + 1) : -1; if (url.PathAndQuery.Contains("mp4:")) slash3Index = url.PathAndQuery.IndexOf("mp4:"); if (slash3Index >= 0) { link.app = url.PathAndQuery.Substring(1, slash3Index - 1).Trim('/'); parsePlayPathFrom = url.PathAndQuery.Substring(slash3Index); } else if (slash2Index >= 0) { link.app = url.PathAndQuery.Substring(1, slash2Index - 1).Trim('/'); parsePlayPathFrom = url.PathAndQuery.Substring(slash2Index); } else { link.app = url.PathAndQuery.Trim('/'); } } if (parsePlayPathFrom.StartsWith("/")) parsePlayPathFrom = parsePlayPathFrom.Substring(1); /* * Extracts playpath from RTMP URL. playpath is the file part of the * URL, i.e. the part that comes after rtmp://host:port/app/ * * Returns the stream name in a format understood by FMS. The name is * the playpath part of the URL with formatting depending on the stream * type: * * mp4 streams: prepend "mp4:", remove extension * mp3 streams: prepend "mp3:", remove extension * flv streams: remove extension (Only remove .flv from rtmp URL, not slist params) */ // use slist parameter, if there is one int nPos = parsePlayPathFrom.IndexOf("slist="); if (nPos >= 0) { parsePlayPathFrom = parsePlayPathFrom.Substring(nPos + 6); } else { if (parsePlayPathFrom.EndsWith(".flv")) parsePlayPathFrom = parsePlayPathFrom.Substring(0, parsePlayPathFrom.Length - 4); } if (parsePlayPathFrom.EndsWith(".mp4")) { parsePlayPathFrom = parsePlayPathFrom.Substring(0, parsePlayPathFrom.Length - 4); if (!parsePlayPathFrom.StartsWith("mp4:")) parsePlayPathFrom = "mp4:" + parsePlayPathFrom; } link.playpath = HttpUtility.UrlDecode(parsePlayPathFrom); link.tcUrl = string.Format("{0}://{1}:{2}/{3}", url.Scheme, link.hostname, link.port, link.app); return link; }