From c493ccafce42e0605dcf2845755452d5d291a8e3 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 27 Sep 2019 01:25:25 +0200 Subject: [PATCH] Making memory management more efficient. --- Kyoo.Transcoder/src/Transcoder.cpp | 7 ++- Kyoo.Transcoder/src/Transcoder.h | 4 +- Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs | 48 +++++++++++--------- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/Kyoo.Transcoder/src/Transcoder.cpp b/Kyoo.Transcoder/src/Transcoder.cpp index 170858f7..43886bcb 100644 --- a/Kyoo.Transcoder/src/Transcoder.cpp +++ b/Kyoo.Transcoder/src/Transcoder.cpp @@ -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; } \ No newline at end of file diff --git a/Kyoo.Transcoder/src/Transcoder.h b/Kyoo.Transcoder/src/Transcoder.h index c2750476..ff565417 100644 --- a/Kyoo.Transcoder/src/Transcoder.h +++ b/Kyoo.Transcoder/src/Transcoder.h @@ -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); diff --git a/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs b/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs index 656f4845..4fd7d326 100644 --- a/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs +++ b/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs @@ -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(); + int size = Marshal.SizeOf(); - 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(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(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); } } }