Android search problem with proxy stream and stagefright player

I want to play an audio stream from a URL that is valid only for a period of time. This doesn’t work very well using the built-in real-time streaming functionality due to the buffering mechanism (the URL will be dead by the time the buffer is full a second time), so I implemented a proxy stream similar to what was done in the npr application

http://code.google.com/p/npr-android-app/source/browse/trunk/Npr/src/org/npr/android/news/StreamProxy.java

This really works very well, with one exception, any call to the call effectively destroys the proxy flow. I find it difficult to determine exactly how searches work in the stage. Every time I search, I get a message

01-12 13:35:57.201: ERROR/(4870): Connection reset by peer
01-12 13:35:57.201: ERROR/(4870): java.net.SocketException: Connection reset by peer
01-12 13:35:57.201: ERROR/(4870): at org.apache.harmony.luni.platform.OSNetworkSystem.writeSocketImpl(Native Method)
01-12 13:35:57.201: ERROR/(4870):     at org.apache.harmony.luni.platform.OSNetworkSystem.write(OSNetworkSystem.java:723)
01-12 13:35:57.201: ERROR/(4870):     at org.apache.harmony.luni.net.PlainSocketImpl.write(PlainSocketImpl.java:578)
01-12 13:35:57.201: ERROR/(4870):     at org.apache.harmony.luni.net.SocketOutputStream.write(SocketOutputStream.java:59)
01-12 13:35:57.201: ERROR/(4870):     at com.soundcloud.utils.StreamProxy.processRequest(StreamProxy.java:209)

, stagefright URL- ( , - reset). , , - :

while (isRunning && (readBytes = data.read(buff, 0, buff.length)) != -1) 

, , - . ( )?

. - ?

+3
4

MediaPlayer HTTP, , . , -. MediaPlayer.

+1

, MediaPlayer -, Status 206 , (int) .

String headers += "HTTP/1.1 206 OK\r\n";
headers += "Content-Type: audio/mpeg\r\n";
headers += "Accept-Ranges: bytes\r\n";
headers += "Content-Length: " + (fileSize-range) + "\r\n";
headers += "Content-Range: bytes "+range + "-" + fileSize + "/*\r\n";
headers += "\r\n";

MediaPlayer, Range HTTP-, , :

String headers = "HTTP/1.1 200 OK\r\n";
headers += "Content-Type: audio/mpeg\r\n";
headers += "Accept-Ranges: bytes\r\n";
headers += "Content-Length: " + fileSize + "\r\n";
headers += "\r\n";

!

+2

, - , , , -:

  private HttpResponse download(String url, Header[] headers) {
DefaultHttpClient seed = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
registry.register(
        new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
SingleClientConnManager mgr = new MyClientConnManager(seed.getParams(),
    registry);
DefaultHttpClient http = new DefaultHttpClient(mgr, seed.getParams());
HttpGet method = new HttpGet(url);
for (Header header : headers) {
    method.addHeader(header);
}
HttpResponse response = null;
try {
  Log.d(getClass().getName(), "starting download");
  response = http.execute(method);
  Log.d(getClass().getName(), "downloaded");

}catch(java.net.UnknownHostException e)
{
    Intent i=new Intent("org.prx.errorInStream");
    mContext.sendBroadcast(i);
}
catch (ClientProtocolException e) {
  Log.e(getClass().getName(), "Error downloading", e);
} catch (IOException e) {
  Log.e(getClass().getName(), "Error downloading", e);
}
return response;

}

private void processRequest(HttpRequest request, Socket client)
  throws IllegalStateException, IOException {
if (request == null) {
  return;
}
Log.d(getClass().getName(), "processing");
String url = request.getRequestLine().getUri();

HttpResponse realResponse = download(url, request.getAllHeaders());

if (realResponse == null) {
  return;
}

...

}

+1

As @Roberto already mentioned, you need to change the methods downloadand processRequestto bypass the headers of the HTTP requests. But in order to do this, or at least in my case, I need to further modify the method private HttpRequest readRequest(Socket client)for analyzing the headers from the socket, as shown below.

private HttpRequest readRequest(Socket client) {
        HttpRequest request = null;
        InputStream is;
        String firstLine;
        List<Header> headers = new ArrayList<Header>();
        try {
            is = client.getInputStream();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is), 8192);
            firstLine = reader.readLine();
            /**
             * read HTTP request headers from socket
             * */
            String line;
            while (((line = reader.readLine()) != null) && !(line.equals(""))) { // HTTP header ends at ""
                try {
                    StringTokenizer st = new StringTokenizer(line, ":");
                    String h = st.nextToken();
                    String v = st.nextToken();
                    Header header = new BasicHeader(h, v);
                    headers.add(header);
                } catch (NoSuchElementException e) {
                }
            }
        } catch (IOException e) {
            Log.e(LOG_TAG, "Error parsing request", e);
            return request;
        }

        if (firstLine == null) {
            Log.i(LOG_TAG, "Proxy client closed connection without a request.");
            return request;
        }

        /**
         * retrieve REAL url
         * */
        StringTokenizer st = new StringTokenizer(firstLine);
        String method = st.nextToken();
        String uri = st.nextToken();
        Log.d(LOG_TAG, uri);
        String realUri = uri.substring(1);
        Log.d(LOG_TAG, realUri);
        request = new BasicHttpRequest(method, realUri);
        /**
         * add headers back to HTTP request
         * */
        for (Header header : headers) {
            request.addHeader(header);
        }
        return request;
    }
0
source

Source: https://habr.com/ru/post/1785050/


All Articles