Android向EasyN攝像頭髮送http get請求獲取媒體數據響應包

一.描述

Android實現獲取網絡攝像頭的視頻流並且播放,以便實現手機實時監控。

網絡攝像頭:EsayN(普順達)。

網絡:內網。外網應該也也一樣,沒有測試。

協議文檔:ip Camera視音頻訪問協議。

此文檔主要是實現協議文檔中發送 http get請求到網絡攝像頭,獲取媒體數據響應包。

代碼中攝像頭的IP和端口分別是192.168.10.253  81。

二.發送http get請求包

1.使用Socket建立與攝像頭的連接。

2.獲得socket的OutputStream流。

3.StringBuffer中寫要發送的請求包的內容。

4.OutputStream對象write StringBuffer中的數據。

三.獲取媒體數據響應數據

1.獲得socket的InputStream流。

2.根據InputStream流獲取BufferedReader對象。

3.讀取響應數據。

四.代碼如下:

<span style="font-family:Microsoft YaHei;font-size:18px;">        Socket socket = new Socket("192.168.10.253", 81);
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream();
        StringBuffer sb = new StringBuffer();
        
        sb.append("GET http://192.168.10.253:81/livestream/11?action=play&media=video_audio_data HTTP/1.1\r\n");
        sb.append("User-Agent: HiIpcam/V100R003 VodClient/1.0.0\r\n");
        sb.append("Connection: Keep-Alive\r\n");
        sb.append("Cache-Control: no-cache\r\n");
        sb.append("Authorization: admin admin\r\n");
        sb.append("Content-Length: 57\r\n");
        sb.append("Cseq: 1\r\n");
        sb.append("Transport:RTP/AVP/TCP;unicast;interleaved=0-1\r\n");
        sb.append("\r\n");
        
        out.write(sb.toString().getBytes());
        out.flush();
         
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String tmp = "";
        System.out.println("response:\r\n");
        /*for(int i = 0;i<20;i++){
        	tmp = reader.readLine();
        	System.out.println(tmp);
        }*/
        while ((tmp = reader.readLine()) != null) {
        	System.out.println(tmp); 
        } 
        socket.close();</span>


 

五.問題

1.發送請求包之後響應包一直爲空,獲取不到數據。

原因:Authorization: admin admin,賬號,也就是第一個admin之前必須要有空格。

2.針對上述代碼在使用Juit測試的時候被卡死。

原因:查看Logcat發現一直在輸出數據,不是在http包結束之後就停止輸出了。,在http包之後輸出的數據應該就是視頻流數據。

六.總結

看到文檔時的第一思路就是首先要獲取響應包,但是在獲取響應包之後不知道該怎麼做,怎麼處理獲得的視頻流,最後發現EasyN支持rtsp獲得視頻流,視頻流地址格式爲rtsp://[account]:[password]@[ip][port]/11

account: 賬號

Password  密碼

Ip 網絡攝像頭的ip

Port  rtsp端口。默認是554

11代表主碼流,12代表次碼流。

例如:rtsp://admin:[email protected]:554/11   一定要注意端口。

七. ip Camera視音頻訪問協議文檔

