Fix aspect ratio for recorded content

The pixel aspect ratio for recorded content is not stored.
The pixel aspect ratio is applied to the width and the
modified width is stored.

Bug: 133364485
Change-Id: I8f6de7a9719167fa3e6c47e403e2f21b494159c3
Test: manual
diff --git a/src/com/android/tv/dvr/ui/playback/DvrPlayer.java b/src/com/android/tv/dvr/ui/playback/DvrPlayer.java
index e8325e1..6cfa2e2 100644
--- a/src/com/android/tv/dvr/ui/playback/DvrPlayer.java
+++ b/src/com/android/tv/dvr/ui/playback/DvrPlayer.java
@@ -521,13 +521,17 @@
                                 for (TvTrackInfo trackInfo : trackInfos) {
                                     if (trackInfo.getId().equals(trackId)) {
                                         float videoAspectRatio;
+                                        float videoPixelAspectRatio =
+                                                trackInfo.getVideoPixelAspectRatio();
                                         int videoWidth = trackInfo.getVideoWidth();
                                         int videoHeight = trackInfo.getVideoHeight();
                                         if (videoWidth > 0 && videoHeight > 0) {
                                             videoAspectRatio =
-                                                    trackInfo.getVideoPixelAspectRatio()
-                                                            * trackInfo.getVideoWidth()
+                                                    (float) trackInfo.getVideoWidth()
                                                             / trackInfo.getVideoHeight();
+                                            videoAspectRatio *=
+                                                    videoPixelAspectRatio > 0 ?
+                                                            videoPixelAspectRatio : 1;
                                         } else {
                                             // Aspect ratio is unknown. Pass the message to
                                             // listeners.
diff --git a/src/com/android/tv/ui/TunableTvView.java b/src/com/android/tv/ui/TunableTvView.java
index 49f7d4c..a736e79 100644
--- a/src/com/android/tv/ui/TunableTvView.java
+++ b/src/com/android/tv/ui/TunableTvView.java
@@ -290,12 +290,12 @@
                                         if (mVideoWidth <= 0 || mVideoHeight <= 0) {
                                             mVideoDisplayAspectRatio = 0.0f;
                                         } else {
-                                            float VideoPixelAspectRatio =
+                                            float videoPixelAspectRatio =
                                                     track.getVideoPixelAspectRatio();
-                                            mVideoDisplayAspectRatio =
-                                                    VideoPixelAspectRatio
-                                                            * mVideoWidth
-                                                            / mVideoHeight;
+                                            mVideoDisplayAspectRatio = (float) mVideoWidth
+                                                    / mVideoHeight;
+                                            mVideoDisplayAspectRatio *= videoPixelAspectRatio > 0 ?
+                                                    videoPixelAspectRatio : 1;
                                         }
                                     } else if (type == TvTrackInfo.TYPE_AUDIO) {
                                         mAudioChannelCount = track.getAudioChannelCount();
diff --git a/tuner/src/com/android/tv/tuner/exoplayer/buffer/SampleChunkIoHelper.java b/tuner/src/com/android/tv/tuner/exoplayer/buffer/SampleChunkIoHelper.java
index 1454bde..82bf0df 100644
--- a/tuner/src/com/android/tv/tuner/exoplayer/buffer/SampleChunkIoHelper.java
+++ b/tuner/src/com/android/tv/tuner/exoplayer/buffer/SampleChunkIoHelper.java
@@ -26,12 +26,14 @@
 import android.util.Pair;
 
 import com.android.tv.common.SoftPreconditions;
+import com.android.tv.common.flags.DvrFlags;
 import com.android.tv.tuner.exoplayer.buffer.RecordingSampleBuffer.BufferReason;
 
 import com.google.android.exoplayer.MediaFormat;
 import com.google.android.exoplayer.SampleHolder;
 import com.google.android.exoplayer.util.MimeTypes;
 import com.google.auto.factory.AutoFactory;
+import com.google.auto.factory.Provided;
 
 import java.io.IOException;
 import java.util.ArrayList;
@@ -67,6 +69,7 @@
     private final BufferManager mBufferManager;
     private final SamplePool mSamplePool;
     private final IoCallback mIoCallback;
+    private final DvrFlags mDvrFlags;
 
     private Handler mIoHandler;
     private final ConcurrentLinkedQueue<SampleHolder> mReadSampleBuffers[];
@@ -147,7 +150,8 @@
             @BufferReason int bufferReason,
             BufferManager bufferManager,
             SamplePool samplePool,
-            IoCallback ioCallback) {
+            IoCallback ioCallback,
+            @Provided DvrFlags dvrFlags) {
         mTrackCount = ids.size();
         mIds = ids;
         mMediaFormats = mediaFormats;
@@ -155,6 +159,7 @@
         mBufferManager = bufferManager;
         mSamplePool = samplePool;
         mIoCallback = ioCallback;
+        mDvrFlags = dvrFlags;
 
         mReadSampleBuffers = new ConcurrentLinkedQueue[mTrackCount];
         mHandlerReadSampleBuffers = new ConcurrentLinkedQueue[mTrackCount];
@@ -208,6 +213,14 @@
                     android.media.MediaFormat format =
                             mMediaFormats.get(i).getFrameworkMediaFormatV16();
                     format.setLong(android.media.MediaFormat.KEY_DURATION, mBufferDurationUs);
+                    if (mDvrFlags.storeVideoAspectRatio() &&
+                            mMediaFormats.get(i).pixelWidthHeightRatio > 0) {
+                        // MediaFormats doesn't store aspect ratio so updating the width
+                        // to maintain aspect ratio.
+                        format.setInteger(android.media.MediaFormat.KEY_WIDTH,
+                                (int) (mMediaFormats.get(i).width *
+                                        mMediaFormats.get(i).pixelWidthHeightRatio));
+                    }
                     if (MimeTypes.isAudio(mMediaFormats.get(i).mimeType)) {
                         audios.add(new BufferManager.TrackFormat(mIds.get(i), format));
                     } else if (MimeTypes.isVideo(mMediaFormats.get(i).mimeType)) {
@@ -316,6 +329,14 @@
                     android.media.MediaFormat format =
                             mMediaFormats.get(i).getFrameworkMediaFormatV16();
                     format.setLong(android.media.MediaFormat.KEY_DURATION, mBufferDurationUs);
+                    if (mDvrFlags.storeVideoAspectRatio() &&
+                            mMediaFormats.get(i).pixelWidthHeightRatio > 0) {
+                        // MediaFormats doesn't store aspect ratio so updating the width
+                        // to maintain aspect ratio.
+                        format.setInteger(android.media.MediaFormat.KEY_WIDTH,
+                                (int) (mMediaFormats.get(i).width *
+                                        mMediaFormats.get(i).pixelWidthHeightRatio));
+                    }
                     if (MimeTypes.isAudio(mMediaFormats.get(i).mimeType)) {
                         audios.add(new BufferManager.TrackFormat(mIds.get(i), format));
                     } else if (MimeTypes.isVideo(mMediaFormats.get(i).mimeType)) {