1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

Fixed: A bug caused way too much data being read during MediaInfo discovery (often the entire file).

Fixed: Further optimized the MediaInfo discovery to reduce the read data to a couple of dozen kbytes, but less detailed.
This commit is contained in:
Taloth Saldono 2015-06-02 21:57:47 +02:00
parent 724981db57
commit 1fbbfb3317
2 changed files with 48 additions and 6 deletions

View File

@ -2,10 +2,20 @@
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Instrumentation;
using NLog;
namespace NzbDrone.Core.MediaFiles.MediaInfo namespace NzbDrone.Core.MediaFiles.MediaInfo
{ {
[Flags]
public enum BufferStatus
{
Accepted = 1,
Filled = 2,
Updated = 4,
Finalized = 8
}
public enum StreamKind public enum StreamKind
{ {
General, General,
@ -14,7 +24,7 @@ public enum StreamKind
Text, Text,
Other, Other,
Image, Image,
Menu, Menu
} }
public enum InfoKind public enum InfoKind
@ -48,6 +58,7 @@ public enum InfoFileOptions
public class MediaInfo : IDisposable public class MediaInfo : IDisposable
{ {
private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(MediaInfo));
private IntPtr _handle; private IntPtr _handle;
public bool MustUseAnsi { get; set; } public bool MustUseAnsi { get; set; }
@ -172,31 +183,40 @@ public int Open(string fileName)
public int Open(Stream stream) public int Open(Stream stream)
{ {
var buffer = new byte[64 * 1024];
var isValid = (int)MediaInfo_Open_Buffer_Init(_handle, stream.Length, 0); var isValid = (int)MediaInfo_Open_Buffer_Init(_handle, stream.Length, 0);
if (isValid == 1) if (isValid == 1)
{ {
var buffer = new byte[16 * 1024];
long seekStart = 0;
long totalRead = 0;
int bufferRead; int bufferRead;
do do
{ {
bufferRead = stream.Read(buffer, 0, buffer.Length); bufferRead = stream.Read(buffer, 0, buffer.Length);
totalRead += bufferRead;
if (MediaInfo_Open_Buffer_Continue(_handle, buffer, (IntPtr)bufferRead) == (IntPtr)0) var status = (BufferStatus)MediaInfo_Open_Buffer_Continue(_handle, buffer, (IntPtr)bufferRead);
if (status.HasFlag(BufferStatus.Finalized) || status <= 0 || bufferRead == 0)
{ {
Logger.Trace("Read file offset {0}-{1} ({2} bytes)", seekStart, stream.Position, stream.Position - seekStart);
break; break;
} }
var seekPos = MediaInfo_Open_Buffer_Continue_GoTo_Get(_handle); var seekPos = MediaInfo_Open_Buffer_Continue_GoTo_Get(_handle);
if (seekPos != -1) if (seekPos != -1)
{ {
Logger.Trace("Read file offset {0}-{1} ({2} bytes)", seekStart, stream.Position, stream.Position - seekStart);
seekPos = stream.Seek(seekPos, SeekOrigin.Begin); seekPos = stream.Seek(seekPos, SeekOrigin.Begin);
seekStart = seekPos;
MediaInfo_Open_Buffer_Init(_handle, stream.Length, seekPos); MediaInfo_Open_Buffer_Init(_handle, stream.Length, seekPos);
} }
} while (bufferRead > 0); } while (bufferRead > 0);
MediaInfo_Open_Buffer_Finalize(_handle); MediaInfo_Open_Buffer_Finalize(_handle);
Logger.Trace("Read a total of {0} bytes ({1:0.0}%)", totalRead, totalRead * 100.0 / stream.Length);
} }
return isValid; return isValid;

View File

@ -39,7 +39,7 @@ public MediaInfoModel GetMediaInfo(string filename)
mediaInfo = new MediaInfo(); mediaInfo = new MediaInfo();
_logger.Debug("Getting media info from {0}", filename); _logger.Debug("Getting media info from {0}", filename);
mediaInfo.Option("ParseSpeed", "0.2"); mediaInfo.Option("ParseSpeed", "0.0");
int open; int open;
@ -48,6 +48,28 @@ public MediaInfoModel GetMediaInfo(string filename)
open = mediaInfo.Open(stream); open = mediaInfo.Open(stream);
} }
if (open != 0)
{
int audioRuntime;
int videoRuntime;
int generalRuntime;
//Runtime
Int32.TryParse(mediaInfo.Get(StreamKind.Video, 0, "PlayTime"), out videoRuntime);
Int32.TryParse(mediaInfo.Get(StreamKind.Audio, 0, "PlayTime"), out audioRuntime);
Int32.TryParse(mediaInfo.Get(StreamKind.General, 0, "PlayTime"), out generalRuntime);
if (audioRuntime == 0 && videoRuntime == 0 && generalRuntime == 0)
{
mediaInfo.Option("ParseSpeed", "1.0");
using (var stream = _diskProvider.OpenReadStream(filename))
{
open = mediaInfo.Open(stream);
}
}
}
if (open != 0) if (open != 0)
{ {
int width; int width;