webrtc 오디오 대역폭 제한

7200 단어 webrtc
우리가 웹rtc를 사용할 때 대역폭이 너무 높은 문제에 부딪힌 적이 있다.다른 사람의 블로그를 봐도 그들의 프로젝트는 일반적으로 한 서버에서 5~6명의 음성을 지원하는 것으로 나타났다.이것은 원가가 너무 높은 문제다.
사실 웹rtc는 스스로 이런 것을 설정할 수 있다.
웹rtc의 원본 코드를 자세히 읽으십시오. 저는 아래의 매개 변수를 설정하여 대역폭을 반 이상 줄였습니다.5M 서버는 30명 가까이 지원할 수 있습니다.
private static final boolean _preferIsac = true;
private static final int _audioStartBitrate = 0;

다음 두 함수를 호출합니다.
private static String preferCodec(
            String sdpDescription, String codec, boolean isAudio) {
        String[] lines = sdpDescription.split("\r
"); int mLineIndex = -1; String codecRtpMap = null; // a=rtpmap: / [/] String regex = "^a=rtpmap:(\\d+) " + codec + "(/\\d+)+[\r]?$"; Pattern codecPattern = Pattern.compile(regex); String mediaDescription = "m=video "; if (isAudio) { mediaDescription = "m=audio "; } for (int i = 0; (i < lines.length) && (mLineIndex == -1 || codecRtpMap == null); i++) { if (lines[i].startsWith(mediaDescription)) { mLineIndex = i; continue; } Matcher codecMatcher = codecPattern.matcher(lines[i]); if (codecMatcher.matches()) { codecRtpMap = codecMatcher.group(1); } } if (mLineIndex == -1) { Log.w(TAG, "No " + mediaDescription + " line, so can't prefer " + codec); return sdpDescription; } if (codecRtpMap == null) { Log.w(TAG, "No rtpmap for " + codec); return sdpDescription; } Log.d(TAG, "Found " + codec + " rtpmap " + codecRtpMap + ", prefer at " + lines[mLineIndex]); String[] origMLineParts = lines[mLineIndex].split(" "); if (origMLineParts.length > 3) { StringBuilder newMLine = new StringBuilder(); int origPartIndex = 0; // Format is: m= ... newMLine.append(origMLineParts[origPartIndex++]).append(" "); newMLine.append(origMLineParts[origPartIndex++]).append(" "); newMLine.append(origMLineParts[origPartIndex++]).append(" "); newMLine.append(codecRtpMap); for (; origPartIndex < origMLineParts.length; origPartIndex++) { if (!origMLineParts[origPartIndex].equals(codecRtpMap)) { newMLine.append(" ").append(origMLineParts[origPartIndex]); } } lines[mLineIndex] = newMLine.toString(); Log.d(TAG, "Change media description: " + lines[mLineIndex]); } else { Log.e(TAG, "Wrong SDP media description format: " + lines[mLineIndex]); } StringBuilder newSdpDescription = new StringBuilder(); for (String line : lines) { newSdpDescription.append(line).append("\r
"); } return newSdpDescription.toString(); }
 private static String setStartBitrate(String codec, boolean isVideoCodec,
                                          String sdpDescription, int bitrateKbps) {
        String[] lines = sdpDescription.split("\r
"); int rtpmapLineIndex = -1; boolean sdpFormatUpdated = false; String codecRtpMap = null; // Search for codec rtpmap in format // a=rtpmap: / [/] String regex = "^a=rtpmap:(\\d+) " + codec + "(/\\d+)+[\r]?$"; Pattern codecPattern = Pattern.compile(regex); for (int i = 0; i < lines.length; i++) { Matcher codecMatcher = codecPattern.matcher(lines[i]); if (codecMatcher.matches()) { codecRtpMap = codecMatcher.group(1); rtpmapLineIndex = i; break; } } if (codecRtpMap == null) { Log.w(TAG, "No rtpmap for " + codec + " codec"); return sdpDescription; } Log.d(TAG, "Found " + codec + " rtpmap " + codecRtpMap + " at " + lines[rtpmapLineIndex]); // Check if a=fmtp string already exist in remote SDP for this codec and // update it with new bitrate parameter. regex = "^a=fmtp:" + codecRtpMap + " \\w+=\\d+.*[\r]?$"; codecPattern = Pattern.compile(regex); for (int i = 0; i < lines.length; i++) { Matcher codecMatcher = codecPattern.matcher(lines[i]); if (codecMatcher.matches()) { Log.d(TAG, "Found " + codec + " " + lines[i]); if (isVideoCodec) { lines[i] += "; " + VIDEO_CODEC_PARAM_START_BITRATE + "=" + bitrateKbps; } else { lines[i] += "; " + AUDIO_CODEC_PARAM_BITRATE + "=" + (bitrateKbps * 1000); } Log.d(TAG, "Update remote SDP line: " + lines[i]); sdpFormatUpdated = true; break; } } StringBuilder newSdpDescription = new StringBuilder(); for (int i = 0; i < lines.length; i++) { newSdpDescription.append(lines[i]).append("\r
"); // Append new a=fmtp line if no such line exist for a codec. if (!sdpFormatUpdated && i == rtpmapLineIndex) { String bitrateSet; if (isVideoCodec) { bitrateSet = "a=fmtp:" + codecRtpMap + " " + VIDEO_CODEC_PARAM_START_BITRATE + "=" + bitrateKbps; } else { bitrateSet = "a=fmtp:" + codecRtpMap + " " + AUDIO_CODEC_PARAM_BITRATE + "=" + (bitrateKbps * 1000); } Log.d(TAG, "Add remote SDP line: " + bitrateSet); newSdpDescription.append(bitrateSet).append("\r
"); } } return newSdpDescription.toString(); }

이 두 함수의 사용 방법은 webrtc의 원본 코드에서 얻을 수 있습니다. webrtc-master-webrtc\examples\androidapp\src\org\appspot\appprtc\PeerConnectionClient.java
여러분도 다른 방법을 시도해 보세요.어쩌면 더 기쁜 방안을 얻을 수 있을지도 모른다.우리의 결과는 ICE 연결이 23KB 정도를 차지하고 음성 관련 35KB를 차지하는 것이다.
더 좋은 방안이 있으면 메일로 보내주세요.
[email protected]

좋은 웹페이지 즐겨찾기