예제 #1
0
        private string FillPlaybackOptions(VideoInfo video, AMFArray renditions)
        {
            SortedList <string, string> options = new SortedList <string, string>(new StreamComparer());

            for (int i = 0; i < renditions.Count; i++)
            {
                AMFObject rendition    = renditions.GetObject(i);
                int       encodingRate = rendition.GetIntProperty("encodingRate");
                string    nm           = String.Format("{0}x{1} | {2} kbps",
                                                       rendition.GetIntProperty("frameWidth"), rendition.GetIntProperty("frameHeight"),
                                                       encodingRate / 1000);
                string url = HttpUtility.UrlDecode(rendition.GetStringProperty("defaultURL"));
                if (url.StartsWith("rtmp"))
                {
                    string auth = String.Empty;
                    if (url.Contains('?'))
                    {
                        auth = '?' + url.Split('?')[1];
                    }
                    string[] parts = url.Split('&');

                    string rtmp     = parts[0] + auth;
                    string playpath = parts[1].Split('?')[0] + auth;

                    url = new MPUrlSourceFilter.RtmpUrl(rtmp)
                    {
                        PlayPath  = playpath,
                        SwfUrl    = "http://admin.brightcove.com/viewer/us20111207.0737/connection/ExternalConnection_2.swf",
                        SwfVerify = true
                    }.ToString();
                }
                if (!options.ContainsKey(nm))
                {
                    options.Add(nm, url);
                }
            }

            video.PlaybackOptions = new Dictionary <string, string>();
            foreach (KeyValuePair <string, string> key in options)
            {
                video.PlaybackOptions.Add(key.Key, key.Value);
            }

            return(StreamComparer.GetBestPlaybackUrl(video.PlaybackOptions, StreamQualityPref, AutoSelectStream));
        }
