Pion WebRTC is a WebRTC framework library implemented purely in Go. It can be used to develop WebRTC server and WebRTC client. The API interface is similar to JavaScript, reducing learning costs. In the case of Pion, there are only video streams, so the pull stream will definitely only pull the video stream, not the audio stream. So how do you add an audio stream and play it in the browser? This article studies the implementation method.
Since the Pion example does not have an audio stream, you will use Webrtpion to add an audio stream to it. What needs to be modified is webrtpion’s Go service to add an audio stream. Let’s start with a logical flow analysis from the Go server to the browser side.
1. Modification of Go service
1. The video stream added by pion is as follows:
So we need to add an audio stream here.
2, pion received the video stream, and added a variable save:
I need to add another variable here to save the audio track.
3, pull flow add track:
The go side of the modification is complete, the browser side of the modification. The browser, while relatively simple, also needs to be modified.
Second, the browser side modification
1. Push flow to WebRTC Go service, parameters need to be modified:
2, pull WebRTC stream, need to add audio:
At this point, the analysis of the two ends is complete, and now we can roughly know how to modify the two ends. The specific modification content is as follows:
1. Modify the Go server
1) Add audio
2) Save the audio stream
3) Additional audio tracks need to be added when pulling streams
2. Modify or add the browser
1) Push the flow to WebRTC
2) Pull WebRTC flow