begeekmyfriend / yasea

RTMP live streaming client for Android
MIT License
4.87k stars 1.32k forks source link

Audio is flickering while streaming to Youtube #554

Open thakurmayu07 opened 6 years ago

thakurmayu07 commented 6 years ago

I can successfully live stream on youtube by using this library but the audio quality of uploaded video is not so good. It is flickering. How can I solve that?

begeekmyfriend commented 6 years ago

I have expanded the PCM sampling buffer. Please try this commit

thakurmayu07 commented 6 years ago

Hi, Thanks for the reply. I did exactly what you have committed but I got BufferOverflowException Below is the error log report

java.nio.BufferOverflowException
at java.nio.DirectByteBuffer.put(DirectByteBuffer.java:264)
at net.ossrs.yasea.SrsEncoder.onGetPcmFrame(SrsEncoder.java:354)
at net.ossrs.yasea.SrsPublisher$2.run(SrsPublisher.java:108)
at java.lang.Thread.run(Thread.java:761)
thakurmayu07 commented 6 years ago

Hi, when I set mPcmBuffer byte array size in SrsPublisher.java to 16384 it gives me BufferOverflowException on bb.put(data, 0, size); in onGetPcmFrame(byte[] data, int size) method. But when I set mPcmBuffer size to 4096 it works fine. Please help.

begeekmyfriend commented 6 years ago

I am afraid 4096 is the limitation for microphone reading.

thakurmayu07 commented 6 years ago

Yes, you were right. 1 frame of a Stereo 48khz 16bit PCM stream is 4 bytes. So to fix the flickering of sound I changed the audio sampling rate to 16KHz and audio bit rate to 64kbps. Now sound is smooth while streaming.

begeekmyfriend commented 6 years ago

But the sampling frequency is 44.1K by default.

thakurmayu07 commented 6 years ago

Yes I changed it to 16000 and keep PCM buffer size to 4096

st4ycool commented 6 years ago

Same problem here. @thakurmayu07 so, the problem is solved? Can you link a commit?

begeekmyfriend commented 6 years ago

@st4ycool You may change this line. But do not issue a PR for it is not a universal solution.

thakurmayu07 commented 6 years ago

@st4ycool As @begeekmyfriend has mentioned it, change it to 16000

st4ycool commented 6 years ago

@begeekmyfriend 16khz doesnt really solve the problem. Here is stream with 16khz audio: https://www.youtube.com/watch?v=3ZXoZMTezmk

@thakurmayu07 have you tried to go below 16k? My MediaCodec crashes if there are lower values

st4ycool commented 6 years ago

It seems to me the issue is in AAC part. ADTS headers writes in some wrong way

begeekmyfriend commented 6 years ago

The ADTS writing code is here. Would you please show me anything wrong?

st4ycool commented 6 years ago

I don't know what exactly wrong, I am trying to solve it for a while. Maybe they are written too often?

begeekmyfriend commented 6 years ago

ADTS is only for AAC-LC. It works all right for other players except Youtube. What kind of format of AAC for Youtube? AAC-HE or AAC-HEv2?

st4ycool commented 6 years ago

No details provided by youtube... does it mean they accept any of them? https://i.imgur.com/vHgNvC0.jpg https://support.google.com/youtube/answer/2853702?hl=en

begeekmyfriend commented 6 years ago

@st4ycool @thakurmayu07 There are two solutions:

Note both of two solutions above are not compatible with some old flash media players.

st4ycool commented 6 years ago

@begeekmyfriend

I tried streaming using both solutions, but it didn't solve issue. But I noticed something very strange:

When I stream LIVE I hear audio flickering, but when youtube saves live stream to videos, it sounds OK. Why?

I was streaming with 4g, and watching my live stream with wired 100mbps, so it's for sure not the bandwidth problem.

It looks like youtube cant decode quickly enough?

begeekmyfriend commented 6 years ago

@st4ycool Maybe. Then what about 16K sample rate?

st4ycool commented 6 years ago

yasea he v2 24000: youtube stream is not starting. yasea he v2 44100: youtube stream is not starting. yasea master 16000: stream is ok, audio is ok(I swear yesterday audio was flickering), also youtube says to set audio sampling rate to 41.1khz or 48khz

What can be wrong with youtube when sample rate is 41.1?

begeekmyfriend commented 6 years ago

Never sound 41.1K, maybe the best choice is 48K for you...

st4ycool commented 6 years ago

My bad, I meant 44.1khz, of course. 48k is flickering.

st4ycool commented 6 years ago

@begeekmyfriend I still havent found a solution. Why we have MediaCodec converting PCM to aac, and why we have to add adts manually?

begeekmyfriend commented 6 years ago

PCM is the format for voice sampling. ADTS header is for AAC-LC and necessary for decoding. But it would not be produced automatically by MediaCodec.

