Debug Android emulator audio errors
Tags: android, webdev, mobile
Published: 2021-03-30 16:55:14

When we built our [audio-only demo](https://www.daily.co/blog/how-to-build-a-billion-dollar-audio-app-in-a-weekend?utm_source=dev&utm_campaign=debug-android-emulator-audio-errors) [Party Line](https://partyline.daily.co/?utm_source=dev&utm_campaign=debug-android-emulator-audio-errors), we knew we wanted to offer multi-platform support from day one. Android support for audio applications has been notoriously limited, so we knew to brace ourselves for some fun new ~~bugs~~ learning opportunities along the way. 

We found them fast! When we loaded [our gist prototype](https://gist.github.com/kwindla/5b55e4eef09a7352452608e5859f903f#file-audio-client-html?utm_source=dev&utm_campaign=debug-android-emulator-audio-errors) in a WebView, everything worked on our physical devices, but in the emulator no audio came through. This was awkward for any audio-only app, and especially one that we wanted to work across multiple Android devices and versions. 

This post walks through how we debugged this error, in case that could be helpful for troubleshooting similar but different errors. Of course, we’ll also go over our solution (scroll to the bottom if you want to skip ahead!). 

> Before we dive in, it’s worth noting that we only ran into this problem with Android API level 30; audio worked out of the box when we switched to &lt; 29. 

## Find the root error 

First, we wanted to rule out a mic permissions issue as a potential cause. A few lines in our [gist ](https://gist.github.com/kwindla/5b55e4eef09a7352452608e5859f903f#file-audio-client-html?utm_source=dev&utm_campaign=debug-android-emulator-audio-errors)stop local audio from playing to prevent an on-call echo. We commented them out to test if we could hear our own audio. 

```javascript 
// don't play the local audio track (echo!)
// if (evt.participant.local) {
//   return;
// }
```

We could hear our local audio with those lines removed, so we eliminated mic issues as the culprit. 

Next, we loaded the [webRTC troubleshooter](https://test.webrtc.org/) in the emulator WebView. 

![Screenshot of the webRTC troubleshooter screen in the Android emulator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2l7si9heozb7czm27rdb.png)

We could join the meeting room, our local audio still worked, and the troubleshooter got stuck at the Udp test. This pointed us to a networking issue, and, since we could join the room, it also suggested a socket layer problem. We now knew what to look for in Logcat. 

Logcat dumps a lot of information. We went through many rounds of quitting and relaunching to spot the error message we needed right after the popup for mic permissions: 

```
2021-02-13 23:16:11.993 19189-19275/com.daily.audioonly E/chromium: [ERROR:address_tracker_linux.cc(245)] Could not send NETLINK request: Permission denied (13) 
2021-02-13 23:16:11.987 19189-19189/com.daily.audioonly W/ThreadPoolForeg: type=1400 audit(0.0:457): avc: denied { nlmsg_readpriv } for scontext=u:r:untrusted_app:s0:c156,c256,c512,c768 tcontext=u:r:untrusted_app:s0:c156,c256,c512,c768 tclass=netlink_route_socket permissive=0 b/155595000 app=com.daily.audioonly
```

The Android [docs](https://source.android.com/security/selinux/validate) helped us parse this error and determine that SELinux was preventing the emulator from communicating with the socket. It wasn’t granting those `nlmsg_readpriv` so the emulator could listen to audio. 

## Run `audit2allow` to grant read privileges 

Continuing to read the docs, we found the [`audit2allow`](https://source.android.com/security/selinux/validate#using_audit2allow) command that converts denials into corresponding policy statements. All we had to do was run:

```
adb pull /sys/fs/selinux/policy
adb logcat -b all -d | audit2allow -p policy
```

And with that, we had audio in our emulator! 

From there, we had a lot of fun building out the full [Party Line codebase](https://github.com/daily-demos/party-line?utm_source=dev&utm_campaign=debug-android-emulator-audio-errors) (we walk through the whole thing on [our blog](https://www.daily.co/blog/how-to-build-a-billion-dollar-audio-app-in-a-weekend/?utm_source=dev&utm_campaign=debug-android-emulator-audio-errors). Checkout the Android source code for both [Java](https://github.com/daily-demos/party-line/tree/main/android/java?utm_source=dev&utm_campaign=debug-android-emulator-audio-errors) and [Kotlin](https://github.com/daily-demos/party-line/tree/main/android/kotlin?utm_source=dev&utm_campaign=debug-android-emulator-audio-errors), and keep us posted on how it goes!