Add TV tuner audio preselection support

DVB MPEG-2 transport streams can include audio preselection descriptors
for next-generation audio (NGA) as specified in ETSI EN 300 468. Add
support for audio preselections in DemuxFilterMediaEvent.

Note that, when an audio preselection descriptor is present for a NGA
stream, receivers are required to ignore (supplementary) audio
descriptors (see Annex M.2).

Bug: 264812332
Test: `atest VtsHalTvTunerTargetTest` on cf_x86_tv-userdebug
Change-Id: I5b6d9a86aa74feb87879125f57ca36d7e71bbdf4
diff --git a/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPreselection.aidl b/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPreselection.aidl
new file mode 100644
index 0000000..ab0404e
--- /dev/null
+++ b/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPreselection.aidl
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.hardware.tv.tuner;
+/* @hide */
+@VintfStability
+parcelable AudioPreselection {
+  int preselectionId;
+  android.hardware.tv.tuner.AudioPreselectionLabel[] labels;
+  String language;
+  android.hardware.tv.tuner.AudioPreselectionRenderingIndicationType renderingIndication;
+  boolean hasAudioDescription;
+  boolean hasSpokenSubtitles;
+  boolean hasDialogueEnhancement;
+}
diff --git a/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPreselectionLabel.aidl b/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPreselectionLabel.aidl
new file mode 100644
index 0000000..79f3b6f
--- /dev/null
+++ b/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPreselectionLabel.aidl
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.hardware.tv.tuner;
+/* @hide */
+@VintfStability
+parcelable AudioPreselectionLabel {
+  String language;
+  String text;
+}
diff --git a/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPreselectionRenderingIndicationType.aidl b/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPreselectionRenderingIndicationType.aidl
new file mode 100644
index 0000000..783511f
--- /dev/null
+++ b/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPreselectionRenderingIndicationType.aidl
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.hardware.tv.tuner;
+/* @hide */
+@Backing(type="int") @VintfStability
+enum AudioPreselectionRenderingIndicationType {
+  NOT_INDICATED = 0,
+  STEREO = 1,
+  TWO_DIMENSIONAL = 2,
+  THREE_DIMENSIONAL = 3,
+  HEADPHONE = 4,
+}
diff --git a/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPresentation.aidl b/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPresentation.aidl
new file mode 100644
index 0000000..96a6d98
--- /dev/null
+++ b/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/AudioPresentation.aidl
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a snapshot of an AIDL file. Do not edit it manually. There are
+// two cases:
+// 1). this is a frozen version file - do not edit this in any case.
+// 2). this is a 'current' file. If you make a backwards compatible change to
+//     the interface (from the latest frozen version), the build system will
+//     prompt you to update this file with `m <name>-update-api`.
+//
+// You must not make a backward incompatible change to any AIDL file built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
+package android.hardware.tv.tuner;
+/* @hide */
+@VintfStability
+parcelable AudioPresentation {
+  android.hardware.tv.tuner.AudioPreselection preselection;
+  int ac4ShortProgramId = -1;
+}
diff --git a/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/DemuxFilterMediaEventExtraMetaData.aidl b/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/DemuxFilterMediaEventExtraMetaData.aidl
index ab36188..28caf19 100644
--- a/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/DemuxFilterMediaEventExtraMetaData.aidl
+++ b/tv/tuner/aidl/aidl_api/android.hardware.tv.tuner/current/android/hardware/tv/tuner/DemuxFilterMediaEventExtraMetaData.aidl
@@ -37,4 +37,5 @@
 union DemuxFilterMediaEventExtraMetaData {
   boolean noinit;
   android.hardware.tv.tuner.AudioExtraMetaData audio;
+  android.hardware.tv.tuner.AudioPresentation[] audioPresentations = {};
 }
