CasperPas / flutter-sound-stream

stream audio data in & out
GNU General Public License v3.0
82 stars 110 forks source link

Cross Platform Audio stream not playing #2

Open dhalloop7 opened 4 years ago

dhalloop7 commented 4 years ago

I am writing code to stream audio data over a socket. It's working fine with Android to Android and iOS to iOS devices. But When I am trying to stream bytes from Android and trying to play on iOS It's not playing the received buffer. I also noticed that the chunk length received from the plugin is 7156 white from iOS its length is 16386.

CasperPas commented 4 years ago

I think the problem is on Android side, Uint16 (16bit PCM) is converted to Uint8 to transfer to dart side, iOS does the same thing but I used "pointer conversion" (withMemoryRebound). I think Android's algorithm should be refactored. I'll fix & test it over a socket. Are you using WebSocket, @dhalloop7 ?

dhalloop7 commented 4 years ago

Yes, @CasperPas I am using WebSocket.

RizanPSTU commented 4 years ago

Dose cross-platform works?

RizanPSTU commented 4 years ago

I am also using socket io

dhalloop7 commented 4 years ago

I tried today ios to ios as well but it won't work for me. Android to Android share byte and play works good for me

CasperPas commented 4 years ago

@dhalloop7 could you provide the steps to reproduce the issue? I've tried multiple times with websocket and it's fine as long as it's not cross-platform (I'm working on that)

CasperPas commented 4 years ago

I'm working on it and if you guys have any ideas, all contributions are really appreciated https://github.com/CasperPas/flutter-sound-stream/pull/5

dhalloop7 commented 4 years ago

I am using socket.io for sending and receiving bytes from one device to another.

During initializing recorder and player I am using sampleRate 44100.

At the sender side when I am getting the bytes in the stream I am emitting those bytes with some additional information over the event.

_recorder.audioStream.listen((data) {
      socket.emit("eventName",data);
});

At the receiver side as soon as socket connects I am initializing the player and called method start. i.e

await _player.initialize();
await _player.start();

And when I am receiving the data bytes or chunks over the socket I am trying to play chunks as below.

 _socket.on(event, (data){
       _player.writeChunk(data);
});

I am also doing trial and error with changing the bit format (8, 16, 32) of recorder and player. You can try by doing it as well.

CasperPas commented 4 years ago

@dhalloop7 @RizanPSTU hey guys, try my PR again. I've just pushed a new commit to fix this. The main problem is byte's order: LITTLE_ENDIAN. https://github.com/CasperPas/flutter-sound-stream/pull/5

I'll do some extra tests and if it's ok, I'll publish a new version 😄

dhalloop7 commented 4 years ago

Sounds great. I am trying it now.

CasperPas commented 4 years ago

I updated the package, is it working well?

dhalloop7 commented 4 years ago

Yes, I tried from the branch. It's player for iOS to Android but not played for Android to iOS.

CasperPas commented 4 years ago

Can you try it again from master? I tested it with both my iPhone and simulator and it works fine. Did iOS receive the data from Android properly? Please help me test it with logging/debugging.

dhalloop7 commented 4 years ago

I tried with the master branch. It plays the bytes from iOS to Android. iOS receiving the bytes from Android device properly but it's not playing any chunks. Whereas bytes from iOS received at the Android side are playing but not playing in iOS devices.

CasperPas commented 4 years ago

which devices (iOS and Android) are you using?

dhalloop7 commented 4 years ago

Can we connect over some calling platforms or on Google Meet or Google Hangout?

CasperPas commented 4 years ago

I think we can. You can contact me directly via the email on my profile. Both Hangout or Duo works. (please email me first 😄 )

dhalloop7 commented 4 years ago

I tried with the master branch. It's submitting audio over the cross devices. but found something new that

  1. Audio stream not playing if device is in silent/vibrate mode
  2. It's not starting the recorder sometime in iOS devices
  3. The received audio voice is very low if the device has same speaker and earpiece.
  4. If the Recording device mic comes near the input it gives the better output. I think it's a noise cancellation issue.
CasperPas commented 4 years ago

1.I think it it's iOS's policy. But I'll double-check that. 2.Please help me get more info on this, how to reproduce it. 3 & 4. I'll investigate further.

Thanks!

dhalloop7 commented 4 years ago

I think if you check earpiece issue and route audio through the Speaker only it may solve 1st issue as well. Also, it's great if you provide a method to change the audio playing route i.e. Speaker or Earpiece or any connected device like Bluetooth or Earphone. 2nd issue I am facing sometimes only. Also, If I manually disable the microphone permission and again start the app it's not asking for permission again.

CasperPas commented 4 years ago

Thanks for your PR! I'm testing it. I'm so freaking busy these days 😅

CasperPas commented 4 years ago

Tried your PR and it's not working on my Android phone but I found out how to make it works on Android anyway 😁 Trying your iOS code now. It's not working, I might be missing something. Gotta go now 😅

dhalloop7 commented 4 years ago

Are you sure it was not worked for iOS? It's worked for me. Can we connect for the same?

CasperPas commented 4 years ago

No it's not. I copy-paste your code and tried on my Pixel 3 XL, with my bluetooth earbuds. I'll try again to make sure if it's really working or not.

CasperPas commented 4 years ago

I'm making progress with iOS. It can be switched from headsets to built-in devices (both input & output at the same time). But on Android side, only output is switched, it always uses built-in mic for recording even when I change input source value. Trying harder now!!!

CasperPas commented 4 years ago

https://github.com/CasperPas/flutter-sound-stream/pull/11 hi guys, please help me check this PR if it's good. It's still WIP but I'm so busy lately. 😅 Thanks!

dhalloop7 commented 4 years ago

@CasperPas I tried it with android device by adding the line await _player.usePhoneSpeaker(false); It's giving output from the speaker of the phone and if I am using true it's giving output from the earpiece. That's working great. You can merge this PR #11

CasperPas commented 4 years ago

oh really? so true and false should be the other way around 🤣 true -> use built-in speaker & mic false -> use something else (bluetooth headsets, headphones

dhalloop7 commented 4 years ago

Yes. But I observed that I need to keep my device in Ringing Mode. It's not playing audio while it's in silent mode even if keep Media Volume high. That's the issue. @CasperPas

FilipeOS commented 4 years ago

Yes. But I observed that I need to keep my device in Ringing Mode. It's not playing audio while it's in silent mode even if keep Media Volume high. That's the issue. @CasperPas

This is normal on Android and that's a good think actually. You don't want to have phone mode in silent and have something making noise

dhalloop7 commented 4 years ago

Yes. But I observed that I need to keep my device in Ringing Mode. It's not playing audio while it's in silent mode even if keep Media Volume high. That's the issue. @CasperPas

This is normal on Android and that's a good think actually. You don't want to have phone mode in silent and have something making noise

Yes, it's like a media application. Even if in Silent mode it's returning Audio with respective to Media Volume

CasperPas commented 4 years ago

@dhalloop7 can you help me try to fix it? Really appreciate it 😁 And I think we can create another issue for that.

dhalloop7 commented 4 years ago

@dhalloop7 can you help me try to fix it? Really appreciate it 😁 And I think we can create another issue for that.

@CasperPas Surely I will help you with this. I am also running busy these days. We can create another bug for this issue and close the current one as the cross-platform audio is streaming ;)

dhalloop7 commented 4 years ago

@dhalloop7 can you help me try to fix it? Really appreciate it 😁 And I think we can create another issue for that.

@CasperPas raised PR #13 for if the device is silent or vibrate mode it can play streamed audio bytes. Can you please review and test it?