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

PR: RTMP push streaming, WebRTC playback, dual-channel audio has issues, single-channel audio is normal. #2172

Closed
18981707877 opened this issue Jan 26, 2021 · 2 comments
Assignees
Labels
Bug It might be a bug. TransByAI Translated by AI/GPT. WebRTC WebRTC, RTC2RTMP or RTMP2RTC.
Milestone

Comments

@18981707877
Copy link

18981707877 commented Jan 26, 2021

Description'

Please ensure that the markdown structure is maintained.

Please describe the issue you encountered here.
'
Make sure to maintain the markdown structure.

  1. SRS version: 4.0.62
  2. The log of SRS is as follows:
The log is normal.
  1. The configuration of SRS is as follows (Config):
daemon              off;
listen              1935;
max_connections     1000;
srs_log_file        srs.log;

http_server {
    enabled         on;
    listen          8088;
    dir             html;
}

http_api {
    enabled         on;
    listen          1985;
}
stats {
    network         0;
}

# RTSP
stream_caster {
    # whether stream caster is enabled.
    # default: off
    enabled         on;
    # the caster type of stream, the casters:
    #       rtsp, Real Time Streaming Protocol (RTSP).
    caster          rtsp;
    # the output rtmp url.
    # for rtsp caster, the typically output url:
    #           rtmp://127.0.0.1/[app]/[stream]
    #       for example, the rtsp url:
    #           rtsp://192.168.1.173:8544/live/livestream.sdp
    #       where the [app] is "live" and [stream] is "livestream", output is:
    #           rtmp://127.0.0.1/live/livestream
    output          rtmp://127.0.0.1:1936/[app]/[stream];
    # the listen port for stream caster.
    #       for rtsp caster, listen at tcp port. for example, 554.
    listen          1554;
    # for the rtsp caster, the rtp server local port over udp,
    # which reply the rtsp setup request message, the port will be used:
    #       [rtp_port_min, rtp_port_max)
    rtp_port_min    57200;
    rtp_port_max    57300;
}

rtc_server {
    enabled         on;
    # Listen at udp://8000
    listen          8000;
    #
    # The $CANDIDATE means fetch from env, if not configed, use * as default.
    #
    # The * means retrieving server IP automatically, from all network interfaces,
    # @see https://github.com/ossrs/srs/issues/307#issuecomment-599028124
    candidate       $CANDIDATE;
}

vhost __defaultVhost__ {
    gop_cache       off;
    queue_length    10;
    min_latency     on;
    mr {
        enabled     off;
    }
    mw_latency      100;
    tcp_nodelay     on;
    rtc {
        enabled     on;
        bframe      discard;
    }
    http_remux {
        enabled     on;
        mount       [vhost]/[app]/[stream].flv;
        hstrs       on;
    }
}

Replay

How to replay bug?

Steps to reproduce the bug

  1. ffmpeg -re -i D:\code\ffmpegabout\ffmpeg4.2\ffmpeg-20200223-90913ab-win32-static\bin\123.mp4 -vcodec libx264 -bf 0 -x264-params keyint=120 -acodec aac -ac 2 -ar 48000 -f flv rtmp://192.168.1.165:1935/live/669E0DB78AC84B86B031E587FB330C2B
    1. ffmpeg -re -i D:\code\ffmpegabout\ffmpeg4.2\ffmpeg-20200223-90913ab-win32-static\bin\123.mp4 -vcodec libx264 -bf 0 -x264-params keyint=120 -acodec aac -ac 1 -ar 48000 -f flv rtmp://192.168.1.165:1935/live/669E0DB78AC84B86B031E587FB330C2B
      Audio playback is normal when using 2 WebRTC streams for pushing, but there is noise when using 1 WebRTC stream for pushing.

The repair process is as follows: it is necessary to determine whether the decoded audio data is in plane format. Based on this situation, copy the left and right channel data to contiguous memory space. When resampling, the left and right channel data should also be organized according to this rule. The processing process of the completed PCM data is the same as above.


git diff 4bb3ad5637b5541df515fa9a9d69143310397c60
diff --git a/trunk/src/app/srs_app_rtc_codec.cpp b/trunk/src/app/srs_app_rtc_codec.cpp
index b3db983..ceb3837 100644
--- a/trunk/src/app/srs_app_rtc_codec.cpp
+++ b/trunk/src/app/srs_app_rtc_codec.cpp
@@ -129,13 +129,24 @@ srs_error_t SrsAudioDecoder::decode(SrsSample *pkt, char *buf, int &size)
         if (pcm_size < 0) {
             return srs_error_new(ERROR_RTC_RTP_MUXER, "Failed to calculate data size");
         }
