Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mariodivece committed Mar 17, 2019
2 parents a851efa + ad7b361 commit 2d98ed5
Show file tree
Hide file tree
Showing 129 changed files with 1,665 additions and 1,267 deletions.
95 changes: 86 additions & 9 deletions Unosquare.FFME.Common/ClosedCaptions/ClosedCaptionPacket.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Unosquare.FFME.ClosedCaptions
{
using Shared;
using Engine;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -202,6 +202,8 @@ public sealed class ClosedCaptionPacket : IComparable<ClosedCaptionPacket>

#region Constructors

private readonly byte[] Data;

/// <summary>
/// Initializes a new instance of the <see cref="ClosedCaptionPacket" /> class.
/// </summary>
Expand Down Expand Up @@ -458,19 +460,14 @@ internal ClosedCaptionPacket(TimeSpan timestamp, byte header, byte d0, byte d1)
finally
{
Channel = FieldParity != 0 && FieldChannel != 0 ?
ComputeChannel(FieldParity, FieldChannel) : CaptionsChannel.CCP;
ComputeChannel(FieldParity, FieldChannel) : Constants.DefaultClosedCaptionsChannel;
}
}

#endregion

#region Properties

/// <summary>
/// Gets the original packet data.
/// </summary>
public byte[] Data { get; }

/// <summary>
/// Gets the first of the two-byte packet data
/// </summary>
Expand Down Expand Up @@ -581,6 +578,69 @@ public byte D1

#endregion

#region Operators

/// <summary>
/// Implements the operator.
/// </summary>
/// <param name="left">The left-hand side operand.</param>
/// <param name="right">The right-hand side operand.</param>
/// <returns>The result of the operation.</returns>
public static bool operator ==(ClosedCaptionPacket left, ClosedCaptionPacket right)
{
if (left is null)
return right is null;

return left.Equals(right);
}

/// <summary>
/// Implements the operator.
/// </summary>
/// <param name="left">The left-hand side operand.</param>
/// <param name="right">The right-hand side operand.</param>
/// <returns>The result of the operation.</returns>
public static bool operator !=(ClosedCaptionPacket left, ClosedCaptionPacket right) =>
!(left == right);

/// <summary>
/// Implements the operator.
/// </summary>
/// <param name="left">The left-hand side operand.</param>
/// <param name="right">The right-hand side operand.</param>
/// <returns>The result of the operation.</returns>
public static bool operator <(ClosedCaptionPacket left, ClosedCaptionPacket right) =>
left == null ? right != null : left.CompareTo(right) < 0;

/// <summary>
/// Implements the operator.
/// </summary>
/// <param name="left">The left-hand side operand.</param>
/// <param name="right">The right-hand side operand.</param>
/// <returns>The result of the operation.</returns>
public static bool operator <=(ClosedCaptionPacket left, ClosedCaptionPacket right) =>
left == null || left.CompareTo(right) <= 0;

/// <summary>
/// Implements the operator.
/// </summary>
/// <param name="left">The left-hand side operand.</param>
/// <param name="right">The right-hand side operand.</param>
/// <returns>The result of the operation.</returns>
public static bool operator >(ClosedCaptionPacket left, ClosedCaptionPacket right) =>
left != null && left.CompareTo(right) > 0;

/// <summary>
/// Implements the operator.
/// </summary>
/// <param name="left">The left-hand side operand.</param>
/// <param name="right">The right-hand side operand.</param>
/// <returns>The result of the operation.</returns>
public static bool operator >=(ClosedCaptionPacket left, ClosedCaptionPacket right) =>
left == null ? right == null : left.CompareTo(right) >= 0;

#endregion

#region Methods

