Fix race condition in MediaHTTPConnection
getSize() and getMIMEType() rely on seekTo() having completed.
If seekTo() was called on a different thread, for example because
NuCachedSource2 was doing reads on a background thread, getSize()
could return -1 if it was called after connection was established,
but before the response had been parsed.
Bug: 110230427
Test: manual
Change-Id: I469b47e025f1c7a7ef3123d3347668f0541e6f2d
(cherry picked from commit d4f64f27b0de1c5d21c93b4d73acd05ede28fde2)
diff --git a/media/java/android/media/MediaHTTPConnection.java b/media/java/android/media/MediaHTTPConnection.java
index aae1f51..6bf52bd 100644
--- a/media/java/android/media/MediaHTTPConnection.java
+++ b/media/java/android/media/MediaHTTPConnection.java
@@ -323,8 +323,10 @@
StrictMode.setThreadPolicy(policy);
try {
- if (offset != mCurrentOffset) {
- seekTo(offset);
+ synchronized(this) {
+ if (offset != mCurrentOffset) {
+ seekTo(offset);
+ }
}
int n = mInputStream.read(data, 0, size);
@@ -366,7 +368,7 @@
}
@Override
- public long getSize() {
+ public synchronized long getSize() {
if (mConnection == null) {
try {
seekTo(0);
@@ -379,7 +381,7 @@
}
@Override
- public String getMIMEType() {
+ public synchronized String getMIMEType() {
if (mConnection == null) {
try {
seekTo(0);