optimize featching source data · leelizk/AndroidVideoCache@f8f19c5 · GitHub
Skip to content

Commit f8f19c5

Browse files
committed
optimize featching source data
1 parent c0ef7dd commit f8f19c5

11 files changed

Lines changed: 60 additions & 41 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ publish {
2626
userOrg = 'alexeydanilov'
2727
groupId = 'com.danikula'
2828
artifactId = 'videocache'
29-
publishVersion = '2.0.7'
29+
publishVersion = '2.0.9'
3030
description = 'Cache support for android VideoView'
3131
website = 'https://github.com/danikula/AndroidVideoCache'
3232
}

library/src/main/java/com/danikula/videocache/HttpProxyCacheServer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ private void processSocket(Socket socket) {
150150
onError(new ProxyCacheException("Error processing request", e));
151151
} finally {
152152
releaseSocket(socket);
153+
Log.d(LOG_TAG, "Opened connections: " + getClientsCount());
153154
}
154155
}
155156

@@ -164,6 +165,16 @@ private HttpProxyCacheServerClients getClients(String url) throws ProxyCacheExce
164165
}
165166
}
166167

168+
private int getClientsCount() {
169+
synchronized (clientsLock) {
170+
int count = 0;
171+
for (HttpProxyCacheServerClients clients : clientsMap.values()) {
172+
count += clients.getClientsCount();
173+
}
174+
return count;
175+
}
176+
}
177+
167178
private void releaseSocket(Socket socket) {
168179
closeSocketInput(socket);
169180
closeSocketOutput(socket);

library/src/main/java/com/danikula/videocache/HttpProxyCacheServerClients.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,23 @@ public HttpProxyCacheServerClients(String url, FileNameGenerator fileNameGenerat
3434
}
3535

3636
public void processRequest(GetRequest request, Socket socket) throws ProxyCacheException, IOException {
37-
proxyCache = proxyCache == null ? newHttpProxyCache() : proxyCache;
37+
startProcessRequest();
3838
try {
3939
clientsCount.incrementAndGet();
4040
proxyCache.processRequest(request, socket);
4141
} finally {
42-
int count = clientsCount.decrementAndGet();
43-
if (count <= 0) {
44-
proxyCache.shutdown();
45-
proxyCache = null;
46-
}
42+
finishProcessRequest();
43+
}
44+
}
45+
46+
private synchronized void startProcessRequest() throws ProxyCacheException {
47+
proxyCache = proxyCache == null ? newHttpProxyCache() : proxyCache;
48+
}
49+
50+
private synchronized void finishProcessRequest() {
51+
if (clientsCount.decrementAndGet() <= 0) {
52+
proxyCache.shutdown();
53+
proxyCache = null;
4754
}
4855
}
4956

@@ -65,6 +72,10 @@ public void shutdown() {
6572
clientsCount.set(0);
6673
}
6774

75+
public int getClientsCount() {
76+
return clientsCount.get();
77+
}
78+
6879
private HttpProxyCache newHttpProxyCache() throws ProxyCacheException {
6980
HttpUrlSource source = new HttpUrlSource(url);
7081
FileCache cache = new FileCache(fileNameGenerator.generate(url));

library/src/main/java/com/danikula/videocache/HttpUrlSource.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import android.text.TextUtils;
44
import android.util.Log;
55

6+
import java.io.BufferedInputStream;
67
import java.io.IOException;
78
import java.io.InputStream;
89
import java.io.InterruptedIOException;
910
import java.net.HttpURLConnection;
1011
import java.net.URL;
1112

13+
import static com.danikula.videocache.ProxyCacheUtils.DEFAULT_BUFFER_SIZE;
1214
import static com.danikula.videocache.ProxyCacheUtils.LOG_TAG;
1315
import static java.net.HttpURLConnection.HTTP_OK;
1416
import static java.net.HttpURLConnection.HTTP_PARTIAL;
@@ -36,7 +38,7 @@ public HttpUrlSource(String url, String mime) {
3638
}
3739

3840
@Override
39-
public int available() throws ProxyCacheException {
41+
public synchronized int available() throws ProxyCacheException {
4042
if (available == Integer.MIN_VALUE) {
4143
fetchContentInfo();
4244
}
@@ -52,7 +54,7 @@ public void open(int offset) throws ProxyCacheException {
5254
connection.setRequestProperty("Range", "bytes=" + offset + "-");
5355
}
5456
mime = connection.getContentType();
55-
inputStream = connection.getInputStream();
57+
inputStream = new BufferedInputStream(connection.getInputStream(), DEFAULT_BUFFER_SIZE);
5658
available = readSourceAvailableBytes(connection, offset);
5759
} catch (IOException e) {
5860
throw new ProxyCacheException("Error opening connection for " + url + " with offset " + offset, e);
@@ -91,22 +93,27 @@ public int read(byte[] buffer) throws ProxyCacheException {
9193
private void fetchContentInfo() throws ProxyCacheException {
9294
Log.d(LOG_TAG, "Read content info from " + url);
9395
HttpURLConnection urlConnection = null;
96+
InputStream inputStream = null;
9497
try {
9598
urlConnection = (HttpURLConnection) new URL(url).openConnection();
99+
urlConnection.setConnectTimeout(10000);
100+
urlConnection.setReadTimeout(10000);
96101
urlConnection.setRequestMethod("HEAD");
97102
available = urlConnection.getContentLength();
98103
mime = urlConnection.getContentType();
99-
Log.i(LOG_TAG, "Info read: " + this);
104+
inputStream = urlConnection.getInputStream();
105+
Log.i(LOG_TAG, "Content info for `" + url + "`: mime: " + mime + ", content-length: " + available);
100106
} catch (IOException e) {
101107
throw new ProxyCacheException("Error fetching Content-Length from " + url);
102108
} finally {
109+
ProxyCacheUtils.close(inputStream);
103110
if (urlConnection != null) {
104111
urlConnection.disconnect();
105112
}
106113
}
107114
}
108115

109-
public String getMime() throws ProxyCacheException {
116+
public synchronized String getMime() throws ProxyCacheException {
110117
if (TextUtils.isEmpty(mime)) {
111118
fetchContentInfo();
112119
}
@@ -115,10 +122,6 @@ public String getMime() throws ProxyCacheException {
115122

116123
@Override
117124
public String toString() {
118-
return "HttpUrlSource{" +
119-
"url='" + url + '\'' +
120-
", available=" + available +
121-
", mime='" + mime + '\'' +
122-
'}';
125+
return "HttpUrlSource{url='" + url + "}";
123126
}
124127
}

library/src/main/java/com/danikula/videocache/ProxyCache.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,11 @@ public int read(byte[] buffer, long offset, int length) throws ProxyCacheExcepti
4040
while (!cache.isCompleted() && cache.available() < (offset + length) && !stopped) {
4141
readSourceAsync();
4242
waitForSourceData();
43-
checkIsCacheValid();
4443
checkReadSourceErrorsCount();
4544
}
4645
return cache.read(buffer, offset, length);
4746
}
4847

49-
private void checkIsCacheValid() throws ProxyCacheException {
50-
int sourceAvailable = source.available();
51-
if (sourceAvailable > 0 && cache.available() > sourceAvailable) {
52-
throw new ProxyCacheException("Unexpected cache: cache [" + cache.available() + " bytes] > source[" + sourceAvailable + " bytes]");
53-
}
54-
}
55-
5648
private void checkReadSourceErrorsCount() throws ProxyCacheException {
5749
int errorsCount = readSourceErrorsCount.get();
5850
if (errorsCount >= MAX_READ_SOURCE_ATTEMPTS) {
@@ -76,10 +68,10 @@ public void shutdown() {
7668
}
7769
}
7870

79-
private void readSourceAsync() throws ProxyCacheException {
71+
private synchronized void readSourceAsync() throws ProxyCacheException {
8072
boolean readingInProgress = sourceReaderThread != null && sourceReaderThread.getState() != Thread.State.TERMINATED;
8173
if (!stopped && !cache.isCompleted() && !readingInProgress) {
82-
sourceReaderThread = new Thread(new SourceReaderRunnable(), "Source reader for ProxyCache");
74+
sourceReaderThread = new Thread(new SourceReaderRunnable(), "Source reader for " + source);
8375
sourceReaderThread.start();
8476
}
8577
}
@@ -102,7 +94,7 @@ private void notifyNewCacheDataAvailable(int cachePercentage) {
10294
}
10395
}
10496

105-
protected void onCacheAvailable(int percents){
97+
protected void onCacheAvailable(int percents) {
10698
}
10799

108100
private void readSource() {

library/src/main/java/com/danikula/videocache/ProxyCacheUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.danikula.videocache;
22

33
import android.text.TextUtils;
4+
import android.util.Log;
45
import android.webkit.MimeTypeMap;
56

7+
import java.io.Closeable;
68
import java.io.File;
79
import java.io.IOException;
810
import java.io.UnsupportedEncodingException;
@@ -74,4 +76,14 @@ static String decode(String url) {
7476
throw new RuntimeException("Error decoding url", e);
7577
}
7678
}
79+
80+
static void close(Closeable closeable) {
81+
if (closeable != null) {
82+
try {
83+
closeable.close();
84+
} catch (IOException e) {
85+
Log.e(LOG_TAG, "Error closing resource", e);
86+
}
87+
}
88+
}
7789
}

sample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dependencies {
3939
// compile project(':library')
4040
compile 'com.android.support:support-v4:23.0.0'
4141
compile 'org.androidannotations:androidannotations-api:3.3.2'
42-
compile 'com.danikula:videocache:2.0.7'
42+
compile 'com.danikula:videocache:2.0.9'
4343
compile 'com.viewpagerindicator:library:2.4.2-SNAPSHOT@aar'
4444
apt 'org.androidannotations:androidannotations:3.3.2'
4545
}

sample/src/main/java/com/danikula/videocache/sample/GalleryVideoFragment.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public void onPause() {
100100
public void onDestroy() {
101101
super.onDestroy();
102102

103+
videoView.stopPlayback();
103104
App.getProxy(getActivity()).unregisterCacheListener(this);
104105
}
105106

sample/src/main/java/com/danikula/videocache/sample/VideoFragment.java

Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)