예제 #2
0
        public override string GetVideoUrl(VideoInfo video)
        {
            string   result     = string.Empty;
            string   videoId    = string.Empty;
            AMFArray renditions = null;

            if (RequestType.Equals(BrightCoveType.FindMediaById))
            {
                /*
                 * sample AMF input (expressed as JSON)
                 * ["466faf0229239e70a6df8fe66fc04f25f50e6fa7",48543011001,1401332946001,15364602001]
                 */
                videoId = video.VideoUrl;

                object[] values = new object[4];
                values[0] = hashValue;
                values[1] = Convert.ToDouble(playerId);
                values[2] = Convert.ToDouble(videoId);
                values[3] = Convert.ToDouble(publisherId);

                AMFSerializer serializer = new AMFSerializer();
                AMFObject     response   = AMFObject.GetResponse(brightcoveUrl, serializer.Serialize2("com.brightcove.player.runtime.PlayerMediaFacade.findMediaById", values, AMFVersion.AMF3));
                //Log.Debug("AMF Response: {0}", response.ToString());

                renditions = response.GetArray("renditions");
            }
            else
            {
                /*
                 * sample AMF input for ViewerExperience (expressed in pretty-printed JSON for clarity - captured using Flashbug in Firebug)
                 * [
                 *  {"targetURI":"com.brightcove.experience.ExperienceRuntimeFacade.getDataForExperience",
                 *   "responseURI":"/1",
                 *   "length":"461 B",
                 *   "data":["82c0aa70e540000aa934812f3573fd475d131a63",
                 *      {"contentOverrides":[
                 *          {"contentType":0,
                 *           "target":"videoPlayer",
                 *           "featuredId":null,
                 *           "contentId":1411956407001,
                 *           "featuredRefId":null,
                 *           "contentIds":null,
                 *           "contentRefId":null,
                 *           "contentRefIds":null,
                 *           "__traits":
                 *              {"type":"com.brightcove.experience.ContentOverride",
                 *               "members":["contentType","target","featuredId","contentId","featuredRefId","contentIds","contentRefId","contentRefIds"],
                 *               "count":8,
                 *               "externalizable":false,
                 *               "dynamic":false}}],
                 *        "TTLToken":"",
                 *        "deliveryType":null,
                 *        "playerKey":"AQ~~,AAAABDk7A3E~,xYAUE9lVY9-LlLNVmcdybcRZ8v_nIl00",
                 *        "URL":"http://ww3.tvo.org/video/171480/safe-houses",
                 *        "experienceId":756015080001,
                 *        "__traits":
                 *          {"type":"com.brightcove.experience.ViewerExperienceRequest",
                 *           "members":["contentOverrides","TTLToken","deliveryType","playerKey","URL","experienceId"],
                 *           "count":6,
                 *           "externalizable":false,
                 *           "dynamic":false}}]}]
                 */
                videoId = getBrightCoveVideoIdForViewerExperienceRequest(video.VideoUrl);
                if (!string.IsNullOrEmpty(videoId))
                {
                    // content override
                    AMFObject contentOverride = new AMFObject(@"com.brightcove.experience.ContentOverride");
                    contentOverride.Add("contentId", videoId);
                    contentOverride.Add("contentIds", null);
                    contentOverride.Add("contentRefId", null);
                    contentOverride.Add("contentRefIds", null);
                    contentOverride.Add("contentType", 0);
                    contentOverride.Add("featuredId", double.NaN);
                    contentOverride.Add("featuredRefId", null);
                    contentOverride.Add("target", "videoPlayer");
                    AMFArray contentOverrideArray = new AMFArray();
                    contentOverrideArray.Add(contentOverride);

                    // viewer experience request
                    AMFObject viewerExperenceRequest = new AMFObject(@"com.brightcove.experience.ViewerExperienceRequest");
                    viewerExperenceRequest.Add("contentOverrides", contentOverrideArray);
                    viewerExperenceRequest.Add("experienceId", Convert.ToDouble(playerId));
                    viewerExperenceRequest.Add("deliveryType", null);
                    viewerExperenceRequest.Add("playerKey", string.Empty);
                    viewerExperenceRequest.Add("URL", video.VideoUrl);
                    viewerExperenceRequest.Add("TTLToken", string.Empty);

                    //Log.Debug("About to make AMF call: {0}", viewerExperenceRequest.ToString());
                    AMFSerializer serializer = new AMFSerializer();
                    AMFObject     response   = AMFObject.GetResponse(brightcoveUrl, serializer.Serialize(viewerExperenceRequest, "com.brightcove.experience.ExperienceRuntimeFacade.getDataForExperience", hashValue));
                    //Log.Debug("AMF Response: {0}", response.ToString());

                    renditions = response.GetArray("programmedContent").GetObject("videoPlayer").GetObject("mediaDTO").GetArray("renditions");
                }
            }

            if (renditions == null)
            {
                return(result);
            }

            video.PlaybackOptions = new Dictionary <string, string>();
            // keep track of bitrates and URLs
            Dictionary <string, string> urlsDictionary = new Dictionary <string, string>();

            for (int i = 0; i < renditions.Count; i++)
            {
                AMFObject rendition = renditions.GetObject(i);
                int       bitrate   = rendition.GetIntProperty("encodingRate");
                string    optionKey = String.Format("{0}x{1} {2}K",
                                                    rendition.GetIntProperty("frameWidth"),
                                                    rendition.GetIntProperty("frameHeight"),
                                                    bitrate / 1024);
                string url = HttpUtility.UrlDecode(rendition.GetStringProperty("defaultURL"));
                Log.Debug("Option: {0} URL: {1}", optionKey, url);

                // typical rtmp url from "defaultURL" is:
                // rtmp://brightcove.fcod.llnwd.net/a500/d20/&mp4:media/1351824783/1351824783_1411974095001_109856X-640x360-1500k.mp4&1330020000000&1b163106256f448754aff72969869479
                //
                // following rtmpdump style command works
                // rtmpdump
                // --app 'a500/d20/?videoId=1411956407001&lineUpId=&pubId=18140038001&playerId=756015080001&playerTag=&affiliateId='
                // --auth 'mp4:media/1351824783/1351824783_1411974097001_109856X-400x224-300k.mp4&1330027200000&23498dd8f4659cd07ad1b6c4ee5a013d'
                // --rtmp 'rtmp://brightcove.fcod.llnwd.net/a500/d20/?videoId=1411956407001&lineUpId=&pubId=18140038001&playerId=756015080001&playerTag=&affiliateId='
                // --flv 'TVO_org_-_Safe_as_Houses.flv'
                // --playpath 'mp4:media/1351824783/1351824783_1411974097001_109856X-400x224-300k.mp4'

                Match rtmpUrlMatch = rtmpUrlRegex.Match(url);

                if (rtmpUrlMatch.Success)
                {
                    string leftover = rtmpUrlMatch.Groups["leftover"].Value;
                    string query    = string.Format(@"videoId={0}&lineUpId=&pubId={1}&playerId={2}&playerTag=&affiliateId=",
                                                    videoId, publisherId, playerId);

                    int questionMarkPosition = leftover.IndexOf('?');
                    // use existing query (if present) in the new query string
                    if (questionMarkPosition != -1)
                    {
                        query = string.Format(@"{0}{1}", leftover.Substring(questionMarkPosition + 1, leftover.Length - questionMarkPosition - 1), query);
                    }

                    string app     = String.Format("{0}?{1}", rtmpUrlMatch.Groups["app"], query);
                    string auth    = leftover;
                    string rtmpUrl = String.Format("{0}://{1}/{2}?{3}",
                                                   rtmpUrlMatch.Groups["rtmp"].Value,
                                                   rtmpUrlMatch.Groups["host"].Value,
                                                   rtmpUrlMatch.Groups["app"].Value,
                                                   query);
                    int    ampersandPosition = leftover.IndexOf('&');
                    string playPath          = ampersandPosition == -1 ? leftover : leftover.Substring(0, ampersandPosition);
                    Log.Debug(@"rtmpUrl: {0}, PlayPath: {1}, App: {2}, Auth: {3}", rtmpUrl, playPath, app, auth);
                    urlsDictionary.Add(optionKey, new RtmpUrl(rtmpUrl)
                    {
                        PlayPath = playPath,
                        App      = app,
                        Auth     = auth
                    }.ToString());
                }
            }

            // sort the URLs ascending by bitrate
            foreach (var item in urlsDictionary.OrderBy(u => u.Key, new BitrateComparer()))
            {
                video.PlaybackOptions.Add(item.Key, item.Value);
                // return last URL as the default (will be the highest bitrate)
                result = item.Value;
            }
            return(result);
        }