4

許可なしで IP カメラからビデオをストリーミングすることができましたが、今は許可を得てこれを行う必要があります。Android が RTSP による認証をサポートしていないという情報はほとんど見つかりませんでしたが、そのメソッドで HEADERS を追加することで API レベル 14 で可能であるという別の情報を見つけました: setDataSource (Context context, Uri uri, Map headers)。私のコードは次のようになります。

@Override
public void surfaceCreated(SurfaceHolder holder){
    String authHeader = getB64Auth("user","password");
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Authorization", authHeader);
    Uri srcUri = Uri.parse("rtsp://10.0.0.113:554/channel1");
        try{
            m.setDisplay(h);
            m.setDataSource (getApplicationContext(), srcUri,headers);
            m.prepare();
            m.setAudioStreamType(AudioManager.STREAM_MUSIC);
            m.start();
        }catch(Exception e){
            e.printStackTrace();
        }

}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    m.release();
}
private String getB64Auth (String login, String pass) {
    String source=login+":"+pass;
    String ret="Basic "+Base64.encodeToString(source.getBytes(),Base64.URL_SAFE|Base64.NO_WRAP);
    Log.e("Authorization",ret);
    return ret;
}

しかし、それは機能せず、どこに問題があるのか​​ わかりません。その種のストリーミングの経験がある人はいますか? それとも、MediaPlayer クラスの新しいメソッドの効果を誤解しただけでしょうか?

4

2 に答える 2

4

RTSP は、TCP 制御ソケットを使用してハンドシェークを実行し、制御メッセージを送信します。実際のビデオ データは、TCP または UDP の別のポートを介して送信されます。RTSP 制御情報は、HTTP または HTTPS を介してトンネリングすることもできます。Android MediaPlayer を使用して遭遇した問題は、RTSP 認証をサポートしていないことでした。ただし、認証情報を追加するローカル RTSP プロキシを作成することで、その制限を回避することができました。幸いなことに、RTSP ヘッダーは非常にシンプルでクリア テキストであるため、必要な認証を簡単に追加できます。

これが私がやった方法です:

  1. プロキシとして機能するローカル ServerSocket を作成します。
  2. ユーザーのパスワードから認証文字列を作成する
  3. MediaPlayer をローカル プロキシに接続する
  4. 送信データの場合、送信 TCP パケットに認証を追加します

RTSP データは非常にまばらであるため、すべての RTSP データの解析と変更のオーバーヘッドは通常無視できることに注意してください。

私が何を意味するかを示すためのコード例を次に示します。

import android.util.Base64;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

public class RTSPConnection implements Runnable {

    private final IRTSPListener listener;

    private String targetRtspServerHost;
    private String targetRtspServerProto;
    private String targetRtspUserInfo;
    private String targetRtspServerPath;
    private int targetRtspServerPort = -1;

    public RTSPConnection(IRTSPListener l, String u) throws RTSPException {
        // Parse the host (content provider) from the URL
        this.listener = l;

        if (u == null) {
            throw new RTSPException();
        }

        URI parsedURL;
        try {
            parsedURL = new URI(u);
        } catch (URISyntaxException e) {
            throw new RTSPException();
        }

        targetRtspServerHost = parsedURL.getHost();     // none is null
        if (targetRtspServerHost == null || targetRtspServerHost.equals("")) {
            throw new RTSPException();
        }

        targetRtspServerProto = parsedURL.getScheme();  // none is null
        if (targetRtspServerProto == null) {
            targetRtspServerProto = "rtsp";             // default to rtsp:
        }
        if (!targetRtspServerProto.equalsIgnoreCase("rtsp")) {
            throw new RTSPException();                  // error for http: or https:
        }

        targetRtspServerPort = parsedURL.getPort();     // none is -1
        if (targetRtspServerPort == -1) {
            targetRtspServerPort = 554;                 // Use the RTSP default port
        }

        targetRtspUserInfo = parsedURL.getUserInfo();   // none is null

        targetRtspServerPath = parsedURL.getPath();     // none is an empty string ""
    }

