Skip to content

Commit 492eab9

Browse files
committed
- Code clean up
1 parent ec7c4e0 commit 492eab9

File tree

3 files changed

+92
-112
lines changed

3 files changed

+92
-112
lines changed

FFMpeg.Xamarin/FFMpeg.Xamarin.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<AndroidResgenFile>Resources\Resource.Designer.cs</AndroidResgenFile>
1616
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
1717
<AndroidUseLatestPlatformSdk>True</AndroidUseLatestPlatformSdk>
18-
<TargetFrameworkVersion>v6.0</TargetFrameworkVersion>
18+
<TargetFrameworkVersion>v7.0</TargetFrameworkVersion>
1919
</PropertyGroup>
2020
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
2121
<DebugSymbols>true</DebugSymbols>
@@ -45,6 +45,7 @@
4545
</ItemGroup>
4646
<ItemGroup>
4747
<Compile Include="FFMpegLibrary.cs" />
48+
<Compile Include="FFMpegSource.cs" />
4849
<Compile Include="Resources\Resource.Designer.cs" />
4950
<Compile Include="Properties\AssemblyInfo.cs" />
5051
</ItemGroup>

FFMpeg.Xamarin/FFMpegLibrary.cs

Lines changed: 28 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ namespace FFMpeg.Xamarin
1515
{
1616
public class FFMpegLibrary
1717
{
18-
1918
public string CDNHost { get; set; } = "raw.githubusercontent.com";
20-
21-
19+
2220
public static FFMpegLibrary Instance = new FFMpegLibrary();
2321

2422
private bool _initialized = false;
@@ -30,16 +28,13 @@ public class FFMpegLibrary
3028
/// </summary>
3129
/// <param name="context"></param>
3230
/// <returns></returns>
33-
public async Task Init(
34-
Context context,
35-
string cdn = null,
36-
string downloadTitle = null,
37-
string downloadMessage = null)
31+
public async Task Init(Context context, string cdn = null, string downloadTitle = null, string downloadMessage = null)
3832
{
3933
if (_initialized)
4034
return;
4135

42-
if (cdn != null) {
36+
if (cdn != null)
37+
{
4338
CDNHost = cdn;
4439
}
4540

@@ -48,7 +43,7 @@ public async Task Init(
4843

4944
ffmpegFile = new Java.IO.File(filesDir + "/ffmpeg");
5045

51-
FFMPEGSource source = FFMPEGSource.Get();
46+
FFMpegSource source = FFMpegSource.Get();
5247

5348
await Task.Run(() =>
5449
{
@@ -65,7 +60,8 @@ await Task.Run(() =>
6560
return;
6661
}
6762
}
68-
catch(Exception ex) {
63+
catch (Exception ex)
64+
{
6965
System.Diagnostics.Debug.WriteLine($" Error validating file {ex}");
7066
}
7167

@@ -86,7 +82,8 @@ await Task.Run(() =>
8682
return;
8783
}
8884

89-
if (ffmpegFile.Exists()) {
85+
if (ffmpegFile.Exists())
86+
{
9087
ffmpegFile.Delete();
9188
}
9289

@@ -97,53 +94,44 @@ await Task.Run(() =>
9794
dlg.Indeterminate = false;
9895
dlg.SetProgressStyle(ProgressDialogStyle.Horizontal);
9996
dlg.SetCancelable(false);
100-
dlg.CancelEvent += (s, e) => {
101-
97+
dlg.CancelEvent += (s, e) =>
98+
{
99+
102100
};
103101

104102
dlg.SetCanceledOnTouchOutside(false);
105103
dlg.Show();
106104

107-
108105
using (var c = new System.Net.Http.HttpClient())
109106
{
110107
using (var fout = System.IO.File.OpenWrite(ffmpegFile.AbsolutePath))
111108
{
112-
113109
string url = source.Url;
114110
var g = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
115-
116-
var h = await c.SendAsync(g, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
117-
118111

119-
120-
121-
122-
112+
var h = await c.SendAsync(g, System.Net.Http.HttpCompletionOption.ResponseHeadersRead);
123113

124114
var buffer = new byte[51200];
125115

126-
127116
var s = await h.Content.ReadAsStreamAsync();
128117
long total = h.Content.Headers.ContentLength.GetValueOrDefault();
129118

130119
IEnumerable<string> sl;
131120
if (h.Headers.TryGetValues("Content-Length", out sl))
132121
{
133-
if (total == 0 && sl.Any()) {
122+
if (total == 0 && sl.Any())
123+
{
134124
long.TryParse(sl.FirstOrDefault(), out total);
135125
}
136126
}
137127

138-
139128
int count = 0;
140-
141129
int progress = 0;
142130

143131
dlg.Max = (int)total;
144132

145-
146-
while ((count = await s.ReadAsync(buffer, 0, buffer.Length)) > 0) {
133+
while ((count = await s.ReadAsync(buffer, 0, buffer.Length)) > 0)
134+
{
147135

148136
await fout.WriteAsync(buffer, 0, count);
149137

@@ -155,10 +143,6 @@ await Task.Run(() =>
155143
}
156144

157145
dlg.Hide();
158-
159-
160-
161-
162146
}
163147
}
164148

@@ -180,22 +164,18 @@ await Task.Run(() =>
180164
/// <param name="cmd"></param>
181165
/// <param name="logger"></param>
182166
/// <returns></returns>
183-
public static async Task<int> Run(Context context, string cmd, Action<string> logger = null) {
184-
167+
public static async Task<int> Run(Context context, string cmd, Action<string> logger = null)
168+
{
185169
try
186170
{
187171
TaskCompletionSource<int> source = new TaskCompletionSource<int>();
188172

189-
190-
191173
await Instance.Init(context);
192174

193175
await Task.Run(() =>
194176
{
195177
try
196178
{
197-
198-
199179
int n = _Run(context, cmd, logger);
200180
source.SetResult(n);
201181
}
@@ -208,8 +188,8 @@ await Task.Run(() =>
208188

209189
return await source.Task;
210190
}
211-
catch (Exception ex) {
212-
191+
catch (Exception ex)
192+
{
213193
System.Diagnostics.Debug.WriteLine(ex);
214194

215195
throw ex;
@@ -218,16 +198,10 @@ await Task.Run(() =>
218198

219199
public static string EndOfFFMPEGLine = "final ratefactor:";
220200

221-
private static int _Run(
222-
Context context,
223-
string cmd,
224-
Action<string> logger = null)
201+
private static int _Run(Context context, string cmd, Action<string> logger = null)
225202
{
226-
227203
TaskCompletionSource<int> task = new TaskCompletionSource<int>();
228204

229-
230-
231205
System.Diagnostics.Debug.WriteLine($"ffmpeg initialized");
232206

233207
//var process = Java.Lang.Runtime.GetRuntime().Exec( Instance.ffmpegFile.CanonicalPath + " " + cmd );
@@ -248,8 +222,6 @@ private static int _Run(
248222

249223
process.Start();
250224

251-
252-
253225

254226
Task.Run(() =>
255227
{
@@ -270,7 +242,8 @@ private static int _Run(
270242
if (line.StartsWith(EndOfFFMPEGLine))
271243
{
272244

273-
Task.Run(async () => {
245+
Task.Run(async () =>
246+
{
274247
await Task.Delay(TimeSpan.FromMinutes(1));
275248
finished = true;
276249
});
@@ -283,78 +256,22 @@ private static int _Run(
283256

284257
}
285258
}
286-
catch (Exception ex) {
259+
catch (Exception ex)
260+
{
287261
System.Diagnostics.Debug.WriteLine(ex);
288262
}
289263
});
290264

291265
while (!finished)
292266
{
293267
process.WaitForExit(10000);
294-
if (process.HasExited) {
268+
if (process.HasExited)
269+
{
295270
break;
296271
}
297272
}
298273

299274
return process.ExitCode;
300-
301-
302-
}
303-
304-
305-
}
306-
307-
308-
public class FFMPEGSource {
309-
310-
311-
public static FFMPEGSource Get() {
312-
313-
string osArchitecture = Java.Lang.JavaSystem.GetProperty("os.arch");
314-
315-
foreach (var source in Sources) {
316-
if (source.IsArch(osArchitecture))
317-
return source;
318-
}
319-
320-
throw new NotImplementedException();
321275
}
322-
323-
324-
325-
326-
327-
328-
public FFMPEGSource(string arch, Func<string,bool> isArch, string hash)
329-
{
330-
this.Arch = arch;
331-
this.IsArch = isArch;
332-
this.Hash = hash;
333-
}
334-
335-
public static string FFMPEGVersion { get; } = "3.0.1";
336-
337-
public static FFMPEGSource[] Sources = new FFMPEGSource[] {
338-
new FFMPEGSource("arm", x=> !x.EndsWith("86"), "4nzzxDKxIYlvyK8tFH7/iNMHTdU="),
339-
new FFMPEGSource("x86", x=> x.EndsWith("86"), "DdTbrTBf8Zeh6p5hWL0ggdIp5w4=")
340-
};
341-
342-
public string Arch { get; }
343-
344-
public string Hash { get; }
345-
346-
347-
//https://cdn.rawgit.com/neurospeech/xamarin-android-ffmpeg/master/binary/3.0.1/arm/ffmpeg
348-
//https://raw.githubusercontent.com/neurospeech/xamarin-android-ffmpeg/master/binary/3.0.1/arm/ffmpeg
349-
public string Url => $"https://{FFMpegLibrary.Instance.CDNHost}/neurospeech/xamarin-android-ffmpeg/v1.0.7/binary/{FFMPEGVersion}/{Arch}/ffmpeg";
350-
351-
public Func<string, bool> IsArch { get; }
352-
353-
public bool IsHashMatch(byte[] data) {
354-
var sha = System.Security.Cryptography.SHA1.Create();
355-
string h = Convert.ToBase64String(sha.ComputeHash(data));
356-
return h == Hash;
357-
}
358-
359276
}
360277
}

FFMpeg.Xamarin/FFMpegSource.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
using Android.App;
7+
using Android.Content;
8+
using Android.OS;
9+
using Android.Runtime;
10+
using Android.Views;
11+
using Android.Widget;
12+
13+
namespace FFMpeg.Xamarin
14+
{
15+
public class FFMpegSource
16+
{
17+
public FFMpegSource(string arch, Func<string, bool> isArch, string hash)
18+
{
19+
this.Arch = arch;
20+
this.IsArch = isArch;
21+
this.Hash = hash;
22+
}
23+
24+
public static string FFMPEGVersion { get; } = "3.0.1";
25+
26+
public static FFMpegSource[] Sources = new FFMpegSource[] {
27+
new FFMpegSource("arm", x=> !x.EndsWith("86"), "4nzzxDKxIYlvyK8tFH7/iNMHTdU="),
28+
new FFMpegSource("x86", x=> x.EndsWith("86"), "DdTbrTBf8Zeh6p5hWL0ggdIp5w4=")
29+
};
30+
31+
public string Arch { get; }
32+
33+
public string Hash { get; }
34+
35+
36+
//https://cdn.rawgit.com/neurospeech/xamarin-android-ffmpeg/master/binary/3.0.1/arm/ffmpeg
37+
//https://raw.githubusercontent.com/neurospeech/xamarin-android-ffmpeg/master/binary/3.0.1/arm/ffmpeg
38+
public string Url => $"https://{FFMpegLibrary.Instance.CDNHost}/neurospeech/xamarin-android-ffmpeg/v1.0.7/binary/{FFMPEGVersion}/{Arch}/ffmpeg";
39+
40+
public Func<string, bool> IsArch { get; }
41+
42+
public static FFMpegSource Get()
43+
{
44+
string osArchitecture = Java.Lang.JavaSystem.GetProperty("os.arch");
45+
46+
foreach (var source in Sources)
47+
{
48+
if (source.IsArch(osArchitecture))
49+
return source;
50+
}
51+
52+
throw new NotImplementedException();
53+
}
54+
55+
public bool IsHashMatch(byte[] data)
56+
{
57+
var sha = System.Security.Cryptography.SHA1.Create();
58+
string h = Convert.ToBase64String(sha.ComputeHash(data));
59+
return h == Hash;
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)