IP Camera 視音頻訪問協議
此文檔描述瞭如何獲取網絡攝像機的視音頻及其他媒體數據。網絡攝像機支持公用通信
協議和私有通信協議,此文檔主要描述私有協議的內容和協議的實現方式。
1. 公用通信協議 RTSP Over Http
Quicktime 支持此協議。詳細請參考RTSP 協議,RTP 協議和http 協議。
2. 公用通信協議 RTSP Over UDP
Quicktime, VLC 支持此協議。詳細請參考RTSP 協議,RTP 協議。
3. 私有通信協議
通過此協議實現獲取媒體數據。
媒體數據包括,視頻數據,音頻數據,移動檢測報警數據,外置報警數據。
此協議建立在 TCP/IP 之上。
3.1 通信協議工作流程
1). 媒體數據接收流程
發送媒體數據請求包
循環接收媒體數據
關閉 TCP 連接
接收響應包
建立 TCP 連接
3.2 通信協議詳細說明
1). 媒體數據
a.媒體數據請求包
GET
http://[IP]:[port]/livestream/[number]?action=play&media=[type] HTTP/1.1\r\n
User-Agent: HiIpcam/V100R003 VodClient/1.0.0\r\n
Connection: Keep-Alive\r\n
Cache-Control: no-cache\r\n
Authorization: [username] [password] \r\n
Content-Length: [length] \r\n
\r\n
Cseq: 1\r\n
Transport: RTP/AVP/TCP;unicast;interleaved=0-1\r\n
\r\n
字段
說明
IP
設備IP地址。
port
設備端口號。
number
碼流號。
11: 第一通道主碼流。
12: 第一通道次碼流。
21: 第二通道主碼流。
22: 第二通道次碼流。
注意:目前設備只有第一個通道
type
請求數據類型
video 僅視頻數據
audio 僅音頻數據
data 僅報警數據(目前不支持)
video_audio 音視頻數據
video_data 視頻報警數據
audio_data 音頻報警數據
video_audio_data 音視頻,報警數據
username
用戶名
password
密碼
length
該字段以後數據的長度(從Cseq開始的字節數),可以使用strlen()函數
示例:
GET
http://192.168.1.88:80/livestream/11?action=play&media=video_audio_data HTTP/1.1\r\n
User-Agent: HiIpcam/V100R003 VodClient/1.0.0\r\n
Connection: Keep-Alive\r\n
Cache-Control: no-cache\r\n
Authorization: guest guest\r\n
Content-Length: 57\r\n
Cseq: 1\r\n
Transport: RTP/AVP/TCP;unicast;interleaved=0-1\r\n
\r\n\
b. 媒體數據響應包
1). 用戶名密碼校驗錯誤
示例:
HTTP/1.1 401 Unauthorized\r\n
Host: 192.168.1.88\r\n
Connection: Keep-Alive\r\n
2). 權限校驗成功,返回音視頻類型
HTTP/1.1 200 OK\r\n Host: [IP]\r\n Connection: Keep-Alive\r\n Server: HiIpcam/V100R003 VodServer/1.0.0\r\n Cache-Control: no-cache\r\n Accept-Ranges: Bytes\r\n Content-Type: application/octet-stream\r\n Connection: close\r\n
\r\n
Session: 15547656\r\n
Cseq: 1\r\n
m=video 96 H264/90000/[Width]/[Height]\r\n
m=audio 97 G726/8000/1\r\n
Transport: RTP/AVP/TCP;unicast;hisiinterleaved=0-1;ssrc=614fd4a1\r\n
\r\n
字段
說明
video
Width:視頻寬。
Height:視頻高。
視頻只有H264
audio
97 G726
8 G711a
所有音頻,都是單聲道,8K採樣,16位寬。
G711a碼流64kbps
G726 碼流16kbps
示例:
HTTP/1.1 200 OK\r\n Host: 192.168.1.88\r\n Connection: Keep-Alive\r\n Server: HiIpcam/V100R003 VodServer/1.0.0\r\n Cache-Control: no-cache\r\n Accept-Ranges: Bytes\r\n Content-Type: application/octet-stream\r\n Connection: close\r\n
\r\n
Session: 15547656\r\n
Cseq: 1\r\n
m=video 96 H264/90000/704/576\r\n
m=audio 97 G726/8000/1\r\n
Transport: RTP/AVP/TCP;unicast;hisiinterleaved=0-1;ssrc=614fd4a1\r\n
\r\n
注意:一定按照HTTP協議判斷HTTP包結束。
c. 媒體數據包內容
1). 數據包頭結構圖
1. RTSP數據包頭格式:
typedef struct
{
Unsigned char daollar; /*8, $:dollar sign(24 decimal)*/
Unsigned char channelid; /*8, channel id*/
Unsigned short resv; /*16, reseved*/
Unsigned int payloadLen; /*32, payload length*/
RTP_HDR_S rtpHead; /*rtp head*/
}RTSP_ITLEAVED_HDR_S;
字段
說明
payloadLen
代表後面的RTP數據包的長度(包括RTP_HDR_S頭的長度)
注意:該字段爲網絡字節序
其他值可暫時不處理
2. RTP數據包頭格式:
typedef struct
{
/* byte 0 */
Unsigned short cc :4; /* CSRC count */
Unsigned short x :1; /* header extension flag */
Unsigned short p :1; /* padding flag */
Unsigned short version :2; /* protocol version */
/* byte 1 */
Unsigned short pt :7; /* payload type */
Unsigned short marker :1; /* marker bit */
/* bytes 2, 3 */
Unsigned short seqno :16; /* sequence number */
/* bytes 4-7 */
Unsigned int ts; /* timestamp in ms */
/* bytes 8-11 */
Unsigned int ssrc; /* synchronization source */
} RTP_HDR_S;
字段
說明
音頻編碼數據或視頻編碼數據或報警數據
RTP數據包頭
RTSP數據包頭
pt
96 H.264
97 G.726
8 G.711a
100 報警數據
ts
時間戳,單位爲毫秒
注意:該字段爲網絡字節序
其他值可暫時不處理
3. 視頻編碼數據
每次收到的視頻數據爲H.264的一個Nalu包。它可能是一個完整幀也可能不是.
當要
多個Nalu包組成一個完整幀時,就要進行組幀操作。
有一
個簡便的方法可以組幀,判斷時間戳,時間戳相同爲一幀。
4. 音頻編碼數據
音頻數據解碼時,要先去掉4字節的私有數據頭。
5. 報警數據
移動檢測數據內容
當4個報警區域都有報警時如下:
“MDName1-x-y-width-height| MDName2-x-y-width-height| MDName3-x-y-width-height|MDName4-x-y-width-height|”
字段
說明
MDName
移動檢測區域名稱(值:1, 2, 3, 4)
x
X坐

y
Y坐

width
寬度
height
高度
“|”
數據分隔符
示例:
1-10-10-20-20|
外置報警數據內容
alarmin:1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章