99import java .net .SocketAddress ;
1010import java .util .ArrayList ;
1111
12- import com .immortalplayer .HttpGetProxyUtils .ProxyRequest ;
13- import com .immortalplayer .HttpGetProxyUtils .ProxyResponse ;
14-
12+ import com .immortalplayer .HttpParser .ProxyRequest ;
13+ import com .immortalplayer .HttpParser .ProxyResponse ;
1514
15+ import android .content .Context ;
16+ import android .content .Intent ;
1617import android .net .Uri ;
18+ import android .os .Environment ;
1719import android .util .Log ;
1820
1921public class HttpGetProxy {
@@ -25,17 +27,18 @@ public class HttpGetProxy{
2527 private long urlsize =0 ;
2628 private String remoteHost ;
2729 private int localPort ;
28- public ServerSocket localServer = null ;
30+ private ServerSocket localServer = null ;
2931 private SocketAddress serverAddress ;
30- private ProxyResponse proxyResponse =null ;
3132 private String mUrl ;
32- private String mMediaFilePath , newPath , newPath1 ;
33+ private String mMediaFilePath , newPath , newPath1 , file2 , cachefolder ;
3334 private File file , file1 ;
3435 private Proxy proxy =null ;
35- ArrayList <range > ranges = new ArrayList <range >();
36+ private ArrayList <range > ranges = new ArrayList <range >();
3637 private boolean startProxy , error =false ;
3738 private Thread prox ;
38-
39+ public boolean seek =false ;
40+ private Context ctx ;
41+
3942 /**
4043 * Initialize the proxy server, and start the proxy server
4144 */
@@ -62,10 +65,15 @@ public void setend(long en)
6265 * Get playing link
6366 */
6467 public String getLocalURL () {
68+ File file =new File (Environment .getExternalStorageDirectory ()
69+ .getAbsolutePath () + cachefolder +"/" +file2 );
70+ if (file .exists ()==true ){return file .getAbsolutePath ();} else
71+ {
6572 Uri originalURI = Uri .parse (mUrl );
6673 remoteHost = originalURI .getHost ();
6774 String localUrl = mUrl .replace (remoteHost , LOCAL_IP_ADDRESS + ":" + localPort );
6875 return localUrl ;
76+ }
6977 }
7078
7179 public void stopProxy ()
@@ -80,36 +88,40 @@ public void stopProxy ()
8088 }
8189 }
8290
83- public void setPaths (String dirPath , String file5 , String url , int MaxSize ,int maxnum )
91+ public void setPaths (String dirPath , String url , int MaxSize ,int maxnum , Context ctxx )
8492 {
93+ cachefolder =dirPath ;
94+ ctx =ctxx ;
95+ dirPath =Environment .getExternalStorageDirectory ().getAbsolutePath ()+dirPath ;
8596 new File (dirPath ).mkdirs ();
8697 long maxsize1 =MaxSize *1024 *1024 ;
8798 Utils .asynRemoveBufferFile (dirPath , maxnum , maxsize1 );
8899 mUrl =url ;
89- mMediaFilePath = dirPath + "/" + file5 ;
100+ file2 =Uri .decode (mUrl .substring (mUrl .lastIndexOf ("/" )+1 ));
101+ mMediaFilePath = dirPath + "/" + file2 ;
90102 file = new File (mMediaFilePath );
91- file1 = new File (dirPath + "/- " + file5 );
103+ file1 = new File (dirPath + "/" + file2 + "-" );
92104 error =false ;
93105 }
94106
95107 public void startProxy ()
96108 {
97109 startProxy =true ;
98- Log . i ( TAG , "1 start proxy" );
110+
99111 prox =new Thread () {
100112 public void run () {
101113 while (startProxy ) {
102114 // --------------------------------------
103115 // MediaPlayer's request listening, MediaPlayer-> proxy server
104116 // --------------------------------------
117+ Log .i ("111" , "1 start proxy" );
105118 try {
106119 if (proxy !=null ){
107120 proxy .closeSockets ();
108121 }
109122 Socket s = localServer .accept ();
110123 if (startProxy ==false ) break ;
111124 proxy = new Proxy (s );
112- proxy .run ();
113125 } catch (IOException e ) {
114126 Log .e (TAG , e .toString ());
115127 Log .e (TAG , Utils .getExceptionMessage (e ));
@@ -119,15 +131,25 @@ public void run() {
119131 };
120132 prox .start ();
121133 }
122-
123- private class Proxy {
134+
135+ public void scan (Uri url )
136+ {
137+ Intent scanFileIntent = new Intent (Intent .ACTION_MEDIA_SCANNER_SCAN_FILE , url );//scan for other players
138+ ctx .sendBroadcast (scanFileIntent );
139+
140+ }
141+
142+ private class Proxy
143+ {
124144 /** Socket receive requests Media Player */
125145 private Socket sckPlayer = null ;
126146 /** Socket transceiver Media Server requests */
127147 private Socket sckServer = null ;
148+
128149
129150 public Proxy (Socket sckPlayer ){
130151 this .sckPlayer =sckPlayer ;
152+ run ();
131153 }
132154
133155 /**
@@ -145,41 +167,42 @@ public void closeSockets(){
145167 sckServer =null ;
146168 }
147169 } catch (IOException e1 ) {}
148- }
149-
150-
170+ }
151171
152172 public void run () {
153173 HttpParser httpParser = null ;
154174 HttpGetProxyUtils utils = null ;
155175 int bytes_read ;
156- byte [] file_buffer = new byte [1024 ];
176+ byte [] file_buffer = new byte [1448 ];
157177 File file2 , file3 ;
158178 byte [] local_request = new byte [1024 ];
159- byte [] remote_reply = new byte [1024 * 50 ];
179+ byte [] remote_reply = new byte [1448 * 50 ];
160180 ProxyRequest request = null ;
181+ ProxyResponse proxyResponse =null ;
161182 boolean sentResponseHeader = false ;
162183 boolean isExists =false ;
163184 String header ="" ;
164185 RandomAccessFile os = null , fInputStream = null ;
186+ if (mMediaFilePath !=newPath ) {
187+ error =false ; urlsize =0 ; ranges .clear (); ranges .trimToSize (); newPath =mMediaFilePath ; newPath1 =file1 .getAbsolutePath ();
188+ }
165189 try {
166190 httpParser = new HttpParser (remoteHost , remotePort , LOCAL_IP_ADDRESS , localPort );
167- while ((bytes_read = sckPlayer .getInputStream ().read (
168- local_request )) != -1 ) {
191+ while ((( bytes_read = sckPlayer .getInputStream ().read (
192+ local_request )) != -1 ) && ( mMediaFilePath == newPath )) {
169193 byte [] buffer = httpParser .getRequestBody (local_request ,
170194 bytes_read );
171195 if (buffer != null ) {
172- request = httpParser .getProxyRequest (buffer );
196+ request = httpParser .getProxyRequest (buffer , urlsize );
173197 break ;
174198 }
175199 }
176200 serverAddress = new InetSocketAddress (remoteHost , HTTP_PORT );
177201 utils = new HttpGetProxyUtils (sckPlayer , serverAddress );
178202 isExists = file .exists ();
179- if (mMediaFilePath !=newPath ) {error =false ; urlsize =0 ; ranges .clear (); ranges .trimToSize (); newPath =mMediaFilePath ; newPath1 =file1 .getAbsolutePath ();}
180203
181204 //Read from file
182- if ((isExists ) || ((file1 .exists ()) && (error ==true )))
205+ if ((( isExists ) || ((file1 .exists ()) && (error ==true ))) && ( mMediaFilePath == newPath ))
183206 {//Send pre-loaded file to MediaPlayer
184207 Log .i (TAG , "Send cache to the MediaPlayer" );
185208 try {
@@ -194,10 +217,10 @@ public void run() {
194217 if (request ._rangePosition > 0 ) {
195218 fInputStream .seek (request ._rangePosition );
196219 }
197-
220+ error = false ;
198221 sckPlayer .getOutputStream ().write (header .getBytes (), 0 , header .length ());
199222 while (((bytes_read = fInputStream .read (file_buffer )) != -1 ) &&
200- (sckPlayer .isClosed ()==false )) {
223+ (sckPlayer .isClosed ()==false ) && ( mMediaFilePath == newPath ) ) {
201224 sckPlayer .getOutputStream ().write (file_buffer , 0 , bytes_read );
202225 }
203226 sckPlayer .getOutputStream ().flush ();
@@ -216,13 +239,13 @@ public void run() {
216239
217240 //Read from Internet
218241 try {
219- if ((request != null ) && (isExists ==false ))
242+ if ((request != null ) && (isExists ==false ) && ( mMediaFilePath == newPath ) )
220243 {
221244 try {
222245 sckServer = utils .sentToServer (request ._body );// Send MediaPlayer's request to server
223246 } catch (Exception e ) {
224247 error =true ;
225- if (( file1 .exists ()) && ( error == true ))
248+ if (file1 .exists ())
226249 {//Send pre-loaded file to MediaPlayer
227250 Log .i (TAG , "Send cache to the MediaPlayer" );
228251 try {
@@ -233,7 +256,7 @@ public void run() {
233256 header = "HTTP/1.1 206 Partial Content\r \n Accept-Ranges: bytes\r \n Content-Length: " +Integer .toString ((int )(file1 .length ()-request ._rangePosition ))+"\r \n Content-Range: bytes " +Integer .toString ((int )request ._rangePosition )+"-" +Integer .toString ((int )(file1 .length ()-1 ))+"/" +file1 .length ()+"\r \n Content-Type: application/octet-stream\r \n \r \n " ;
234257 sckPlayer .getOutputStream ().write (header .getBytes (), 0 , header .length ());
235258 while (((bytes_read = fInputStream .read (file_buffer )) != -1 ) &&
236- (sckPlayer .isClosed ()==false )) {
259+ (sckPlayer .isClosed ()==false ) && ( mMediaFilePath == newPath ) ) {
237260 sckPlayer .getOutputStream ().write (file_buffer , 0 , bytes_read );
238261 }
239262 sckPlayer .getOutputStream ().flush ();
@@ -246,8 +269,10 @@ public void run() {
246269 }
247270 return ;
248271 }
272+ sckServer .setSoTimeout (1000 ); // without this flac not work.
273+ sckServer .setSoLinger (true , 500 ); //for correct to close sockets
274+ sckPlayer .setSoLinger (true , 500 );
249275 error =false ;
250-
251276 os = new RandomAccessFile (file1 , "rwd" );
252277 } else {// MediaPlayer's request is invalid
253278 closeSockets ();
@@ -257,18 +282,16 @@ public void run() {
257282 // ------------------------------------------------------
258283 // The feedback network server sent to the MediaPlayer, network server -> Proxy -> MediaPlayer
259284 // ------------------------------------------------------
260-
261- //sckServer.setSoTimeout(200); // this parameter is experimental
262- //sckPlayer.setSoTimeout(200);
285+ seek =false ;
263286 while ((sckServer != null ) &&
264287 ((bytes_read = sckServer .getInputStream ().read (remote_reply )) != -1 ) &&
265- (sckPlayer . isClosed () ==false ))
288+ (seek ==false ) && ( mMediaFilePath == newPath ))
266289 {
267290 if (sentResponseHeader ) {
268291 try {// When you drag the progress bar, easy this exception, to disconnect and reconnect
269292 os .write (remote_reply , 0 , bytes_read );
270293 proxyResponse ._currentPosition += bytes_read ;
271- utils .sendToMP (remote_reply , bytes_read );
294+ utils .sendToMP (remote_reply , bytes_read );
272295 } catch (Exception e ) {
273296 Log .e (TAG , e .toString ());
274297 Log .e (TAG , Utils .getExceptionMessage (e ));
@@ -282,10 +305,9 @@ public void run() {
282305 sentResponseHeader = true ;
283306 // send http header to mediaplayer
284307 utils .sendToMP (proxyResponse ._body );
285-
286-
308+
287309 // Send the binary data
288- if (proxyResponse ._other != null ) {
310+ if (proxyResponse ._other != null ) {
289311 utils .sendToMP (proxyResponse ._other );
290312 os .seek (proxyResponse ._currentPosition );
291313 os .write (proxyResponse ._other , 0 , proxyResponse ._other .length );
@@ -303,7 +325,7 @@ public void run() {
303325 try {
304326 if (os != null )
305327 {
306- os .close ();
328+ os .close ();
307329 file2 =new File (newPath );
308330 file3 =new File (newPath1 );
309331 if (file2 .exists ()==false ) {
@@ -325,6 +347,7 @@ public void run() {
325347 }
326348 if (urlsize ==(h -1 )) {
327349 file3 .renameTo (file2 );
350+ scan (Uri .fromFile (file2 ));
328351 }
329352 }
330353 }
0 commit comments