Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanduke committed Dec 20, 2023
1 parent 5120bb0 commit cd7813a
Show file tree
Hide file tree
Showing 18 changed files with 1,311 additions and 1 deletion.
56 changes: 56 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Release

on:
workflow_dispatch: # run manually only

permissions:
contents: write

jobs:

publish:
name: Build and publish release
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Get release version
run: |
grep -Eo "(\<Version.*)[0-9]+(\.[0-9]+)+" Directory.Build.props > ver.tmp
sed -E "s/.*[^0-9.]([0-9.]+)$/X_VERSION=\1/" ver.tmp >> $GITHUB_ENV
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Build win-x86
run: dotnet publish -c Release -r win-x86 --sc false -p DebugType=embedded -p PublishSingleFile=true
- name: Build win-x64
run: dotnet publish -c Release -r win-x64 --sc false -p DebugType=embedded -p PublishSingleFile=true
- name: Build win-arm64
run: dotnet publish -c Release -r win-arm64 --sc false -p DebugType=embedded -p PublishSingleFile=true
- name: Build linux-x64
run: dotnet publish -c Release -r linux-x64 --sc false -p DebugType=embedded -p PublishSingleFile=true
- name: Build linux-arm
run: dotnet publish -c Release -r linux-arm --sc false -p DebugType=embedded -p PublishSingleFile=true
- name: Build linux-arm64
run: dotnet publish -c Release -r linux-arm64 --sc false -p DebugType=embedded -p PublishSingleFile=true
- name: Build osx-x64
run: dotnet publish -c Release -r osx-x64 --sc false -p DebugType=embedded -p PublishSingleFile=true
- name: Build osx-arm64
run: dotnet publish -c Release -r osx-arm64 --sc false -p DebugType=embedded -p PublishSingleFile=true
- name: Create release assets
run: |
for folder in FixHdhrAspect/bin/Release/*/*/; do
platform=$(basename "$folder")
publish=${folder%/}/publish
zip -vr "FixHdhrAspect-$X_VERSION-${platform}.zip" -j "$publish"/* -x "*.Development.*"
done
- name: Update release
uses: "marvinpinto/action-automatic-releases@6273874b61ebc8c71f1a61b2d98e234cf389b303"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
prerelease: false
files: FixHdhrAspect-*.zip
automatic_release_tag: latest
title: ${{ env.X_VERSION }}
31 changes: 31 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
Based on: https://gist.github.com/jonathanduke/ab4fc1880e8b245d3f939b122f301938#file-directory-build-props
Dependent on: https://gist.github.com/jonathanduke/ab4fc1880e8b245d3f939b122f301938#file-buildassemblyinfo-targets
Public domain: http://unlicense.org/
Modify as you wish, but you are encouraged to leave this comment for future reference in case the original is updated.
-->
<Project>

<PropertyGroup>
<Product>HDHomeRun Proxy Service</Product>
<Description>This service functions as a proxy for an HDHomeRun device and fixes the aspect ratio on certain MPEG streams.</Description>
<Company>Jonathan Duke Software, LLC</Company>
<Copyright>Copyright © $([System.String]::Format("2023-{0:yyyy}",$([System.DateTime]::Now)).Replace("2023-2023", "2023")) $(Company)</Copyright>
<Authors>Jonathan Duke</Authors>
</PropertyGroup>

<PropertyGroup>
<VersionPrefix>1.0.0</VersionPrefix>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)$(Description)' == '' ">
<!-- Use the assembly name as the description if none is provided. -->
<Description>$(AssemblyName)</Description>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' != 'Release' And '$(VersionSuffix)' == '' ">
<!-- Make it obvious if we've got a debug version and no other tag like "beta" has been applied. -->
<VersionSuffix>$(Configuration.ToLower())</VersionSuffix>
</PropertyGroup>

</Project>
28 changes: 28 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
Based on: https://gist.github.com/jonathanduke/ab4fc1880e8b245d3f939b122f301938#file-directory-build-targets
Dependent on: https://gist.github.com/jonathanduke/ab4fc1880e8b245d3f939b122f301938#file-gitbuildprops-targets
Public domain: http://unlicense.org/
Modify as you wish, but you are encouraged to leave this comment for future reference in case the original is updated.
-->
<Project>

<PropertyGroup Condition=" '$(AssemblyVersion)' == '' And '$(VersionPrefix)' != '' ">
<!-- Zero out the version minor/patch numbers for simpler assembly binding. -->
<AssemblyVersion>$(VersionPrefix.Substring(0,$(VersionPrefix.IndexOf('.')))).0.0.0</AssemblyVersion>
</PropertyGroup>

<Target Name="SetSourceRevisionId" BeforeTargets="InitializeSourceControlInformation" Condition=" '$(SourceRevisionId)' == '' And '$(Configuration)' == 'Release' ">
<!-- Put the Git hash in the SourceRevisionId property so that it is automatically included in the informational version. -->
<Exec Command="git describe --long --always --dirty --exclude=*" EchoOff="true" StandardOutputImportance="low" ConsoleToMSBuild="true">
<Output TaskParameter="ConsoleOutput" PropertyName="SourceRevisionId" />
</Exec>
</Target>

<Target Name="BasePathMapOnSourceRevision" BeforeTargets="BeforeBuild" DependsOnTargets="InitializeSourceControlInformation" Condition=" '$(Configuration)' == 'Release' ">
<!-- Remove the local development path from the .pdb file and use the Git hash for uniqueness. -->
<PropertyGroup Condition=" '$(SourceRevisionId)' != '' ">
<PathMap>$(SolutionDir)=$(SolutionName)\$(SourceRevisionId)</PathMap>
</PropertyGroup>
</Target>

</Project>
33 changes: 33 additions & 0 deletions FixHdhrAspect.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34322.80
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "HDHomeRun Proxy Service", "HDHomeRun Proxy Service", "{55B7CD7B-087E-4566-AE54-1E4F559FFF44}"
ProjectSection(SolutionItems) = preProject
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
LICENSE = LICENSE
README.md = README.md
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FixHdhrAspect", "FixHdhrAspect\FixHdhrAspect.csproj", "{346F01F1-E2C1-43CE-B3EA-9E28A4F70E6F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{346F01F1-E2C1-43CE-B3EA-9E28A4F70E6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{346F01F1-E2C1-43CE-B3EA-9E28A4F70E6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{346F01F1-E2C1-43CE-B3EA-9E28A4F70E6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{346F01F1-E2C1-43CE-B3EA-9E28A4F70E6F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {562B520D-F997-494C-9E6B-60FE7EC5CD4F}
EndGlobalSection
EndGlobal
80 changes: 80 additions & 0 deletions FixHdhrAspect/DeviceIdentifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.Text.RegularExpressions;

namespace JonathanDuke.FixHdhrAspect;

public partial class DeviceIdentifier
{
[GeneratedRegex("^[0-9A-F]{8}$", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en-US")]
private static partial Regex DeviceIdRegex();
private static readonly Regex _rxDeviceId = DeviceIdRegex();
private static readonly byte[] _checksumLookupTable = [0xA, 0x5, 0xF, 0x6, 0x7, 0xC, 0x1, 0xB, 0x9, 0x2, 0x8, 0xD, 0x4, 0x3, 0xE, 0x0];

private uint _value;

public DeviceIdentifier(uint deviceId)
{
_value = deviceId;
}

public bool Validate()
{
return CalculateChecksum(_value) == 0;
}

internal static uint CalculateChecksum(uint value)
{
// checksum algorithm from: https://github.com/Silicondust/libhdhomerun/blob/master/hdhomerun_discover.c#L1773
byte checksum = 0;
checksum ^= _checksumLookupTable[(value >> 28) & 0x0F];
checksum ^= (byte)((value >> 24) & 0x0F);
checksum ^= _checksumLookupTable[(value >> 20) & 0x0F];
checksum ^= (byte)((value >> 16) & 0x0F);
checksum ^= _checksumLookupTable[(value >> 12) & 0x0F];
checksum ^= (byte)((value >> 8) & 0x0F);
checksum ^= _checksumLookupTable[(value >> 4) & 0x0F];
checksum ^= (byte)((value >> 0) & 0x0F);
return checksum;
}

public static explicit operator uint(DeviceIdentifier id) => id._value;

public static DeviceIdentifier Parse(string value)
{
if (value.Length != 8 || !_rxDeviceId.IsMatch(value)) throw new ArgumentException("The device ID should be 8 hex digits.", nameof(value));
return ParseInternal(value);
}

private static DeviceIdentifier ParseInternal(string value)
{
uint parsedId = 0;

for (int i = 0; i < value.Length; i += 2)
{
parsedId <<= 8;
parsedId |= (byte)int.Parse(value.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
}

return new DeviceIdentifier(parsedId);
}

public static bool TryParse(string value, out DeviceIdentifier? instance)
{
if (value.Length == 8 && _rxDeviceId.IsMatch(value))
{
try
{
instance = ParseInternal(value);
return true;
}
catch { }
}

instance = null;
return false;
}

public override string ToString()
{
return BitConverter.ToString(BitConverter.GetBytes(_value).Reverse().ToArray()).Replace("-", "");
}
}
14 changes: 14 additions & 0 deletions FixHdhrAspect/FixHdhrAspect.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>JonathanDuke.FixHdhrAspect</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions FixHdhrAspect/MpegAspectRatio.Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace JonathanDuke.FixHdhrAspect;

public static class AspectRatioExtensions
{
public static string ToRatioString(this MpegAspectRatio value)
{
if (value == MpegAspectRatio.Default)
{
return value.ToString();
}

return value.ToString().Replace("x", ":").Replace("_", ".").TrimStart('.');
}

public static string FromAspectRatio(this string aspectRatioString)
{
if (string.IsNullOrEmpty(aspectRatioString) || string.Equals(aspectRatioString, MpegAspectRatio.Default.ToString(), StringComparison.OrdinalIgnoreCase))
{
return MpegAspectRatio.Default.ToString();
}

return "_" + aspectRatioString.Replace(".", "_").Replace(":", "x");
}
}
29 changes: 29 additions & 0 deletions FixHdhrAspect/MpegAspectRatio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace JonathanDuke.FixHdhrAspect;

/// <summary>
/// Supported aspect ratio values in the MPEG specification.
/// </summary>
/// <see cref="http://dvdnav.mplayerhq.hu/dvdinfo/mpeghdrs.html"/>
public enum MpegAspectRatio : byte
{
/// <summary>
/// Do not override the aspect ratio.
/// </summary>
Default = 0,
/// <summary>
/// Override the stream with a 1:1 aspect ratio.
/// </summary>
_1x1 = 1,
/// <summary>
/// Override the stream with a 4:3 aspect ratio.
/// </summary>
_4x3 = 2,
/// <summary>
/// Override the stream with a 16:9 aspect ratio.
/// </summary>
_16x9 = 3,
/// <summary>
/// Override the stream with a 2.21:1 aspect ratio.
/// </summary>
_2_21x1 = 4,
}
28 changes: 28 additions & 0 deletions FixHdhrAspect/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Template: https://learn.microsoft.com/en-us/dotnet/core/extensions/windows-service?pivots=dotnet-7-0#rewrite-the-program-class

using JonathanDuke.FixHdhrAspect;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Logging.EventLog;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddWindowsService(options =>
{
options.ServiceName = "HDHomeRun Proxy Service";
});

// EventLog is only supported on Windows: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1416#how-to-fix-violations
if (OperatingSystem.IsWindows())
{
LoggerProviderOptions.RegisterProviderOptions<
EventLogSettings, EventLogLoggerProvider>(builder.Services);
}

builder.Services.AddSingleton<ProxyService>();
builder.Services.AddHostedService<Worker>();

// See: https://github.com/dotnet/runtime/issues/47303
builder.Logging.AddConfiguration(
builder.Configuration.GetSection("Logging"));

IHost host = builder.Build();
host.Run();
11 changes: 11 additions & 0 deletions FixHdhrAspect/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"JonathanDuke.FixHdhrAspect": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
Loading

0 comments on commit cd7813a

Please sign in to comment.