Play Android mediaplayer Media Proxy

Source: Internet
Author: User
Tags ranges

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

This article aims to further optimize the proxy server based on Android mediaplayer and HTTP proxy, and Support HTTP 302 and 301 redirection to obtain the text content of HTTP request and HTTP response. This article uses video playback and HTTP proxy to describe the handshaking process during mediaplayer playback.

Sputation: Google search for "Proxy Server" is invalid. Therefore, we use Media Proxy in this article ......

Let's take a look at the screenshot animation running in this article:

Let's take a look at the log information output when the program is running... here is the key:

07-29 15:51:30. 692: E/httpgetproxy (449): ...... sckplayer connected ...... Send a request
07-29 15:51:30. 692: E/to media server ----> (449): Get/vids/family_guy_penis_car.3gp HTTP/1.1

07-29 15:51:30. 692: E/to media server ----> (449): User-Agent: stagefright/1.0 (Linux; Android 2.2)

07-29 15:51:30. 692: E/to media server ----> (449): Host: daily3gp.com

07-29 15:51:30. 692: E/to media server ----> (449 ):

07-29 15:51:31. 119: E/httpgetproxy (449): ...... remote server connected ..........
07-29 15:51:31. 122: E/httpgetproxy (449): ...... remote start to receive ..........
07-29 15:51:31. 775: E/from media server ----> (449): HTTP/1.1 200 OK

07-29 15:51:31. 775: E/from media server ----> (449): Date: Sun, 29 Jul 2012 15:51:33 GMT

07-29 15:51:31. 775: E/from media server ----> (449): SERVER: Apache

07-29 15:51:31. 775: E/from media server ----> (449): Last-modified: Thu, 14 Jan 2010 23:29:02 GMT

07-29 15:51:31. 775: E/from media server ----> (449): Accept-ranges: bytes

07-29 15:51:31. 775: E/from media server ----> (449): Content-Length: 754777

07-29 15:51:31. 775: E/from media server ----> (449): Content-Type: Video/3GPP


07-29 15:51:34. 512: E/httpgetproxy (449): ...... sckplayer connected ...... Send a request
07-29 15:51:34. 532: E/to media server ----> (449): Get/vids/family_guy_penis_car.3gp HTTP/1.1

07-29 15:51:34. 532: E/to media server ----> (449): User-Agent: stagefright/1.0 (Linux; Android 2.2)

07-29 15:51:34. 532: E/to media server ----> (449): Host: daily3gp.com

07-29 15:51:34. 532: E/to media server ----> (449): range: bytes = 720896-

07-29 15:51:34. 532: E/to media server ----> (449 ):

07-29 15:51:34. 873: E/httpgetproxy (449): ...... remote server connected ..........
07-29 15:51:34. 873: E/httpgetproxy (449): ...... remote start to receive ..........
07-29 15:51:35. 505: E/from media server ----> (449): HTTP/1.1 206 partial content

07-29 15:51:35. 505: E/from media server ----> (449): Date: Sun, 29 Jul 2012 15:51:36 GMT

07-29 15:51:35. 505: E/from media server ----> (449): SERVER: Apache

07-29 15:51:35. 505: E/from media server ----> (449): Last-modified: Thu, 14 Jan 2010 23:29:02 GMT

07-29 15:51:35. 505: E/from media server ----> (449): Accept-ranges: bytes

07-29 15:51:35. 505: E/from media server ----> (449): Content-Length: 33881

07-29 15:51:35. 505: E/from media server ----> (449): Content-range: bytes 720896-754776/754777

07-29 15:51:35. 505: E/from media server ----> (449): Content-Type: Video/3GPP
07-29 15:51:38. 754: E/httpgetproxy (449): ...... over ..........


07-29 15:51:51. 461: E/httpgetproxy (449): ...... sckplayer connected ...... Send a request
07-29 15:51:51. 471: E/to media server ----> (449): Get/vids/family_guy_penis_car.3gp HTTP/1.1

07-29 15:51:51. 471: E/to media server ----> (449): User-Agent: stagefright/1.0 (Linux; Android 2.2)

07-29 15:51:51. 471: E/to media server ----> (449): Host: daily3gp.com

