Skip to content

Commit 2fe47bc

Browse files
committed
fixing minor item with media worker set as suggested by codacy
1 parent b546ade commit 2fe47bc

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

Unosquare.FFME.Common/Workers/MediaWorkerSet.cs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using Primitives;
44
using System;
5+
using System.Collections.Generic;
56
using System.Threading.Tasks;
67

78
/// <summary>
@@ -124,29 +125,11 @@ public void ResumePaused() => Resume(
124125
/// <param name="render">if set to <c>true</c> executes the opration on the rendering worker.</param>
125126
private void Pause(bool wait, bool read, bool decode, bool render)
126127
{
127-
if (IsDisposed) return;
128-
129-
var tasks = new Task[(read ? 1 : 0) + (decode ? 1 : 0) + (render ? 1 : 0)];
130-
var index = 0;
131-
if (read)
132-
{
133-
tasks[index] = Reading.PauseAsync();
134-
index++;
135-
}
136-
137-
if (decode)
138-
{
139-
tasks[index] = Decoding.PauseAsync();
140-
index++;
141-
}
142-
143-
if (render)
144-
{
145-
tasks[index] = Rendering.PauseAsync();
146-
}
128+
if (IsDisposed)
129+
return;
147130

148-
if (wait)
149-
Task.WaitAll(tasks);
131+
var tasks = CaptureTasks(read, decode, render, WorkerState.Paused);
132+
if (wait) Task.WaitAll(tasks);
150133
}
151134

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

163-
var tasks = new Task[(read ? 1 : 0) + (decode ? 1 : 0) + (render ? 1 : 0)];
164-
var index = 0;
165-
if (read)
166-
{
167-
tasks[index] = Reading.ResumeAsync();
168-
index++;
169-
}
146+
var tasks = CaptureTasks(read, decode, render, WorkerState.Running);
147+
if (wait) Task.WaitAll(tasks);
148+
}
170149

171-
if (decode)
172-
{
173-
tasks[index] = Decoding.ResumeAsync();
174-
index++;
175-
}
150+
/// <summary>
151+
/// Captures the awaitable tasks for the given workers.
152+
/// </summary>
153+
/// <param name="read">The read worker.</param>
154+
/// <param name="decode">The decode worker.</param>
155+
/// <param name="render">The render worker.</param>
156+
/// <param name="targetState">The target state.</param>
157+
/// <returns>The awaitable tasks.</returns>
158+
private Task<WorkerState>[] CaptureTasks(bool read, bool decode, bool render, WorkerState targetState)
159+
{
160+
var tasks = new List<Task<WorkerState>>(3);
161+
var workers = new List<IMediaWorker>(3);
162+
163+
if (read) workers.Add(Reading);
164+
if (decode) workers.Add(Decoding);
165+
if (render) workers.Add(Rendering);
176166

177-
if (render)
167+
foreach (var worker in workers)
178168
{
179-
tasks[index] = Rendering.ResumeAsync();
169+
switch (targetState)
170+
{
171+
case WorkerState.Paused:
172+
tasks.Add(worker.PauseAsync());
173+
break;
174+
case WorkerState.Running:
175+
tasks.Add(worker.ResumeAsync());
176+
break;
177+
case WorkerState.Stopped:
178+
tasks.Add(worker.StopAsync());
179+
break;
180+
default:
181+
throw new NotSupportedException($"{nameof(targetState)} '{targetState}' is not supported.");
182+
}
180183
}
181184

182-
if (wait)
183-
Task.WaitAll(tasks);
185+
return tasks.ToArray();
184186
}
185187

186188
/// <summary>

0 commit comments

Comments
 (0)