[Android] [PATCH 2/5] Remote playback: add a HTTP server to serve local media file

Adrien Maglo magsoft at gmail.com
Tue Jul 8 16:33:46 CEST 2014


---
 .../org/videolan/vlc/RemotePlaybackHTTPServer.java | 181 +++++++++++++++++++++
 1 file changed, 181 insertions(+)
 create mode 100644 vlc-android/src/org/videolan/vlc/RemotePlaybackHTTPServer.java

diff --git a/vlc-android/src/org/videolan/vlc/RemotePlaybackHTTPServer.java b/vlc-android/src/org/videolan/vlc/RemotePlaybackHTTPServer.java
new file mode 100644
index 0000000..ef4b48b
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/RemotePlaybackHTTPServer.java
@@ -0,0 +1,181 @@
+/*****************************************************************************
+ * RemotePlaybackHTTPServer.java
+ *****************************************************************************
+ * Copyright © 2011-2012 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+package org.videolan.vlc;
+
+
+import java.io.File;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketException;
+import java.net.URLDecoder;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpException;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.conn.util.InetAddressUtils;
+import org.apache.http.entity.FileEntity;
+import org.apache.http.impl.DefaultConnectionReuseStrategy;
+import org.apache.http.impl.DefaultHttpResponseFactory;
+import org.apache.http.impl.DefaultHttpServerConnection;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.BasicHttpProcessor;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.protocol.HttpRequestHandler;
+import org.apache.http.protocol.HttpRequestHandlerRegistry;
+import org.apache.http.protocol.HttpService;
+
+
+public class RemotePlaybackHTTPServer extends Thread {
+    private static final String MESSAGE_PATTERN = "/video";
+
+    private int serverPort = 4212;
+
+    private BasicHttpContext mHttpContext;
+    private HttpService mHttpService;
+
+    private ServerSocket mServerSocket;
+    private Socket mSocket;
+
+    private VideoRequestHandler mRequestHandler;
+
+    public RemotePlaybackHTTPServer(String mediaLocation) {
+        mRequestHandler = new VideoRequestHandler(mediaLocation);
+
+        BasicHttpProcessor httpproc = new BasicHttpProcessor();
+        mHttpContext = new BasicHttpContext();
+
+        mHttpService = new HttpService(httpproc,
+                new DefaultConnectionReuseStrategy(),
+                new DefaultHttpResponseFactory());
+
+        HttpRequestHandlerRegistry registry = new HttpRequestHandlerRegistry();
+
+        registry.register(MESSAGE_PATTERN, mRequestHandler);
+
+        mHttpService.setHandlerResolver(registry);
+    }
+
+    @Override
+    public void run() {
+        super.run();
+
+        try {
+            mServerSocket = new ServerSocket(serverPort);
+            mServerSocket.setReuseAddress(true);
+
+            mSocket = mServerSocket.accept();
+
+            DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();
+
+            serverConnection.bind(mSocket, new BasicHttpParams());
+
+            mHttpService.handleRequest(serverConnection, mHttpContext);
+
+            serverConnection.shutdown();
+
+        }
+        catch (IOException e) { }
+        catch (HttpException e) {
+            e.printStackTrace();
+        }
+    }
+
+
+    public void stopServer()
+    {
+        try {
+            if (mServerSocket != null)
+                mServerSocket.close();
+        } catch (IOException e) { }
+
+        try {
+            if (mSocket != null)
+                mSocket.close();
+        } catch (IOException e) { }
+
+        try {
+            join();
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        return;
+    }
+
+
+    private class VideoRequestHandler implements HttpRequestHandler {
+
+        private String mMediaLocation;
+
+        public VideoRequestHandler(String mediaLocation) {
+            try {
+                mediaLocation = URLDecoder.decode(mediaLocation, "UTF-8");
+            } catch (UnsupportedEncodingException e) {
+                e.printStackTrace();
+            }
+            mMediaLocation = mediaLocation.substring("file://".length());
+        }
+
+        @Override
+        public void handle(HttpRequest request, HttpResponse response,
+                HttpContext context) throws HttpException, IOException {
+            HttpEntity entity = new FileEntity(new File(mMediaLocation), "video/mp4");
+            response.setEntity(entity);
+        }
+    };
+
+
+    public String getIPAddress(boolean useIPv4) {
+        List<NetworkInterface> interfaces;
+        try {
+            interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
+        } catch (SocketException e) {
+            e.printStackTrace();
+            return null;
+        }
+        for (NetworkInterface intf : interfaces) {
+            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
+            for (InetAddress addr : addrs) {
+                if (!addr.isLoopbackAddress()) {
+                    String sAddr = addr.getHostAddress().toUpperCase();
+                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
+                    if (useIPv4) {
+                        if (isIPv4)
+                            return sAddr;
+                    } else {
+                        if (!isIPv4) {
+                            int delim = sAddr.indexOf('%'); // Drop ip6 port suffix
+                            return delim<0 ? sAddr : sAddr.substring(0, delim);
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+}
-- 
1.9.1



More information about the Android mailing list