Merge pull request #2 from AnonymusRaccoon/beta

Making memory management more efficient.
This commit is contained in:
Zoe Roux 2019-09-27 01:26:57 +02:00
commit 51694fbc53
3 changed files with 35 additions and 24 deletions

View File

@ -222,7 +222,7 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount
if (inputCodecpar->codec_type != AVMEDIA_TYPE_SUBTITLE)
outputList[i] = NULL;
else
{
{
//Get metadata for file name
Stream stream(NULL, //title
av_dict_get(inputStream->metadata, "language", NULL, 0)->value, //language
@ -344,4 +344,9 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount
return subtitleStreams;
//*streamCount = subtitleStreams->size();
//return subtitleStreams->data();
}
void FreeMemory(Stream *streamsPtr)
{
delete[] streamsPtr;
}

View File

@ -11,7 +11,9 @@
extern "C" API int Init();
extern "C" API int Transmux(const char *path, const char *outPath);
//Take the path of the file and the path of the output directory. It will return the list of subtitle streams in the streams variable. The int returned is the number of subtitles extracted.
extern "C" API Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount, int *subtitleCount);
extern "C" API int Transmux(const char *path, const char *outPath);
extern "C" API void FreeMemory(Stream *streamsPtr);

View File

@ -16,33 +16,37 @@ namespace Kyoo.InternalAPI.TranscoderLink
public extern static int Transmux(string path, string outPath);
[DllImport(TranscoderPath, CallingConvention = CallingConvention.Cdecl)]
[return:MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStruct, SizeParamIndex = 3)]
private extern static Stream[] ExtractSubtitles(string path, string outPath, out int arrayLength/*, out int trackCount*/);
private extern static IntPtr ExtractSubtitles(string path, string outPath, out int arrayLength, out int trackCount);
[DllImport(TranscoderPath, CallingConvention = CallingConvention.Cdecl)]
private extern static void FreeMemory(IntPtr streamsPtr);
public static void ExtractSubtitles(string path, string outPath, out Track[] tracks)
{
//int size = Marshal.SizeOf<Stream>();
int size = Marshal.SizeOf<Stream>();
Stream[] streamsPtr = ExtractSubtitles(path, outPath, out int count/*, out int trackCount*/);
tracks = null;
//if (trackCount > 0)
//{
// tracks = new Track[trackCount];
IntPtr ptr = ExtractSubtitles(path, outPath, out int arrayLength, out int trackCount);
IntPtr streamsPtr = ptr;
if (trackCount > 0)
{
tracks = new Track[trackCount];
// int j = 0;
// for (int i = 0; i < arrayLength; i++)
// {
// Stream stream = Marshal.PtrToStructure<Stream>(streamsPtr);
// if (stream.Codec != null) //If the codec is null, the stream doesn't represent a subtitle.
// {
// tracks[j] = Track.From(stream, StreamType.Subtitle);
// j++;
// }
// streamsPtr += size;
// }
//}
//else
// tracks = null;
int j = 0;
for (int i = 0; i < arrayLength; i++)
{
Stream stream = Marshal.PtrToStructure<Stream>(streamsPtr);
if (stream.Codec != null) //If the codec is null, the stream doesn't represent a subtitle.
{
tracks[j] = Track.From(stream, StreamType.Subtitle);
j++;
}
streamsPtr += size;
}
}
else
tracks = null;
FreeMemory(ptr);
}
}
}