07-29 15:51:51. 471: E/to media server ----> (449): range: bytes = 196608-

07-29 15:51:51. 471: E/to media server ----> (449 ):

07-29 15:51:51. 722: E/httpgetproxy (449): ...... remote server connected ..........
07-29 15:51:51. 722: E/httpgetproxy (449): ...... remote start to receive ..........
07-29 15:51:52. 285: E/from media server ----> (449): HTTP/1.1 206 partial content

07-29 15:51:52. 285: E/from media server ----> (449): Date: Sun, 29 Jul 2012 15:51:53 GMT

07-29 15:51:52. 285: E/from media server ----> (449): SERVER: Apache

07-29 15:51:52. 285: E/from media server ----> (449): Last-modified: Thu, 14 Jan 2010 23:29:02 GMT

07-29 15:51:52. 285: E/from media server ----> (449): Accept-ranges: bytes

07-29 15:51:52. 285: E/from media server ----> (449): Content-Length: 558169

07-29 15:51:52. 285: E/from media server ----> (449): Content-range: bytes 196608-754776/754777

07-29 15:51:52. 285: E/from media server ----> (449): Content-Type: Video/3GPP


07-29 15:51:54. 812: E/httpgetproxy (449): ...... sckplayer connected ...... Send a request
07-29 15:51:54. 822: E/to media server ----> (449): Get/vids/family_guy_penis_car.3gp HTTP/1.1

07-29 15:51:54. 822: E/to media server ----> (449): User-Agent: stagefright/1.0 (Linux; Android 2.2)

07-29 15:51:54. 822: E/to media server ----> (449): Host: daily3gp.com

07-29 15:51:54. 822: E/to media server ----> (449): range: bytes = 589824-

07-29 15:51:54. 822: E/to media server ----> (449 ):

07-29 15:51:55. 117: E/httpgetproxy (449): ...... remote server connected ..........
07-29 15:51:55. 117: E/httpgetproxy (449): ...... remote start to receive ..........
07-29 15:51:55. 693: E/from media server ----> (449): HTTP/1.1 206 partial content

07-29 15:51:55. 693: E/from media server ----> (449): Date: Sun, 29 Jul 2012 15:51:57 GMT

07-29 15:51:55. 693: E/from media server ----> (449): SERVER: Apache

07-29 15:51:55. 693: E/from media server ----> (449): Last-modified: Thu, 14 Jan 2010 23:29:02 GMT

07-29 15:51:55. 693: E/from media server ----> (449): Accept-ranges: bytes

07-29 15:51:55. 693: E/from media server ----> (449): Content-Length: 164953

07-29 15:51:55. 693: E/from media server ----> (449): Content-range: bytes 589824-754776/754777

07-29 15:51:55. 693: E/from media server ----> (449): Content-Type: Video/3GPP
07-29 15:51:59. 620: E/httpgetproxy (449): ...... over ..........

 

From the screenshot animation and log information, we can see that the manual seek is performed once, but the mediaplayer performs multiple range requests. This indicates that "mediaplayer automatically seek" may be related to the local cache of mediaplayer. In addition, mediaplayer implemented by different hardware solution manufacturers may have different operations.

 

The code in this article can be downloaded here:

Http://download.csdn.net/detail/hellogv/4463651

Httpgetproxy. Java is the key part of this article:

