Skip to content

Commit

Permalink
- code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioseric committed Oct 27, 2016
1 parent 492eab9 commit 26832f4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 54 deletions.
89 changes: 39 additions & 50 deletions FFMpeg.Xamarin/FFMpegLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ namespace FFMpeg.Xamarin
{
public class FFMpegLibrary
{
public static string EndOfFFMPEGLine = "final ratefactor:";

public string CDNHost { get; set; } = "raw.githubusercontent.com";

public static FFMpegLibrary Instance = new FFMpegLibrary();
public readonly static FFMpegLibrary Instance = new FFMpegLibrary();

private bool _initialized = false;

private Java.IO.File ffmpegFile;
private Java.IO.File _ffmpegFile;

/// <summary>
///
Expand All @@ -41,21 +43,20 @@ public async Task Init(Context context, string cdn = null, string downloadTitle
// do all initialization...
var filesDir = context.FilesDir;

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

FFMpegSource source = FFMpegSource.Get();

await Task.Run(() =>
{

if (ffmpegFile.Exists())
if (_ffmpegFile.Exists())
{
try
{
if (source.IsHashMatch(System.IO.File.ReadAllBytes(ffmpegFile.CanonicalPath)))
if (source.IsHashMatch(System.IO.File.ReadAllBytes(_ffmpegFile.CanonicalPath)))
{
if (!ffmpegFile.CanExecute())
ffmpegFile.SetExecutable(true);
if (!_ffmpegFile.CanExecute())
_ffmpegFile.SetExecutable(true);
_initialized = true;
return;
}
Expand All @@ -69,10 +70,10 @@ await Task.Run(() =>

// delete the file...

if (ffmpegFile.CanExecute())
ffmpegFile.SetExecutable(false);
ffmpegFile.Delete();
System.Diagnostics.Debug.WriteLine($"ffmpeg file deleted at {ffmpegFile.AbsolutePath}");
if (_ffmpegFile.CanExecute())
_ffmpegFile.SetExecutable(false);
_ffmpegFile.Delete();
System.Diagnostics.Debug.WriteLine($"ffmpeg file deleted at {_ffmpegFile.AbsolutePath}");
}
});

Expand All @@ -82,29 +83,28 @@ await Task.Run(() =>
return;
}

if (ffmpegFile.Exists())
if (_ffmpegFile.Exists())
{
ffmpegFile.Delete();
_ffmpegFile.Delete();
}

// lets try to download
var dlg = new ProgressDialog(context);
dlg.SetTitle(downloadMessage ?? "Downloading Video Converter");
var dialog = new ProgressDialog(context);
dialog.SetTitle(downloadMessage ?? "Downloading Video Converter");
//dlg.SetMessage(downloadMessage ?? "Downloading Video Converter");
dlg.Indeterminate = false;
dlg.SetProgressStyle(ProgressDialogStyle.Horizontal);
dlg.SetCancelable(false);
dlg.CancelEvent += (s, e) =>
dialog.Indeterminate = false;
dialog.SetProgressStyle(ProgressDialogStyle.Horizontal);
dialog.SetCancelable(false);
dialog.CancelEvent += (s, e) =>
{

};

dlg.SetCanceledOnTouchOutside(false);
dlg.Show();

dialog.SetCanceledOnTouchOutside(false);
dialog.Show();
using (var c = new System.Net.Http.HttpClient())
{
using (var fout = System.IO.File.OpenWrite(ffmpegFile.AbsolutePath))
using (var fout = System.IO.File.OpenWrite(_ffmpegFile.AbsolutePath))
{
string url = source.Url;
var g = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
Expand All @@ -128,29 +128,27 @@ await Task.Run(() =>
int count = 0;
int progress = 0;

dlg.Max = (int)total;
dialog.Max = (int)total;

while ((count = await s.ReadAsync(buffer, 0, buffer.Length)) > 0)
{

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

progress += count;

//System.Diagnostics.Debug.WriteLine($"Downloaded {progress} of {total} from {url}");

dlg.Progress = progress;
dialog.Progress = progress;
}

dlg.Hide();
dialog.Hide();
}
}

System.Diagnostics.Debug.WriteLine($"ffmpeg file copied at {ffmpegFile.AbsolutePath}");
System.Diagnostics.Debug.WriteLine($"ffmpeg file copied at {_ffmpegFile.AbsolutePath}");

if (!ffmpegFile.CanExecute())
if (!_ffmpegFile.CanExecute())
{
ffmpegFile.SetExecutable(true);
_ffmpegFile.SetExecutable(true);
System.Diagnostics.Debug.WriteLine($"ffmpeg file made executable");
}

Expand Down Expand Up @@ -195,9 +193,7 @@ await Task.Run(() =>
throw ex;
}
}

public static string EndOfFFMPEGLine = "final ratefactor:";


private static int _Run(Context context, string cmd, Action<string> logger = null)
{
TaskCompletionSource<int> task = new TaskCompletionSource<int>();
Expand All @@ -206,7 +202,7 @@ private static int _Run(Context context, string cmd, Action<string> logger = nul

//var process = Java.Lang.Runtime.GetRuntime().Exec( Instance.ffmpegFile.CanonicalPath + " " + cmd );

var startInfo = new System.Diagnostics.ProcessStartInfo(Instance.ffmpegFile.CanonicalPath, cmd);
var startInfo = new System.Diagnostics.ProcessStartInfo(Instance._ffmpegFile.CanonicalPath, cmd);

startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
Expand All @@ -221,39 +217,32 @@ private static int _Run(Context context, string cmd, Action<string> logger = nul
string error = null;

process.Start();



Task.Run(() =>
{
try
{
using (var reader = process.StandardError)
{
string processOutput = "";
do
StringBuilder processOutput = new StringBuilder();
while (!finished)
{
var line = reader.ReadLine();
if (line == null)
break;
logger?.Invoke(line);
processOutput += line;


processOutput.Append(line);

if (line.StartsWith(EndOfFFMPEGLine))
{

Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromMinutes(1));
finished = true;
});

}

} while (!finished);
error = processOutput;


}
error = processOutput.ToString();
}
}
catch (Exception ex)
Expand Down
7 changes: 3 additions & 4 deletions FFMpeg.Xamarin/FFMpegSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace FFMpeg.Xamarin
{
public class FFMpegSource
{
public static string FFMPEGVersion { get; } = "3.0.1";

public FFMpegSource(string arch, Func<string, bool> isArch, string hash)
{
this.Arch = arch;
this.IsArch = isArch;
this.Hash = hash;
}

public static string FFMPEGVersion { get; } = "3.0.1";

public static FFMpegSource[] Sources = new FFMpegSource[] {
new FFMpegSource("arm", x=> !x.EndsWith("86"), "4nzzxDKxIYlvyK8tFH7/iNMHTdU="),
new FFMpegSource("x86", x=> x.EndsWith("86"), "DdTbrTBf8Zeh6p5hWL0ggdIp5w4=")
Expand All @@ -31,8 +31,7 @@ public FFMpegSource(string arch, Func<string, bool> isArch, string hash)
public string Arch { get; }

public string Hash { get; }



//https://cdn.rawgit.com/neurospeech/xamarin-android-ffmpeg/master/binary/3.0.1/arm/ffmpeg
//https://raw.githubusercontent.com/neurospeech/xamarin-android-ffmpeg/master/binary/3.0.1/arm/ffmpeg
public string Url => $"https://{FFMpegLibrary.Instance.CDNHost}/neurospeech/xamarin-android-ffmpeg/v1.0.7/binary/{FFMPEGVersion}/{Arch}/ffmpeg";
Expand Down

0 comments on commit 26832f4

Please sign in to comment.