Emscripten code for image data download using emscripten_wget or emscripten_async_wget

562 views
Skip to first unread message

Pau Ballart

unread,
Oct 15, 2015, 3:27:52 PM10/15/15
to emscripten-discuss
Hello developers,

I'm starting a project and I'm new at Javascript and emscripten. I'm trying to download some image data from a server and then process it using some c functions I already have working. I asked this question in Stackoverflow so I share you the link and maybe one of you can help me.


Thank you,

Pau B

Alon Zakai

unread,
Oct 15, 2015, 3:55:49 PM10/15/15
to emscripten-discuss
It would be good to focus the question more, it's hard to tell what the core issue is. Try to reduce the problem to just one concrete thing, like just loading a file, and doing it without that custom PHP script, for example.


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pau Ballart

unread,
Oct 15, 2015, 3:59:21 PM10/15/15
to emscripte...@googlegroups.com
Okay so the php is just calling a function I declared. Because I need to pass the image url as an argument and a key, I don't know how to do it using the main function so I changed the main function for this one with the two arguments and then called this function from my php code. 
Is there a better way to call emscripten passing parameters? 

Thanks.

--
You received this message because you are subscribed to a topic in the Google Groups "emscripten-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/emscripten-discuss/BEb-1mjDvZI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to emscripten-disc...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Pau Ballart

Alon Zakai

unread,
Oct 15, 2015, 8:07:27 PM10/15/15
to emscripten-discuss
I see you have

P3Func = Module.cwrap('p3stuff', 'number', ['string', 'string']);

which looks good.  You can then call it with something like

result = P3Func('first', 'second');

which I see you are doing. What part of that are you trying to improve?

Pau Ballart

unread,
Oct 17, 2015, 1:47:57 PM10/17/15
to emscripte...@googlegroups.com
I think the php code is correct so I'm just trying to download an image and process it's data but I think I'm missing something about the lifecycle or wget function because I get unexpected exit errors.

So I'd like to know if the  emscripten_wget(imgUrl, path); is correct because I have the weirdest behaviour ever that depending on the p3stuff function param "wrappedkey" the download succeeds or not.

Alon Zakai

unread,
Oct 17, 2015, 10:37:46 PM10/17/15
to emscripten-discuss
The only tricky thing with emscripten_wget is that as mentioned in the docs,

http://kripken.github.io/emscripten-site/docs/api_reference/emscripten.h.html#c.emscripten_wget

it requires the ASYNCIFY option. It's easier in general to use the async version, emscripten_async_wget, which does not require ASYNCIFY.

Pau Ballart

unread,
Oct 19, 2015, 1:54:18 PM10/19/15
to emscripte...@googlegroups.com
Okay I will try the async version and let you know.

Thanks

Pau Ballart

unread,
Oct 20, 2015, 4:37:11 PM10/20/15
to emscripte...@googlegroups.com
Hello,

After some research and some refactor I did the following:
  • In my p3stuff function I call emscripten_set_main_loop(wait_wgets, 0, 0); so the application is not terminated.
  • In wait_wgets I call the emscripten_async_wget(imageUrl, path, onLoaded, onError); only the first time the method is called.
  • In the onLoaded callback I call emscripten_cancel_main_loop(); and then I call my c code that perform the jpeg stuff.
Problem is:
the p3stuff function has 2 arguments: int p3stuff(char *imgUrl, unsigned char *wrappedkey)
When it's called i have two global variables unsigned char *wrappedKey; and char *imageUrl; which are assigned with the argument like that:

wrappedKey = wrappedkey;
imageUrl = imgUrl;

I do that in order to access the image url from the wait_wgets() or the wrapped key from the onLoaded callback but I think there is some memory management problems cause if I print the url just before calling emscripten_async_wget the imageUrl variable content is completely rubbish but id I don't print anything it works.

So how can I use the global variables in C in order to not get overwritten? Maybe using malloc, or memcopy? 

Thanks,

Pau
--
Pau Ballart

Alon Zakai

unread,
Oct 20, 2015, 5:05:36 PM10/20/15
to emscripten-discuss
One possible issue is that if those values are on the stack, and the scope exits, then that stack memory might be reused. For example this is unsafe:

{
  char buffer[100];
  // write into buffer
  emscripten_async_wget(..., (void*)buffer);
}

The buffer will be released before the async callback occurs, and other data might be written into it before the callback reads from it.