/// <summary>
Expand All @@ -593,7 +653,7 @@ public static CaptionsChannel ComputeChannel(int fieldParity, int fieldChannel)
{
// packets with 0 field parity are null or unknown
if (fieldParity <= 0)
return CaptionsChannel.CCP;
return Constants.DefaultClosedCaptionsChannel;

var parity = fieldParity.Clamp(1, 2);
var channel = fieldChannel.Clamp(1, 2);
Expand Down Expand Up @@ -627,7 +687,7 @@ public override string ToString()
{
string output;
var ts = $"{Timestamp.TotalSeconds:0.0000}";
var channel = Channel == CaptionsChannel.CCP ?
var channel = Channel == Constants.DefaultClosedCaptionsChannel ?
ComputeChannel(FieldParity, FieldChannel) + "*" : Channel + " ";
var prefixData = $"{ts} | {channel} | P: {FieldParity} D: {FieldChannel} | {D0:x2}h {D1:x2}h |";

Expand Down Expand Up @@ -666,6 +726,23 @@ public int CompareTo(ClosedCaptionPacket other)
return Timestamp.Ticks.CompareTo(other.Timestamp.Ticks);
}

/// <inheritdoc />
public override bool Equals(object obj)
{
if (obj is ClosedCaptionPacket other)
return ReferenceEquals(this, other);

return false;
}

/// <inheritdoc />
public override int GetHashCode()
{
return
Timestamp.GetHashCode() ^
Data.GetHashCode();
}

#endregion

#region Static Methods
Expand Down
24 changes: 12 additions & 12 deletions Unosquare.FFME.Common/Commands/CommandManager.Direct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
using Core;
using Decoding;
using Engine;
using Primitives;
using Shared;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -262,7 +262,7 @@ private bool CommandOpenMedia(IMediaInputStream inputStream, Uri streamUri)
var containerConfig = new ContainerConfiguration();

// Convert the URI object to something the Media Container understands (Uri to String)
var mediaUrl = Uri.EscapeUriString(source.ToString());
var mediaSource = Uri.EscapeUriString(source.ToString());

// When opening via URL (and not via custom input stream), fix up the protocols and stuff
if (inputStream == null)
Expand All @@ -277,7 +277,7 @@ private bool CommandOpenMedia(IMediaInputStream inputStream, Uri streamUri)
// The async protocol prefix by default does not ssem to provide
// any performance improvements. Just leaving it for future reference below.
// containerConfig.ProtocolPrefix = "async"
mediaUrl = source.LocalPath;
mediaSource = source.LocalPath;
}
}
catch { /* Ignore exception and continue */ }
Expand All @@ -295,18 +295,18 @@ private bool CommandOpenMedia(IMediaInputStream inputStream, Uri streamUri)
// ReSharper disable once CommentTypo
// Example: streamOptions.PrivateOptions["framerate"] = "20"
containerConfig.ForcedInputFormat = source.Host;
mediaUrl = Uri.UnescapeDataString(source.Query).TrimStart('?');
mediaSource = Uri.UnescapeDataString(source.Query).TrimStart('?');
this.LogInfo(Aspects.EngineCommand,
$"Media URI will be updated. Input Format: {source.Host}, Input Argument: {mediaUrl}");
$"Media URI will be updated. Input Format: {source.Host}, Input Argument: {mediaSource}");
}
}

// Allow the stream input options to be changed
MediaCore.SendOnMediaInitializing(containerConfig, mediaUrl);
MediaCore.SendOnMediaInitializing(containerConfig, mediaSource);

// Instantiate the internal container using either a URL (default) or a custom input stream.
MediaCore.Container = inputStream == null ?
new MediaContainer(mediaUrl, containerConfig, MediaCore) :
new MediaContainer(mediaSource, containerConfig, MediaCore) :
new MediaContainer(inputStream, containerConfig, MediaCore);

// Notify the user media is opening and allow for media options to be modified
Expand Down Expand Up @@ -432,7 +432,7 @@ private void StopWorkers()

// Call close on all renderers
foreach (var renderer in MediaCore.Renderers.Values)
renderer.Close();
renderer.OnClose();

// Remove the renderers disposing of them
MediaCore.Renderers.Clear();
Expand Down Expand Up @@ -481,7 +481,7 @@ private void InitializeRendering()
// We always remove the audio renderer in case there is a change in audio device.
if (MediaCore.Renderers.ContainsKey(MediaType.Audio))
{
MediaCore.Renderers[MediaType.Audio].Close();
MediaCore.Renderers[MediaType.Audio].OnClose();
MediaCore.Renderers.Remove(MediaType.Audio);
}

Expand All @@ -501,7 +501,7 @@ private void InitializeRendering()
if (!MediaCore.Renderers.ContainsKey(t))
continue;

MediaCore.Renderers[t].Close();
MediaCore.Renderers[t].OnClose();
MediaCore.Renderers.Remove(t);
}

Expand Down Expand Up @@ -531,7 +531,7 @@ private void InitializeRendering()
MediaCore.Renderers[t] = MediaEngine.Platform.CreateRenderer(t, MediaCore);

