Skip to content

Commit 10aa871

Browse files
authored
Merge pull request #603 from unoplatform/chore/update.tube.player
chore: (Tube Player) move logic to service
2 parents d683a3d + d7dbb6a commit 10aa871

File tree

4 files changed

+50
-39
lines changed

4 files changed

+50
-39
lines changed

reference/TubePlayer/TubePlayer/Business/IYoutubeService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ namespace TubePlayer.Business;
33
public interface IYoutubeService
44
{
55
Task<YoutubeVideoSet> SearchVideos(string searchQuery, string nextPageToken, uint maxResult, CancellationToken ct);
6+
7+
Task<string?> GetVideoSourceUrl(string videoId, CancellationToken ct);
68
}

reference/TubePlayer/TubePlayer/Business/YoutubeService.cs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
namespace TubePlayer.Business;
22

3-
public class YoutubeService : IYoutubeService
3+
public class YoutubeService(IYoutubeEndpoint client, IYoutubePlayerEndpoint playerClient) : IYoutubeService
44
{
5-
private readonly IYoutubeEndpoint _client;
6-
7-
public YoutubeService(IYoutubeEndpoint client)
8-
{
9-
_client = client;
10-
}
11-
125
public async Task<YoutubeVideoSet> SearchVideos(string searchQuery, string nextPageToken, uint maxResult, CancellationToken ct)
136
{
14-
var resultData = await _client.SearchVideos(searchQuery, nextPageToken, maxResult, ct);
7+
var resultData = await client.SearchVideos(searchQuery, nextPageToken, maxResult, ct);
158

169
var results = resultData?.Items?.Where(result =>
1710
!string.IsNullOrWhiteSpace(result.Snippet?.ChannelId)
@@ -33,8 +26,8 @@ public async Task<YoutubeVideoSet> SearchVideos(string searchQuery, string nextP
3326
.Distinct(StringComparer.OrdinalIgnoreCase)
3427
.ToArray();
3528

36-
var asyncDetails = _client.GetVideoDetails(videoIds, ct);
37-
var asyncChannels = _client.GetChannels(channelIds, ct);
29+
var asyncDetails = client.GetVideoDetails(videoIds, ct);
30+
var asyncChannels = client.GetChannels(channelIds, ct);
3831
await Task.WhenAll(asyncDetails, asyncChannels);
3932

4033
var detailsItems = (await asyncDetails)?.Items;
@@ -67,4 +60,39 @@ public async Task<YoutubeVideoSet> SearchVideos(string searchQuery, string nextP
6760

6861
return new(videoSet.ToImmutableList(), resultData?.NextPageToken ?? string.Empty);
6962
}
63+
64+
65+
public async Task<string?> GetVideoSourceUrl(string videoId, CancellationToken ct)
66+
{
67+
var streamVideo = $$"""
68+
{
69+
"videoId": "{{videoId}}",
70+
"context": {
71+
"client": {
72+
"clientName": "ANDROID_TESTSUITE",
73+
"clientVersion": "1.9",
74+
"androidSdkVersion": 30,
75+
"hl": "en",
76+
"gl": "US",
77+
"utcOffsetMinutes": 0
78+
}
79+
}
80+
}
81+
""";
82+
83+
// Get the available stream data
84+
var streamData = await playerClient.GetStreamData(streamVideo, ct);
85+
86+
// Get the video stream with the highest video quality
87+
var streamWithHighestVideoQuality = streamData.Content?.
88+
StreamingData?
89+
.Formats?
90+
.OrderByDescending(s => s.QualityLabel)
91+
.FirstOrDefault();
92+
93+
// Get the stream URL
94+
var streamUrl = streamWithHighestVideoQuality?.Url;
95+
96+
return streamUrl;
97+
}
7098
}

reference/TubePlayer/TubePlayer/Business/YoutubeServiceMock.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ public YoutubeServiceMock(ISerializer serializer)
1515
_channels = channelsData.Items!.ToDictionary(channel => channel.Id!, StringComparer.OrdinalIgnoreCase);
1616
}
1717

18+
public Task<string?> GetVideoSourceUrl(string videoId, CancellationToken ct)
19+
{
20+
return Task.FromResult<string?>(default);
21+
}
22+
1823
public Task<YoutubeVideoSet> SearchVideos(string searchQuery, string nextPageToken, uint maxResult, CancellationToken ct)
1924
{
2025
var filtered = _details
Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,17 @@
11
using Windows.Media.Core;
2-
using TubePlayer.Services;
32

43
namespace TubePlayer.Presentation;
54

6-
public partial record VideoDetailsModel(YoutubeVideo Video, IYoutubePlayerEndpoint YoutubeClient)
5+
public partial record VideoDetailsModel(YoutubeVideo Video, IYoutubeService YoutubeService)
76
{
87
public IFeed<MediaSource> VideoSource => Feed.Async(GetVideoSource);
98

109
private async ValueTask<MediaSource> GetVideoSource(CancellationToken ct)
1110
{
12-
var streamVideo = $$"""
13-
{
14-
"videoId": "{{Video.Id}}",
15-
"context": {
16-
"client": {
17-
"clientName": "ANDROID_TESTSUITE",
18-
"clientVersion": "1.9",
19-
"androidSdkVersion": 30,
20-
"hl": "en",
21-
"gl": "US",
22-
"utcOffsetMinutes": 0
23-
}
24-
}
25-
}
26-
""";
27-
28-
// Get the available stream data
29-
var streamData = await YoutubeClient.GetStreamData(streamVideo, ct);
30-
31-
// Get the video stream with the highest video quality
32-
var streamWithHighestVideoQuality = streamData.Content?.StreamingData?.Formats?.OrderByDescending(s => s.QualityLabel).FirstOrDefault() ??
33-
throw new InvalidOperationException("Input stream collection is empty.");
34-
35-
// Get the stream URL
36-
var streamUrl = streamWithHighestVideoQuality.Url;
11+
var streamUrl = await YoutubeService.GetVideoSourceUrl(Video?.Id, ct)
12+
?? throw new InvalidOperationException("Input stream collection is empty.");
3713

3814
// Return the MediaSource using the stream URL
39-
return MediaSource.CreateFromUri(new Uri(streamUrl!));
15+
return MediaSource.CreateFromUri(new Uri(streamUrl));
4016
}
4117
}

0 commit comments

Comments
 (0)