st4ycool commented 6 years ago

@begeekmyfriend I don't see those parts of header in yeasea's writeAdtsHeader: https://i.imgur.com/OOKGZ7c.jpg

P.S. mp4 record sounds great, but it doesn't relate to AAC we are talking about, right?

st4ycool commented 6 years ago

That's how I hear it on youtube stream LIVE: Recording (35).zip and that's how it sounds when youtube autosaves live stream to videos: https://youtu.be/339ILW_WgnY?t=26

begeekmyfriend commented 6 years ago

I forgot to tell you that ADTS is for conventional flash media player which has been abandoned by Youtube.

st4ycool commented 6 years ago

@begeekmyfriend I can start youtube stream without ADTS, but it sounds same terrible when live. What can be wrong, keeping in mind that autosaved stream's sound is perfect?

begeekmyfriend commented 6 years ago

Try it

ffmpeg -re -i test.mp4 -vcodec libx264 -acodec libfdk_aac -f flv rtmp://youtube.com/app/stream

thakurmayu07 commented 6 years ago

@st4ycool @begeekmyfriend Have you found any solution? Live stream with 16K sampling frequency also creates noise when audio is loud or multiple persons are talking. Also I observed that voice is cutting after every 2-3 seconds.

st4ycool commented 6 years ago

@thakurmayu07 the thing is YouTube can't decode yasea's AAC fast enough to broadcast without audio lags (because auto-saved video is fine). So I think the only solution is to improve/replace the AAC part.

I ve been streaming on facebook and there is no such problem like audio lags.

thakurmayu07 commented 6 years ago

As I can see in SrsEncoder class the audio codec is "audio/mp4a-latm". Can we change it to something else?

thakurmayu07 commented 6 years ago

image

We need this configuration

begeekmyfriend commented 6 years ago

You can try on your own. I think it is the issue of Youtube not the streaming. @st4ycool has said that streaming on Facebook is OK, right?

thakurmayu07 commented 6 years ago

Hi guys, I have found the issue. The library is fantastic, you just need to set latency to normal in YouTube admin panel. For more details : https://support.google.com/youtube/answer/7444635?hl=en

yasea

st4ycool commented 6 years ago

@thakurmayu07 genius. How could it have been so easy? Wow! Thank you! Tell me how you decided to try adjust some settings?

I always thought it was something with audio codecs. Sorry, @begeekmyfriend , your codecs are shiny!

begeekmyfriend commented 6 years ago

Unfortunately I do not get access to Youtube quite conveniently in China you know it...

st4ycool commented 6 years ago

@begeekmyfriend I totally forgot about China firewall. Thanks for support.

thakurmayu07 commented 6 years ago

@st4ycool I was going in details about the requirements of video stream. There I found latency settings. I changed that from admin panel and boom! everything works like charm.

@begeekmyfriend This library is awesome. As we understand the issue and come to know that library doesn't do anything wrong, you can close this thread now.

begeekmyfriend commented 6 years ago

@thakurmayu07 Better keep it open and let someone know it.

thakurmayu07 commented 6 years ago

@begeekmyfriend Yes, Sure.

huongtran84 commented 5 years ago

Hi guys, I have found the issue. The library is fantastic, you just need to set latency to normal in YouTube admin panel. For more details : https://support.google.com/youtube/answer/7444635?hl=en

yasea

But I still need low latency and ultra low latency because of An ultra-low-latency stream further reduces the time it takes for video to be visible to viewers, making interaction with viewers easier.If I use normal latency long time.

maniacs-engineerica commented 5 years ago

@thakurmayu07 , I am trying to stream to Youtube too, but it is not working. The logcat is no showing errors, so it seems to work fine. I think the problem is regarding the authentication. How do you push to the rtmp link being authenticated?

Thanks in advance.

huongtran84 commented 5 years ago

long time I still not resolve audio flicker while using youtube with ultra low latency mode.any one help me.

kboskin commented 5 years ago

getting same trouble with AWS MediaLive service. When streaming from obs everything seems to be fine

begeekmyfriend commented 5 years ago

@kostyaBoss please check out the configuration settings on AWS MediaLive service and try more times.

kboskin commented 5 years ago

@begeekmyfriend thanks for quick responding. Maybe you know what exactly should I configure? Stub with this issue for 2 days right now

begeekmyfriend commented 5 years ago

@kostyaBoss In China it is hard to connect with AWS. The reason you know...

kboskin commented 5 years ago

@begeekmyfriend after some research I've found that the problem is in default google encoder. If I am setting force to harware everything goes fine. Btw it also fixes YouTube problem. But it works only on some devices (for ex Samsung) which have their own encoder. How do you think, is it possible to make some expanding and install custom encoder on runtime?

begeekmyfriend commented 5 years ago

@kostyaBoss Your issue is out of my knowledge but the information you provided might be very valuable for others.