Video Streaming threading local images

Hi,

I´m doing some kind of video streaming, the main idea is use threads to allocate images and show those the images when those are available.

It´s using 2 addons: ofxDirList, and ofxThread.

[url=http://personales.ya.com/charli\_e/OF/LoaderImgThread.zip]The source :wink:

this is the error: Acces violation reading location 0xfeeefeee

  
  
void ofImage::swapChannels(bool bAlpha){  
  
  
	// this swaps the red and blue channel of an image  
	// turning an bgr image to rgb and vice versa...  
	// needed because of windows byte order...  
  
  
	if (isValid()){  
  
		unsigned char * pixels = imgPixels;  
		unsigned char temp;  
		int totalPixels = width*height;  
		// unoptimized for now...  
  
		if (bAlpha == false){  
			for (int i = 0; i < totalPixels; i++){  
HERE ----> temp = pixels[i*3 + 2];  
				pixels[i*3 + 2] = pixels[i*3];  
				pixels[i*3] = temp;  
			}  
  
		} else {  
			for (int i = 0; i < totalPixels; i++){  
				temp = pixels[i*4];  
				pixels[i*4] = pixels[i*4 + 2];  
				pixels[i*4 + 2] = temp;  
			}  
		}  
	}  
}  
  

I think this “video streaming” should be improved.
For example:

Charge only a dynamic buffer of images, and not all the images.
Calculate the charge times and say to the software when start to play the images at some velocity (It depend of framerate of the video)
Cleaning code? :slight_smile: May be there are some variables of the thread that is better don´t be public… any suggestion?

the best,

Hi Carles. Due to my inexperience with threading, I coundn´t adapt your code to my project. So I ask whats the most simple structure for a thread to call a function, like the example below?

  
  
// Like in a testApp key Pressed routine (pseudo code):  
  
if keypressed = d { // change directory  
   start thread ?????  
   changeDirectory()  
}  
  
//////////////////////////////////////////////////////  
  
// and on the changeDirectory() routine:  
  
//my changeDirectory stuff loads - ok  
  
//my changeDirectory stuff draws - ok  
  
// check if it´s done loading new dir   
  
// return array of videos  
  
// clears thread ????  
  
  

HI Alexandre,

For this “video streaming” the idea is only use images… I’m novice with threads, but I’ve understand that basically there are a Start& stop, and a function is called all time is:threadedFunction. there might be some lock function inside to control the access to the data, but I’m lost how is this running.

ImgLoader have fetchTextureNames that looks for new images in a folder. To change folder we must close the thread and start a new one with different data path.

Did you see this topic? may be it´s more usefully.

to can swap the actual video for another…delete all and charge another time the Loaderimg thread

Testapp:

  
  
void testApp::keyPressed(int key) {  
   
    if (key == 'a'){  
		loader->stop();  
		folder_path= "folder1";  
		loader = new imgLoader(folder_path);  
		loader->start();  
		  
    } else if (key == 's'){  
		loader->stop();  
		folder_path= "folder2";  
		loader = new imgLoader(folder_path);  
		loader->start();  
    }  
   
}  
  

For clear memory:

imgLoader.cpp

  
  
void imgLoader::stop() {  
	  
	for (int i=0; i < numTextures; i++) {  
        delete(images[i]);  
		images[i]=NULL;  
    }  
	delete (images);    
	images = NULL;  
	  
	stopThread();  
}  
  

It´s necessary wait the load of images of the current thread, before change to another folder. Otherwise there will be memory leaks…

U sign more than one "videoStreaming"

The apps crash always (memory leak) when I order change folder…may be somebody can help me saying how to do it right.

this kind of situation gives the memory leak here:

  
  
void ofImage::clear(){  
  
	//-------------------------------  
	if(isValid() && bFreeImageInited) {  
		// zach - I added "if (bFreeImageInited)" since   
		// app runner might kill freeImage, so images killing themselves might not  
		// want to call anything freeImage related.    
HERE-->	FreeImage_Unload(bmp);  
	}  
	//-------------------------------  
	if (imgPixels != NULL && bAllocatedPixels == true){  
		delete [] imgPixels;	// delete anything that might have existed  
		//delete imgPixels;  
		bAllocatedPixels = false;  
	}  
	//-------------------------------  
	width = height = bpp = 0;  
	type 					= OF_IMAGE_UNDEFINED;  
	bUseTexture 			= true;		// the default is, yes, use a texture  
	bAllocatedPixels		= false;  
	tex.clear();  
}  
  

Thanks

by

Carles, I´m still struggling with the most simple setup of threads…
Could you please shoe me a recipe for a very simple function done on a different thread? Like playing a sound file on a thread.