Skip to content

Commit

Permalink
Fixed MediaPlayerObserver Excetion
Browse files Browse the repository at this point in the history
Changed Directory of database
  • Loading branch information
Zexuz committed May 2, 2019
1 parent 77a02f8 commit fedffe4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Mpv.JsonIpc/Manager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<Response<T>> Execute<T>(Request request)

var messageToSend = JsonConvert.SerializeObject(request);
await _pipeWriter.WriteLineAsync(messageToSend);
await _pipeWriter.FlushAsync();
await _pipeWriter.FlushAsync(); //TODO Maybe return a flag instead of just throwing...

var counter = 0;
while (true)
Expand Down
6 changes: 4 additions & 2 deletions SnackTime.Core/Database/DatabaseFactory.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System.IO;
using LiteDB;

namespace SnackTime.Core.Database
{
public class DatabaseFactory
{
private const string ConnectionString = "Media.db";

public LiteDatabase GetDatabase()
{
return new LiteDatabase(ConnectionString);
var connectionString = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Media.db");
return new LiteDatabase(connectionString);
}

public LiteRepository GetRepository()
Expand Down
62 changes: 32 additions & 30 deletions SnackTime.WebApi/MediaPlayerObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Mpv.JsonIpc;
using SnackTime.Core;
using SnackTime.Core.Session;
Expand All @@ -12,23 +13,26 @@ namespace SnackTime.WebApi
{
public class MediaPlayerObserver : IHostedService
{
private readonly Queue<Item> _queue;
private readonly SessionFactory _sessionFactory;
private readonly IApi _api;
private readonly TimeService _timeService;
private readonly ISessionRepoFactory _sessionRepoFactory;
private readonly ILogger<MediaPlayerObserver> _logger;
private readonly Queue<Item> _queue;
private readonly SessionFactory _sessionFactory;
private readonly IApi _api;
private readonly TimeService _timeService;
private readonly ISessionRepoFactory _sessionRepoFactory;

private CancellationToken _token;
private Task _task;

public MediaPlayerObserver
(
ILogger<MediaPlayerObserver> logger,
Queue<Item> queue,
SessionFactory sessionFactory,
IApi api, TimeService timeService,
ISessionRepoFactory sessionRepoFactory
)
{
_logger = logger;
_queue = queue;
_sessionFactory = sessionFactory;
_api = api;
Expand All @@ -49,37 +53,35 @@ private async Task Tick()
{
await Task.Delay(500 * 1, _token);

if (_queue.HasItems())
if (currentSession != null)
{
if (currentSession != null)
{
//update the current session before overiing it
}

var item = _queue.Pop();
await _api.PlayMedia(item.Path, item.StartPosition);
await _api.ShowText($"Now playing {item.Path.Substring(item.Path.LastIndexOf('\\') + 1)}", TimeSpan.FromSeconds(5));

var duration = await _api.GetDuration();
currentSession = _sessionFactory.CreateNewSession(item.MediaFileId, duration);
await UpdateCurrentSession(currentSession);
}

if (currentSession != null)
{
currentSession.Duration.EndPostionInSec = (await _api.GetCurrentPosition()).TotalSeconds;
currentSession.EndUTC = _timeService.GetCurrentTimeAsUnixSeconds();
if (!_queue.HasItems()) continue;

var sessionRepo = await _sessionRepoFactory.GetRepo();
await sessionRepo.UpsertSession(currentSession);
}
var item = _queue.Pop();
await _api.PlayMedia(item.Path, item.StartPosition);
await _api.ShowText($"Now playing {item.Path.Substring(item.Path.LastIndexOf('\\') + 1)}", TimeSpan.FromSeconds(5));

//How do we create a new WatchSession?
// Add a WatchQueue, the rest endpoint insted of starting the service
// it adds the request to a queue, and here we look for that queue
// And for every new Request, we create a new watchSession
var duration = await _api.GetDuration();
currentSession = _sessionFactory.CreateNewSession(item.MediaFileId, duration);
}
}

private async Task UpdateCurrentSession(Session currentSession)
{
try
{
currentSession.Duration.EndPostionInSec = (await _api.GetCurrentPosition()).TotalSeconds;
currentSession.EndUTC = _timeService.GetCurrentTimeAsUnixSeconds();

// And if we are already playing a video, and receive a new item in out queue,
// We replaces the media playing and add a new watch session
var sessionRepo = await _sessionRepoFactory.GetRepo();
await sessionRepo.UpsertSession(currentSession);
}
catch (Exception e)
{
_logger.LogWarning(e, "Received error when trying to get current position");
}
}

Expand Down
2 changes: 1 addition & 1 deletion SnackTime.sln
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ Global
{601FBF93-D8EE-4D3D-93CB-3D9799D1DE32} = {089BC780-7731-4F03-BDC0-0623325D0746}
{9814ADC0-24D5-413C-8EC6-C33CB820555C} = {089BC780-7731-4F03-BDC0-0623325D0746}
{8099FC72-B870-4943-A393-9D5F56124758} = {61B95832-EC6A-41F1-9AEC-1A2CBCFA5733}
{E830AE20-451E-45F1-9AEF-CB680A06A283} = {5E40D03A-E726-4AAC-AA95-58D212B407F1}
{E830AE20-451E-45F1-9AEF-CB680A06A283} = {61B95832-EC6A-41F1-9AEC-1A2CBCFA5733}
EndGlobalSection
EndGlobal

0 comments on commit fedffe4

Please sign in to comment.