Esempio n. 1
0
        public async Task TestSimpleConversion()
        {
            var client = GetClient();

            Uri storageUri = new Uri($"https://{TestEnvironment.StorageAccountName}.blob.core.windows.net/{TestEnvironment.BlobContainerName}");

            AssetConversionInputOptions input = new AssetConversionInputOptions(storageUri, "testBox.fbx")
            {
                // We use SAS for live testing, as there can be a delay before DRAM-based access is available for new accounts.
                StorageContainerReadListSas = TestEnvironment.SasToken,
                BlobPrefix = "Input"
            };
            AssetConversionOutputOptions output = new AssetConversionOutputOptions(storageUri)
            {
                StorageContainerWriteSas = TestEnvironment.SasToken,
                BlobPrefix = "Output"
            };
            AssetConversionOptions conversionOptions = new AssetConversionOptions(input, output);

            string conversionId = Recording.Random.NewGuid().ToString();

            AssetConversionOperation conversionOperation = await client.StartConversionAsync(conversionId, conversionOptions);

            Assert.AreEqual(conversionId, conversionOperation.Id);
            Assert.AreEqual(conversionOptions.InputOptions.RelativeInputAssetPath, conversionOperation.Value.Options.InputOptions.RelativeInputAssetPath);
            Assert.AreNotEqual(AssetConversionStatus.Failed, conversionOperation.Value.Status);

            AssetConversion conversion = await client.GetConversionAsync(conversionId);

            Assert.AreEqual(conversion.ConversionId, conversionId);
            Assert.AreNotEqual(AssetConversionStatus.Failed, conversion.Status);

            AssetConversion conversion2 = await conversionOperation.WaitForCompletionAsync();

            Assert.AreEqual(conversionId, conversion2.ConversionId);
            Assert.AreEqual(AssetConversionStatus.Succeeded, conversion2.Status);
            Assert.IsTrue(conversionOperation.Value.Output.OutputAssetUri.EndsWith("Output/testBox.arrAsset"));

            bool foundConversion = false;

            await foreach (var s in client.GetConversionsAync())
            {
                if (s.ConversionId == conversionId)
                {
                    foundConversion = true;
                }
            }
            Assert.IsTrue(foundConversion);
        }