    @Override
    public void run() {
        ServerSocket listen;

        try {
            // Start the local proxy server on a random high port
            listen = new ServerSocket(0);
        } catch (IOException e) {
            // Error starting local server
        }

        // Connects to the RTSP server
        Socket remote;
        try {
            remote = new Socket(targetRtspServerHost, targetRtspServerPort);
        } catch (IOException e) {
            try {
                listen.close();
            } catch (IOException ignored) {}
            return;
        }

        // Notify user on other thread that the server is about to start
        // Build the string that the VideoPlayer should connect to in order to be routed to the actual source
        int listeningOnPort = listen.getLocalPort();
        String connectUrl = targetRtspServerProto + "://localhost:" + listeningOnPort + targetRtspServerPath;
        listener.onRtspProxyReady(connectUrl);

        // Wait for a local connection (Blocking)
        Socket local;
        try {
            // Wait for 5 seconds for a connection
            listen.setSoTimeout(5000);
            local = listen.accept();
        } catch (IOException e) {
            // Error on server connect/accept or timed out waiting
            try {
                listen.close();
            } catch (IOException ignored) {}
            try {
                remote.close();
            } catch (IOException ignored) {}
            listener.onRtspProxyError(RTSPError.ProxyTimedOut);
            return;
        }

        // Create the Authorization header for the outgoing RTSP packets
        Map<String, String> headers = null;
        if (targetRtspUserInfo != null) {
            String authHeader = "Basic " + Base64.encodeToString(targetRtspUserInfo.getBytes(),
                    Base64.URL_SAFE | Base64.NO_WRAP);
            headers = new HashMap<String, String>();
            headers.put("Authorization", authHeader);
        }

        try {
            new ForwardAndAddHeadersThread(local, remote, headers).start();
            new PassThru(remote, local).start();
        } catch (IOException e) {
            try {
                local.close();
            } catch (IOException ignored) {}
            try {
                listen.close();
            } catch (IOException ignored) {}
            try {
                remote.close();
            } catch (IOException ignored) {}
            listener.onRtspProxyError(RTSPError.CouldNotStartProxy);
        }
    }

    private abstract class ForwardTCPThread extends Thread {
        protected InputStream in;
        protected OutputStream out;
        private Socket socket_in, socket_out;

        public ForwardTCPThread(Socket i, Socket o) throws IOException {
            socket_in = i;
            socket_out = o;
            in = socket_in.getInputStream();
            out = socket_out.getOutputStream();
        }

        protected void shutdown() {
            // Close things down...
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ignored) {}
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ignored) {}
            }

            try {
                socket_in.close();
            } catch (IOException ignored) {}
            try {
                socket_out.close();
            } catch (IOException ignored) {}

        }
    }

    private class PassThru extends ForwardTCPThread {

        public PassThru(Socket in, Socket out) throws IOException {
            super(in, out);
            setName("Forward TCP");
        }

        public void run() {
            byte[] buf = new byte[4096];
            try {
                int count;
                while ((count = in.read(buf)) > 0) {
                    out.write(buf, 0, count);
                }
            } catch (IOException e) {
                listener.onRtspProxyError(RTSPError.RemoteConnectionDropped);
            }

            shutdown();
        }
    }

    public class ForwardAndAddHeadersThread extends ForwardTCPThread {
        private final Map<String,String> headers;

        public ForwardAndAddHeadersThread(Socket in, Socket out, Map<String,String> headers) throws IOException {
            super(in, out);
            this.headers = headers;
            setName("Forward TCP Add Headers");
        }

        public void run() {
            byte[] buf = new byte[4096];
            try {
                int count;
                /*
                 * This code looks for the sequence number header in the RTSP packet and inserts additional headers
                 * on the next rows, separated by \r\n
                 */
                while ((count = in.read(buf)) > 0) {
                    /**
                     * Note: This code is NOT optimized for speed.  It is assumed to be a very low data channel that
                     * only contains infrequent/short TCP/RTSP commands.
                     *
                     * Warn: This code assumes that the RTSP packet is read all-at-once, such that the CSeq: header
                     * is never split into two "reads".
                     */
                    String temp = new String(buf, 0, count, "UTF-8");
                    String strings[] = temp.split("\r\n",-1);
                    String str_buf = "";
                    for (String s: strings) {
                        str_buf += s + "\r\n";
                        if (headers != null) {
                            if (s.contains("CSeq:")) {
                                for (Map.Entry<String, String> entry : headers.entrySet()) {
                                    str_buf += entry.getKey() + ": " + entry.getValue() + "\r\n";
                                }
                            }
                        }
                    }
                    out.write(str_buf.getBytes("UTF-8"), 0, str_buf.length());
                }
            } catch (IOException e) {
                listener.onRtspProxyError(RTSPError.LocalConnectionDropped);
            }

            shutdown();
        }
    }
}
于 2015-10-24T20:36:03.757 に答える
2

rtsp://username:password@10.0.0.113:554/channel1 のように指定します。また、サーバーが MD5 ダイジェスト ベースの認証を期待している場合、Android 4.2 までは十分ではありませんでした。

于 2013-11-18T06:45:23.190 に答える