Skip to content

Commit

Permalink
Added support for movie sync with Plex plugins in Kodi
Browse files Browse the repository at this point in the history
  • Loading branch information
nicko88 committed Aug 20, 2022
1 parent e781b57 commit 71b6cca
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
6 changes: 3 additions & 3 deletions HTFanControl/HTFanControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
<ApplicationIcon>htfancontrol.ico</ApplicationIcon>
<Configurations>Debug;ReleaseWin;ReleaseLinux</Configurations>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<AssemblyVersion>0.3.2.0</AssemblyVersion>
<FileVersion>0.3.2.0</FileVersion>
<Version>0.3.2</Version>
<AssemblyVersion>0.3.3.0</AssemblyVersion>
<FileVersion>0.3.3.0</FileVersion>
<Version>0.3.3</Version>
<Description>4D Theater Wind Effect - DIY Home Theater Project</Description>
<Copyright>nicko88</Copyright>
<Authors>nicko88</Authors>
Expand Down
2 changes: 1 addition & 1 deletion HTFanControl/Main/WebUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private string GetStatusData()
{
if (!string.IsNullOrEmpty(_HTFanCtrl._loadedVideoFilename) && _HTFanCtrl._loadedVideoFilename != "Loading Video Fingerprints...")
{
htmlData.AppendLine("No wind track file found named: " + _HTFanCtrl._loadedVideoFilename + ".txt");
htmlData.AppendLine("No wind track file found named: " + _HTFanCtrl._loadedVideoFilename + ".zip/.txt");
htmlData.AppendLine("<br /><br />");
}
htmlData.AppendLine("<b>Current Command:</b>");
Expand Down
35 changes: 32 additions & 3 deletions HTFanControl/Players/KodiPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Web;
Expand All @@ -10,8 +11,8 @@ namespace HTFanControl.Players
class KodiPlayer : IPlayer
{
private HttpClient _httpClient;

private Settings _settings;
private string _playerID = null;

public bool IsPlaying { get; private set; }
public long VideoTime { get; private set; }
Expand All @@ -34,7 +35,16 @@ public bool Update()
{
try
{
StringContent filenameJSONRequest = new StringContent(@"{""jsonrpc"": ""2.0"", ""method"": ""Player.GetItem"", ""params"": {""properties"": [""file""], ""playerid"": 1}, ""id"": 1 }", System.Text.Encoding.UTF8, "application/json");
if(_playerID is null)
{
StringContent playerIDJSONRequest = new StringContent(@"{""jsonrpc"": ""2.0"", ""method"": ""Player.GetActivePlayers"", ""id"": 1}", System.Text.Encoding.UTF8, "application/json");
string playerIDJSONResponse = _httpClient.PostAsync($"http://{_settings.MediaPlayerIP}:{_settings.MediaPlayerPort}/jsonrpc", playerIDJSONRequest).Result.Content.ReadAsStringAsync().Result;

using JsonDocument playerIdJSON = JsonDocument.Parse(playerIDJSONResponse);
_playerID = playerIdJSON.RootElement.GetProperty("result")[0].GetProperty("playerid").GetRawText();
}

StringContent filenameJSONRequest = new StringContent(@"{""jsonrpc"": ""2.0"", ""method"": ""Player.GetItem"", ""params"": {""properties"": [""file""], ""playerid"": 1}, ""id"": " + "1" + "}", System.Text.Encoding.UTF8, "application/json");
string filenameJSONResponse = _httpClient.PostAsync($"http://{_settings.MediaPlayerIP}:{_settings.MediaPlayerPort}/jsonrpc", filenameJSONRequest).Result.Content.ReadAsStringAsync().Result;

using JsonDocument fileInfoJSON = JsonDocument.Parse(filenameJSONResponse);
Expand All @@ -44,6 +54,25 @@ public bool Update()
FileName = fileInfo.Item1;
FilePath = fileInfo.Item2;

//for PlexKodiConnect plugin
if (FileName.Contains("plex_id"))
{
FileName = FileName.Substring(FileName.LastIndexOf("&filename=") + 10);
}

//for Plex for Kodi plugin
if(filePath.Contains("plex.direct"))
{
if(filePath.Contains("/transcode/"))
{
FileName = "Video being transcoded by Plex, only Direct Play (Original quality) supported";
}
else
{
FileName = new string(fileInfoJSON.RootElement.GetProperty("result").GetProperty("item").GetProperty("label").GetString().Where(ch => !Path.GetInvalidFileNameChars().Contains(ch)).ToArray());
}
}

bool getKodiTime = true;

if (_settings.MediaPlayerType == "KodiMPC")
Expand Down Expand Up @@ -73,7 +102,7 @@ public bool Update()

if (getKodiTime)
{
StringContent timeJSONRequest = new StringContent(@"{""jsonrpc"": ""2.0"", ""method"": ""Player.GetProperties"", ""params"": {""properties"": [""time"", ""speed""], ""playerid"": 1}, ""id"": 1}", System.Text.Encoding.UTF8, "application/json");
StringContent timeJSONRequest = new StringContent(@"{""jsonrpc"": ""2.0"", ""method"": ""Player.GetProperties"", ""params"": {""properties"": [""time"", ""speed""], ""playerid"": 1}, ""id"": " + _playerID + "}", System.Text.Encoding.UTF8, "application/json");
string timeJSONResponse = _httpClient.PostAsync($"http://{_settings.MediaPlayerIP}:{_settings.MediaPlayerPort}/jsonrpc", timeJSONRequest).Result.Content.ReadAsStringAsync().Result;

using JsonDocument time = JsonDocument.Parse(timeJSONResponse);
Expand Down
13 changes: 11 additions & 2 deletions HTFanControl/Players/MPCPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace HTFanControl.Players
class MPCPlayer : IPlayer
{
private HttpClient _httpClient;

private Settings _settings;
private string _playerID = null;

public bool IsPlaying { get; private set; }
public long VideoTime { get; private set; }
Expand Down Expand Up @@ -70,7 +70,16 @@ public bool Update()

private void GetFileFromKodi()
{
StringContent filenameJSONRequest = new StringContent(@"{""jsonrpc"": ""2.0"", ""method"": ""Player.GetItem"", ""params"": {""properties"": [""file""], ""playerid"": 1}, ""id"": 1 }", System.Text.Encoding.UTF8, "application/json");
if (_playerID is null)
{
StringContent playerIDJSONRequest = new StringContent(@"{""jsonrpc"": ""2.0"", ""method"": ""Player.GetActivePlayers"", ""id"": 1}", System.Text.Encoding.UTF8, "application/json");
string playerIDJSONResponse = _httpClient.PostAsync($"http://{_settings.MediaPlayerIP}:8080/jsonrpc", playerIDJSONRequest).Result.Content.ReadAsStringAsync().Result;

using JsonDocument playerIdJSON = JsonDocument.Parse(playerIDJSONResponse);
_playerID = playerIdJSON.RootElement.GetProperty("result")[0].GetProperty("playerid").GetRawText();
}

StringContent filenameJSONRequest = new StringContent(@"{""jsonrpc"": ""2.0"", ""method"": ""Player.GetItem"", ""params"": {""properties"": [""file""], ""playerid"": 1}, ""id"": " + _playerID + "}", System.Text.Encoding.UTF8, "application/json");

HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(1);
Expand Down
5 changes: 5 additions & 0 deletions HTFanControl/html/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@
document.getElementById('mediaPlayerDiv').style.display = 'block';
document.getElementById('plexDiv').style.display = 'block';
document.getElementById('audioDiv').style.display = 'none';

var msg = "Direct Plex connection is not recommended as it doesn't provide accurate time sync."
msg += "\r\n\r\n";
msg += "Please use Plex plugin for Kodi or Audio (microphone) sync methods instead.";
alert(msg);
}
function audio()
{
Expand Down

0 comments on commit 71b6cca

Please sign in to comment.