MediaCore.Blocks[t].Clear();
MediaCore.Renderers[t].WaitForReadyState();
MediaCore.Renderers[t].OnStarting();
MediaCore.InvalidateRenderer(t);
}
}
Expand All @@ -542,7 +542,7 @@ private void InitializeRendering()
private void PreLoadSubtitles()
{
DisposePreloadedSubtitles();
var subtitlesUrl = MediaCore.MediaOptions.SubtitlesUrl;
var subtitlesUrl = MediaCore.MediaOptions.SubtitlesSource;

// Don't load a thing if we don't have to
if (string.IsNullOrWhiteSpace(subtitlesUrl))
Expand Down
8 changes: 4 additions & 4 deletions Unosquare.FFME.Common/Commands/CommandManager.Priority.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Unosquare.FFME.Commands
{
using Engine;
using Primitives;
using Shared;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
Expand Down Expand Up @@ -73,7 +73,7 @@ private void ClearPriorityCommands()
private bool CommandPlayMedia()
{
foreach (var renderer in MediaCore.Renderers.Values)
renderer.Play();
renderer.OnPlay();

State.UpdateMediaState(PlaybackStatus.Play);

Expand All @@ -92,7 +92,7 @@ private bool CommandPauseMedia()
MediaCore.PausePlayback();

foreach (var renderer in MediaCore.Renderers.Values)
renderer.Pause();
renderer.OnPause();

MediaCore.ChangePlaybackPosition(SnapPositionToBlockPosition(MediaCore.PlaybackPosition));
State.UpdateMediaState(PlaybackStatus.Pause);
Expand All @@ -113,7 +113,7 @@ private bool CommandStopMedia()
SeekMedia(new SeekOperation(TimeSpan.MinValue, SeekMode.Stop), CancellationToken.None);

foreach (var renderer in MediaCore.Renderers.Values)
renderer.Stop();
renderer.OnStop();

State.UpdateMediaState(PlaybackStatus.Stop);
return true;
Expand Down
12 changes: 7 additions & 5 deletions Unosquare.FFME.Common/Commands/CommandManager.Seek.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Unosquare.FFME.Commands
{
using Engine;
using Primitives;
using Shared;
using System;
using System.Runtime.CompilerServices;
using System.Threading;
Expand Down Expand Up @@ -314,7 +314,7 @@ private bool TrySignalBlocksAvailable(SeekMode mode, MediaType main, MediaBlockB
// We need to update the clock immediately because
// the renderer will need this position
MediaCore.ChangePlaybackPosition(mode != SeekMode.Normal && mode != SeekMode.Stop
? mainBlocks[targetPosition].StartTime
? mainBlocks[targetPosition.Ticks].StartTime
: targetPosition);

SeekBlocksAvailable.Set();
Expand Down Expand Up @@ -388,11 +388,13 @@ private void Dispose(bool alsoManaged)
lock (SyncLock)
{
if (IsDisposed) return;
SeekCompleted.Set();

if (alsoManaged)
SeekCompleted.Dispose();

IsDisposed = true;
}

SeekCompleted.Set();
SeekCompleted.Dispose();
}
}

Expand Down
2 changes: 1 addition & 1 deletion Unosquare.FFME.Common/Commands/CommandManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Unosquare.FFME.Commands
{
using Core;
using Engine;
using Primitives;
using Shared;
using System;
using System.Runtime.CompilerServices;
using System.Text;
Expand Down
20 changes: 9 additions & 11 deletions Unosquare.FFME.Common/Core/FFAudioParams.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Unosquare.FFME.Core
{
using Engine;
using FFmpeg.AutoGen;
using Shared;
using System;

/// <summary>
Expand All @@ -15,7 +15,12 @@ internal sealed unsafe class FFAudioParams
/// <summary>
/// The standard output audio spec
/// </summary>
public static readonly FFAudioParams Output;
public static readonly FFAudioParams Output = new FFAudioParams
{
ChannelCount = Constants.AudioChannelCount,
SampleRate = Constants.AudioSampleRate,
Format = Constants.AudioSampleFormat
};

#endregion

Expand All @@ -26,17 +31,10 @@ internal sealed unsafe class FFAudioParams
/// </summary>
static FFAudioParams()
{
Output = new FFAudioParams
{
ChannelCount = Constants.Audio.ChannelCount,
SampleRate = Constants.Audio.SampleRate,
Format = Constants.Audio.SampleFormat
};

Output.ChannelLayout = ffmpeg.av_get_default_channel_layout(Output.ChannelCount);
Output.SamplesPerChannel = Output.SampleRate;
Output.BufferLength = ffmpeg.av_samples_get_buffer_size(
null, Output.ChannelCount, Output.SamplesPerChannel + Constants.Audio.BufferPadding, Output.Format, 1);
null, Output.ChannelCount, Output.SamplesPerChannel + Constants.AudioBufferPadding, Output.Format, 1);
}

/// <summary>
Expand Down Expand Up @@ -133,7 +131,7 @@ internal static FFAudioParams CreateTarget(AVFrame* frame)
// The target transform is just a ratio of the source frame's sample. This is how many samples we desire
spec.SamplesPerChannel = Convert.ToInt32(Convert.ToDouble(frame->nb_samples) * spec.SampleRate / frame->sample_rate);
spec.BufferLength = ffmpeg.av_samples_get_buffer_size(
null, spec.ChannelCount, spec.SamplesPerChannel + Constants.Audio.BufferPadding, spec.Format, 1);
null, spec.ChannelCount, spec.SamplesPerChannel + Constants.AudioBufferPadding, spec.Format, 1);
return spec;
}

Expand Down
4 changes: 2 additions & 2 deletions Unosquare.FFME.Common/Core/FFInterop.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Unosquare.FFME.Core
{
using Engine;
using FFmpeg.AutoGen;
using Shared;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand Down Expand Up @@ -339,7 +339,7 @@ private static unsafe void OnFFmpegMessageLogged(void* p0, int level, string for
if (FFmpegLogLevels.ContainsKey(level))
messageType = FFmpegLogLevels[level];

if (!line.EndsWith("\n", StringComparison.InvariantCulture)) return;
if (!line.EndsWith("\n", StringComparison.Ordinal)) return;
line = string.Join(string.Empty, FFmpegLogBuffer);
line = line.TrimEnd();
FFmpegLogBuffer.Clear();
Expand Down
Loading

0 comments on commit 2d98ed5

Please sign in to comment.