Esempio n. 2
0
    private async Task ConvertModel(string modelPath, Stream modelStream = null)
    {
        try
        {
            // Settings
            string       msg;
            const string inputContainerName  = "arrinput";
            const string outputContainerName = "arroutput";

            string modelFile  = Path.GetFileName(modelPath);
            string outputFile = Path.ChangeExtension(modelFile, "arrAsset");

            msg = $"File selected for conversion: {modelPath}\n{modelFile}.";
            AppServices.AppNotificationService.RaiseNotification(msg, AppNotificationType.Info);
            Debug.Log(msg);

            // Default model folder name
            string folderName = string.Empty;

            // AAR Profile
            var loadedProfile = AppServices.RemoteRendering.LoadedProfile;

            // Initialize storage
            var storageAccountData = loadedProfile.StorageAccountData;
            StorageCredentials storageCredentials;

            if (storageAccountData.AuthType == AuthenticationType.AccountKey)
            {
                string authKey = await loadedProfile.StorageAccountData.GetAuthData();

                storageCredentials = new StorageCredentials(loadedProfile.StorageAccountData.StorageAccountName, authKey);
                if (loadedProfile.StorageModelPathByUsername)
                {
                    folderName = AKStorageAccountData.MODEL_PATH_BY_USERNAME_FOLDER;
                }
            }
            else
            {
                string authToken = await loadedProfile.StorageAccountData.GetAuthData();

                storageCredentials = new StorageCredentials(new TokenCredential(authToken));
                if (loadedProfile.StorageModelPathByUsername)
                {
                    folderName = AADAuth.SelectedAccount.Username;
                }
            }
            var storageAccount = new CloudStorageAccount(storageCredentials, loadedProfile.StorageAccountData.StorageAccountName, null, true);

            // Storage client
            var blobClient = storageAccount.CreateCloudBlobClient();

            // Input container
            var inputContainer = blobClient.GetContainerReference(inputContainerName);
            await inputContainer.CreateIfNotExistsAsync();

            // Output container
            var outputContainer = blobClient.GetContainerReference(outputContainerName);
            await outputContainer.CreateIfNotExistsAsync();

            msg = $"Uploading model.";
            AppServices.AppNotificationService.RaiseNotification(msg, AppNotificationType.Info);
            Debug.Log(msg);

            // Upload model
            CloudBlockBlob modelBlockBlob;
            if (loadedProfile.StorageModelPathByUsername)
            {
                var modelFolder = inputContainer.GetDirectoryReference(folderName);
                modelBlockBlob = modelFolder.GetBlockBlobReference(modelFile);
            }
            else
            {
                modelBlockBlob = inputContainer.GetBlockBlobReference(modelFile);
            }

            // Upload using path or provided stream
            if (modelStream == null)
            {
                await modelBlockBlob.UploadFromFileAsync(modelPath);
            }
            else
            {
                await modelBlockBlob.UploadFromStreamAsync(modelStream);
            }

            // Conversion parameters
            string inputUri  = $"https://{loadedProfile.StorageAccountName}.blob.core.windows.net/{inputContainerName}";
            string outputUri = $"https://{loadedProfile.StorageAccountName}.blob.core.windows.net/{outputContainerName}";

            var inputParams  = new AssetConversionInputOptions(inputUri, null, folderName, modelFile);
            var outputParams = new AssetConversionOutputOptions(outputUri, null, folderName, outputFile);
            var options      = new AssetConversionOptions(null, inputParams, outputParams);

            // Azure authentication
            var client = await loadedProfile.GetClient(loadedProfile.PreferredDomain);

            msg = $"Starting conversion.";
            AppServices.AppNotificationService.RaiseNotification(msg, AppNotificationType.Info);
            Debug.Log(msg);

            // Start conversion
            var   conversion = client.StartAssetConversionAsync(options);
            await conversion;

            string conversionId;

            // Conversion result
            if (conversion.IsCompleted)
            {
                msg = $"Conversion started.";
                AppServices.AppNotificationService.RaiseNotification(msg, AppNotificationType.Info);
                Debug.Log(msg);
                conversionId = conversion.Result.ConversionUuid;
            }
            else
            {
                msg = $"Error starting conversion.";
                AppServices.AppNotificationService.RaiseNotification(msg, AppNotificationType.Error);
                Debug.LogError(msg);
                return;
            }

            // Poll conversion process
            while (true)
            {
                // Wait 10 seconds
                await Task.Delay(10000);

                // Poll conversion status
                var task = await client.GetAssetConversionStatusAsync(conversionId);

                ConversionSessionStatus status = task.Result;
                if (status == ConversionSessionStatus.Created || status == ConversionSessionStatus.Running)
                {
                    // In progress
                    msg = $"Conversion Session In Progress...";
                    AppServices.AppNotificationService.RaiseNotification(msg, AppNotificationType.Info);
                    Debug.Log(msg);
                }
                else
                {
                    // Done, success/fail
                    switch (status)
                    {
                    case ConversionSessionStatus.Unknown:
                    case ConversionSessionStatus.Aborted:
                    case ConversionSessionStatus.Failure:
                        msg = $"Conversion Session Failed.";
                        AppServices.AppNotificationService.RaiseNotification(msg, AppNotificationType.Error);
                        Debug.LogError(msg);
                        break;

                    case ConversionSessionStatus.Success:
                        msg = $"Conversion Session Completed Successfully.";
                        AppServices.AppNotificationService.RaiseNotification(msg, AppNotificationType.Info);
                        OnModelConversionSuccess?.Invoke();
                        Debug.Log(msg);
                        break;
                    }

                    break;
                }
            }
        }
        catch (Exception e)
        {
            var msg = $"Conversion Process Failed.\n{e}";
            AppServices.AppNotificationService.RaiseNotification(msg, AppNotificationType.Error);
            Debug.LogError(msg);
            Debug.LogError(e.StackTrace);
        }
    }