Bonjour ,
je veux afficher une image jpg qui vien du "net" avec ma PSP en boucle (car c'est une webcam) mais je perd 400 a 800ko de memoire par boucle :
#include <pspkernel.h>
#include <oslib/oslib.h>
#include <psphttp.h>
#include <psprtc.h>
PSP_MODULE_INFO("Net Test", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_KB(20*1024);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Globals:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define ADDRESS "192.168.10.90"
static int runningFlag = 1;
static OSL_IMAGE *bkg = NULL;
static char message[100] = "";
static char buffer[100] = "";
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Callbacks:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
runningFlag = 0;
return 0;
}
/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0);
if(thid >= 0)
sceKernelStartThread(thid, 0, 0);
return thid;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Init OSLib:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int initOSLib(){
oslInit(0);
oslInitGfx(OSL_PF_8888, 1);
oslInitAudio();
oslSetQuitOnLoadFailure(1);
oslSetKeyAutorepeatInit(40);
oslSetKeyAutorepeatInterval(10);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Connect to Access Point:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int connectAPCallback(int state){
oslStartDrawing();
oslDrawImageXY(bkg, 0, 0);
oslDrawString(30, 200, "Connecting to AP...");
sprintf(buffer, "State: %i", state);
oslDrawString(30, 230, buffer);
oslEndDrawing();
oslEndFrame();
oslSyncFrame();
return 0;
}
int connectToAP(int config){
oslStartDrawing();
oslDrawImageXY(bkg, 0, 0);
oslDrawString(30, 200, "Connecting to AP...");
oslEndDrawing();
oslEndFrame();
oslSyncFrame();
int result = oslConnectToAP(config, 10, connectAPCallback);
if (!result){
return 1;
}else{
oslStartDrawing();
oslDrawImageXY(bkg, 0, 0);
sprintf(buffer, "Error connecting to AP!");
oslDrawString(30, 200, buffer);
oslEndDrawing();
oslEndFrame();
oslSyncFrame();
sceKernelDelayThread(3*1000000);
}
//oslDisconnectFromAP();
return 0;
}
int Net_IMG_Load(char *url, int save_MS){
int template, connection, request, ret, status,fd;
template = sceHttpCreateTemplate("OSL-agent/0.0.1 libhttp/1.0.0", 1, 1);
if(template < 0) return 0;
ret = sceHttpSetResolveTimeOut(template, 500000);
if(ret < 0) return 0;
ret = sceHttpSetRecvTimeOut(template, 10000000);
if(ret < 0) return 0;
ret = sceHttpSetSendTimeOut(template, 10000000);
if(ret < 0) return 0;
connection = sceHttpCreateConnectionWithURL(template, url, 0);
if(connection < 0) return 0L;
request = sceHttpCreateRequestWithURL(connection, PSP_HTTP_METHOD_GET, (char*)url, 0);
if(request < 0) return 0;
ret = sceHttpSendRequest(request, 0, 0);
if(ret < 0) return 0;
ret = sceHttpGetStatusCode(request, &status);
if(ret < 0) return 0;
if(status != 200) return 0;
//***merci biscottealacrevette
SceULong64 length;
sceHttpGetContentLength (request, &length);
void* target = malloc((SceULong64)length);
sceHttpReadData(request,target,(SceULong64)length);
//***
sceHttpDeleteRequest(request); sceHttpDeleteConnection(connection); sceHttpDeleteTemplate(template);
free(ret);
//Set data for the temporary file
oslSetTempFileData(target, (SceULong64)length, &VF_MEMORY);
//Load a JPG file using the temporary file (oslGetTempFileName to get its name)
bkg = NULL;
bkg = oslLoadImageFileJPG(oslGetTempFileName(), OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_5650);
if (save_MS>0) {
char *filepath="";
pspTime TimePSP;
sceRtcGetCurrentClockLocalTime(&TimePSP);
sprintf(filepath,"ms0:/PSP/PHOTO/FOX_%02dH%02dM%02dS%02dms.jpg" , TimePSP.hour , TimePSP.minutes , TimePSP.seconds , (TimePSP.microseconds>>12) & 0xff);
fd = sceIoOpen(filepath, PSP_O_WRONLY | PSP_O_CREAT, 0777);
sceIoWrite(fd, target, (SceULong64)length);
sceIoClose(fd);
}
free(target);
//free(oslSetTempFileData);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Main:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(){
int skip = 0;
int enabled = 1;
int selectedConfig = 0;
SetupCallbacks();
initOSLib();
oslIntraFontInit(INTRAFONT_CACHE_MED);
oslNetInit();
//Loads image:
bkg = oslLoadImageFilePNG("bkg.png", OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
//Load font:
OSL_FONT *font = oslLoadFontFile("flash0:/font/ltn0.pgf");
oslSetFont(font);
if (!oslIsWlanPowerOn())
sprintf(message, "Please turn on the WLAN.");
//Get connections list:
struct oslNetConfig configs[OSL_MAX_NET_CONFIGS];
int numconfigs = oslGetNetConfigs(configs);
if (!numconfigs){
sprintf(message, "No configuration found!");
enabled = 0;
}
int connected = 0;
while(runningFlag && !osl_quit){
if (!skip){
oslStartDrawing();
oslDrawImageXY(bkg, 0, 0);
if (enabled){
sprintf(buffer, "Press X to connect to %s.", configs[selectedConfig].name);
oslDrawString(30, 50, buffer);
oslDrawString(30, 80, "Press UP and DOWN to change settings.");
}
oslDrawString(30, 150, "Press /\\ to quit.");
oslDrawString(30, 250, "Press () to screen.");
sprintf(buffer, "Memory Available %iko.", oslGetRamStatus().maxAvailable/1024);
oslDrawString(30, 100, buffer);
oslDrawString(30, 200, message);
oslEndDrawing();
}
oslEndFrame();
skip = oslSyncFrame();
oslReadKeys();
if (osl_keys->released.triangle)
runningFlag = 0;
if (osl_keys->released.cross){
connected = connectToAP(selectedConfig + 1);
while (connected==0)
{connected = connectToAP(selectedConfig + 1);}
}else if (osl_keys->released.circle){
Net_IMG_Load("http://192.168.10.90:8888/", 0);
}else if (osl_keys->released.up){
if (++selectedConfig >= numconfigs)
selectedConfig = numconfigs - 1;
}else if (osl_keys->released.down){
if (--selectedConfig < 0)
selectedConfig = 0;
}
}
//Quit OSL:
oslNetTerm();
oslEndGfx();
sceKernelExitGame();
return 0;
}
donc tout se passe bien (pas de fuite) sauf lorsque j'appuis sur rond (circle) pour prendre un screenshot de la cam (en realité recuperer un jpg sur le serveur) ce qui lance donc ma fonction int Net_IMG_Load(char *url, int save_MS) et me fait perdre 400 a 800ko a chaque fois , avec 18Mo de ram ca va pas tres loin , je pense que la probleme vien de :
int Net_IMG_Load(char *url, int save_MS){
int template, connection, request, ret, status,fd;
template = sceHttpCreateTemplate("OSL-agent/0.0.1 libhttp/1.0.0", 1, 1);
if(template < 0) return 0;
ret = sceHttpSetResolveTimeOut(template, 500000);
if(ret < 0) return 0;
ret = sceHttpSetRecvTimeOut(template, 10000000);
if(ret < 0) return 0;
ret = sceHttpSetSendTimeOut(template, 10000000);
if(ret < 0) return 0;
connection = sceHttpCreateConnectionWithURL(template, url, 0);
if(connection < 0) return 0L;
request = sceHttpCreateRequestWithURL(connection, PSP_HTTP_METHOD_GET, (char*)url, 0);
if(request < 0) return 0;
ret = sceHttpSendRequest(request, 0, 0);
if(ret < 0) return 0;
ret = sceHttpGetStatusCode(request, &status);
if(ret < 0) return 0;
if(status != 200) return 0;
//***merci biscottealacrevette
SceULong64 length;
sceHttpGetContentLength (request, &length);
void* target = malloc((SceULong64)length);
sceHttpReadData(request,target,(SceULong64)length);
//***
sceHttpDeleteRequest(request); sceHttpDeleteConnection(connection); sceHttpDeleteTemplate(template);
free(ret);
//Set data for the temporary file
oslSetTempFileData(target, (SceULong64)length, &VF_MEMORY);
//Load a JPG file using the temporary file (oslGetTempFileName to get its name)
bkg = NULL;
bkg = oslLoadImageFileJPG(oslGetTempFileName(), OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_5650);
if (save_MS>0) {
char *filepath="";
pspTime TimePSP;
sceRtcGetCurrentClockLocalTime(&TimePSP);
sprintf(filepath,"ms0:/PSP/PHOTO/FOX_%02dH%02dM%02dS%02dms.jpg" , TimePSP.hour , TimePSP.minutes , TimePSP.seconds , (TimePSP.microseconds>>12) & 0xff);
fd = sceIoOpen(filepath, PSP_O_WRONLY | PSP_O_CREAT, 0777);
sceIoWrite(fd, target, (SceULong64)length);
sceIoClose(fd);
}
free(target);
//free(oslSetTempFileData);
return 0;
}