<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: moubarak</title>
    <description>The latest articles on DEV Community by moubarak (@moubarak).</description>
    <link>https://dev.to/moubarak</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F604149%2F7a499dd9-5bba-49fe-8374-8c4e4622670b.png</url>
      <title>DEV Community: moubarak</title>
      <link>https://dev.to/moubarak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/moubarak"/>
    <language>en</language>
    <item>
      <title>Debug Android emulator audio errors</title>
      <dc:creator>moubarak</dc:creator>
      <pubDate>Tue, 30 Mar 2021 16:55:14 +0000</pubDate>
      <link>https://dev.to/trydaily/debug-android-emulator-audio-errors-4bn4</link>
      <guid>https://dev.to/trydaily/debug-android-emulator-audio-errors-4bn4</guid>
      <description>&lt;p&gt;When we built our &lt;a href="https://www.daily.co/blog/how-to-build-a-billion-dollar-audio-app-in-a-weekend?utm_source=dev&amp;amp;utm_campaign=debug-android-emulator-audio-errors" rel="noopener noreferrer"&gt;audio-only demo&lt;/a&gt; &lt;a href="https://partyline.daily.co/?utm_source=dev&amp;amp;utm_campaign=debug-android-emulator-audio-errors" rel="noopener noreferrer"&gt;Party Line&lt;/a&gt;, 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 &lt;del&gt;bugs&lt;/del&gt; learning opportunities along the way. &lt;/p&gt;

&lt;p&gt;We found them fast! When we loaded &lt;a href="https://gist.github.com/kwindla/5b55e4eef09a7352452608e5859f903f#file-audio-client-html?utm_source=dev&amp;amp;utm_campaign=debug-android-emulator-audio-errors" rel="noopener noreferrer"&gt;our gist prototype&lt;/a&gt; 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. &lt;/p&gt;

&lt;p&gt;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!). &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;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 &amp;lt; 29. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Find the root error
&lt;/h2&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// don't play the local audio track (echo!)&lt;/span&gt;
&lt;span class="c1"&gt;// if (evt.participant.local) {&lt;/span&gt;
&lt;span class="c1"&gt;//   return;&lt;/span&gt;
&lt;span class="c1"&gt;// }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could hear our local audio with those lines removed, so we eliminated mic issues as the culprit. &lt;/p&gt;

&lt;p&gt;Next, we loaded the &lt;a href="https://test.webrtc.org/" rel="noopener noreferrer"&gt;webRTC troubleshooter&lt;/a&gt; in the emulator WebView. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2l7si9heozb7czm27rdb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2l7si9heozb7czm27rdb.png" alt="Screenshot of the webRTC troubleshooter screen in the Android emulator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Android &lt;a href="https://source.android.com/security/selinux/validate" rel="noopener noreferrer"&gt;docs&lt;/a&gt; helped us parse this error and determine that SELinux was preventing the emulator from communicating with the socket. It wasn’t granting those &lt;code&gt;nlmsg_readpriv&lt;/code&gt; so the emulator could listen to audio. &lt;/p&gt;

&lt;h2&gt;
  
  
  Run &lt;code&gt;audit2allow&lt;/code&gt; to grant read privileges
&lt;/h2&gt;

&lt;p&gt;Continuing to read the docs, we found the &lt;a href="https://source.android.com/security/selinux/validate#using_audit2allow" rel="noopener noreferrer"&gt;&lt;code&gt;audit2allow&lt;/code&gt;&lt;/a&gt; command that converts denials into corresponding policy statements. All we had to do was run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;adb pull /sys/fs/selinux/policy
adb logcat -b all -d | audit2allow -p policy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with that, we had audio in our emulator! &lt;/p&gt;

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

</description>
      <category>android</category>
      <category>webdev</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
