Exemplo n.º 1
0
        public static AkamaiFlowPlayerPluginClipDetails GetAkamaiProgressiveClipDetails_M3U8(int episodeId, int assetId, HttpRequestBase req, System.Security.Principal.IPrincipal thisUser, Progressive progressive, VideoQualityCdnReference? quality = null)
        {
            AkamaiFlowPlayerPluginClipDetails clipDetails = null;

            var offeringId = Helpers.GlobalConfig.offeringId;
            var videoUrl = string.Empty;
            var canPlay = false;
            var countryCode = Helpers.MyUtility.getCountry(req.GetUserHostAddressFromCloudflare()).getCode();
            string uCountryCode = countryCode;

            var context = new IPTV2Entities();
            var offering = context.Offerings.Find(offeringId);

            var episode = context.Episodes.Find(episodeId);
            var asset = context.Assets.Find(assetId);

            var cdnId = 2; // akamai's cdn id
            if (quality != null)
                cdnId = (int)quality;

            if ((episode != null) & (asset != null))
            {
                clipDetails = new AkamaiFlowPlayerPluginClipDetails { EpisodeId = episodeId, AssetId = assetId, UserId = (thisUser.Identity.IsAuthenticated ? thisUser.Identity.Name : null) };

                canPlay = clipDetails.IsFree = (episode.PreviewAssets.FirstOrDefault(a => a.AssetId == asset.AssetId) != null) | (episode.FreeAssets.FirstOrDefault(a => a.AssetId == asset.AssetId) != null);
                // check with anonymous default package
                var packageId = GlobalConfig.AnonymousDefaultPackageId;
                if (!canPlay)
                {
                    canPlay = User.IsAssetEntitled(context, offeringId, packageId, episodeId, assetId, countryCode, RightsType.Online);
                }
                else
                {
                    clipDetails.IsFree = true;
                }

                // check user's access rights
                if (!canPlay && thisUser.Identity.IsAuthenticated)
                {
                    var user = context.Users.FirstOrDefault(u => u.UserId == new System.Guid(thisUser.Identity.Name));
                    if (user != null)
                    {
                        uCountryCode = user.CountryCode;
                        // check access from default logged in user package
                        packageId = Helpers.GlobalConfig.LoggedInDefaultPackageId;
                        canPlay = User.IsAssetEntitled(context, offeringId, packageId, episodeId, assetId, countryCode, RightsType.Online);

                        if (!canPlay)
                        {
                            // check if user has entitlements that can play the video
                            //canPlay = user.CanPlayVideo(offering, episode, asset, RightsType.Online);
                            if (GlobalConfig.IsTVERegionBlockingEnabled)
                            {
                                var userCountryCode = MyUtility.GetCountryCodeViaIp();
                                int GomsSubsidiaryId = 0;
                                if (GlobalConfig.UseGomsSubsidiaryForTVECheck)
                                    GomsSubsidiaryId = ContextHelper.GetGomsSubsidiaryIdOfCountry(context, userCountryCode);
                                else
                                    GomsSubsidiaryId = -1;
                                //var GomsSubsidiaryId = ContextHelper.GetGomsSubsidiaryIdOfCountry(context, userCountryCode);
                                var IncludePackageIds = MyUtility.StringToIntList(GlobalConfig.CanPlayIncludedPackageIds);
                                var ExcludePackageIds = MyUtility.StringToIntList(GlobalConfig.CanPlayExcludedPackageIds);
                                canPlay = user.CanPlayVideo(offering, episode, asset, RightsType.Online, ExcludePackageIds, IncludePackageIds, userCountryCode, GomsSubsidiaryId);
                                if (GlobalConfig.IsTVEIpCheckEnabled)
                                {
                                    try
                                    {
                                        string ip = GlobalConfig.IpWhiteList;
                                        string[] IpAddresses = ip.Split(';');
                                        //if (IpAddresses.Contains(req.GetUserHostAddressFromCloudflare()))
                                        canPlay = user.CanPlayVideo(offering, episode, asset, RightsType.Online);
                                    }
                                    catch (Exception e) { MyUtility.LogException(e, "AkamaiToken canPlay IP Whitelisting"); }
                                }
                            }
                            else
                                canPlay = user.CanPlayVideo(offering, episode, asset, RightsType.Online);
                        }
                        else
                        {
                            clipDetails.IsFree = true;
                        }
                    }
                }

                // get asset URL
                var assetCdn = asset.AssetCdns.FirstOrDefault(a => a.CdnId == cdnId);
                if (assetCdn != null)
                {
                    var hlsPrefixPattern = GlobalConfig.hlsProgressivePrefixPattern;
                    var hlsSuffixPattern = GlobalConfig.hlsProgressiveSuffixPattern;
                    var zeriPrefixPattern = GlobalConfig.zeriProgressivePrefixPattern;
                    var zeriSuffixPattern = GlobalConfig.zeriProgressiveSuffixPattern;
                    var httpPrefixPatternMobile = GlobalConfig.httpProgressivePrefixPatternMobile;
                    bool isIpad = IsIpad(req);
                    bool isIos = IsIos(req);

                    videoUrl = assetCdn.CdnReference.Replace(hlsPrefixPattern, String.Empty).Replace(hlsSuffixPattern, zeriSuffixPattern);

                    var ProgressiveHDBitrate = GlobalConfig.PMDHDBitrate;
                    var ProgressiveHighBitrate = GlobalConfig.PMDHighBitrate;
                    var ProgressiveLowBitrate = GlobalConfig.PMDLowBitrate;
                    try
                    {
                        var mp4capableObj = MyUtility.CheckIfMp4Compatible(req, episode.EpisodeId, "4.4");
                        if (mp4capableObj.UseMp4ForPlayback)
                        {
                            int platform = req.Browser.IsMobileDevice ? (int)Platform.Mobile : (int)Platform.Desktop;
                            var bitrate = context.CountryBitrates.FirstOrDefault(c => String.Compare(uCountryCode, c.CountryCode, true) == 0 && c.Platform == platform);
                            if (bitrate != null)
                            {
                                if (bitrate.ProgressiveHDBitrate != null)
                                    ProgressiveHDBitrate = String.Format("{0}", bitrate.ProgressiveHDBitrate);
                                if (bitrate.ProgressiveHighBitrate != null)
                                    ProgressiveHighBitrate = String.Format("{0}", bitrate.ProgressiveHighBitrate);
                                if (bitrate.ProgressiveLowBitrate != null)
                                    ProgressiveLowBitrate = String.Format("{0}", bitrate.ProgressiveLowBitrate); ;
                            }
                        }
                    }
                    catch (Exception) { }

                    if (progressive == Progressive.High)
                    {
                        //videoUrl = videoUrl.Replace(",150000,300000,500000,800000,1000000,1300000,1500000,", GlobalConfig.PMDHighBitrate); // max 800Kbps
                        //videoUrl = videoUrl.Replace(",300000,500000,800000,1000000,1300000,1500000,", GlobalConfig.PMDHighBitrate); // max 800Kbps
                        if (episode.EpisodeId == GlobalConfig.JDCEpisodeId)
                            videoUrl = ReplaceWithPMDBitRate(videoUrl, ProgressiveHDBitrate);
                        else
                            videoUrl = ReplaceWithPMDBitRate(videoUrl, ProgressiveHighBitrate);
                    }
                    else
                    {
                        //videoUrl = videoUrl.Replace(",150000,300000,500000,800000,1000000,1300000,1500000,", GlobalConfig.PMDLowBitrate); // max 500Kbps
                        //videoUrl = videoUrl.Replace(",300000,500000,800000,1000000,1300000,1500000,", GlobalConfig.PMDLowBitrate); // max 500Kbps
                        videoUrl = ReplaceWithPMDBitRate(videoUrl, ProgressiveLowBitrate);
                    }


                    string ipAddress = string.Empty;
                    if (!req.IsLocal)
                        ipAddress = req.GetUserHostAddressFromCloudflare();

                    int snippetStart = 0;
                    int snippetEnd = 0;

                    if (!canPlay)
                    {
                        if ((asset.SnippetStart != null) && (asset.SnippetEnd != null) && (asset.SnippetEnd > asset.SnippetStart))
                        {
                            snippetStart = Convert.ToInt32(asset.SnippetStart.Value.TotalSeconds);
                            snippetEnd = Convert.ToInt32(asset.SnippetEnd.Value.TotalSeconds);
                        }
                        else
                        {
                            snippetStart = 0;
                            snippetEnd = GlobalConfig.snippetEnd;
                        }
                        clipDetails.PromptToSubscribe = true;
                    }
                    else
                    {
                        clipDetails.PromptToSubscribe = false;
                    }

                    clipDetails.SubClip = (snippetStart + snippetEnd > 0) ? new SubClip { Start = snippetStart, End = snippetEnd } : null;

                    TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(GlobalConfig.AkamaiAddSeconds));
                    double unixTime = ts.TotalSeconds;

                    var tokenConfig = new AkamaiTokenConfig();
                    tokenConfig.TokenAlgorithm = Algorithm.HMACSHA256;
                    tokenConfig.StartTime = Convert.ToUInt32(unixTime);
                    tokenConfig.Window = 300;
                    tokenConfig.Key = Helpers.GlobalConfig.AkamaiTokenKey;
                    tokenConfig.Acl = "/*";
                    tokenConfig.IP = ipAddress;
                    tokenConfig.PreEscapeAcl = false;
                    tokenConfig.IsUrl = false;
                    tokenConfig.SessionID = string.Empty;
                    tokenConfig.Payload = asset.AssetId.ToString() + ((clipDetails.SubClip == null) ? string.Empty : ":" + snippetStart.ToString() + ":" + snippetEnd.ToString());
                    tokenConfig.Salt = string.Empty;
                    tokenConfig.FieldDelimiter = '~';

                    var token = AkamaiTokenGenerator.GenerateToken(tokenConfig);

                    TimeSpan tsPMD = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));

                    if (!String.IsNullOrEmpty(videoUrl))
                        videoUrl = new AkamaiPMDTokenGenerator(videoUrl, (long)GlobalConfig.AkamaiPMDAddSeconds, GlobalConfig.PMDSalt, "", (long)ts.TotalSeconds, "").AuthUrl;
                    //videoUrl += (videoUrl.IndexOf('?') > 0 ? "&" : "?") + "hdnea=" + token;

                    if (isIos) //Apple devices
                        if (clipDetails.SubClip != null) // isPreview
                            videoUrl = String.Empty;

                    if (!canPlay && (progressive == Progressive.High || progressive == Progressive.Low)) // If user is not entitled, and cannot play, remove PMDlink
                        videoUrl = String.Empty;

                    if (!String.IsNullOrEmpty(videoUrl))
                    {
                        if (isIos)
                            videoUrl = String.Format("{0}{1}", httpPrefixPatternMobile, videoUrl);
                        else
                            videoUrl = String.Format("{0}{1}", zeriPrefixPattern, videoUrl);
                    }
                    clipDetails.Url = videoUrl;
                }
            }
            return (clipDetails);
        }
