Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
Moved ICaptureHandler to MMALDownstreamComponent constructor to allow…
Browse files Browse the repository at this point in the history
… for more refined use of 'using' statements and clearup of unmanaged resources. Moved FFmpeg specific code to another project. Updated build scripts and README
  • Loading branch information
techyian committed Apr 7, 2017
1 parent b6103e5 commit e5cf493
Show file tree
Hide file tree
Showing 28 changed files with 665 additions and 452 deletions.
21 changes: 21 additions & 0 deletions MMALSharp.Common/Handlers/ICaptureHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MMALSharp.Handlers
{
public interface ICaptureHandler : IDisposable
{
/// <summary>
/// Used to process the byte array containing our image data
/// </summary>
/// <param name="data">A byte array containing image data</param>
void Process(byte[] data);
/// <summary>
/// Used for any further processing once we have completed capture
/// </summary>
void PostProcess();
}
}
13 changes: 13 additions & 0 deletions MMALSharp.Common/Handlers/ImageStreamCaptureHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MMALSharp.Handlers
{
public class ImageStreamCaptureHandler : StreamCaptureHandler
{
public ImageStreamCaptureHandler(string directory, string extension) : base(directory, extension) { }
}
}
86 changes: 86 additions & 0 deletions MMALSharp.Common/Handlers/StreamCaptureHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using MMALSharp.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MMALSharp.Handlers
{
/// <summary>
/// Processes the image data to a stream.
/// </summary>
public abstract class StreamCaptureHandler : ICaptureHandler
{
protected Stream CurrentStream { get; set; }
public List<string> ProcessedStreams { get; set; } = new List<string>();
protected int Processed { get; set; }
protected string Directory { get; set; }
protected string Extension { get; set; }

public StreamCaptureHandler(string directory, string extension)
{
this.Directory = directory.TrimEnd('/');
this.Extension = extension.TrimStart('.');
}

public void NewFile()
{
if (this.CurrentStream != null)
this.CurrentStream.Dispose();
this.CurrentStream = File.Create(this.Directory + "/" + DateTime.Now.ToString("dd-MMM-yy HH-mm-ss") + "." + this.Extension);
}

public void Process(byte[] data)
{
this.Processed += data.Length;

if (this.CurrentStream.CanWrite)
this.CurrentStream.Write(data, 0, data.Length);
else
throw new IOException("Stream not writable.");

}

public void PostProcess()
{
this.ProcessedStreams.Add(this.GetDirectory() + "/" + this.GetFilename() + "." + this.GetExtension());
Console.WriteLine(string.Format("Successfully processed {0}", Helpers.ConvertBytesToMegabytes(this.Processed)));
}

public string GetDirectory()
{
if(this.CurrentStream.GetType() == typeof(FileStream))
{
return Path.GetDirectoryName(((FileStream)this.CurrentStream).Name);
}
return null;
}

private string GetExtension()
{
if (this.CurrentStream.GetType() == typeof(FileStream))
{
return Path.GetExtension(((FileStream)this.CurrentStream).Name);
}
return null;
}

private string GetFilename()
{
if (this.CurrentStream.GetType() == typeof(FileStream))
{
return Path.GetFileNameWithoutExtension(((FileStream)this.CurrentStream).Name);
}
return null;
}

public void Dispose()
{
if (this.CurrentStream != null)
this.CurrentStream.Dispose();
}

}
}
29 changes: 29 additions & 0 deletions MMALSharp.Common/Handlers/VideoStreamCaptureHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MMALSharp.Handlers
{
public class VideoStreamCaptureHandler : StreamCaptureHandler
{
public VideoStreamCaptureHandler(string directory, string extension) : base(directory, extension) { }

public bool CanSplit()
{
if (this.CurrentStream.GetType() == typeof(FileStream))
return true;
return false;
}

public void Split()
{
if (this.CanSplit())
{
this.NewFile();
}
}
}
}
59 changes: 59 additions & 0 deletions MMALSharp.Common/MMALSharp.Common.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D8CA0BC9-CA3B-4EC1-9898-542A6F5346F8}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MMALSharp.Common</RootNamespace>
<AssemblyName>MMALSharp.Common</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Handlers\ICaptureHandler.cs" />
<Compile Include="Handlers\ImageStreamCaptureHandler.cs" />
<Compile Include="Handlers\ProcessResult.cs" />
<Compile Include="Handlers\StreamCaptureHandler.cs" />
<Compile Include="Handlers\VideoStreamCaptureHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utility\Helpers.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions MMALSharp.Common/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MMALSharp-Common")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MMALSharp-Common")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d8ca0bc9-ca3b-4ec1-9898-542a6f5346f8")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace MMALSharp.Handlers
{
/// <summary>
/// Currently experimental. Not working fully.
/// </summary>
public class FFmpegCaptureHandler : ICaptureHandler
{
public Process MyProcess { get; set; }
Expand Down Expand Up @@ -35,7 +38,7 @@ public FFmpegCaptureHandler(string streamName, string streamUrl)

public bool CanSplit()
{
throw new NotImplementedException();
return false;
}

public string GetDirectory()
Expand Down Expand Up @@ -67,9 +70,10 @@ public void Split()
throw new NotImplementedException();
}

~FFmpegCaptureHandler()
public void Dispose()
{
this.MyProcess.Close();
}

}
}
61 changes: 61 additions & 0 deletions MMALSharp.FFmpeg/MMALSharp.FFmpeg.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{8F2E7EDB-4533-4FC5-A8AA-17F11302CC84}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MMALSharp.FFmpeg</RootNamespace>
<AssemblyName>MMALSharp.FFmpeg</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Handlers\FFmpegCaptureHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="VideoUtilities.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MMALSharp.Common\MMALSharp.Common.csproj">
<Project>{d8ca0bc9-ca3b-4ec1-9898-542a6f5346f8}</Project>
<Name>MMALSharp.Common</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions MMALSharp.FFmpeg/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MMALSharp-FFmpeg")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MMALSharp-FFmpeg")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8f2e7edb-4533-4fc5-a8aa-17f11302cc84")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit e5cf493

Please sign in to comment.