-
-        for (int i = 0; i < frame_->nb_samples; i++) {
-            if (size + pcm_size * codec_ctx_->channels <= max) {
-                memcpy(buf + size,frame_->data[0] + pcm_size*codec_ctx_->channels * i, pcm_size * codec_ctx_->channels);
-                size += pcm_size * codec_ctx_->channels;
+        bool bplane = av_sample_fmt_is_planar(codec_ctx_->sample_fmt);
+        if (bplane) {
+            for (int i = 0; i < frame_->nb_samples; i++) {
+                for (int channel=0;channel< codec_ctx_->channels; channel++){
+                    if (size + pcm_size <= max) {
+                        memcpy(buf + size,frame_->data[channel] + pcm_size* i, pcm_size);
+                        size += pcm_size;// * codec_ctx_->channels;
+                    }
+                }
             }
         }
+        else{
+            int framesize = av_samples_get_buffer_size(frame_->linesize[0], frame_->channels,
+                                                       frame_->nb_samples, codec_ctx_->sample_fmt, 1);
+
+            memcpy(buf + size, frame_->data[0], framesize);
+            size += framesize;
+        }
     }

     return err;

@@ -368,7 +381,21 @@ srs_error_t SrsAudioResample::resample(SrsSample *pcm, char *buf, int &size)
     if (src_linesize_ * plane < pcm->size || pcm->size < 0) {
         return srs_error_new(ERROR_RTC_RTP_MUXER, "size not ok");
     }
-    memcpy(src_data_[0], pcm->bytes, pcm->size);
+
+    bool bsrcplane = av_sample_fmt_is_planar(src_sample_fmt_);
+    if (bsrcplane){
+        int offset = 0;
+        int src_pcm_size = av_get_bytes_per_sample(src_sample_fmt_);
+        for (int i = 0; i < src_nb_samples_; i++) {
+            for (int channel=0;channel< src_nb_channels_; channel++){
+                memcpy(src_data_[channel] + i * src_pcm_size, pcm->bytes + offset, src_pcm_size);
+                offset += src_pcm_size;
+            }
+        }
+    }
+    else{
+        memcpy(src_data_[0], pcm->bytes, pcm->size);
+    }

     dst_nb_samples_ = av_rescale_rnd(swr_get_delay(swr_ctx_, src_rate_) +
                                     src_nb_samples_, dst_rate_, src_rate_, AV_ROUND_UP);
@@ -389,16 +416,35 @@ srs_error_t SrsAudioResample::resample(SrsSample *pcm, char *buf, int &size)

     int dst_bufsize = av_samples_get_buffer_size(&dst_linesize_, dst_nb_channels_,
                                                 ret, dst_sample_fmt_, 1);
+
+
     if (dst_bufsize < 0) {
         return srs_error_new(ERROR_RTC_RTP_MUXER, "Could not get sample buffer size");
     }

-    int max = size;
-    size = 0;
-    if (max >= dst_bufsize) {
-        memcpy(buf, dst_data_[0], dst_bufsize);
-        size = dst_bufsize;
+    bool bdstplane = av_sample_fmt_is_planar(dst_sample_fmt_);
+    if (bdstplane){
+        int pcm_size = av_get_bytes_per_sample(dst_sample_fmt_);
+        int max = size;
+        size = 0;
+        for (int i = 0; i < dst_nb_samples_; i++) {
+            for (int channel=0;channel< dst_nb_channels_; channel++){
+                if (size + pcm_size <= max) {
+                    memcpy(buf + size,dst_data_[channel] + pcm_size* i, pcm_size);
+                    size += pcm_size;// * codec_ctx_->channels;
+                }
+            }
+        }
     }
+    else{
+        int max = size;
+        size = 0;
+        if (max >= dst_bufsize) {
+            memcpy(buf, dst_data_[0], dst_bufsize);
+            size = dst_bufsize;
+        }
+    }
+

     return err;
 }

TRANS_BY_GPT3

@winlinvip winlinvip changed the title srs 接收rtmp推流双通道音频webrtc转码部分有问题,单通道音频正常 WebRTC:RTMP推流,双通道音频有问题,单通道音频正常 Feb 28, 2021
@winlinvip winlinvip added the Bug It might be a bug. label Feb 28, 2021
@winlinvip winlinvip changed the title WebRTC:RTMP推流,双通道音频有问题,单通道音频正常 PR:RTMP推流WebRTC播放,双通道音频有问题,单通道音频正常 Feb 28, 2021
@winlinvip winlinvip added the WebRTC WebRTC, RTC2RTMP or RTMP2RTC. label Feb 28, 2021
@winlinvip
Copy link
Member

winlinvip commented Feb 28, 2021

See also #2011 has not been merged yet.

TRANS_BY_GPT3

@winlinvip
Copy link
Member

Fixed.

@winlinvip winlinvip self-assigned this Aug 28, 2021
@winlinvip winlinvip added this to the 4.0 milestone Sep 4, 2021
@winlinvip winlinvip changed the title PR:RTMP推流WebRTC播放,双通道音频有问题,单通道音频正常 PR: RTMP push streaming, WebRTC playback, dual-channel audio has issues, single-channel audio is normal. Jul 29, 2023
@winlinvip winlinvip added the TransByAI Translated by AI/GPT. label Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug It might be a bug. TransByAI Translated by AI/GPT. WebRTC WebRTC, RTC2RTMP or RTMP2RTC.
Projects
None yet
Development

No branches or pull requests

2 participants