Public class httpgetproxy {final static private string tag = "httpgetproxy"; Final Static private string local_ip_address_1 = "127.0.0.1"; Final Static private string local_ip_address_2 = "10.0.2.2 "; final Static private int http_port = 80; Final Static private string http_end = "\ r \ n";/** port used by the proxy server */private int proxy_ip_port; /** port with Link */private string original_ip_port;/** remote server address */private s Tring remotehost;/** local server address */private string localhost; private serversocket localserver = NULL;/** socket */private socket sckplayer = NULL for sending and receiving Media Player requests; /** socket */private socket sckserver = NULL for sending and receiving Media Server requests; private socketaddress address; /*** initialize the proxy server * @ Param localport the port listened by the proxy server */Public httpgetproxy (INT localport) {try {_ httpgetproxy (local_ip_address_1, localport);} catch (exce Ption e) {log. E (TAG, local_ip_address_1 + "??? "+ E. tostring (); try {_ httpgetproxy (local_ip_address_2, localport);} catch (exception E1) {log. E (TAG, local_ip_address_2 + "??? "+ E. tostring (); system. exit (0) ;}} private void _ httpgetproxy (string IPaddress, int localport) throws unknownhostexception, ioexception {proxy_ip_port = localport; localserver = new serversocket (localport, 1, inetaddress. getbyname (IPaddress); localhost = IPaddress;}/*** convert the network URL to a local URL, 127.0.0.1 replace network domain name * @ Param URL network URL * @ return local URL */Public String getlocalurl (string urlstring) {// ---- exclude HTTP special ---- // String TargetUrl = proxyutils. getredirecturl (urlstring); // ---- obtain the link of the local proxy server ---- // string result = NULL; Uri originaluri = Uri. create (TargetUrl); remotehost = originaluri. gethost (); If (originaluri. getport ()! =-1) {// URL with port address = new inetsocketaddress (remotehost, originaluri. getport (); // use the default port original_ip_port = originaluri. getport () + ""; // Save the port. Replace result = TargetUrl in transit. replace (remotehost + ":" + originaluri. getport (), localhost + ":" + proxy_ip_port);} else {// URL without port address = new inetsocketaddress (remotehost, http_port ); // use port 80 original_ip_port = ""; Result = TargetUrl. replace (remotehost, localhost + ":" + proxy _ Ip_port);} return result;}/*** start Proxy Server * @ throws ioexception */Public void asynstartproxy () {New thread () {public void run () {int bytes_read; byte [] local_request = new byte [1024]; byte [] remote_reply = new byte [1024]; while (true) {try {// -------------------------------------- // listen for mediaplayer requests, mediaplayer-> Proxy Server // ------------------------------------------ sckplayer = localserver. accept (); Log. E (TAG ,".......... sckplayer connected .......... "); string requeststr =" "; while (bytes_read = sckplayer. getinputstream (). read (local_request ))! =-1) {byte [] tmpbuffer = new byte [bytes_read]; system. arraycopy (local_request, 0, tmpbuffer, 0, bytes_read); string STR = new string (tmpbuffer); // log. E ("from mediaplayer ---->", STR); requeststr = requeststr + STR; If (requeststr. contains ("get") & requeststr. contains (http_end) {break ;}// change the local IP address in the request to the remote IP address requeststr = requeststr. replace (localhost, remotehost); // change the proxy server port to the original URL port if (textutils. is Empty (original_ip_port) requeststr = requeststr. replace (":" + proxy_ip_port, ""); else requeststr = requeststr. replace (":" + proxy_ip_port, ":" + original_ip_port); log. E ("to media server ---->", requeststr); // ---------------------------------------- // send the mediaplayer request to the network server, proxy Server-> network server // ---------------------------------------- sckserver = new socket (); sckserver. connect (Address); log. E (TAG ,".......... re Mote server connected .......... "); sckserver. getoutputstream (). write (requeststr. getbytes (); // send the mediaplayer request // -------------------------------------------------------- // send the network server feedback to the mediaplayer, network server> Proxy Server> mediaplayer/upload log. E (TAG ,".......... remote start to receive .......... "); string responsestr =" "; Boolean iscaptured = false; while (bytes_r EAD = sckserver. getinputstream (). Read (remote_reply ))! =-1) {byte [] tmpbuffer = new byte [bytes_read]; system. arraycopy (remote_reply, 0, tmpbuffer, 0, bytes_read); // ---- capture the received response text content ---- // If (! Iscaptured) {string STR = new string (tmpbuffer); responsestr + = STR; If (responsestr. contains ("HTTP/") & responsestr. contains (http_end) {int endindex = responsestr. indexof (http_end, 0); responsestr = responsestr. substring (0, endindex); log. E ("from media server ---->", responsestr); iscaptured = true ;}} sckplayer. getoutputstream (). write (tmpbuffer); sckplayer. getoutputstream (). flush ();} log. E (TAG ,".......... over .......... "); // close internal, internal 2 socket sckplayer. close (); sckserver. close ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();}}}}. start ();}}

Related Article

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.