0

I'm working on a project and I need to send an audio stream from a webpage (through javascript) to a server written in C++. Is this possible? How can I do this? I was thinking on use WebRTC and a WebRTC library for C++ but I don't really know hoy to achieve this.

In general I need some king of webserver in C++, that allows me to send/recieve audio stream and json and works with multiple web clients.

I have worked with Socket.io and once I coded a webserver in Java EE 7, with those I was able to send/recieve json from the webpage but I don't really know if I can send audio stream via websocket or json.

JCAguilera
  • 944
  • 1
  • 13
  • 32
  • There is not enough detail here to answer. What *specific* problem are you having with hosting a webserver in C++? – Tom W Jan 17 '18 at 17:25
  • Useful rules of thumb: The answer to "is it possible to do X?" is almost always "yes". The question "how do I do X"? is almost always too broad. – molbdnilo Jan 17 '18 at 17:31
  • Hi, I'm really a noob in this webserver thing, so please bear with me. I'm trying to do a webserver so I can send audio stream from a webpage using javascript. I have worked with socket.io and once I made a webserver in java using websockets to send/recieve json (I don't really know if I can send audio stream with json, tho). Now I need to code a server in C++ but I don't know what's the best way to do it or if something like that already exists. – JCAguilera Jan 17 '18 at 17:32
  • You can send the stream to the server as `ArrayBuffer`s and parse the `ArrayBuffer` at server, or pass the `ArrayBuffer` to a `TypedArray` constructor, convert the `TypedArray` to `JSON` and send `JSON` to the server. – guest271314 Jan 17 '18 at 17:48

1 Answers1

3

The question (or implementation in answer to the question) really consists of two parts, which are:

  • How to send audio stream from browser in Javascript
  • How to receive audio stream on server in C/C++

This is because sending data over the network only loosely couples the client and the server when they use the same protocol. You could write a server in C++, then write two different clients that communicate with it, one in Javascript, then also a desktop app written in Java.

Javascript on Client Side

For the client side, sending audio from the browser in Javascript should follow the normal libraries available for WebRTC; the WebRTC site has some useful information on this, including a video streaming example here (https://webrtc.github.io/samples/)

Some of the links which might be of interest on that page:

  1. Audio-only getUserMedia() output to local audio element
  2. Stream from a video element to a video element

There are some StackOverflow answers already about WebRTC and audio in javascript, here are a couple, these (and libraries) will be more plentiful than C++ questions on the topic:

  1. Sending video and audio stream to server
  2. Sending a MediaStream to host Server with WebRTC after it is captured by getUserMedia

For the C++ Server:

The WebRTC site has a link to the Native API for the libraries here (https://webrtc.org/native-code/native-apis/) and an excellent simple example of a peer connection WebRTC server using them is here (https://webrtc.googlesource.com/src/+/master/examples/peerconnection). It also has an implementation of a C++ client there, which may help in testing the server to get it working first, or see the general principles.

Halcyon
  • 1,376
  • 1
  • 15
  • 22
  • Hi, thanks for the answer. I didn't understand the part where u mention a Java app. Can you explain that part? Thanks a lot. – JCAguilera Jan 17 '18 at 18:17
  • Hello, the main point of mentioning a Java app was that you can write multiple clients in various language to work with the same server-- as long as they send the correct information to the server over the network the server won't care how they are written or in what language. So any information you find in other peoples' questions/answers about a Javascript WebRTC client will be valid for your problem too. That just means you have a lot more resources to choose from in other peoples' questions exclusively about Javascript. I hope that helps! – Halcyon Jan 17 '18 at 19:22