AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

VideoFileReader in Multithreading Application

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

VideoFileReader in Multithreading Application

Postby Kasko » Wed Jun 20, 2018 4:37 pm

I have a CustomControl called PlaylistView. It displays elements in a playlist with name and thumbnail. The method DisplayPlaylist ensures that a thread is started, in which the individual elements are added one by one and the thumbnails (30th frame) are read out:

Code: Select all
public void DisplayPlaylist(Playlist playlist)
{
    Thread thread = new Thread(() => DisplayElements(playlist));
    thread.Start();
}

private void DisplayElements(Playlist playlist)
{
    for (int i = 0; i < playlist.elements.Count; i++)
        DisplayElement(playlist.elements[i], i);
}

private void DisplayElement(IPlayable element, int index)
{
    VideoSelect videoSelect = null;

    if (element is Audio)
        //
    else if (element is Video)
        videoSelect = new VideoSelect(index, element.name, GetThumbnail(element.path, SystemData.thumbnailFrame));

    videoSelect.Location = GetElementsPosition(index);

    panel_List.BeginInvoke(new Action(() =>
    {
        panel_List.Controls.Add(videoSelect);
    }));
}

private Bitmap GetThumbnail(string path, int frame)
{
    VideoFileReader reader = new VideoFileReader();
    try
    {
        reader.Open(path);

        for (int i = 1; i < frame; i++)
            reader.ReadVideoFrame();

        return reader.ReadVideoFrame();
    }
    catch
    {
        return null;
    }
}

But there is a problem.
It is much too slow (about 10 elements/sec). With a playlist length of 614, you would have to wait more than a minute until all are displayed. Each time you change the playlist, such as adding or deleting an item, the procedure starts with the new item. Adding 2 or more will make it even more complicated.
I now had the approach to use multiple threads and the number of threads used for this to be specified by the user (1 to max 10). The implementation in the code currently looks like this (only changed parts compared to the previously posted code)

Code: Select all
public void DisplayPlaylist(Playlist playlist)
{
    for (int i = 0; i < SystemData.usedDisplayingThreads; i++)
    {
        Thread thread = new Thread(() => DisplayElements(playlist, i));
        thread.Start();
    }
}

private void DisplayElements(Playlist playlist, int startIndex)
{
    for (int i = startIndex; i < playlist.elements.Count; i += SystemData.usedDisplayingThreads)
        DisplayElement(playlist.elements[i], i);
}

Another possible way is using the Paralell.For Methode:

Code: Select all
public void DisplayContent()
{
    Thread thread = new Thread(() => DisplayElements(playlist, i));
    thread.Start();
}

private void DisplayElements(Playlist playlist)
{
    Parallel.For(0, files.Length, i =>
    {
        DisplayElement(playlist.elements[i], i);
    });
}

The problem is that now very often null is returned by the GetThumbnail function, so an error occurs. The error that causes null to be returned is always the AForge.Video.VideoException. In addition, a System.AccessViolationException is often thrown out. In my opinion, the reason for this is, that the VideoFileReader is not able to work in multithreading applications. So my question is how can I use the VideoFileReader in multithreading Applications.
Kasko
 
Posts: 2
Joined: Wed Jun 20, 2018 4:13 pm

Re: VideoFileReader in Multithreading Application

Postby andrew.kirillov » Thu Jun 21, 2018 7:28 am

Kasko wrote:The error that causes null to be returned is always the AForge.Video.VideoException. In addition, a System.AccessViolationException is often thrown out. In my opinion, the reason for this is, that the VideoFileReader is not able to work in multithreading applications. So my question is how can I use the VideoFileReader in multithreading Applications.

Not sure there is quick and easy answer to this. If you think it is a bug in AForge.NET Framework, then I would advice getting its source code and simply debug. Try to find where this exception comes from and what causes it.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: VideoFileReader in Multithreading Application

Postby Kasko » Thu Jun 21, 2018 9:42 am

The exception is always triggered by the line reader.Open (string path) and I can not look into the method.
Kasko
 
Posts: 2
Joined: Wed Jun 20, 2018 4:13 pm

Re: VideoFileReader in Multithreading Application

Postby andrew.kirillov » Thu Jun 21, 2018 9:47 am

Kasko wrote:and I can not look into the method.

Why not? That part is written in Managed C++, the code is open.
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK




Return to AForge.NET Framework