Android的媒体播放器没有添加自定义标题

时间:2015-09-23 18:40:32

标签: java android android-mediaplayer rtsp rtp

我尝试使用内置MediaPlayer类的Android来播放RTSP流。我编写了一个单独的库,可以正确地形成我的IP摄像机所需的摘要式身份验证。

我正在尝试使用以下代码附加生成的Authorization标头:

private void initPlayback(){
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDisplay(mSurfaceHolder);

        Context context = getActivity();
        Map<String, String> headers = getRtspHeaders();
        Uri source = Uri.parse(mCameraURI);

        try {
            // Specify the IP camera's URL and auth headers.
            mMediaPlayer.setDataSource(context, source, headers);

            // Begin the process of setting up a video stream.
            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.prepareAsync();
        }
        catch (Exception e) {

        }
    }

为了测试目的,我在以下函数中硬编码了hashmap:

private Map<String, String> getRtspHeaders() {
    Map<String, String> headers = new HashMap<String, String>();
    String digestAuthValue = "Digest username=\"admin\", realm=\"d8eb97cdf7d8\", nonce=\"3583e1908677b7af62f3e295eddec2f0\", uri=\"rtsp://10.20.30.62:554/Streaming/Channels/1?transportmode=unicast&profile=Profile_1\", response=\"42682812bfaf14b5406abafa179dabf9\"";
    headers.put("Authorization", digestAuthValue);
    return headers;
}

现在理论上我跑:

mMediaPlayer.start();

它应该发送一个DESCRIBE请求,其中包含我添加的标题。

MediaPlayer代替发送此数据包(使用Wireshark / Ettercap捕获):

DESCRIBE rtsp://10.20.30.62:554/Streaming/Channels/1?transportmode=unicast&profile=Profile_1 RTSP/1.0
Accept: application/sdp
User-Agent: stagefright/1.2 (Linux;Android 5.1.1)
CSeq: 1

我已经将函数调用一直追溯到本机代码,但仍无法弄清楚它为什么不添加此标题。这些值会一直显示nativeSetDataSource中的MediaPlayer调用,但看起来仍然正确。据我所知,我没有办法在本机代码中放置断点。

非常感谢任何指导。

1 个答案:

答案 0 :(得分:0)

最终,我的同事和我能够深入挖掘Android源代码以找到解决方法,但不足以实际找出未添加自定义标头的原因。

特别是对于Authorization标题,您实际上可以通过MediaPlayer修改后的uri。对于rtsp流,它看起来像这样: rtsp://user:password@192.168.0.3:554/stream

当用户名和密码在uri中时,Android MediaPlayer实际上会将它们拉出来并将它们放入授权标题中(使用WireShark进行验证)。我们能够在Android 4.0及更新版本上进行测试,但在旧版本中也可能如此。

希望这可以帮助其他人走出困境。