-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from insomniachi/media_detection
Media detection
- Loading branch information
Showing
46 changed files
with
1,130 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Totoro.Plugins.MediaDetection.Generic; | ||
|
||
public class Mpv : GenericMediaPlayer | ||
{ | ||
protected override string ParseFromWindowTitle(string windowTitle) | ||
{ | ||
return windowTitle.Replace("- mpv", string.Empty); | ||
} | ||
} | ||
|
||
public class MpcHc : GenericMediaPlayer | ||
{ | ||
public MpcHc() | ||
{ | ||
GetTitleFromWindow = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System.Reflection; | ||
using Totoro.Plugins.Contracts; | ||
using Totoro.Plugins.MediaDetection.Contracts; | ||
using Totoro.Plugins.Options; | ||
|
||
namespace Totoro.Plugins.MediaDetection.Generic; | ||
|
||
public abstract class GenericPlugin<T> : IPlugin<INativeMediaPlayer> | ||
where T : INativeMediaPlayer, new() | ||
{ | ||
public INativeMediaPlayer Create() => new T(); | ||
|
||
public abstract PluginInfo GetInfo(); | ||
public PluginOptions GetOptions() => new(); | ||
public void SetOptions(PluginOptions options) { } | ||
object IPlugin.Create() => Create(); | ||
} | ||
|
||
public class MpvPlugin : GenericPlugin<Mpv> | ||
{ | ||
public override PluginInfo GetInfo() | ||
{ | ||
return new PluginInfo | ||
{ | ||
Name = "mpv", | ||
DisplayName = "MPV", | ||
Description = "", | ||
Version = Assembly.GetExecutingAssembly().GetName().Version! | ||
}; | ||
} | ||
} | ||
|
||
public class MpcHcPlugin : GenericPlugin<MpcHc> | ||
{ | ||
public override PluginInfo GetInfo() | ||
{ | ||
return new PluginInfo | ||
{ | ||
Name = "mpc-hc64", | ||
DisplayName = "MPC-HC", | ||
Description = "", | ||
Version = Assembly.GetExecutingAssembly().GetName().Version! | ||
}; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Totoro.Plugins.MediaDetection.Mpv/Totoro.Plugins.MediaDetection.Generic.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0-windows</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<Platforms>x64</Platforms> | ||
<Version>1.0</Version> | ||
<OutputPath Condition="$(Configuration) == Release">..\Plugins\</OutputPath> | ||
<AppendTargetFrameworkToOutputPath Condition="$(Configuration) == Release">false</AppendTargetFrameworkToOutputPath> | ||
<AppendRuntimeIdentifierToOutputPath Condition="$(Configuration) == Release">false</AppendRuntimeIdentifierToOutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Totoro.Plugins.MediaDetection\Totoro.Plugins.MediaDetection.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
using System.Diagnostics; | ||
using System.Reactive.Linq; | ||
using System.Reactive.Subjects; | ||
using System.Text.RegularExpressions; | ||
using FlaUI.Core; | ||
using FlaUI.Core.AutomationElements; | ||
using FlaUI.Core.Definitions; | ||
using FlaUI.UIA3; | ||
using ReactiveUI; | ||
using ReactiveUI.Fody.Helpers; | ||
using Totoro.Plugins.MediaDetection.Contracts; | ||
|
||
namespace Totoro.Plugins.MediaDetection.Vlc | ||
{ | ||
internal sealed partial class MediaPlayer : ReactiveObject, INativeMediaPlayer, IHavePosition | ||
{ | ||
private Application? _application; | ||
private Window? _mainWindow; | ||
private Slider? _slider; | ||
private readonly Subject<TimeSpan> _positionChanged = new(); | ||
|
||
[Reactive] TimeSpan Duration { get; set; } | ||
public IObservable<TimeSpan> PositionChanged => _positionChanged; | ||
public IObservable<TimeSpan> DurationChanged { get; } | ||
public Process? Process { get; private set; } | ||
|
||
public MediaPlayer() | ||
{ | ||
DurationChanged = this.WhenAnyValue(x => x.Duration); | ||
} | ||
|
||
public string GetTitle() | ||
{ | ||
if (_mainWindow is null) | ||
{ | ||
return ""; | ||
} | ||
|
||
var title = _mainWindow.Title; | ||
|
||
return title.Replace("- Vlc media player", string.Empty).Trim(); | ||
} | ||
|
||
public void Initialize(string fileName) | ||
{ | ||
_application = Application.Launch("", fileName); | ||
InitializeInternal(); | ||
} | ||
|
||
|
||
public void Initialize(Window window) | ||
{ | ||
_mainWindow = window; | ||
Process = Process.GetProcessById(window.Properties.ProcessId); | ||
InitializeInternal(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if(_application is null) | ||
{ | ||
return; | ||
} | ||
|
||
_application.Dispose(); | ||
} | ||
|
||
private void InitializeInternal() | ||
{ | ||
while(_mainWindow is not { IsAvailable : true }) | ||
{ | ||
try | ||
{ | ||
_mainWindow = _application!.GetMainWindow(new UIA3Automation()); | ||
} | ||
catch { } | ||
} | ||
|
||
GetDuration(); | ||
GetSlider(); | ||
} | ||
|
||
private void GetDuration() | ||
{ | ||
foreach (var item in _mainWindow!.FindAllDescendants(cf => cf.ByControlType(ControlType.Text))) | ||
{ | ||
var match = DurationRegex().Match(item.Name); | ||
if (match.Success && item.Patterns.LegacyIAccessible.Pattern.Description.Value.Contains("Total")) | ||
{ | ||
var parts = item.Name.Split(':'); | ||
|
||
if(parts.Length == 3) | ||
{ | ||
Duration = new TimeSpan(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2])); | ||
} | ||
else | ||
{ | ||
Duration = new TimeSpan(0, int.Parse(parts[0]), int.Parse(parts[1])); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void GetSlider() | ||
{ | ||
foreach (var item in _mainWindow!.FindAllDescendants(cf => cf.ByControlType(ControlType.Slider)).Select(x => x.AsSlider())) | ||
{ | ||
if (!item.Patterns.LegacyIAccessible.Pattern.Description.Value.Contains('%')) | ||
{ | ||
_slider = item; | ||
var property = item.Patterns.Value.Pattern.PropertyIds.Value; | ||
var handler = item.RegisterPropertyChangedEvent(TreeScope.Element, (ae, _, _) => | ||
{ | ||
var percent = ae.AsSlider().Value; | ||
_positionChanged.OnNext(TimeSpan.FromSeconds(Duration.TotalSeconds * percent / 10000)); | ||
}, property); | ||
} | ||
} | ||
} | ||
|
||
[GeneratedRegex(@"(\d)+:(\d)+")] | ||
private static partial Regex DurationRegex(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Reflection; | ||
using Totoro.Plugins.Contracts; | ||
using Totoro.Plugins.MediaDetection.Contracts; | ||
using Totoro.Plugins.Options; | ||
|
||
namespace Totoro.Plugins.MediaDetection.Vlc; | ||
|
||
public class Plugin : IPlugin<INativeMediaPlayer> | ||
{ | ||
public INativeMediaPlayer Create() => new MediaPlayer(); | ||
|
||
public PluginInfo GetInfo() | ||
{ | ||
return new PluginInfo | ||
{ | ||
Name = "vlc", | ||
DisplayName = "VLC Media Player", | ||
Description = "", | ||
Version = Assembly.GetExecutingAssembly().GetName().Version! | ||
}; | ||
} | ||
|
||
public PluginOptions GetOptions() => new(); | ||
|
||
public void SetOptions(PluginOptions options) { } | ||
|
||
object IPlugin.Create() => Create(); | ||
} |
18 changes: 18 additions & 0 deletions
18
Totoro.Plugins.MediaDetection.Vlc/Totoro.Plugins.MediaDetection.Vlc.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0-windows</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<Platforms>x64</Platforms> | ||
<Version>1.0</Version> | ||
<OutputPath Condition="$(Configuration) == Release">..\Plugins\</OutputPath> | ||
<AppendTargetFrameworkToOutputPath Condition="$(Configuration) == Release">false</AppendTargetFrameworkToOutputPath> | ||
<AppendRuntimeIdentifierToOutputPath Condition="$(Configuration) == Release">false</AppendRuntimeIdentifierToOutputPath> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Totoro.Plugins.MediaDetection\Totoro.Plugins.MediaDetection.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.