diff --git a/tv/tuner/aidl/android/hardware/tv/tuner/AudioPreselection.aidl b/tv/tuner/aidl/android/hardware/tv/tuner/AudioPreselection.aidl
new file mode 100644
index 0000000..16ef0dd
--- /dev/null
+++ b/tv/tuner/aidl/android/hardware/tv/tuner/AudioPreselection.aidl
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hardware.tv.tuner;
+
+import android.hardware.tv.tuner.AudioPreselectionLabel;
+import android.hardware.tv.tuner.AudioPreselectionRenderingIndicationType;
+
+/**
+ * Audio preselection metadata according to ETSI EN 300 468 V1.17.1.
+ * @hide
+ */
+@VintfStability
+parcelable AudioPreselection {
+    /**
+     * Identifies this audio preselection. The value of this field shall match the corresponding
+     * field in the elementary stream.
+     */
+    int preselectionId;
+
+    /**
+     * Collection of audio preselection labels.
+     */
+    AudioPreselectionLabel[] labels;
+
+    /**
+     * ISO 639-2 3-character code.
+     */
+    String language;
+
+    /**
+     * A hint for a preferred reproduction channel layout.
+     */
+    AudioPreselectionRenderingIndicationType renderingIndication;
+
+    /**
+     * Audio preselection contains audio description for the visually impaired.
+     */
+    boolean hasAudioDescription;
+
+    /**
+     * Audio preselection contains spoken subtitles.
+     */
+    boolean hasSpokenSubtitles;
+
+    /**
+     * Audio preselection provides support for dialogue enhancement.
+     */
+    boolean hasDialogueEnhancement;
+}
diff --git a/tv/tuner/aidl/android/hardware/tv/tuner/AudioPreselectionLabel.aidl b/tv/tuner/aidl/android/hardware/tv/tuner/AudioPreselectionLabel.aidl
new file mode 100644
index 0000000..429b026
--- /dev/null
+++ b/tv/tuner/aidl/android/hardware/tv/tuner/AudioPreselectionLabel.aidl
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hardware.tv.tuner;
+
+/**
+ * Audio preselection label according to ETSI EN 300 468 V1.17.1.
+ * @hide
+ */
+@VintfStability
+parcelable AudioPreselectionLabel {
+    /**
+     * ISO 639-2 3-character code.
+     */
+    String language;
+
+    /**
+     * Text information, written in the language specified, pertaining to the preselection.
+     */
+    String text;
+}
diff --git a/tv/tuner/aidl/android/hardware/tv/tuner/AudioPreselectionRenderingIndicationType.aidl b/tv/tuner/aidl/android/hardware/tv/tuner/AudioPreselectionRenderingIndicationType.aidl
new file mode 100644
index 0000000..963c55a
--- /dev/null
+++ b/tv/tuner/aidl/android/hardware/tv/tuner/AudioPreselectionRenderingIndicationType.aidl
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hardware.tv.tuner;
+
+/**
+ * Audio preselection audio rendering indication according to ETSI EN 300 468 V1.17.1.
+ * @hide
+ */
+@VintfStability
+@Backing(type="int")
+enum AudioPreselectionRenderingIndicationType {
+    /**
+     * No preference given for the reproduction channel layout.
+     */
+    NOT_INDICATED,
+
+    /**
+     * Preferred reproduction channel layout is stereo.
+     */
+    STEREO,
+
+    /**
+     * Preferred reproduction channel layout is two-dimensional (e.g. 5.1 multi-channel).
+     */
+    TWO_DIMENSIONAL,
+
+    /**
+     * Preferred reproduction channel layout is three-dimensional.
+     */
+    THREE_DIMENSIONAL,
+
+    /**
+     * Content is pre-rendered for consumption with headphones.
+     */
+    HEADPHONE,
+}
diff --git a/tv/tuner/aidl/android/hardware/tv/tuner/AudioPresentation.aidl b/tv/tuner/aidl/android/hardware/tv/tuner/AudioPresentation.aidl
new file mode 100644
index 0000000..50f9d90
--- /dev/null
+++ b/tv/tuner/aidl/android/hardware/tv/tuner/AudioPresentation.aidl
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.hardware.tv.tuner;
+
+import android.hardware.tv.tuner.AudioPreselection;
+
+/**
+ * Audio presentation metadata.
+ * @hide
+ */
+@VintfStability
+parcelable AudioPresentation {
+    /**
+     * Audio preselection.
+     */
+    AudioPreselection preselection;
+
+    /**
+     * Dolby AC-4 short program id specified in ETSI TS 103 190-2. For use in conjunction with
+     * an audio preselection to ensure contininuity of personalized experience during program
+     * transitions.
+     */
+    int ac4ShortProgramId = -1;
+}
diff --git a/tv/tuner/aidl/android/hardware/tv/tuner/DemuxFilterMediaEventExtraMetaData.aidl b/tv/tuner/aidl/android/hardware/tv/tuner/DemuxFilterMediaEventExtraMetaData.aidl
index f01952b..4ff33a2 100644
--- a/tv/tuner/aidl/android/hardware/tv/tuner/DemuxFilterMediaEventExtraMetaData.aidl
+++ b/tv/tuner/aidl/android/hardware/tv/tuner/DemuxFilterMediaEventExtraMetaData.aidl
@@ -17,6 +17,7 @@
 package android.hardware.tv.tuner;
 
 import android.hardware.tv.tuner.AudioExtraMetaData;
+import android.hardware.tv.tuner.AudioPresentation;
 
 /**
  * Extra Meta Data for DemuxFilterMediaEvent.
@@ -30,4 +31,9 @@
     boolean noinit;
 
     AudioExtraMetaData audio;
+
+    /**
+     * Audio presentations available for user selection.
+     */
+    AudioPresentation[] audioPresentations = {};
 }
