Enables broadcast radio in Car emulator

Also exposes the FM tuner as an audio input device in emulator.

Bug: 118763832
Test: Launch Radio app (with startAudioSource) in emulator
Change-Id: I75732a68382f8453bc858da8de2de32417c63d98
diff --git a/emulator/audio/car_emulator_audio.mk b/emulator/audio/car_emulator_audio.mk
index efe7fa4..bef5c2e 100644
--- a/emulator/audio/car_emulator_audio.mk
+++ b/emulator/audio/car_emulator_audio.mk
@@ -19,6 +19,7 @@
 PRODUCT_PROPERTY_OVERRIDES += ro.hardware.audio.primary=caremu
 
 PRODUCT_COPY_FILES += \
+    frameworks/native/data/etc/android.hardware.broadcastradio.xml:system/etc/permissions/android.hardware.broadcastradio.xml \
     frameworks/av/services/audiopolicy/config/a2dp_audio_policy_configuration.xml:$(TARGET_COPY_OUT_VENDOR)/etc/a2dp_audio_policy_configuration.xml \
     frameworks/av/services/audiopolicy/config/usb_audio_policy_configuration.xml:$(TARGET_COPY_OUT_VENDOR)/etc/usb_audio_policy_configuration.xml \
     frameworks/av/services/audiopolicy/config/r_submix_audio_policy_configuration.xml:$(TARGET_COPY_OUT_VENDOR)/etc/r_submix_audio_policy_configuration.xml \
diff --git a/emulator/audio/driver/audio_hw.c b/emulator/audio/driver/audio_hw.c
index 8521f7e..b1297cf 100644
--- a/emulator/audio/driver/audio_hw.c
+++ b/emulator/audio/driver/audio_hw.c
@@ -727,6 +727,17 @@
     return 0;
 }
 
+#define STEP (3.14159265 / 180)
+// Generates pure tone for FM_TUNER
+static int pseudo_pcm_read(void *data, unsigned int count) {
+    unsigned int length = count / sizeof(short);
+    short *sdata = (short *)data;
+    for (int index = 0; index < length; index++) {
+        sdata[index] = (short)(sin(index * STEP) * 4096);
+    }
+    return count;
+}
+
 static void *in_read_worker(void *args) {
     struct generic_stream_in *in = (struct generic_stream_in *)args;
     struct pcm *pcm = NULL;
@@ -819,7 +830,10 @@
     if (in->worker_standby) {
         in->worker_standby = false;
     }
-    pthread_cond_signal(&in->worker_wake);
+    // FM_TUNER fills the buffer via pseudo_pcm_read directly
+    if (in->device != AUDIO_DEVICE_IN_FM_TUNER) {
+        pthread_cond_signal(&in->worker_wake);
+    }
 
     int64_t current_position;
     struct timespec current_time;
@@ -854,7 +868,10 @@
     }
     in->standby_frames_read += frames;
 
-    if (popcount(in->req_config.channel_mask) == 1 &&
+    if (in->device == AUDIO_DEVICE_IN_FM_TUNER) {
+        int read_bytes = pseudo_pcm_read(buffer, bytes);
+        read_frames = read_bytes / audio_stream_in_frame_size(stream);
+    } else if (popcount(in->req_config.channel_mask) == 1 &&
         in->pcm_config.channels == 2) {
         // Need to resample to mono
         if (in->stereo_to_mono_buf_size < bytes*2) {