Which version of emscripten are you using, btw? I remember a while back we had a bug around this.

Pau Ballart

unread,
Oct 20, 2015, 5:16:22 PM10/20/15
to emscripte...@googlegroups.com
$ emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.34.8
clang version 3.7.0
Target: x86_64-apple-darwin15.0.0
Thread model: posix
INFO     root: (Emscripten: Running sanity checks)

Alon Zakai

unread,
Oct 20, 2015, 5:18:33 PM10/20/15
to emscripten-discuss
Might be worth trying current master (or incoming) then, which is now 1.35.0.

Pau Ballart

unread,
Oct 20, 2015, 6:52:03 PM10/20/15
to emscripte...@googlegroups.com
Updated to 1.35 but same problem...
Maybe the question to formulate would be: How can I save in a global/static variable some information so that I can acces it later in the callback function and make sure it's not overwritten?
What do you think about using malloc() somehow? Would it work?

Alon Zakai

unread,
Oct 20, 2015, 6:57:24 PM10/20/15
to emscripten-discuss
Yes, if you want something to live beyond the current function call, you should use malloc(). That will only be reused after you call free() on it.

Pau Ballart

unread,
Oct 20, 2015, 11:20:31 PM10/20/15
to emscripte...@googlegroups.com
I did some tries with no success.
If I have this declaration:
int p3stuff(char *imgUrl, unsigned char *wrappedkey);
How can I make sure the contents of imgUrl will persist even out of the scope?
Because it's a pointer so maybe I need to malloc first the space and then memcpy the contents?
Suppose I have a global var like this char *imageUrl; I'd be very thankful if you can provide me an example.

Alon Zakai

unread,
Oct 21, 2015, 12:24:05 AM10/21/15
to emscripten-discuss
Yes, malloc the space, and memcpy the contents to that space.

Pau Ballart

unread,
Oct 29, 2015, 6:49:39 PM10/29/15
to emscripte...@googlegroups.com
Hello,

I've been working hard with my implementation and I almost got it.
I'd say it's working fine in Safari but I got a DOM exception in Chrome and Firefox and I have no idea why. the DOM Exception doesn't give any message nor error code...
The error happens when inside my C code i have an EM_ASM_ARGS() block and inside this I use the Javascript WebCrypto API to decrypt the image. The think is the same decrypt method works when I use it outside emscripten in a normal webpage and inside emscripten it works with Safari! But why it doesn't work in Chrome?

Thanks

Pau Ballart

unread,
Oct 29, 2015, 6:59:01 PM10/29/15
to emscripte...@googlegroups.com
The chrome debugger says:
Imatge inserida 1
And the Firefox:
Imatge inserida 2
--
Pau Ballart

Alon Zakai

unread,
Oct 29, 2015, 8:39:05 PM10/29/15
to emscripten-discuss
It could be a bug in chrome. I would try to narrow it down and file a testcase for them.

Pau Ballart

unread,
Nov 2, 2015, 7:52:11 PM11/2/15
to emscripte...@googlegroups.com
Hi again,

I don't know exactly why but the next day it was working. Maybe it has to do with the https connection that may be required.

I have my code finished and ready to move into production. My a.out.js file sizes 1.9 MB approx. and when using optimization flags as exposed in the documentation I have a problem. If I use -O1 it works and I get a 1.5 MB file size. Using -O2 or -O3 produces a file of 400 KB but when I run the code it doesn't work.
Imatge inserida 1

Any suggestions?

Alon Zakai

unread,
Nov 3, 2015, 12:06:15 AM11/3/15
to emscripten-discuss
An unlikely theory, but -O2 and above will use a .mem file, if that file can't be loaded you should get an error. Can try disabling that with --mem-init-file 0. But, I don't see how that could lead to the particular error you are getting, never seen that before.

Pau Ballart

unread,
Nov 4, 2015, 6:53:54 PM11/4/15
to emscripte...@googlegroups.com
I tried this but no luck... The mem file is there. What I'm seeing is that the bytes I copy using malloc and memcpy are corrupted, are null. Maybe the optimization does something with mallocs?

Alon Zakai

unread,
Nov 5, 2015, 5:48:50 PM11/5/15
to emscripten-discuss
If there is undefined behavior in the program, it's possible, I think.
Reply all
Reply to author
Forward
0 new messages