Exemplo n.º 2
0
        public static AkamaiFlowPlayerPluginClipDetails GetAkamaiClipDetails(int episodeId, int assetId, HttpRequestBase req, System.Security.Principal.IPrincipal thisUser, StreamType streamType = StreamType.ADAPTIVE)
        {
            AkamaiFlowPlayerPluginClipDetails clipDetails = new AkamaiFlowPlayerPluginClipDetails();

            var offeringId = Global.OfferingId;
            var videoUrl = string.Empty;
            var countryCode = Global.DefaultCountry;

            var context = new IPTV2Entities();
            var offering = context.Offerings.Find(offeringId);

            var episode = context.Episodes.Find(episodeId);
            var asset = context.Assets.Find(assetId);

            var cdnId = 2; // akamai's cdn id

            if ((episode != null) & (asset != null))
            {
                // get asset URL
                var assetCdn = asset.AssetCdns.FirstOrDefault(a => a.CdnId == cdnId);
                if (assetCdn != null)
                {
                    var hlsPrefixPattern = streamType == StreamType.ADAPTIVE ? Global.hlsPrefixPattern : Global.hlsProgressivePrefixPattern;
                    var hlsSuffixPattern = streamType == StreamType.ADAPTIVE ? Global.hlsSuffixPattern : Global.hlsProgressiveSuffixPattern;
                    var zeriPrefixPattern = streamType == StreamType.ADAPTIVE ? Global.zeriPrefixPattern : Global.zeriProgressivePrefixPattern;
                    var zeriSuffixPattern = streamType == StreamType.ADAPTIVE ? Global.zeriSuffixPattern : Global.zeriProgressiveSuffixPattern;
                    var httpPrefixPatternMobile = streamType == StreamType.ADAPTIVE ? Global.httpPrefixPatternMobile : Global.httpProgressivePrefixPatternMobile;

                    if (req.Browser.IsMobileDevice)
                    {
                        videoUrl = assetCdn.CdnReference.Replace(hlsPrefixPattern, httpPrefixPatternMobile);
                    }
                    else
                    {
                        videoUrl = assetCdn.CdnReference.Replace(hlsPrefixPattern, zeriPrefixPattern).Replace(hlsSuffixPattern, zeriSuffixPattern);
                    }
                    string ipAddress = string.Empty;
                    if (!req.IsLocal)
                        ipAddress = req.UserHostAddress;

                    int snippetStart = 0;
                    int snippetEnd = 0;

                    clipDetails.IsFree = true;
                    clipDetails.EpisodeId = episode.EpisodeId;
                    clipDetails.AssetId = asset.AssetId;
                    TimeSpan ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(15));
                    if (streamType == StreamType.ADAPTIVE)
                    {
                        clipDetails.SubClip = (snippetStart + snippetEnd > 0) ? new SubClip { Start = snippetStart, End = snippetEnd } : null;
                        double unixTime = ts.TotalSeconds;

                        var tokenConfig = new AkamaiTokenConfig();
                        tokenConfig.TokenAlgorithm = Algorithm.HMACSHA256;
                        tokenConfig.StartTime = Convert.ToUInt32(unixTime);
                        tokenConfig.Window = 300;
                        tokenConfig.Key = Global.AkamaiTokenKey;
                        tokenConfig.Acl = "/*";
                        tokenConfig.IP = ipAddress;
                        tokenConfig.PreEscapeAcl = false;
                        tokenConfig.IsUrl = false;
                        tokenConfig.SessionID = string.Empty;
                        tokenConfig.Payload = asset.AssetId.ToString() + ((clipDetails.SubClip == null) ? string.Empty : ":" + snippetStart.ToString() + ":" + snippetEnd.ToString());
                        tokenConfig.Salt = string.Empty;
                        tokenConfig.FieldDelimiter = '~';

                        var token = AkamaiTokenGenerator.GenerateToken(tokenConfig);
                        if (!String.IsNullOrEmpty(videoUrl))
                            videoUrl += (videoUrl.IndexOf('?') > 0 ? "&" : "?") + "hdnea=" + token;
                    }
                    else
                    {
                        switch (streamType)
                        {
                            case StreamType.PLAY_HIGH:
                                videoUrl = MyUtility.ReplaceWithPMDBitRate(videoUrl, Global.PMDHighBitrate);
                                break;
                            case StreamType.PLAY_LOW:
                                videoUrl = MyUtility.ReplaceWithPMDBitRate(videoUrl, Global.PMDLowBitrate);
                                break;
                            case StreamType.PLAY_IN_HD:
                                videoUrl = MyUtility.ReplaceWithPMDBitRate(videoUrl, Global.PMDHDBitrate);
                                break;
                        }
                        if (!req.Browser.IsMobileDevice)
                            videoUrl = new AkamaiPMDTokenGenerator(videoUrl, (long)Global.AkamaiAddSeconds, Global.PMDSalt, "", (long)ts.TotalSeconds, "").AuthUrl;
                        else
                            videoUrl = String.Empty;

                    }
                    clipDetails.Url = videoUrl;
                }
            }
            return (clipDetails);
        }