diff --git a/tv/tuner/aidl/default/Filter.cpp b/tv/tuner/aidl/default/Filter.cpp
index 59e301d..ba9602e 100644
--- a/tv/tuner/aidl/default/Filter.cpp
+++ b/tv/tuner/aidl/default/Filter.cpp
@@ -329,7 +329,8 @@
     // All the filter event callbacks in start are for testing purpose.
     switch (mType.mainType) {
         case DemuxFilterMainType::TS:
-            createMediaEvent(events);
+            createMediaEvent(events, false);
+            createMediaEvent(events, true);
             createTsRecordEvent(events);
             createTemiEvent(events);
             break;
@@ -1221,15 +1222,7 @@
     return (stat1.st_dev == stat2.st_dev) && (stat1.st_ino == stat2.st_ino);
 }
 
-void Filter::createMediaEvent(vector<DemuxFilterEvent>& events) {
-    AudioExtraMetaData audio;
-    audio.adFade = 1;
-    audio.adPan = 2;
-    audio.versionTextTag = 3;
-    audio.adGainCenter = 4;
-    audio.adGainFront = 5;
-    audio.adGainSurround = 6;
-
+void Filter::createMediaEvent(vector<DemuxFilterEvent>& events, bool isAudioPresentation) {
     DemuxFilterMediaEvent mediaEvent;
     mediaEvent.streamId = 1;
     mediaEvent.isPtsPresent = true;
@@ -1239,7 +1232,43 @@
     mediaEvent.isSecureMemory = true;
     mediaEvent.mpuSequenceNumber = 6;
     mediaEvent.isPesPrivateData = true;
-    mediaEvent.extraMetaData.set<DemuxFilterMediaEventExtraMetaData::Tag::audio>(audio);
+
+    if (isAudioPresentation) {
+        AudioPresentation audioPresentation0{
+                .preselection.preselectionId = 0,
+                .preselection.labels = {{"en", "Commentator"}, {"es", "Comentarista"}},
+                .preselection.language = "en",
+                .preselection.renderingIndication =
+                        AudioPreselectionRenderingIndicationType::THREE_DIMENSIONAL,
+                .preselection.hasAudioDescription = false,
+                .preselection.hasSpokenSubtitles = false,
+                .preselection.hasDialogueEnhancement = true,
+                .ac4ShortProgramId = 42};
+        AudioPresentation audioPresentation1{
+                .preselection.preselectionId = 1,
+                .preselection.labels = {{"en", "Crowd"}, {"es", "Multitud"}},
+                .preselection.language = "en",
+                .preselection.renderingIndication =
+                        AudioPreselectionRenderingIndicationType::THREE_DIMENSIONAL,
+                .preselection.hasAudioDescription = false,
+                .preselection.hasSpokenSubtitles = false,
+                .preselection.hasDialogueEnhancement = false,
+                .ac4ShortProgramId = 42};
+        vector<AudioPresentation> audioPresentations;
+        audioPresentations.push_back(audioPresentation0);
+        audioPresentations.push_back(audioPresentation1);
+        mediaEvent.extraMetaData.set<DemuxFilterMediaEventExtraMetaData::Tag::audioPresentations>(
+                audioPresentations);
+    } else {
+        AudioExtraMetaData audio;
+        audio.adFade = 1;
+        audio.adPan = 2;
+        audio.versionTextTag = 3;
+        audio.adGainCenter = 4;
+        audio.adGainFront = 5;
+        audio.adGainSurround = 6;
+        mediaEvent.extraMetaData.set<DemuxFilterMediaEventExtraMetaData::Tag::audio>(audio);
+    }
 
     int av_fd = createAvIonFd(BUFFER_SIZE);
     if (av_fd == -1) {
diff --git a/tv/tuner/aidl/default/Filter.h b/tv/tuner/aidl/default/Filter.h
index f2d9248..0985296 100644
--- a/tv/tuner/aidl/default/Filter.h
+++ b/tv/tuner/aidl/default/Filter.h
@@ -230,7 +230,7 @@
     ::ndk::ScopedAStatus createShareMemMediaEvents(vector<int8_t>& output);
     bool sameFile(int fd1, int fd2);
 
-    void createMediaEvent(vector<DemuxFilterEvent>&);
+    void createMediaEvent(vector<DemuxFilterEvent>&, bool isAudioPresentation);
     void createTsRecordEvent(vector<DemuxFilterEvent>&);
     void createMmtpRecordEvent(vector<DemuxFilterEvent>&);
     void createSectionEvent(vector<DemuxFilterEvent>&);