paullouisageneau / datachannel-wasm

C++ WebRTC Data Channels and WebSockets for WebAssembly in browsers
MIT License
156 stars 27 forks source link

datachannel-wasm with C based code #45

Open jonesd opened 11 months ago

jonesd commented 11 months ago

I have used libdatachannel to create a tool that uses signalling, datachannel, and websockets. It is implemented using the capi.c file which is described in the DOC.md file in the repo.

This is building and running on mac and linux well.

I would also like to run the same code, after passing through Emscripten, in a web browser. I had overlooked that datachannel-wasm only mentions C++, rather than C/C++ listed for libdatachannel.

I'm missing why C isn't supported by datachannel-wasm.

I see that the .js files provided in datachannel-wasm look like it has a similar list to the rtc* functions in libdatachannel. Also that libdatachannel the capi.c file seems to just call the existing C++ code, and so it wasn't clear why that same model couldn't apply here too.

I tried to copy across the capi.c to datachannel-wasm but had problems with missing imports/functions. I think some of these are related to media functionality that I'm not using. Would trimming back capi.c to only access the c++ code that was present in datachannel-wasm be a solution?

If it's not possible to support code that uses capi.c (or a trimmed back version), then adding a warning about this in the DOC.md might help someone else not overlook the lack of C support in datachannel-wasm.

Thanks for your help.

paullouisageneau commented 11 months ago

A subset of the libdatachannel C API could be supported by datachannel-wasm, there is nothing preventing it, only it hasn't been implemented yet.

I think there would be two ways to do so:

jonesd commented 11 months ago

Thank you for the response. I spent a little time over the weekend to get a feel for the effort involved. I added a simple wrapper file to the libdatachannel c-api with a libXYZ(...) function for each of the functions in the api. I wasn't sure which rtcX functions were going to cause collisions on the wasm side.

Switching over to the datachannel-wasm side the idea was to preserve the libXyz(...) function names and then call in to the C++ implementations, similar to the c-api approach. I got bogged down pretty quickly due to a lack of experience with the newer C++ features.

For now I've switched back and rewritten the "client peer" C code I had written to instead use the libdatachannel C++ API. The great examples you provide made that easier that I had expected.

If I make any more progress with supporting the c-api in datachanell-wasm I'll submit a pull request.

lolriven commented 5 months ago

Hi @jonesd I too wondered about the same thing. Have you made further progressing in implementing the C api for the lib? I feel it would be a monuments task to start from scratch, if you could offer some assistance that would be great. Or I could offer some help porting it over as well.

jonesd commented 5 months ago

Hello @JadeVand I ended up staying with a variant of the C++ example client sample code provided with libdatachannel.

The mixture of C and C++ hasn't ended up being a problem for my uses of libdatachannel on the client. The server side is still all C.

This has been my first C/C++ project in many years (ie previous projects were pre-99) so the C++17 features were all new for me, and it seemed rather daunting to take on this together with upcoming release deadlines for my project.