Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit b57cb0c

Browse files
committed
[EncodingAndPackagingTool] Fix command line tool crash issue.
[EncodingAndPackagingTool] Fix input file can't be in sub folder issue. [EncodingAndPackagingTool] Add test for command line tool.
1 parent a19a0cd commit b57cb0c

File tree

7 files changed

+159
-4
lines changed

7 files changed

+159
-4
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Auto detect text files and perform LF normalization
22
* text=auto
3+
*.mp4 filter=lfs diff=lfs merge=lfs -text

.github/workflows/ci-build.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,27 @@ jobs:
128128
steps:
129129
- name: Checkout
130130
uses: actions/checkout@v2
131+
with:
132+
lfs: true
131133

132134
- name: Setup DotNet Environment
133135
uses: actions/setup-dotnet@v3
134136
with:
135137
dotnet-version: 6.0.x
136138

139+
- name: Run Azurite
140+
run: |
141+
npm install -g azurite
142+
openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes -keyout /tmp/127.0.0.1.key -out /tmp/127.0.0.1.crt -subj "/CN=127.0.0.1" -addext "subjectAltName=IP:127.0.0.1"
143+
azurite --silent --location /tmp --blobHost 0.0.0.0 --queueHost 0.0.0.0 --tableHost 0.0.0.0 --cert /tmp/127.0.0.1.crt --key /tmp/127.0.0.1.key --oauth basic &
144+
137145
- name: Build and publish
138146
run: |
139147
dotnet publish -c Release EncodingAndPackagingExample
140148
141149
- name: Test
142150
run: |
143-
dotnet test -c Release --no-build EncodingAndPackagingExample
151+
AZURE_CLIENT_ID={{ secrets.AZURE_CLIENT_ID }} AZURE_TENANT_ID={{ secrets.AZURE_TENANT_ID }} AZURE_CLIENT_SECRET={{ secrets.AZURE_CLIENT_SECRET }} TEST_DATA=`pwd`/test-data dotnet test -c Release --no-build EncodingAndPackagingExample
144152
145153
- name: Build Container Image
146154
uses: docker/build-push-action@v4

