1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
package github.daneren2005.dsub.service;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.message.BasicHttpRequest;
import android.os.AsyncTask;
import android.os.Looper;
import android.util.Log;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.util.Constants;
public class StreamProxy implements Runnable {
private static final String TAG = StreamProxy.class.getSimpleName();
private Thread thread;
private boolean isRunning;
private ServerSocket socket;
private int port;
private DownloadService downloadService;
public StreamProxy(DownloadService downloadService) {
// Create listening socket
try {
socket = new ServerSocket(0, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }));
socket.setSoTimeout(5000);
port = socket.getLocalPort();
this.downloadService = downloadService;
} catch (UnknownHostException e) { // impossible
} catch (IOException e) {
Log.e(TAG, "IOException initializing server", e);
}
}
public int getPort() {
return port;
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void stop() {
isRunning = false;
thread.interrupt();
}
@Override
public void run() {
isRunning = true;
while (isRunning) {
try {
Socket client = socket.accept();
if (client == null) {
continue;
}
Log.i(TAG, "client connected");
StreamToMediaPlayerTask task = new StreamToMediaPlayerTask(client);
if (task.processRequest()) {
new Thread(task).start();
}
} catch (SocketTimeoutException e) {
// Do nothing
} catch (IOException e) {
Log.e(TAG, "Error connecting to client", e);
}
}
Log.i(TAG, "Proxy interrupted. Shutting down.");
}
private class StreamToMediaPlayerTask implements Runnable {
DownloadFile downloadFile;
File file;
Socket client;
int cbSkip = 0;
public StreamToMediaPlayerTask(Socket client) {
this.client = client;
}
private HttpRequest readRequest() {
HttpRequest request = null;
InputStream is;
String firstLine;
BufferedReader reader = null;
try {
is = client.getInputStream();
reader = new BufferedReader(new InputStreamReader(is), 8192);
firstLine = reader.readLine();
} catch (IOException e) {
Log.e(TAG, "Error parsing request", e);
return request;
}
if (firstLine == null) {
Log.i(TAG, "Proxy client closed connection without a request.");
return request;
}
StringTokenizer st = new StringTokenizer(firstLine);
String method = st.nextToken();
String uri = st.nextToken();
String realUri = uri.substring(1);
Log.i(TAG, realUri);
request = new BasicHttpRequest(method, realUri);
// Get all of the headers
try {
String line;
while((line = reader.readLine()) != null && !"".equals(line)) {
String headerName = line.substring(0, line.indexOf(':'));
String headerValue = line.substring(line.indexOf(": ") + 2);
request.addHeader(headerName, headerValue);
}
} catch(IOException e) {
// Don't really care once past first line
}
return request;
}
public boolean processRequest() {
HttpRequest request = readRequest();
if (request == null) {
return false;
}
// Read HTTP headers
Log.i(TAG, "Processing request");
String localPath;
try {
localPath = URLDecoder.decode(request.getRequestLine().getUri(), Constants.UTF_8);
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Unsupported encoding", e);
return false;
}
Log.i(TAG, "Processing request for file " + localPath);
downloadFile = downloadService.getCurrentPlaying();
File partialFile = new File(localPath);
if (!partialFile.equals(downloadFile.getPartialFile())) {
Log.e(TAG, "File " + localPath + " does not exist");
return false;
}
// Use either partial or complete if downloading finished while StreamProxy was idle
file = downloadFile.isCompleteFileAvailable() ? downloadFile.getCompleteFile() : downloadFile.getPartialFile();
// Try to get range requested
Header rangeHeader = request.getFirstHeader("Range");
if(rangeHeader != null) {
String range = rangeHeader.getValue();
int index = range.indexOf("=");
if(index >= 0) {
range = range.substring(index + 1);
index = range.indexOf("-");
if(index > 0) {
range = range.substring(0, index);
}
cbSkip = Integer.parseInt(range);
// Make sure to not try to read past where the file is downloaded
if(cbSkip >= file.length()) {
return false;
}
}
}
return true;
}
@Override
public void run() {
Log.i(TAG, "Streaming song in background");
MusicDirectory.Entry song = downloadFile.getSong();
Integer contentLength = downloadFile.getContentLength();
if(contentLength == null && downloadFile.isWorkDone()) {
contentLength = (int)file.length();
}
// Create HTTP header
String headers;
if(cbSkip == 0) {
headers = "HTTP/1.0 200 OK\r\n";
} else {
headers = "HTTP/1.0 206 OK\r\n;";
headers += "Content-Range: bytes " + cbSkip + "-" + (file.length() - 1) + "/";
if(contentLength == null) {
headers += "*";
} else {
headers += contentLength;
}
Log.i(TAG, "Streaming starts from: " + cbSkip);
}
headers += "Content-Type: " + "application/octet-stream" + "\r\n";
long fileSize;
if(contentLength == null) {
fileSize = downloadFile.getBitRate() * ((song.getDuration() != null) ? song.getDuration() : 0) * 1000 / 8;
} else {
fileSize = contentLength;
headers += "Content-Length: " + fileSize + "\r\n";
}
Log.i(TAG, "Streaming fileSize: " + fileSize);
headers += "Connection: close\r\n";
headers += "\r\n";
long cbToSend = fileSize - cbSkip;
OutputStream output = null;
byte[] buff = new byte[64 * 1024];
try {
output = new BufferedOutputStream(client.getOutputStream(), 32*1024);
output.write(headers.getBytes());
// Make sure to have file lock
downloadFile.setPlaying(true);
// Loop as long as there's stuff to send
while (isRunning && !client.isClosed()) {
// See if there's more to send
int cbSentThisBatch = 0;
if (file.exists()) {
FileInputStream input = new FileInputStream(file);
input.skip(cbSkip);
int cbToSendThisBatch = input.available();
while (cbToSendThisBatch > 0) {
int cbToRead = Math.min(cbToSendThisBatch, buff.length);
int cbRead = input.read(buff, 0, cbToRead);
if (cbRead == -1) {
break;
}
cbToSendThisBatch -= cbRead;
cbToSend -= cbRead;
output.write(buff, 0, cbRead);
output.flush();
cbSkip += cbRead;
cbSentThisBatch += cbRead;
}
input.close();
}
// Done regardless of whether or not it thinks it is
if(downloadFile.isWorkDone() && cbSkip >= file.length()) {
break;
}
// If we did nothing this batch, block for a second
if (cbSentThisBatch == 0) {
Log.d(TAG, "Blocking until more data appears (" + cbToSend + ")");
Thread.sleep(1000);
}
}
// Release file lock, use of stream proxy means nothing else is using it
downloadFile.setPlaying(false);
}
catch (SocketException socketException) {
Log.e(TAG, "SocketException() thrown, proxy client has probably closed. This can exit harmlessly");
// Release file lock, use of stream proxy means nothing else is using it
downloadFile.setPlaying(false);
}
catch (Exception e) {
Log.e(TAG, "Exception thrown from streaming task:");
Log.e(TAG, e.getClass().getName() + " : " + e.getLocalizedMessage());
}
// Cleanup
try {
if (output != null) {
output.close();
}
client.close();
}
catch (IOException e) {
Log.e(TAG, "IOException while cleaning up streaming task:");
Log.e(TAG, e.getClass().getName() + " : " + e.getLocalizedMessage());
}
}
}
}
|