Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datachannel-wasm with C based code #45

Open
jonesd opened this issue Oct 26, 2023 · 4 comments
Open

datachannel-wasm with C based code #45

jonesd opened this issue Oct 26, 2023 · 4 comments

Comments

@jonesd
Copy link

jonesd commented Oct 26, 2023

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
Copy link
Owner

paullouisageneau commented Oct 27, 2023

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:

  • As you suggest, trimming capi.c to only access methods present in datachannel-wasm would work. I think you would need to rename the internal JavaScript functions called from datachannel-wasm as they are similarly named and therefore would cause collisions during linking.
  • A possibly easier approach would be to expose the very similar internal JavaScript API then make changes or add some wrapper functions so the signatures are the same as the libdatachannel C API. For instance the signature of rtcCreatePeerConnection and rtcCreateDataChannel are different in the datachannel-wasm internal API, but they could be wrapped by public functions essentially doing the same job as the PeerConnection and DataChannel constructors.

@jonesd
Copy link
Author

jonesd commented Nov 3, 2023

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
Copy link

lolriven commented May 7, 2024

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
Copy link
Author

jonesd commented May 10, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants