public static string DoPlayBackWithStreamingEndpoint(PlayerType typeplayer, string Urlstr, CloudMediaContext context,
            IAsset myasset = null, bool DoNotRewriteURL = false, string filter = null, AssetProtectionType keytype = AssetProtectionType.None,
            AzureMediaPlayerFormats formatamp = AzureMediaPlayerFormats.Auto,
            AzureMediaPlayerTechnologies technology = AzureMediaPlayerTechnologies.Auto, bool launchbrowser = true, bool UISelectSEFiltersAndProtocols = true)
        {
            string FullPlayBackLink = null;

            if (!string.IsNullOrEmpty(Urlstr))
            {
                IStreamingEndpoint choosenSE = AssetInfo.GetBestStreamingEndpoint(context);

                // Let's ask for SE if several SEs or Custom Host Names or Filters
                if (!DoNotRewriteURL)
                {
                    if (
                        (myasset != null && UISelectSEFiltersAndProtocols)
                        &&
                        (context.StreamingEndpoints.Count() > 1 || (context.StreamingEndpoints.FirstOrDefault() != null && context.StreamingEndpoints.FirstOrDefault().CustomHostNames.Count > 0) || context.Filters.Count() > 0 || (myasset.AssetFilters.Count() > 0))
                        )

                    {
                        var form = new ChooseStreamingEndpoint(context, myasset, filter, typeplayer);
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            Urlstr = AssetInfo.RW(new Uri(Urlstr), form.SelectStreamingEndpoint, form.SelectedFilter, form.ReturnHttps, form.ReturnSelectCustomHostName, form.ReturnStreamingProtocol, form.ReturnHLSAudioTrackName).ToString();
                            choosenSE = form.SelectStreamingEndpoint;
                        }
                        else
                        {
                            return string.Empty;
                        }
                    }
                    else // no UI but let's rw for filter
                    {
                        if (typeplayer == PlayerType.DASHIFRefPlayer || typeplayer == PlayerType.DASHLiveAzure)
                        {
                            Urlstr = AssetInfo.RW(new Uri(Urlstr), choosenSE, filter, false, null, AMSOutputProtocols.Dash).ToString();
                        }
                        else
                        {
                            Urlstr = RW(Urlstr, choosenSE, filter);
                        }
                    }
                }

                DynamicEncryption.TokenResult tokenresult = new DynamicEncryption.TokenResult();

                if (myasset != null)
                {
                    keytype = AssetInfo.GetAssetProtection(myasset, context); // let's save the protection scheme (use by azure player): AES, PlayReady, Widevine or PlayReadyAndWidevine

                    if (DynamicEncryption.IsAssetHasAuthorizationPolicyWithToken(myasset, context)) // dynamic encryption with token
                    {
                        // user wants perhaps to play an asset with a token, so let's try to generate it
                        switch (typeplayer)
                        {
                            case PlayerType.SilverlightPlayReadyToken:
                                tokenresult = DynamicEncryption.GetTestToken(myasset, context, ContentKeyType.CommonEncryption);
                                if (!string.IsNullOrEmpty(tokenresult.TokenString))
                                {
                                    tokenresult.TokenString = HttpUtility.UrlEncode(Constants.Bearer + tokenresult.TokenString);
                                    keytype = AssetProtectionType.PlayReady;
                                }
                                break;

                            case PlayerType.FlashAESToken:
                                tokenresult = DynamicEncryption.GetTestToken(myasset, context, ContentKeyType.EnvelopeEncryption);
                                if (!string.IsNullOrEmpty(tokenresult.TokenString))
                                {
                                    tokenresult.TokenString = HttpUtility.UrlEncode(Constants.Bearer + tokenresult.TokenString);
                                    keytype = AssetProtectionType.AES;
                                }
                                break;

                            case PlayerType.AzureMediaPlayer:
                            case PlayerType.AzureMediaPlayerFrame:
                            case PlayerType.CustomPlayer:
                                switch (keytype)
                                {
                                    case AssetProtectionType.None:
                                        break;
                                    case AssetProtectionType.AES:
                                    case AssetProtectionType.PlayReady:
                                    case AssetProtectionType.Widevine:
                                    case AssetProtectionType.PlayReadyAndWidevine:
                                        tokenresult = DynamicEncryption.GetTestToken(myasset, context, displayUI: true);
                                        if (!string.IsNullOrEmpty(tokenresult.TokenString))
                                        {
                                            tokenresult.TokenString = HttpUtility.UrlEncode(Constants.Bearer + tokenresult.TokenString);
                                            // if the user selecteed an CENC key, let's assume that the content is protected with PlayReady, otherwise AES
                                            //keytype = (tokenresult.ContentKeyType == ContentKeyType.CommonEncryption) ? AssetProtectionType.PlayReady : AssetProtectionType.AES;
                                        }
                                        break;
                                }
                                break;

                            default:
                                // no token enabled player
                                break;
                        }
                    }

                }

                // let's launch the player
                switch (typeplayer)
                {
                    case PlayerType.AzureMediaPlayer:
                    case PlayerType.AzureMediaPlayerFrame:
                        string playerurl = typeplayer == PlayerType.AzureMediaPlayer ?
                            "http://aka.ms/azuremediaplayer?url={0}"
                            : "http://amsplayer.azurewebsites.net/azuremediaplayer/azuremediaplayer_iframe.html?autoplay=true&url={0}";
                        string protectionsyntax = "&protection={0}";
                        string tokensyntax = "&token={0}";
                        string formatsyntax = "&format={0}";
                        string techsyntax = "&tech={0}";

                        if (keytype != AssetProtectionType.None)
                        {
                            switch (keytype)
                            {
                                case AssetProtectionType.AES:
                                    playerurl += string.Format(protectionsyntax, "aes");
                                    break;

                                case AssetProtectionType.PlayReady:
                                    playerurl += string.Format(protectionsyntax, "playready");
                                    break;

                                case AssetProtectionType.Widevine:
                                    playerurl += string.Format(protectionsyntax, "widevine");
                                    break;

                                case AssetProtectionType.PlayReadyAndWidevine:
                                    playerurl += string.Format(protectionsyntax, "drm");
                                    break;

                                default:
                                    break;
                            }
                            if (!string.IsNullOrEmpty(tokenresult.TokenString))
                            {
                                playerurl += string.Format(tokensyntax, tokenresult.TokenString);
                            }
                        }

                        if (formatamp != AzureMediaPlayerFormats.Auto)
                        {
                            switch (formatamp)
                            {
                                case AzureMediaPlayerFormats.Dash:
                                    playerurl += string.Format(formatsyntax, "dash");
                                    break;

                                case AzureMediaPlayerFormats.Smooth:
                                    playerurl += string.Format(formatsyntax, "smooth");
                                    break;

                                case AzureMediaPlayerFormats.HLS:
                                    playerurl += string.Format(formatsyntax, "hls");
                                    break;

                                case AzureMediaPlayerFormats.VideoMP4:
                                    playerurl += string.Format(formatsyntax, "video/mp4");
                                    break;

                                default: // auto or other
                                    break;
                            }
                            if (tokenresult.TokenString != null)
                            {
                                playerurl += string.Format(tokensyntax, tokenresult);
                            }
                        }
                        else // format auto. If 0 Reserved Unit, and asset is smooth, let's force to smooth (player cannot get the dash stream for example)
                        {
                            if (choosenSE.ScaleUnits == 0 && myasset != null && myasset.AssetType == AssetType.SmoothStreaming)
                                playerurl += string.Format(formatsyntax, "smooth");
                        }

                        if (technology != AzureMediaPlayerTechnologies.Auto)
                        {
                            switch (technology)
                            {
                                case AzureMediaPlayerTechnologies.Flash:
                                    playerurl += string.Format(techsyntax, "flash");
                                    break;

                                case AzureMediaPlayerTechnologies.JavaScript:
                                    playerurl += string.Format(techsyntax, "js");
                                    break;

                                case AzureMediaPlayerTechnologies.NativeHTML5:
                                    playerurl += string.Format(techsyntax, "html5");
                                    break;

                                case AzureMediaPlayerTechnologies.Silverlight:
                                    playerurl += string.Format(techsyntax, "silverlight");
                                    break;

                                default: // auto or other
                                    break;
                            }
                        }

                        FullPlayBackLink = string.Format(playerurl, HttpUtility.UrlEncode(Urlstr));

                        break;

                    case PlayerType.SilverlightMonitoring:
                        FullPlayBackLink = @"http://smf.cloudapp.net/healthmonitor?Autoplay=true&url=" + HttpUtility.UrlEncode(Urlstr);
                        break;

                    case PlayerType.SilverlightPlayReadyToken:
                        FullPlayBackLink = string.Format(@"http://sltoken.azurewebsites.net/#/!?url={0}&token={1}", HttpUtility.UrlEncode(Urlstr), tokenresult);
                        break;

                    case PlayerType.DASHIFRefPlayer:
                        if (!Urlstr.Contains(string.Format(AssetInfo.format_url, AssetInfo.format_dash)))
                        {
                            Urlstr = AssetInfo.AddParameterToUrlString(Urlstr, string.Format(AssetInfo.format_url, AssetInfo.format_dash));
                        }
                        FullPlayBackLink = @"http://dashif.org/reference/players/javascript/1.4.0/samples/dash-if-reference-player/index.html?url=" + Urlstr;
                        break;

                    case PlayerType.DASHAzurePage:
                        FullPlayBackLink = @"http://amsplayer.azurewebsites.net/player.html?player=silverlight&format=mpeg-dash&url=" + HttpUtility.UrlEncode(Urlstr);
                        break;

                    case PlayerType.DASHLiveAzure:
                        if (!Urlstr.Contains(string.Format(AssetInfo.format_url, AssetInfo.format_dash)))
                        {
                            Urlstr = AssetInfo.AddParameterToUrlString(Urlstr, string.Format(AssetInfo.format_url, AssetInfo.format_dash));
                        }
                        FullPlayBackLink = @"http://dashplayer.azurewebsites.net?url=" + Urlstr;
                        break;

                    case PlayerType.FlashAzurePage:
                        FullPlayBackLink = @"http://amsplayer.azurewebsites.net/player.html?player=flash&format=smooth&url=" + HttpUtility.UrlEncode(Urlstr);
                        break;

                    case PlayerType.FlashAESToken:
                        FullPlayBackLink = string.Format(@"http://aestoken.azurewebsites.net/#/!?url={0}&token={1}", HttpUtility.UrlEncode(Urlstr), tokenresult);
                        break;

                    case PlayerType.MP4AzurePage:
                        FullPlayBackLink = string.Format(@"http://amsplayer.azurewebsites.net/player.html?player=html5&format=mp4&url={0}&mp4url={0}", HttpUtility.UrlEncode(Urlstr));
                        break;

                    case PlayerType.CustomPlayer:
                        string myurl = Properties.Settings.Default.CustomPlayerUrl;
                        FullPlayBackLink = myurl.Replace(Constants.NameconvManifestURL, HttpUtility.UrlEncode(Urlstr)).Replace(Constants.NameconvToken, tokenresult.TokenString);
                        break;
                }

                if (FullPlayBackLink != null && launchbrowser) Process.Start(FullPlayBackLink);
            }

            return FullPlayBackLink;
        }
