From 17fedf2834cbb0fecd8fd966a7d4bdb54bf72d1b Mon Sep 17 00:00:00 2001 From: Eduardo Fonseca Date: Sun, 14 Apr 2024 19:54:13 +0000 Subject: [PATCH] Reducing calls to Video Indexer API by making videos public. --- .../Models/FairPlayTube/VideoInfo.cs | 3 ++ .../FairPlayTube/VideoInfo/VideoInfoModel.cs | 1 + .../Common/AzureVideoIndexerService.cs | 3 +- .../FairPlayTube/VideoInfoService.cs | 34 +++++++++++++++++++ .../FairPlayTube/Tables/VideoInfo.sql | 1 + .../VideoIndexStatusBackgroundService.cs | 1 + .../Components/Pages/Public/WatchVideo.razor | 21 +++++------- 7 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/FairPlayCombinedSln/FairPlayCombined.DataAccess/Models/FairPlayTube/VideoInfo.cs b/src/FairPlayCombinedSln/FairPlayCombined.DataAccess/Models/FairPlayTube/VideoInfo.cs index 4e88a5a4..632600f8 100644 --- a/src/FairPlayCombinedSln/FairPlayCombined.DataAccess/Models/FairPlayTube/VideoInfo.cs +++ b/src/FairPlayCombinedSln/FairPlayCombined.DataAccess/Models/FairPlayTube/VideoInfo.cs @@ -101,6 +101,9 @@ public partial class VideoInfo public int? VideoIndexingProcessingPercentage { get; set; } + [StringLength(1000)] + public string PublishedUrl { get; set; } + [ForeignKey("ApplicationUserId")] [InverseProperty("VideoInfo")] public virtual AspNetUsers ApplicationUser { get; set; } diff --git a/src/FairPlayCombinedSln/FairPlayCombined.Models/FairPlayTube/VideoInfo/VideoInfoModel.cs b/src/FairPlayCombinedSln/FairPlayCombined.Models/FairPlayTube/VideoInfo/VideoInfoModel.cs index 9a208514..9a8517cc 100644 --- a/src/FairPlayCombinedSln/FairPlayCombined.Models/FairPlayTube/VideoInfo/VideoInfoModel.cs +++ b/src/FairPlayCombinedSln/FairPlayCombined.Models/FairPlayTube/VideoInfo/VideoInfoModel.cs @@ -28,5 +28,6 @@ public class VideoInfoModel : IListModel public string? EnglishCaptions { get; set; } public int LifetimeSessions { get; set; } public TimeSpan LifetimeWatchTime { get; set; } + public string? PublishedUrl { get; set; } } } diff --git a/src/FairPlayCombinedSln/FairPlayCombined.Services/Common/AzureVideoIndexerService.cs b/src/FairPlayCombinedSln/FairPlayCombined.Services/Common/AzureVideoIndexerService.cs index fdd4e884..b99cf633 100644 --- a/src/FairPlayCombinedSln/FairPlayCombined.Services/Common/AzureVideoIndexerService.cs +++ b/src/FairPlayCombinedSln/FairPlayCombined.Services/Common/AzureVideoIndexerService.cs @@ -110,7 +110,8 @@ public class IndexVideoFromUriParameters $"&language={indexVideoFromUriParameters.Language}" + $"&videoUrl={HttpUtility.UrlEncode(indexVideoFromUriParameters.VideoUri!.ToString())}" + $"&fileName={HttpUtility.UrlEncode(indexVideoFromUriParameters.FileName)}" + - $"&indexingPreset={indexVideoFromUriParameters.IndexingPreset}"; + $"&indexingPreset={indexVideoFromUriParameters.IndexingPreset}" + + $"&Privacy=Public"; requestUrl += $"&sendSuccessEmail={true}"; httpClient.DefaultRequestHeaders.Authorization = diff --git a/src/FairPlayCombinedSln/FairPlayCombined.Services/FairPlayTube/VideoInfoService.cs b/src/FairPlayCombinedSln/FairPlayCombined.Services/FairPlayTube/VideoInfoService.cs index bb60f614..e7d5f9c7 100644 --- a/src/FairPlayCombinedSln/FairPlayCombined.Services/FairPlayTube/VideoInfoService.cs +++ b/src/FairPlayCombinedSln/FairPlayCombined.Services/FairPlayTube/VideoInfoService.cs @@ -126,5 +126,39 @@ CancellationToken cancellationToken .ToArrayAsync(cancellationToken); return result; } + + public async Task GetVideoInfoByVideoIdAsync(string videoId, + CancellationToken cancellationToken) + { + var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken); + var result = await dbContext.VideoInfo + .Where(p => p.VideoId == videoId) + .AsNoTracking() + .Select(p => new VideoInfoModel + { + VideoInfoId = p.VideoInfoId, + AccountId = p.AccountId, + VideoId = p.VideoId, + Location = p.Location, + Name = p.Name, + Description = p.Description, + FileName = p.FileName, + VideoBloblUrl = p.VideoBloblUrl, + IndexedVideoUrl = p.IndexedVideoUrl, + ApplicationUserId = p.ApplicationUserId, + VideoIndexStatusId = p.VideoIndexStatusId, + VideoDurationInSeconds = p.VideoDurationInSeconds, + VideoIndexSourceClass = p.VideoIndexSourceClass, + Price = p.Price, + ExternalVideoSourceUrl = p.ExternalVideoSourceUrl, + VideoLanguageCode = p.VideoLanguageCode, + VideoVisibilityId = p.VideoVisibilityId, + ThumbnailUrl = p.ThumbnailUrl, + YouTubeVideoId = p.YouTubeVideoId, + PublishedUrl = p.PublishedUrl + }) + .SingleOrDefaultAsync(cancellationToken); + return result; + } } } diff --git a/src/FairPlayCombinedSln/FairPlayCombinedDb/FairPlayTube/Tables/VideoInfo.sql b/src/FairPlayCombinedSln/FairPlayCombinedDb/FairPlayTube/Tables/VideoInfo.sql index 5414a03c..a3346542 100644 --- a/src/FairPlayCombinedSln/FairPlayCombinedDb/FairPlayTube/Tables/VideoInfo.sql +++ b/src/FairPlayCombinedSln/FairPlayCombinedDb/FairPlayTube/Tables/VideoInfo.sql @@ -27,6 +27,7 @@ [VideoIndexJSON] NVARCHAR(MAX) NULL, [VideoThumbnailPhotoId] BIGINT NULL, [VideoIndexingProcessingPercentage] INT NULL DEFAULT 0, + [PublishedUrl] NVARCHAR(1000) NULL, CONSTRAINT [FK_VideoInfo_VideoIndexStatus] FOREIGN KEY ([VideoIndexStatusId]) REFERENCES [FairPlayTube].[VideoIndexStatus]([VideoIndexStatusId]), CONSTRAINT [FK_VideoInfo_VideoVisibility] FOREIGN KEY ([VideoVisibilityId]) REFERENCES [FairPlayTube].[VideoVisibility]([VideoVisibilityId]), CONSTRAINT [FK_VideoInfo_Photo_Thumbnail] FOREIGN KEY ([VideoThumbnailPhotoId]) REFERENCES [dbo].[Photo]([PhotoId]) diff --git a/src/FairPlayCombinedSln/FairPlayTube.VideoIndexing/VideoIndexStatusBackgroundService.cs b/src/FairPlayCombinedSln/FairPlayTube.VideoIndexing/VideoIndexStatusBackgroundService.cs index a6195249..f4e87fa4 100644 --- a/src/FairPlayCombinedSln/FairPlayTube.VideoIndexing/VideoIndexStatusBackgroundService.cs +++ b/src/FairPlayCombinedSln/FairPlayTube.VideoIndexing/VideoIndexStatusBackgroundService.cs @@ -124,6 +124,7 @@ await dbContext.VideoIndexingTransaction.AddAsync(new VideoIndexingTransaction() var completedVideoIndex = await azureVideoIndexerService.GetVideoIndexAsync( singleVideoEntity.VideoId, getviTokenResult.AccessToken!, stoppingToken); + singleVideoEntity.PublishedUrl = completedVideoIndex!.videos![0].publishedUrl; singleVideoEntity.VideoIndexJson = JsonSerializer.Serialize(completedVideoIndex); InsertInsights(singleVideoEntity, completedVideoIndex); } diff --git a/src/FairPlayCombinedSln/FairPlayTube/Components/Pages/Public/WatchVideo.razor b/src/FairPlayCombinedSln/FairPlayTube/Components/Pages/Public/WatchVideo.razor index c02a169b..7261bb41 100644 --- a/src/FairPlayCombinedSln/FairPlayTube/Components/Pages/Public/WatchVideo.razor +++ b/src/FairPlayCombinedSln/FairPlayTube/Components/Pages/Public/WatchVideo.razor @@ -8,14 +8,13 @@ @rendermode NoPreRender -@inject AzureVideoIndexerServiceConfiguration azureVideoIndexerServiceConfiguration -@inject AzureVideoIndexerService azureVideoIndexerService @inject IJSRuntime jsRuntime @inject VideoWatchTimeService videoWatchTimeService @inject ILogger logger; @inject IUserProviderService userProviderService @inject SupportedLanguageService supportedLanguageService - +@inject VideoInfoService videoInfoService +@inject AzureVideoIndexerServiceConfiguration azureVideoIndexerServiceConfiguration WatchVideo @@ -55,8 +54,6 @@ else public string? VideoId { get; set; } private static IComponentRenderMode NoPreRender = new InteractiveServerRenderMode(prerender: false); private bool IsBusy { get; set; } - private GetVideoIndexResponseModel? videoIndex { get; set; } - private GetAccessTokenResponseModel? viAccessTokenResponse { get; set; } private CancellationTokenSource cancellationTokenSource = new(); private bool UseFallbackMode { get; set; } private string? FallbackModeUrl { get; set; } @@ -75,13 +72,11 @@ else { this.IsBusy = true; StateHasChanged(); - var armAccessToken = await azureVideoIndexerService.AuthenticateToAzureArmAsync(); - this.viAccessTokenResponse = await azureVideoIndexerService - .GetAccessTokenForArmAccountAsync(armAccessToken, this.cancellationTokenSource.Token); - this.videoIndex = await azureVideoIndexerService - .GetVideoIndexAsync(this.VideoId!, viAccessTokenResponse!.AccessToken!, - this.cancellationTokenSource.Token); - this.VideoUrl = $"{videoIndex!.videos![0].publishedUrl}?accessToken={viAccessTokenResponse.AccessToken}"; + var videoInfo = await this.videoInfoService.GetVideoInfoByVideoIdAsync( + this.VideoId!, + this.cancellationTokenSource.Token + ); + this.VideoUrl = videoInfo!.PublishedUrl; this.SupportedLanguages = await supportedLanguageService .GetAllSupportedLanguageAsync(this.cancellationTokenSource.Token); StateHasChanged(); @@ -130,7 +125,7 @@ else private void OnUseFallbackMode() { - this.FallbackModeUrl = $"https://www.videoindexer.ai/embed/player/{this.videoIndex!.accountId}/{this.videoIndex.id}/?locale=en&location={azureVideoIndexerServiceConfiguration.Location}&accessToken={this.viAccessTokenResponse!.AccessToken}"; + this.FallbackModeUrl = $"https://www.videoindexer.ai/embed/player/{this.azureVideoIndexerServiceConfiguration.AccountId}/{this.VideoId}/?locale=en&location={azureVideoIndexerServiceConfiguration.Location}"; this.UseFallbackMode = true; StateHasChanged(); }