Making a few test with the C# memory management.

This commit is contained in:
Zoe Roux 2019-09-27 01:54:28 +02:00
parent 51694fbc53
commit 229be6e4b9
3 changed files with 28 additions and 8 deletions

View File

@ -205,13 +205,14 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount
AVFormatContext *inputContext = NULL; AVFormatContext *inputContext = NULL;
if (open_input_context(&inputContext, path) != 0) if (open_input_context(&inputContext, path) != 0)
return 0; return nullptr;
*streamCount = inputContext->nb_streams; *streamCount = inputContext->nb_streams;
*subtitleCount = 0; *subtitleCount = 0;
Stream *subtitleStreams = new Stream[*streamCount]; Stream *subtitleStreams = new Stream[*streamCount];
const unsigned int outputCount = inputContext->nb_streams; const unsigned int outputCount = inputContext->nb_streams;
AVFormatContext **outputList = new AVFormatContext * [outputCount]; AVFormatContext **outputList = new AVFormatContext*[outputCount];
//Initialize output and set headers. //Initialize output and set headers.
for (unsigned int i = 0; i < inputContext->nb_streams; i++) 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; subtitleStreams[i] = stream;
*subtitleCount += 1; *subtitleCount += 1;
//subtitleStreams->push_back(stream);
std::cout << "Stream #" << i << "(" << stream.language << "), stream type: " << inputCodecpar->codec_type << " codec: " << stream.codec << std::endl; std::cout << "Stream #" << i << "(" << stream.language << "), stream type: " << inputCodecpar->codec_type << " codec: " << stream.codec << std::endl;
AVFormatContext *outputContext = NULL; AVFormatContext *outputContext = NULL;
@ -340,13 +341,24 @@ Stream *ExtractSubtitles(const char *path, const char *outPath, int *streamCount
} }
delete[] outputList; delete[] outputList;
return subtitleStreams; return subtitleStreams;
//*streamCount = subtitleStreams->size();
//return subtitleStreams->data();
} }
void FreeMemory(Stream *streamsPtr) void FreeMemory(Stream *streamsPtr)
{ {
delete[] 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;
} }

View File

@ -14,6 +14,8 @@ extern "C" API int Init();
extern "C" API int Transmux(const char *path, const char *outPath); 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. //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 void FreeMemory(Stream *streamsPtr);
extern "C" API Stream* TestMemory(const char *path, const char *outPath, int *streamCount, int *subtitleCount);

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Kyoo.Models; using Kyoo.Models;
using Kyoo.Models.Watch; using Kyoo.Models.Watch;
@ -27,7 +28,7 @@ namespace Kyoo.InternalAPI.TranscoderLink
IntPtr ptr = ExtractSubtitles(path, outPath, out int arrayLength, out int trackCount); IntPtr ptr = ExtractSubtitles(path, outPath, out int arrayLength, out int trackCount);
IntPtr streamsPtr = ptr; IntPtr streamsPtr = ptr;
if (trackCount > 0) if (trackCount > 0 && ptr != IntPtr.Zero)
{ {
tracks = new Track[trackCount]; tracks = new Track[trackCount];
@ -47,6 +48,11 @@ namespace Kyoo.InternalAPI.TranscoderLink
tracks = null; tracks = null;
FreeMemory(ptr); 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);
} }
} }