Esempio n. 2
0
        public static string DoPlayBackWithStreamingEndpoint(PlayerType typeplayer, string Urlstr, CloudMediaContext context, Mainform mainForm,
            IAsset myasset = null, bool DoNotRewriteURL = false, string filter = null, AssetProtectionType keytype = AssetProtectionType.None,
            AzureMediaPlayerFormats formatamp = AzureMediaPlayerFormats.Auto,
            AzureMediaPlayerTechnologies technology = AzureMediaPlayerTechnologies.Auto, bool launchbrowser = true, bool UISelectSEFiltersAndProtocols = true)
        {
            string FullPlayBackLink = null;

            if (!string.IsNullOrEmpty(Urlstr))
            {
                IStreamingEndpoint choosenSE = AssetInfo.GetBestStreamingEndpoint(context);
                string selectedBrowser = string.Empty;

                // Let's ask for SE if several SEs or Custom Host Names or Filters
                if (!DoNotRewriteURL)
                {
                    if (
                        (myasset != null && UISelectSEFiltersAndProtocols)
                        &&
                        (context.StreamingEndpoints.Count() > 1 || (context.StreamingEndpoints.FirstOrDefault() != null && context.StreamingEndpoints.FirstOrDefault().CustomHostNames.Count > 0) || context.Filters.Count() > 0 || (myasset.AssetFilters.Count() > 0))
                        )
                    {
                        var form = new ChooseStreamingEndpoint(context, myasset, Urlstr, filter, typeplayer, true);
                        if (form.ShowDialog() == DialogResult.OK)
                        {
                            Urlstr = AssetInfo.RW(new Uri(Urlstr), form.SelectStreamingEndpoint, form.SelectedFilters, form.ReturnHttps, form.ReturnSelectCustomHostName, form.ReturnStreamingProtocol, form.ReturnHLSAudioTrackName, form.ReturnHLSNoAudioOnlyMode).ToString();
                            choosenSE = form.SelectStreamingEndpoint;
                            selectedBrowser = form.ReturnSelectedBrowser;
                        }
                        else
                        {
                            return string.Empty;
                        }
                    }
                    else // no UI but let's rw for filter
                    {
                        if (typeplayer == PlayerType.DASHIFRefPlayer || typeplayer == PlayerType.DASHLiveAzure)
                        {
                            Urlstr = AssetInfo.RW(new Uri(Urlstr), choosenSE, filter, false, null, AMSOutputProtocols.Dash).ToString();
                        }
                        else
                        {
                            Urlstr = RW(Urlstr, choosenSE, filter);
                        }
                    }
                }

                DynamicEncryption.TokenResult tokenresult = new DynamicEncryption.TokenResult();

                if (myasset != null)
                {
                    keytype = AssetInfo.GetAssetProtection(myasset, context); // let's save the protection scheme (use by azure player): AES, PlayReady, Widevine or PlayReadyAndWidevine

                    if (DynamicEncryption.IsAssetHasAuthorizationPolicyWithToken(myasset, context)) // dynamic encryption with token
                    {
                        // user wants perhaps to play an asset with a token, so let's try to generate it
                        switch (typeplayer)
                        {
                            case PlayerType.SilverlightPlayReadyToken:
                                tokenresult = DynamicEncryption.GetTestToken(myasset, context, ContentKeyType.CommonEncryption);
                                if (!string.IsNullOrEmpty(tokenresult.TokenString))
                                {
                                    tokenresult.TokenString = HttpUtility.UrlEncode(Constants.Bearer + tokenresult.TokenString);
                                    keytype = AssetProtectionType.PlayReady;
                                }
                                break;

                            case PlayerType.FlashAESToken:
                                tokenresult = DynamicEncryption.GetTestToken(myasset, context, ContentKeyType.EnvelopeEncryption);
                                if (!string.IsNullOrEmpty(tokenresult.TokenString))
                                {
                                    tokenresult.TokenString = HttpUtility.UrlEncode(Constants.Bearer + tokenresult.TokenString);
                                    keytype = AssetProtectionType.AES;
                                }
                                break;

                            case PlayerType.AzureMediaPlayer:
                            case PlayerType.AzureMediaPlayerFrame:
                            case PlayerType.CustomPlayer:
                                switch (keytype)
                                {
                                    case AssetProtectionType.None:
                                        break;
                                    case AssetProtectionType.AES:
                                    case AssetProtectionType.PlayReady:
                                    case AssetProtectionType.Widevine:
                                    case AssetProtectionType.PlayReadyAndWidevine:
                                        tokenresult = DynamicEncryption.GetTestToken(myasset, context, displayUI: true);
                                        if (!string.IsNullOrEmpty(tokenresult.TokenString))
                                        {
                                            tokenresult.TokenString = HttpUtility.UrlEncode(Constants.Bearer + tokenresult.TokenString);
                                            //tokenresult.TokenString = Constants.Bearer + tokenresult.TokenString;
                                        }
                                        break;
                                }
                                break;

                            default:
                                // no token enabled player
                                break;
                        }
                    }

                }

                // let's launch the player
                switch (typeplayer)
                {
                    case PlayerType.AzureMediaPlayer:
                    case PlayerType.AzureMediaPlayerFrame:
                        /*
                         string playerurl = typeplayer == PlayerType.AzureMediaPlayer ?
                             Constants.PlayerAMPToLaunch
                             : Constants.PlayerAMPIFrameToLaunch;
                            */
                        string playerurl = "";

                        if (keytype != AssetProtectionType.None)
                        {
                            bool insertoken = !string.IsNullOrEmpty(tokenresult.TokenString);

                            if (insertoken)  // token. Let's analyse the token to find the drm technology used
                            {
                                switch (tokenresult.ContentKeyDeliveryType)
                                {
                                    case ContentKeyDeliveryType.BaselineHttp:
                                        playerurl += string.Format(Constants.AMPAes, true.ToString());
                                        playerurl += string.Format(Constants.AMPAesToken, tokenresult.TokenString);
                                        break;

                                    case ContentKeyDeliveryType.PlayReadyLicense:
                                        playerurl += string.Format(Constants.AMPPlayReady, true.ToString());
                                        playerurl += string.Format(Constants.AMPPlayReadyToken, tokenresult.TokenString);
                                        break;

                                    case ContentKeyDeliveryType.Widevine:
                                        playerurl += string.Format(Constants.AMPWidevine, true.ToString());
                                        playerurl += string.Format(Constants.AMPWidevineToken, tokenresult.TokenString);
                                        break;

                                    default:
                                        break;
                                }
                            }
                            else // No token. Open mode. Let's look to the key to know the drm technology
                            {
                                switch (keytype)
                                {
                                    case AssetProtectionType.AES:
                                        playerurl += string.Format(Constants.AMPAes, true.ToString());
                                        break;

                                    case AssetProtectionType.PlayReady:
                                        playerurl += string.Format(Constants.AMPPlayReady, true.ToString());
                                        break;

                                    case AssetProtectionType.Widevine:
                                        playerurl += string.Format(Constants.AMPWidevine, true.ToString());
                                        break;

                                    case AssetProtectionType.PlayReadyAndWidevine:
                                        playerurl += string.Format(Constants.AMPPlayReady, true.ToString());
                                        playerurl += string.Format(Constants.AMPWidevine, true.ToString());
                                        break;

                                    default:
                                        break;
                                }
                            }
                        }

                        if (formatamp != AzureMediaPlayerFormats.Auto)
                        {
                            switch (formatamp)
                            {
                                case AzureMediaPlayerFormats.Dash:
                                    playerurl += string.Format(Constants.AMPformatsyntax, "dash");
                                    break;

                                case AzureMediaPlayerFormats.Smooth:
                                    playerurl += string.Format(Constants.AMPformatsyntax, "smooth");
                                    break;

                                case AzureMediaPlayerFormats.HLS:
                                    playerurl += string.Format(Constants.AMPformatsyntax, "hls");
                                    break;

                                case AzureMediaPlayerFormats.VideoMP4:
                                    playerurl += string.Format(Constants.AMPformatsyntax, "video/mp4");
                                    break;

                                default: // auto or other
                                    break;
                            }
                            if (tokenresult.TokenString != null)
                            {
                                playerurl += string.Format(Constants.AMPtokensyntax, tokenresult);
                            }
                        }
                        else // format auto. If 0 Reserved Unit, and asset is smooth, let's force to smooth (player cannot get the dash stream for example)
                        {
                            if (choosenSE.ScaleUnits == 0 && myasset != null && myasset.AssetType == AssetType.SmoothStreaming)
                                playerurl += string.Format(Constants.AMPformatsyntax, "smooth");
                        }

                        if (technology != AzureMediaPlayerTechnologies.Auto)
                        {
                            switch (technology)
                            {
                                case AzureMediaPlayerTechnologies.Flash:
                                    playerurl += string.Format(Constants.AMPtechsyntax, "flash");
                                    break;

                                case AzureMediaPlayerTechnologies.JavaScript:
                                    playerurl += string.Format(Constants.AMPtechsyntax, "js");
                                    break;

                                case AzureMediaPlayerTechnologies.NativeHTML5:
                                    playerurl += string.Format(Constants.AMPtechsyntax, "html5");
                                    break;

                                case AzureMediaPlayerTechnologies.Silverlight:
                                    playerurl += string.Format(Constants.AMPtechsyntax, "silverlight");
                                    break;

                                default: // auto or other
                                    break;
                            }
                        }

                        //FullPlayBackLink = string.Format(playerurl, HttpUtility.UrlEncode(Urlstr));
                        //FullPlayBackLink = HttpUtility.UrlEncode(string.Format(playerurl, Urlstr));

                        string playerurlbase = typeplayer == PlayerType.AzureMediaPlayer ?
                                                Constants.PlayerAMPToLaunch
                                              : Constants.PlayerAMPIFrameToLaunch;

                        FullPlayBackLink = string.Format(playerurlbase, HttpUtility.UrlEncode(Urlstr)) + playerurl;
                        break;

                    case PlayerType.SilverlightMonitoring:
                        FullPlayBackLink = string.Format(Constants.LinkSMFHealthToLaunch, HttpUtility.UrlEncode(Urlstr));
                        break;

                    case PlayerType.SilverlightPlayReadyToken:
                        FullPlayBackLink = string.Format(Constants.PlayerSLTokenToLaunch, HttpUtility.UrlEncode(Urlstr), tokenresult);
                        break;

                    case PlayerType.DASHIFRefPlayer:
                        if (!Urlstr.Contains(string.Format(AssetInfo.format_url, AssetInfo.format_dash)))
                        {
                            Urlstr = AssetInfo.AddParameterToUrlString(Urlstr, string.Format(AssetInfo.format_url, AssetInfo.format_dash));
                        }
                        FullPlayBackLink = string.Format(Constants.PlayerDASHIFToLaunch, Urlstr);
                        break;

                    case PlayerType.DASHAzurePage:
                        FullPlayBackLink = string.Format(Constants.PlayerDASHAzurePage, HttpUtility.UrlEncode(Urlstr));
                        break;

                    case PlayerType.DASHLiveAzure:
                        if (!Urlstr.Contains(string.Format(AssetInfo.format_url, AssetInfo.format_dash)))
                        {
                            Urlstr = AssetInfo.AddParameterToUrlString(Urlstr, string.Format(AssetInfo.format_url, AssetInfo.format_dash));
                        }
                        FullPlayBackLink = string.Format(Constants.PlayerDASHAzureToLaunch, Urlstr);
                        break;

                    case PlayerType.FlashAzurePage:
                        FullPlayBackLink = string.Format(Constants.PlayerFlashAzurePage, HttpUtility.UrlEncode(Urlstr));
                        break;

                    case PlayerType.FlashAESToken:
                        FullPlayBackLink = string.Format(Constants.PlayerFlashAESToken, HttpUtility.UrlEncode(Urlstr), tokenresult);
                        break;

                    case PlayerType.MP4AzurePage:
                        FullPlayBackLink = string.Format(Constants.PlayerMP4AzurePage, HttpUtility.UrlEncode(Urlstr));
                        break;

                    case PlayerType.CustomPlayer:
                        string myurl = Properties.Settings.Default.CustomPlayerUrl;
                        FullPlayBackLink = myurl.Replace(Constants.NameconvManifestURL, HttpUtility.UrlEncode(Urlstr)).Replace(Constants.NameconvToken, tokenresult.TokenString);
                        break;
                }

                if (FullPlayBackLink != null && launchbrowser)
                {
                    try
                    {
                        if (string.IsNullOrEmpty(selectedBrowser))
                        {
                            Process.Start(FullPlayBackLink);
                        }
                        else
                        {
                            if (selectedBrowser.Contains("edge"))
                            {
                                Process.Start(selectedBrowser + FullPlayBackLink);
                            }
                            else
                            {
                                Process.Start(selectedBrowser, FullPlayBackLink);
                            }
                        }
                    }
                    catch
                    {
                        mainForm.TextBoxLogWriteLine("Error when launching the browser.", true);
                    }
                }
            }

            return FullPlayBackLink;
        }