Android mediaplayer and HTTP Proxy Optimization

Source: Internet
Author: User
Tags connection reset

This article from http://blog.csdn.net/hellogv/, reference must indicate the source!

This article fixes the HTTP Proxy passthrough bug on the basis of video pre-loading (optimization) for playing Android mediaplayer. The proxy server used in the previous articles can only listen to one request from mediaplayer at a time, but in the actual project development process, it is found that some old requests are not interrupted before the m3u8 mediaplayer sends a new request. Therefore, the code in this article adds multiple threads to listen to the request.

The code for this article can be downloaded here: http://download.csdn.net/detail/hellogv/4894109

After the proxy server is initialized, two threads start to listen to the mediaplayer request:

Public httpgetproxy (string dirpath, int size, int maximum) {try {// initialize the proxy server mbufferdirpath = dirpath; mbuffersize = size; mbufferfilemaximum = maximum; localhost = config. local_ip_address; localserver = new serversocket (0, 1, inetaddress. getbyname (localhost); localport = localserver. getlocalport (); // There Is A serversocket automatically allocated port // start the proxy server new thread () {public void run () {startproxy ();}}. start (); menable = true;} catch (exception e) {menable = false ;}}


Private void startproxy () {While (true) {// -------------------------------------- // listen for mediaplayer requests, mediaplayer-> Proxy Server // ---------------------------------- log. I (TAG ,"...... ready to start ........... "); try {socket S = localserver. accept (); If (proxy! = NULL) {proxy. closesockets ();} log. I (TAG ,"...... started ........... "); proxy = new proxy (s); New thread () {public void run () {log. I (TAG ,"...... ready to start ........... "); try {socket S = localserver. accept (); proxy. closesockets (); log. I (TAG ,"...... started ........... "); proxy = new proxy (s); proxy. run ();} catch (ioexception e) {log. E (TAG, E. tostring (); log. E (TAG, utils. getexceptionmessage (E ));}}}. start (); proxy. run ();} catch (ioexception e) {log. E (TAG, E. tostring (); log. E (TAG, utils. getexceptionmessage (E ));}}}

After the proxy server receives the request (seek operation), it uses a new thread to establish a socket, and the old socket will enter Exception Handling due to the write exception of outputstream, so we need to recycle socket resources in exception handling.. The log generated after the proxy server mediaplayer seek is as follows:

13:55:53 am from 12 to 16. 181: I/httpparser <--- (27624): HTTP/1.1 206 partial Content12-16 13:55:53. 181: I/httpparser <--- (27624): Content-Length: 66513973312-16 13:55:53. 181: I/httpparser <--- (27624): Content-Type: Application/force-download12-16 13:55:53. 181: I/httpparser <--- (27624): Content-range: bytes 2906976-668046708/66804670912-16 13:55:53. 181: I/httpparser <--- (27624): Last-modified: Thu, 13 Dec 2012 14:28:00 GMT12-16 13:55:53. 181: I/httpparser <--- (27624): Accept-ranges: bytes12-16 13:55:53. 181: I/httpparser <--- (27624): etag: "80b39bd3ed9cd1: 2d6" 12-16 13:55:53. 181: I/httpparser <--- (27624): SERVER: Microsoft-IIS/6.012-16 13:55:53. 181: I/httpparser <--- (27624): Content-Disposition: attachment12-16 13:55:53. 181: I/httpparser <--- (27624): Date: Sun, 16 dec 2012 05:55:45 GMT12-16 13:55:53. 181: I/httpparser <--- (27624): Connection: close12-16 13:55:53. 181: I/httpparser <--- (27624): 12-16 13:55:59. 631: I/httpgetproxy (27624 ):...... started ........... 13:55:59 am from 12 to 16. 631: I/httpgetproxy (27624): <-----------------------------------> 12-16 13:55:59. 641: I/httpparser (27624 ): GET/% E6 % 89% 8B % E6 % 9C % Ba % E7 % 94% B5 % E5 % BD % B1/201212/Hangzhou HTTP/1.112-16 13:55:59. 641: I/httpparser (27624): User-Agent: applecoremedia/1.0.0.8c148 (IPAD; U; cpu OS 4_2_1 like Mac OS X; zh_cn) 12-16 13:55:59. 641: I/httpparser (27624): Accept: */* 12-16 13:55:59. 641: I/httpparser (27624): range: bytes = 401793617-12-16 13:55:59. 641: I/httpparser (27624): Connection: close12-16 13:55:59. 641: I/httpparser (27624): Host: d1.2mp4. net12-16 13:55:59. 641: I/httpparser (27624): 12-16 13:55:59. 641: I/httpparser (27624): -------> rangeposition: 40179361712-16 13:55:59. 641: E/httpgetproxy (27624): java.net. socketexception: socket is closed12-16 13:55:59. 641: E/httpgetproxy (27624): java.net. socket. checkopenandcreate 641line12-16 13:55:59. 641: E/httpgetproxy (27624): java.net. socket. getoutputstream 381line12-16 13:55:59. 641: E/httpgetproxy (27624): COM. proxy. httpgetproxyutils. sendtomp upload line12-16 13:55:59. 641: E/httpgetproxy (27624): COM. proxy. httpgetproxy $ proxy. run 292line12-16 13:55:59. 641: E/httpgetproxy (27624): COM. proxy. httpgetproxy. startproxy maid 13:55:59. 641: E/httpgetproxy (27624): COM. proxy. httpgetproxy. access $8 183line12-16 13:55:59. 641: E/httpgetproxy (27624): COM. proxy. httpgetproxy $ 1.run 78line12-16 13:55:59. 641: I/httpgetproxy (27624 ):...... ready to start ........... 13:56:01 am from 12 to 16. 181: I/httpparser <--- (27624): HTTP/1.1 206 partial Content12-16 13:56:01. 181: I/httpparser <--- (27624): Content-Length: 26625309212-16 13:56:01. 181: I/httpparser <--- (27624): Content-Type: Application/force-download12-16 13:56:01. 181: I/httpparser <--- (27624): Content-range: bytes 401793617-668046708/66804670912-16 13:56:01. 181: I/httpparser <--- (27624): Last-modified: Thu, 13 Dec 2012 14:28:00 GMT12-16 13:56:01. 181: I/httpparser <--- (27624): Accept-ranges: bytes12-16 13:56:01. 181: I/httpparser <--- (27624): etag: "80b39bd3ed9cd1: 2d6" 12-16 13:56:01. 181: I/httpparser <--- (27624): SERVER: Microsoft-IIS/6.012-16 13:56:01. 181: I/httpparser <--- (27624): Content-Disposition: attachment12-16 13:56:01. 181: I/httpparser <--- (27624): Date: Sun, 16 dec 2012 05:55:54 GMT12-16 13:56:01. 181: I/httpparser <--- (27624): Connection: close12-16 13:56:01. 181: I/httpparser <--- (27624): 12-16 13:56:01. 181: I/httpgetproxy (27624): -----------------> need to send pre-loaded to MediaPlayer12-16 13:56:01. 181: I/httpgetproxy (27624): >>> do not read the pre-loaded file range: 401793617, buffer: 290697612-16 13:56:13. 761: E/httpgetproxy (27624): java.net. socketexception: sendto failed: econnreset (Connection reset by peer) 12-16 13:56:13. 761: E/httpgetproxy (27624): libcore. io. iobridge. maybethrowaftersendto maid 13:56:13. 761: E/httpgetproxy (27624): libcore. io. iobridge. sendto queue line12-16 13:56:13. 761: E/httpgetproxy (27624): java.net. plainsocketimpl. write 507line12-16 13:56:13. 761: E/httpgetproxy (27624): java.net. plainsocketimpl. access $100 46line12-16 13:56:13. 761: E/httpgetproxy (27624): java.net. plainsocketimpl $ plainsocketoutputstream. write 269line12-16 13:56:13. 761: E/httpgetproxy (27624): COM. proxy. httpgetproxyutils. sendtomp upload line12-16 13:56:13. 761: E/httpgetproxy (27624): COM. proxy. httpgetproxy $ proxy. run 292line12-16 13:56:13. 761: E/httpgetproxy (27624): COM. proxy. httpgetproxy $ 2.run 205line12-16 13:56:13. 771: I/httpgetproxy (27624 ):...... started ........... 13:56:13 am from 12 to 16. 771: I/httpgetproxy (27624): <-----------------------------------> 12-16 13:56:13. 771: I/httpparser (27624 ): GET/% E6 % 89% 8B % E6 % 9C % Ba % E7 % 94% B5 % E5 % BD % B1/201212/Hangzhou HTTP/1.112-16 13:56:13. 771: I/httpparser (27624): User-Agent: applecoremedia/1.0.0.8c148 (IPAD; U; cpu OS 4_2_1 like Mac OS X; zh_cn) 12-16 13:56:13. 771: I/httpparser (27624): Accept: */* 12-16 13:56:13. 771: I/httpparser (27624): range: bytes = 612718942-12-16 13:56:13. 771: I/httpparser (27624): Connection: close12-16 13:56:13. 771: I/httpparser (27624): Host: d1.2mp4. net12-16 13:56:13. 771: I/httpparser (27624): 12-16 13:56:13. 771: I/httpparser (27624): -------> rangeposition: 61271894212-16 13:56:13. 781: I/httpgetproxy (27624 ):...... ready to start ........... 13:56:14 am from 12 to 16. 051: I/httpparser <--- (27624): HTTP/1.1 206 partial Content12-16 13:56:14. 051: I/httpparser <--- (27624): Content-Length: 5532776712-16 13:56:14. 051: I/httpparser <--- (27624): Content-Type: Application/force-download12-16 13:56:14. 051: I/httpparser <--- (27624): Content-range: bytes 612718942-668046708/66804670912-16 13:56:14. 051: I/httpparser <--- (27624): Last-modified: Thu, 13 Dec 2012 14:28:00 GMT12-16 13:56:14. 051: I/httpparser <--- (27624): Accept-ranges: bytes12-16 13:56:14. 051: I/httpparser <--- (27624): etag: "80b39bd3ed9cd1: 2d6" 12-16 13:56:14. 051: I/httpparser <--- (27624): SERVER: Microsoft-IIS/6.012-16 13:56:14. 051: I/httpparser <--- (27624): Content-Disposition: attachment12-16 13:56:14. 051: I/httpparser <--- (27624): Date: Sun, 16 dec 2012 05:56:06 GMT12-16 13:56:14. 051: I/httpparser <--- (27624): Connection: close12-16 13:56:14. 051: I/httpparser <--- (27624): 12-16 13:56:14. 051: I/httpgetproxy (27624): -----------------> need to send pre-loaded to MediaPlayer12-16 13:56:14. 051: I/httpgetproxy (27624): >>> do not read the pre-loaded file range: 612718942, buffer: 290697612-16 13:56:49. 051: I/httpgetproxy (27624 ):...... started ........... 13:56:49 am from 12 to 16. 051: I/httpgetproxy (27624): <-----------------------------------> 12-16 13:56:49. 051: E/httpgetproxy (27624): java.net. socketexception: Socket closed12-16 13:56:49. 051: E/httpgetproxy (27624): libcore. io. POSIX. recvfrombytes-2line12-16 13:56:49. 051: E/httpgetproxy (27624): libcore. io. POSIX. recvfrom repeated line12-16 13:56:49. 051: E/httpgetproxy (27624): libcore. io. blockguardos. recvfrom 164line12-16 13:56:49. 051: E/httpgetproxy (27624): libcore. io. iobridge. recvfrom 503line12-16 13:56:49. 051: E/httpgetproxy (27624): java.net. plainsocketimpl. read 488line12-16 13:56:49. 051: E/httpgetproxy (27624): java.net. plainsocketimpl. access $000 46line12-16 13:56:49. 051: E/httpgetproxy (27624): java.net. plainsocketimpl $ plainsocketinputstream. read 240line12-16 13:56:49. 051: E/httpgetproxy (27624): Java. io. inputstream. read 163line12-16 13:56:49. 051: E/httpgetproxy (27624): COM. proxy. httpgetproxy $ proxy. run 288line12-16 13:56:49. 051: E/httpgetproxy (27624): COM. proxy. httpgetproxy. startproxy maid 13:56:49. 051: E/httpgetproxy (27624): COM. proxy. httpgetproxy. access $8 183line12-16 13:56:49. 051: E/httpgetproxy (27624): COM. proxy. httpgetproxy $ 1.run 78line12-16 13:56:49. 051: I/httpgetproxy (27624 ):...... ready to start ........... 13:56:49 am from 12 to 16. 051: I/HTTP Parser (27624 ): GET/% E6 % 89% 8B % E6 % 9C % Ba % E7 % 94% B5 % E5 % BD % B1/201212/Hangzhou HTTP/1.112-16 13:56:49. 051: I/httpparser (27624): User-Agent: applecoremedia/1.0.0.8c148 (IPAD; U; cpu OS 4_2_1 like Mac OS X; zh_cn) 12-16 13:56:49. 051: I/httpparser (27624): Accept: */* 12-16 13:56:49. 051: I/httpparser (27624): range: bytes = 152226030-12-16 13:56:49. 051: I/httpparser (27624): Connection: close12-16 13:56:49. 051: I/httpparser (27624): Host: d1.2mp4. net12-16 13:56:49. 051: I/httpparser (27624): 12-16 13:56:49. 051: I/httpparser (27624): -------> rangeposition: 15222603012-16 13:56:49. 311: I/httpparser <--- (27624): HTTP/1.1 206 partial Content12-16 13:56:49. 311: I/httpparser <--- (27624): Content-Length: 51582067912-16 13:56:49. 311: I/httpparser <--- (27624): Content-Type: Application/force-download12-16 13:56:49. 311: I/httpparser <--- (27624): Content-range: bytes 152226030-668046708/66804670912-16 13:56:49. 311: I/httpparser <--- (27624): Last-modified: Thu, 13 Dec 2012 14:28:00 GMT12-16 13:56:49. 311: I/httpparser <--- (27624): Accept-ranges: bytes12-16 13:56:49. 311: I/httpparser <--- (27624): etag: "80b39bd3ed9cd1: 2d6" 12-16 13:56:49. 311: I/httpparser <--- (27624): SERVER: Microsoft-IIS/6.012-16 13:56:49. 311: I/httpparser <--- (27624): Content-Disposition: attachment12-16 13:56:49. 311: I/httpparser <--- (27624): Date: Sun, 16 dec 2012 05:56:42 GMT12-16 13:56:49. 311: I/httpparser <--- (27624): Connection: close12-16 13:56:49. 311: I/httpparser <--- (27624): 12-16 13:56:49. 311: I/httpgetproxy (27624): -----------------> need to send pre-loaded to MediaPlayer12-16 13:56:49. 311: I/httpgetproxy (27624): >>> do not read the pre-loaded file range: 152226030, buffer: 2906976

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.