Skip to content

Commit

Permalink
fixing minor item with media worker set as suggested by codacy
Browse files Browse the repository at this point in the history
  • Loading branch information
mariodivece committed Mar 17, 2019
1 parent b546ade commit 2fe47bc
Showing 1 changed file with 40 additions and 38 deletions.
78 changes: 40 additions & 38 deletions Unosquare.FFME.Common/Workers/MediaWorkerSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using Primitives;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

/// <summary>
Expand Down Expand Up @@ -124,29 +125,11 @@ public void ResumePaused() => Resume(
/// <param name="render">if set to <c>true</c> executes the opration on the rendering worker.</param>
private void Pause(bool wait, bool read, bool decode, bool render)
{
if (IsDisposed) return;

var tasks = new Task[(read ? 1 : 0) + (decode ? 1 : 0) + (render ? 1 : 0)];
var index = 0;
if (read)
{
tasks[index] = Reading.PauseAsync();
index++;
}

if (decode)
{
tasks[index] = Decoding.PauseAsync();
index++;
}

if (render)
{
tasks[index] = Rendering.PauseAsync();
}
if (IsDisposed)
return;

if (wait)
Task.WaitAll(tasks);
var tasks = CaptureTasks(read, decode, render, WorkerState.Paused);
if (wait) Task.WaitAll(tasks);
}

/// <summary>
Expand All @@ -160,27 +143,46 @@ private void Resume(bool wait, bool read, bool decode, bool render)
{
if (IsDisposed) return;

var tasks = new Task[(read ? 1 : 0) + (decode ? 1 : 0) + (render ? 1 : 0)];
var index = 0;
if (read)
{
tasks[index] = Reading.ResumeAsync();
index++;
}
var tasks = CaptureTasks(read, decode, render, WorkerState.Running);
if (wait) Task.WaitAll(tasks);
}

if (decode)
{
tasks[index] = Decoding.ResumeAsync();
index++;
}
/// <summary>
/// Captures the awaitable tasks for the given workers.
/// </summary>
/// <param name="read">The read worker.</param>
/// <param name="decode">The decode worker.</param>
/// <param name="render">The render worker.</param>
/// <param name="targetState">The target state.</param>
/// <returns>The awaitable tasks.</returns>
private Task<WorkerState>[] CaptureTasks(bool read, bool decode, bool render, WorkerState targetState)
{
var tasks = new List<Task<WorkerState>>(3);
var workers = new List<IMediaWorker>(3);

if (read) workers.Add(Reading);
if (decode) workers.Add(Decoding);
if (render) workers.Add(Rendering);

if (render)
foreach (var worker in workers)
{
tasks[index] = Rendering.ResumeAsync();
switch (targetState)
{
case WorkerState.Paused:
tasks.Add(worker.PauseAsync());
break;
case WorkerState.Running:
tasks.Add(worker.ResumeAsync());
break;
case WorkerState.Stopped:
tasks.Add(worker.StopAsync());
break;
default:
throw new NotSupportedException($"{nameof(targetState)} '{targetState}' is not supported.");
}
}

if (wait)
Task.WaitAll(tasks);
return tasks.ToArray();
}

/// <summary>
Expand Down

0 comments on commit 2fe47bc

Please sign in to comment.