EncodingAndPackagingExample/EncodingAndPackagingExample.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EncodingAndPackagingTool.Cl
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EncodingAndPackagingTool.AzureFunction", "EncodingAndPackagingTool.AzureFunction\EncodingAndPackagingTool.AzureFunction.csproj", "{8D4079CC-067D-401F-B4A7-DAAD0A34A3FE}"
1111
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EncodingAndPackagingTool.Test", "EncodingAndPackagingTool.Test\EncodingAndPackagingTool.Test.csproj", "{A80B57CB-880C-437F-8351-40582DB08CAA}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{8D4079CC-067D-401F-B4A7-DAAD0A34A3FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{8D4079CC-067D-401F-B4A7-DAAD0A34A3FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{8D4079CC-067D-401F-B4A7-DAAD0A34A3FE}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{A80B57CB-880C-437F-8351-40582DB08CAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{A80B57CB-880C-437F-8351-40582DB08CAA}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{A80B57CB-880C-437F-8351-40582DB08CAA}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{A80B57CB-880C-437F-8351-40582DB08CAA}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

EncodingAndPackagingExample/EncodingAndPackagingTool.Core/EncodingAndPackagingTool.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class EncodingAndPackagingTool
2020

2121
public EncodingAndPackagingTool(ILogger<EncodingAndPackagingTool> logger, TokenCredential azureCredential)
2222
{
23-
if (_azureCredential == null)
23+
if (azureCredential == null)
2424
{
2525
throw new ArgumentNullException(nameof(azureCredential));
2626
}
@@ -47,7 +47,7 @@ public async Task EncodeAndPackageAsync(Uri mp4BlobUri, Uri outputStorageUri, Ca
4747
var connectionString = _connectionString;
4848

4949
// Prepare a tmp path.
50-
var tmpPath = Path.Combine(Path.GetTempPath(), $"{DateTime.Now.ToString("yyyyMMMdddhhssmm")}-{Guid.NewGuid()}");
50+
var tmpPath = Path.Combine(Path.GetTempPath(), $"{DateTime.UtcNow.ToString("yyyyMMddhhmmss")}-{Guid.NewGuid()}");
5151
logger.LogInformation($"Create tmp path: {tmpPath}");
5252

5353
try
@@ -75,7 +75,7 @@ public async Task EncodeAndPackageAsync(Uri mp4BlobUri, Uri outputStorageUri, Ca
7575
}
7676

7777
// Download the blob to a local tmp folder
78-
var inputFile = Path.Combine(tmpPath, $"{blob.Name}");
78+
var inputFile = Path.Combine(tmpPath, $"{Path.GetFileName(blob.Name)}");
7979
logger.LogInformation($"Download blob {blob.Name} to {inputFile}");
8080
using (var inputFileStream = System.IO.File.OpenWrite(inputFile))
8181
using (var inputStream = await blob.OpenReadAsync(new BlobOpenReadOptions(allowModifications: false), cancellationToken).ConfigureAwait(false))
@@ -134,6 +134,7 @@ public async Task EncodeAndPackageAsync(Uri mp4BlobUri, Uri outputStorageUri, Ca
134134
var blobTmpContainer = new BlobContainerClient(outputStorageUri);
135135
blobContainer = new BlobContainerClient(connectionString, blobTmpContainer.Name);
136136
}
137+
await blobContainer.CreateIfNotExistsAsync().ConfigureAwait(false);
137138

138139
// Upload.
139140
await Task.WhenAll(Directory.GetFiles(outputDir).Select(async file =>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using Azure.Identity;
2+
using Azure.Storage.Blobs;
3+
using Azure.Storage.Blobs.Specialized;
4+
using System;
5+
using System.Diagnostics;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
using Xunit;
10+
11+
namespace EncodingAndPackagingTool.Test;
12+
13+
public class EncodingAndPackagingTool
14+
{
15+
private static string _storageServiceUri;
16+
private static string _inputContainerUri;
17+
private static string _testDataPath;
18+
private static DefaultAzureCredential _azureCrendentail;
19+
20+
static EncodingAndPackagingTool()
21+
{
22+
_storageServiceUri = "https://127.0.0.1:10000/devstoreaccount1";
23+
_inputContainerUri = $"{_storageServiceUri}/encodingandpackagingtooltest";
24+
_testDataPath = Environment.GetEnvironmentVariable("TEST_DATA") ?? throw new Exception("TEST_DATA environment variable is missing.");
25+
_azureCrendentail = new DefaultAzureCredential();
26+
27+
// Upload test video clip.
28+
var container = new BlobContainerClient(new Uri(_inputContainerUri), _azureCrendentail);
29+
container.CreateIfNotExists();
30+
31+
Task.WhenAll(Directory.GetFiles(_testDataPath).Select(async file =>
32+
{
33+
var blob = container.GetBlockBlobClient($"input/{Path.GetFileName(file)}");
34+
using var stream = System.IO.File.OpenRead(file);
35+
await container.GetBlobClient($"input/{Path.GetFileName(file)}").UploadAsync(stream, overwrite: true);
36+
})).Wait();
37+
}
38+
39+
[Fact]
40+
public async Task EncodingAndPackagingToolTest()
41+
{
42+
var inputUri = $"{_inputContainerUri}/input/bunny.640x480.15fps.mp4";
43+
var outputContainerUri = $"{_storageServiceUri}/output{Guid.NewGuid().ToString("n")}";
44+
45+
// Invoke EncodingAndPackagingTool
46+
var psi = new ProcessStartInfo()
47+
{
48+
FileName = "EncodingAndPackagingTool.exe",
49+
Arguments = $"--input {inputUri} --output {outputContainerUri}"
50+
};
51+
var process = Process.Start(psi) ?? throw new Exception("Start process failed.");
52+
await process.WaitForExitAsync();
53+
Assert.Equal(0, process.ExitCode);
54+
55+
// We should have the output mpd file.
56+
var blob = new BlobClient(new Uri($"{outputContainerUri}/bunny.640x480.15fps.mpd"), _azureCrendentail);
57+
using (var stream = await blob.OpenReadAsync())
58+
{
59+
Assert.True(stream.Length > 2000);
60+
}
61+
62+
// We should have 13 chunk files for stream 0
63+
for (var i = 1; i <= 13; ++i)
64+
{
65+
blob = new BlobClient(new Uri($"{outputContainerUri}/chunk-stream0-{i.ToString("00000")}.m4s"), _azureCrendentail);
66+
using (var stream = await blob.OpenReadAsync())
67+
{
68+
Assert.True(stream.Length > 2000);
69+
}
70+
}
71+
72+
// We should have 24 chunk files for stream 1
73+
for (var i = 1; i <= 24; ++i)
74+
{
75+
blob = new BlobClient(new Uri($"{outputContainerUri}/chunk-stream1-{i.ToString("00000")}.m4s"), _azureCrendentail);
76+
using (var stream = await blob.OpenReadAsync())
77+
{
78+
Assert.True(stream.Length > 2000);
79+
}
80+
}
81+
82+
// We should have 13 chunk files for stream 2
83+
for (var i = 1; i <= 13; ++i)
84+
{
85+
blob = new BlobClient(new Uri($"{outputContainerUri}/chunk-stream2-{i.ToString("00000")}.m4s"), _azureCrendentail);
86+
using (var stream = await blob.OpenReadAsync())
87+
{
88+
Assert.True(stream.Length > 2000);
89+
}
90+
}
91+
92+
// We should have 13 chunk files for stream 3
93+
for (var i = 1; i <= 13; ++i)
94+
{
95+
blob = new BlobClient(new Uri($"{outputContainerUri}/chunk-stream3-{i.ToString("00000")}.m4s"), _azureCrendentail);
96+
using (var stream = await blob.OpenReadAsync())
97+
{
98+
Assert.True(stream.Length > 2000);
99+
}
100+
}
101+
102+
// We should have 4 init chunk files.
103+
for (var i = 0; i < 4; ++i)
104+
{
105+
blob = new BlobClient(new Uri($"{outputContainerUri}/init-stream{i}.m4s"), _azureCrendentail);
106+
using (var stream = await blob.OpenReadAsync())
107+
{
108+
Assert.True(stream.Length > 500);
109+
}
110+
}
111+
112+
// Delete the container if success.
113+
var container = new BlobContainerClient(new Uri(outputContainerUri), _azureCrendentail);
114+
await container.DeleteAsync();
115+
}
116+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net6.0</TargetFramework>
4+
<AssemblyName>EncodingAndPackagingTool.Test</AssemblyName>
5+
<RootNamespace>EncodingAndPackagingTool.Test</RootNamespace>
6+
<Nullable>annotations</Nullable>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
10+
<PackageReference Include="Moq" Version="4.20.69" />
11+
<PackageReference Include="xunit" Version="2.6.1" />
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
13+
<PrivateAssets>all</PrivateAssets>
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
</PackageReference>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ProjectReference Include="..\EncodingAndPackagingTool.Cli\EncodingAndPackagingTool.Cli.csproj" />
19+
</ItemGroup>
20+
</Project>

test-data/bunny.640x480.15fps.mp4

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:0f0596ec3a9f9cc41630ee22f6faab69f7e3938b59e8e1f3ad3ce21e5dc2df7d
3+
size 13371998

0 commit comments

Comments
 (0)