From 229be6e4b9780b90c35ee030b63b8edf5bc5eb91 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 27 Sep 2019 01:54:28 +0200 Subject: [PATCH] Making a few test with the C# memory management. --- Kyoo.Transcoder/src/Transcoder.cpp | 24 +++++++++++++++----- Kyoo.Transcoder/src/Transcoder.h | 4 +++- Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs | 8 ++++++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/Kyoo.Transcoder/src/Transcoder.cpp b/Kyoo.Transcoder/src/Transcoder.cpp index 43886bcb..15ff0e20 100644 --- a/Kyoo.Transcoder/src/Transcoder.cpp +++ b/Kyoo.Transcoder/src/Transcoder.cpp @@ -205,13 +205,14 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount AVFormatContext *inputContext = NULL; if (open_input_context(&inputContext, path) != 0) - return 0; + return nullptr; *streamCount = inputContext->nb_streams; *subtitleCount = 0; Stream *subtitleStreams = new Stream[*streamCount]; + const unsigned int outputCount = inputContext->nb_streams; - AVFormatContext **outputList = new AVFormatContext * [outputCount]; + AVFormatContext **outputList = new AVFormatContext*[outputCount]; //Initialize output and set headers. for (unsigned int i = 0; i < inputContext->nb_streams; i++) @@ -259,7 +260,7 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount subtitleStreams[i] = stream; *subtitleCount += 1; - //subtitleStreams->push_back(stream); + std::cout << "Stream #" << i << "(" << stream.language << "), stream type: " << inputCodecpar->codec_type << " codec: " << stream.codec << std::endl; AVFormatContext *outputContext = NULL; @@ -340,13 +341,24 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount } delete[] outputList; - return subtitleStreams; - //*streamCount = subtitleStreams->size(); - //return subtitleStreams->data(); } void FreeMemory(Stream *streamsPtr) { delete[] streamsPtr; +} + +Stream *TestMemory(const char *path, const char *outPath, int *streamCount, int *subtitleCount) +{ + *streamCount = 4; + *subtitleCount = 2; + + Stream *streams = new Stream[*streamCount]; + streams[0] = Stream(NULL, NULL, NULL, NULL, NULL, NULL); + streams[1] = Stream(NULL, "eng", "ass", false, false, NULL); + streams[2] = Stream(NULL, NULL, NULL, NULL, NULL, NULL); + streams[3] = Stream(NULL, "fre", "ass", false, false, NULL); + + return streams; } \ No newline at end of file diff --git a/Kyoo.Transcoder/src/Transcoder.h b/Kyoo.Transcoder/src/Transcoder.h index ff565417..a4049d79 100644 --- a/Kyoo.Transcoder/src/Transcoder.h +++ b/Kyoo.Transcoder/src/Transcoder.h @@ -14,6 +14,8 @@ 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 Stream* ExtractSubtitles(const char *path, const char *outPath, int *streamCount, int *subtitleCount); extern "C" API void FreeMemory(Stream *streamsPtr); + +extern "C" API Stream* TestMemory(const char *path, const char *outPath, int *streamCount, int *subtitleCount); diff --git a/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs b/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs index 4fd7d326..4cedcfcc 100644 --- a/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs +++ b/Kyoo/InternalAPI/Transcoder/TranscoderAPI.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Runtime.InteropServices; using Kyoo.Models; using Kyoo.Models.Watch; @@ -27,7 +28,7 @@ namespace Kyoo.InternalAPI.TranscoderLink IntPtr ptr = ExtractSubtitles(path, outPath, out int arrayLength, out int trackCount); IntPtr streamsPtr = ptr; - if (trackCount > 0) + if (trackCount > 0 && ptr != IntPtr.Zero) { tracks = new Track[trackCount]; @@ -47,6 +48,11 @@ namespace Kyoo.InternalAPI.TranscoderLink tracks = null; FreeMemory(ptr); + Debug.WriteLine("&One loop done"); } + + [DllImport(TranscoderPath, CallingConvention = CallingConvention.Cdecl)] + private extern static IntPtr TestMemory(string path, string outPath, out int arrayLength, out int trackCount); + } }