private void btnMeteringOrSecureStop_Click(object sender, RoutedEventArgs e)
        {
            btnMeteringOrSecureStopButtonClicked = (Button)sender;
            MediaElement me = btnMeteringOrSecureStopButtonClicked.Name == "btnMeteringOrSecureStop1" ? myME : (btnMeteringOrSecureStopButtonClicked.Name == "btnMeteringOrSecureStop2" ? myME2 : myME3);

            SampleDataItem item = (SampleDataItem)btnMeteringOrSecureStopButtonClicked.DataContext;

            bMeteringOrSecureStopButtonClicked = true;
            PRUtilities prUtilities = new PRUtilities();

            if (item.MeteringCertFile != "null")
            {
                prUtilities.Test_ReportMeteringData(item.MeteringCertFile,
                                                    item.LaurlWithRights,
                                                    item.CustomData,
                                                    "null",
                                                    "null",
                                                    "1");
            }

            if (item.SecureStopCertFile != "null")
            {
                me.Stop();
                prUtilities.Test_ReportSecureStopData(item.SecureStopCertFile,
                                                      item.LaurlWithRights,
                                                      item.CustomData,
                                                      "null",
                                                      "null");
            }
        }
        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            itemIds = (String[])e.NavigationParameter;

            MediaCount = (uint)itemIds.Length;

            playbackContents    = new Playback[MediaCount];
            playbackContents[0] = new Playback(myME);
            if (MediaCount > 1)
            {
                playbackContents[1] = new Playback(myME2);
                if (MediaCount > 2)
                {
                    playbackContents[2] = new Playback(myME3);
                }
            }

            for (uint playerIndex = 0; playerIndex < itemIds.Length; playerIndex++)
            {
                PRUtilities    prUtilities = new PRUtilities();
                SampleDataItem item        = await SampleDataSource.GetItemAsync(itemIds[playerIndex]);

                this.DefaultViewModel["Item" + (playerIndex + 1).ToString()] = item;

                item.PlayerIndex = playerIndex;
                item.IsLDL       = false;

                this.localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

                if (item.Laurl.Contains("http://playready.directtaps.net"))
                {
                    if (this.localSettings.Values.ContainsKey("CustomRightsURL"))
                    {
                        item.LaurlWithRights = item.Laurl + this.localSettings.Values["CustomRightsURL"];
                    }
                    else
                    {
                        item.LaurlWithRights = item.Laurl + "&UseSimpleNonPersistentLicense=1";
                    }
                }
                else
                {
                    item.LaurlWithRights = item.Laurl;
                }

                ConfigLayout((int)playerIndex, item);

                if (!(item.Kid.ToString().Equals("null")))
                {
                    //proactive LA
                    LAAndReportResult licenseAcquisition = new LAAndReportResult(new ReportResultDelegate(ProvactiveActionFinished), item);

                    bool isFirstPlayExpirationUsed = false;
                    item.IsRootLicense = false;

                    if (item.UplinkKey != "null")
                    {
                        LogMessage("pre-acquire a non-expired root license");
                        item.IsRootLicense = true;
                        ProactiveLA(licenseAcquisition, item, playbackContents[playerIndex], item.LaurlWithRights, isFirstPlayExpirationUsed, item.IsRootLicense);
                    }

                    item.IsRootLicense = false;
                    if (item.FirstPlayExpiration != "null")
                    {
                        LogMessage("pre-acquire a SDL(short duration license) first");
                        isFirstPlayExpirationUsed = true;
                    }
                    else
                    {
                        LogMessage("pre-acquire a non-expiration license");
                    }

                    ProactiveLA(licenseAcquisition, item, playbackContents[playerIndex], item.LaurlWithRights, isFirstPlayExpirationUsed, item.IsRootLicense);

                    if (isFirstPlayExpirationUsed &&
                        item.FirstPlayExpiration != "null")
                    {
                        isFirstPlayExpirationUsed = false;
                        uint?uintSec = prUtilities.ActionParamConvertToUint(item.FirstPlayExpiration);
                        if (uintSec != null)
                        {
                            uint waitDuration = (uint)uintSec * 1000 - timeBuffer;
                            if (waitDuration < 0)
                            {
                                waitDuration = 0;
                            }
                            LogMessage("wait for " + waitDuration + " ms");
                            await Task.Delay((int)waitDuration);
                        }
                        else
                        {
                            LogError("invalid FirstPlayExpiration value");
                            break;
                        }

                        LogMessage("acquire a LDL(long duration license) before SDL expired");
                        item.IsLDL = true;
                        ProactiveLA(licenseAcquisition, item, playbackContents[playerIndex], item.LaurlWithRights, isFirstPlayExpirationUsed, item.IsRootLicense);
                    }
                }
                else
                {
                    //reactive LA
                    ServiceRequestConfigData requestConfigData = new ServiceRequestConfigData();

                    if (item.Laurl != "null")
                    {
                        requestConfigData.Uri = new Uri(item.LaurlWithRights);
                    }

                    if (item.DomainUrl != "null")
                    {
                        requestConfigData.DomainUri = new Uri(item.DomainUrl);
                    }

                    if (item.CustomData != "null")
                    {
                        requestConfigData.ChallengeCustomData = item.CustomData;
                    }

                    playbackContents[playerIndex].RequestConfigData = requestConfigData;
                    playbackContents[playerIndex].Play(item.Content);
                }
            }
        }
        private void ProactiveLA(LAAndReportResult f_licenseAcquisition,
                                 SampleDataItem f_item,
                                 Playback f_playMedia,
                                 String f_Laurl,
                                 bool f_useFirstPlayExpiration,
                                 bool f_rootLicense)
        {
            PRUtilities prUtilities = new PRUtilities();
            String      laurl       = f_Laurl;

            ServiceRequestConfigData LArequestConfigData = new ServiceRequestConfigData();


            if (f_rootLicense &&
                f_item.UplinkKey != "null")
            //pre-acquire a root license
            {
                Guid[] GuidRootKids = new Guid[1];
                GuidRootKids[0]            = prUtilities.ActionParamConvertToGuid(f_item.UplinkKey);
                LArequestConfigData.KeyIds = GuidRootKids;

                //chained Root License
                laurl = laurl + "&UseRootLicense=1";
            }
            else if (!(f_item.Kid.ToString().Equals("null")))
            //if kids is not null, proactively acquire simple or leaf license
            {
                if (f_item.UplinkKey != "null")
                {
                    //chained Leaf License
                    Guid uplinkKeyGUID = prUtilities.ActionParamConvertToGuid(f_item.UplinkKey);
                    LogMessage("uplinkKeyGUID=" + uplinkKeyGUID);
                    string base64UplinkKid = prUtilities.GuidToBase64(uplinkKeyGUID);
                    LogMessage("base64UplinkKid=" + base64UplinkKid);
                    {
                        laurl = laurl + "&UseChainLicense=1" + "&UplinkKey=" + base64UplinkKid;
                    }
                }

                int    count = 100;
                int    i     = 0;
                Guid[] guidKids;

                string[] keyIds = f_item.Kid.Split(',');
                int      len    = keyIds.Length;

                if (f_useFirstPlayExpiration &&
                    f_item.FirstPlayExpiration != "null")
                //realtime expiration
                {
                    laurl = laurl + "&RealTimeExpiration=1" + "&FirstPlayExpiration=" + f_item.FirstPlayExpiration;

                    guidKids = new Guid[count];

                    for (; i < count / 2; i++)
                    {
                        guidKids[i] = Guid.NewGuid();
                    }

                    for (int j = 0; j < len; j++)
                    {
                        guidKids[i++] = prUtilities.ActionParamConvertToGuid(keyIds[j]);
                    }

                    for (; i < count; i++)
                    {
                        guidKids[i] = Guid.NewGuid();
                    }
                }
                else
                {
                    guidKids = new Guid[len];
                    for (int j = 0; j < len; j++)
                    {
                        guidKids[j] = prUtilities.ActionParamConvertToGuid(keyIds[j]);
                    }
                }

                LArequestConfigData.KeyIds = guidKids;
            }

            LArequestConfigData.EncryptionAlgorithm = prUtilities.ActionParamConvertToPlayReadyEncryptionAlgorithm(f_item.EncryptionAlgorithm);
            LArequestConfigData.Uri                 = prUtilities.ActionParamConvertToUri(laurl);
            LArequestConfigData.DomainServiceId     = prUtilities.ActionParamConvertToGuid(f_item.DomainId);
            LArequestConfigData.ChallengeCustomData = prUtilities.ActionParamConvertToString(f_item.CustomData);

            f_licenseAcquisition.RequestConfigData = LArequestConfigData;

            if (f_Laurl.Contains("UseSimpleNonPersistentLicense=1"))
            {
                //To acquire in memory license proactively that will later be used for playback, we need to create the media session before hand.
                //Afterward, we need to tie the license session to the playback session. See configMediaProtectionManager for more detail.
                f_licenseAcquisition.Persistent = false;
                f_licenseAcquisition.configMediaProtectionManager(f_playMedia.mediaProtectionManager);
            }
            else
            {
                f_licenseAcquisition.Persistent = true;
            }

            f_licenseAcquisition.AcquireLicenseProactively();
        }