ossrs / srs

SRS is a simple, high-efficiency, real-time media server supporting RTMP, WebRTC, HLS, HTTP-FLV, HTTP-TS, SRT, MPEG-DASH, and GB28181.
https://ossrs.io
MIT License
25.83k stars 5.4k forks source link

WebRTC: Support G711A audio codec #4075

Open thanhbinh89 opened 5 months ago

thanhbinh89 commented 5 months ago

Describe the bug Received a 502 Bad Gateway response when sending the WHIP. The SDP content in the body of the WHIP is provided below. The SDP includes video (H.264) and audio (G.711a), not Opus.

Version Docker ossrs/srs:5 , ossrs/srs:6

To Reproduce sdp

v=0
o=rtc 1451362802 0 IN IP4 127.0.0.1
s=-
t=0 0
a=group:BUNDLE VideoStream AudioStream
a=group:LS VideoStream AudioStream
a=msid-semantic:WMS *
a=setup:actpass
a=ice-ufrag:zyNK
a=ice-pwd:aOKmCz70/I7VgrE0QF5ChO
a=ice-options:ice2,trickle
a=fingerprint:sha-256 ED:BB:E0:8F:A7:D7:A4:E2:E6:0F:D0:87:C1:70:22:51:94:87:37:AC:A6:77:EE:EF:5D:AC:6D:4D:E0:53:38:D7
m=video 52517 UDP/TLS/RTP/SAVPF 102
c=IN IP4 192.168.1.59
a=mid:VideoStream
a=sendonly
a=ssrc:1 cname:VideoStream
a=ssrc:1 msid:Stream VideoStream
a=msid:Stream VideoStream
a=rtcp-mux
a=rtpmap:102 H264/90000
a=rtcp-fb:102 nack
a=rtcp-fb:102 nack pli
a=rtcp-fb:102 goog-remb
a=fmtp:102 profile-level-id=42e01f;packetization-mode=1;level-asymmetry-allowed=1
a=candidate:1 1 UDP 2122317823 192.168.1.59 52517 typ host
a=candidate:2 1 UDP 2122317567 10.112.20.1 52517 typ host
a=candidate:3 1 UDP 1686109695 118.69.167.37 52517 typ srflx raddr 0.0.0.0 rport 0
a=candidate:4 1 UDP 8387839 42.116.138.54 11255 typ relay raddr 0.0.0.0 rport 0
a=candidate:5 1 UDP 8387583 42.116.138.58 16358 typ relay raddr 0.0.0.0 rport 0
a=end-of-candidates
m=audio 52517 UDP/TLS/RTP/SAVPF 8
c=IN IP4 192.168.1.59
a=mid:AudioStream
a=sendrecv
a=ssrc:2 cname:AudioStream
a=ssrc:2 msid:Stream AudioStream
a=msid:Stream AudioStream
a=rtcp-mux
a=rtpmap:8 PCMA/8000/1
suzp1984 commented 5 months ago

SRS only support opus as audio codec.

https://github.com/ossrs/srs/blob/282d94d7bbfa127868caab401bb7616e26d4c54d/trunk/src/app/srs_app_rtc_conn.cpp#L2661-L2664

winlinvip commented 5 months ago

I think it's reasonable to support G711 codec with WebRTC. Note that SRS will convert Opus/G711 to AAC for RTMP/HTTP-FLV/HLS if enabled converting RTC to RTMP.

suzp1984 commented 5 months ago

The first problem is prepare a test/dev env, how to prepare a webrtc client with G711 audio codec?

The common WHIP request sent from the web browser, which generate the sdp offer with opus codec by default. As I know the Gstreamer has an element whipsink to publish webrtc streams without web browser, but need to verify whether G711 works in this scenario.

@thanhbinh89 Could you describe how your webrtc env with G711 works?

thanhbinh89 commented 5 months ago

I am using a G711 sample file, which has been recorded from a camera device. Below is the sample file (rt-recored.g711) and the smaller samples (g711/) that have been cut for transmission into WebRTC. Additionally, I have attached a Python script.

#!/usr/bin/env python3
def split_file(input_file, output_prefix, chunk_size):
    with open(input_file, 'rb') as f:
        i = 0
        while True:
            chunk = f.read(chunk_size)
            if not chunk:
                break
            output_file = f"{output_prefix}{i:d}.g711"
            with open(output_file, 'wb') as chunk_file:
                chunk_file.write(chunk)
            i += 1

input_file = 'rt-recored.g711'
output_prefix = 'g711/sample-'
chunk_size = 160
split_file(input_file, output_prefix, chunk_size)

image

rt-recored.zip

TRANS_BY_GPT4

suzp1984 commented 5 months ago

@thanhbinh89 How do you publish above G711 files to SRS by RTC? more details step to step?

thanhbinh89 commented 5 months ago

@suzp1984 It will involve quite a few steps. I can't provide detailed instructions, but the steps will be:

  1. Compile the following library https://github.com/paullouisageneau/libdatachannel , which includes compiling the examples.
  2. Use the 'streamer' example and modify the code, because by default it only pushes h264 video and opus audio. Therefore, you need to change the audio to g711 as in the sample file I sent last time (rt-recorded.g711).