当前位置:文档之家› RTMP协议

RTMP协议

RTMP ProtocolConnectNetConnect.connect()Flash Play 通过NetConnect.connect连接到RTMP Server时,首先进行握手,再发送connect的参数.1) 握手过程有三步:Step 1,Flash Player 至RTMP Server : 1个byte(0x03)+1536个byte数据.Step 2,RTMP Server至Flash Player : 1个byte(0x03)+1536个byte数据(Server的握手数据) + 1536个byte数据(通过和随机数hash得出, 详见附录)Step 3,Flash Player 至RTMP Server : 1536个byte数据(RTMP Server计算出来的). 注意:这个数据块没有1个byte的0x03.2) 接下是connect参数RTMP Server <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Flash Player 也就是Flash Playe发送的第一个RTMP(AMF)包. (channel 0x03)RTMP包格式,详见TheAMF.pdf3) RTMP Server给Flash Player的返回包(TOCHECK: 是否需要0x04的ping包)RTMP Server >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Flash Player RTMP协议步骤Step 1发送一个0x05的包,即ServerBW, (channel 0x02)(0x00 26 25 a0)Step 2发送一个0x06的包,即ClientBW, (channel 0x02)(0x00 26 25 a0) + (0x02)Step 3发送一个0x14的包,即Invoke, (channel 0x03) (body超过128的长度就要分包, 用0xc3来) string (“_result”)+ number (0x3F F0 00 00 00 00 00 00)+ Object string (“capabilities”) ; number (31.0)string (“fmsV er”) ; string (随便填) (“RubyIZUMI/0,1,2,0”)End Of Object (0x00 00 09)//(connect status)+ Object string (“code”) ; string (“NetConnection.Connect.Success”)string (“level”) ; string (“status”)string (“description”) ; string (“Connection Succeeded.”)End Of Object (0x00 00 09)RTMP Server <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Flash Player 0) 在CreateStream之前RTMP Server会收到一个0x05的ServerBW包(channel 0x02)(0x00 26 25 a0)Stream Functions1. CreateStream: NetStream(nc)RTMP Server <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Flash Player 1) RTMP Server接受到Flash Player的一个0x14的包,即Invoke (channel 0x03)string (“CreateStream”)+ number (0x40 00 00 00 00 00 00 00)+ null (0x05)RTMP Server >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Flash Player 2) RTMP Server返回Flash Player一个0x14的包,即Invoke (channel 0x03)string (“_result”)+ number (0x40 00 00 00 00 00 00 00)+ null (0x05)+ number(0x3F F0 00 00 00 00 00)RTMP Server <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Flash Player 3) RTMP Server收到FlashPlayer的一个0x04的Ping包(channel 0x02)V alue1 (0x00 03) 表示buffer time的请求V alue2 (0x00 00 00 01) 表示streamIdV alue3 (0x00 00 00 00) 表示buffer time2. Play: NetStream.play()RTMP Server <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Flash Player 1)RTMP Server收到Flash Player的一个为0x14的包,即Invoke(channel 0x08?????)Play file:NetStream.Play(“test.mp4”);string (“play”)+ number(0x00 00 00 00 00 00 00 00)+ null(0x05)+ string (“test.mp4”)Play live:NetStream.Play(“red5StreamDemo_video”);string (“play”)+ number(0x00 00 00 00 00 00 00 00)+ null(0x05)+ string (“red5StreamDemo_video”)Play file with parameters:NetStream.Play(“red5StreamDemo_video”, -1);string (“play”)+ number(0x00 00 00 00 00 00 00 00)+ null(0x05)+ string (“red5StreamDemo_video”)+ number(0xc0 6f 40 00 00 00 00 00)RTMP Server >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Flash Player 2) RTMP Server发送Flash Player一个0x01的包,即ChunkSize (channel 0x02)number (0x00 00 10 00)RTMP Server >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Flash Player 3) RTMP Server给FlashPlayer发送两个0x04的Ping包(channel 0x02)RTMP Server >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Flash Player 4) RTMP Server返回Flash Player发送一个0x14的包,即Invoke (channel 0x05, stream 0x01 00 00 00)string(“onStatus”)+ number(0x00 00 00 00 00 00 00 00)+ null(0x05)+ Object : string(“code”) ; string(“NetStream.Play.Start”)string ( “level”) ; string (“status”)string ( “description”) ; string (“-”)0x00 00 09(End Bytes)RTMP Server >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Flash Player 5) RTMP Server 给Flash Player发送一个0x09的包,即V ideoData(channel 0x05, stream 0x01 00 00 00)这个VideoData是A vcConfiguration的信息RTMP Server >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Flash Player 6)RTMP Server给Flash Player发送H.264的0x09 V ideoData包(channel 0x05, stream 0x01 00 00 00), streaming start…..3. Close: NetStream.CloseRTMP Server <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Flash Player 2)RTMP Server收到Flash Player的一个为0x14的包,即Invoke(channel 0x08?????)String(“closeStream”)+ number(0x00 00 00 00 00 00 00 00)+ null(0x05)4. Publish: NetStream.Publish在createStream之后的行为RTMP Server <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Flash Player 1) RTMP Server收到FlashPlayer的一个0x04的Ping包(channel 0x02)V alue1 (0x00 03) 表示buffer time的请求V alue2 (0x00 00 00 01) 表示streamIdV alue3 (0x00 00 00 00) 表示buffer timeRTMP Server <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Flash Player 2)RTMP Server收到FlashPlayer的一个0x14的Invoke包(channel 0x08???)String(“publish”)+ number(0x00 00 00 00 00 00 00 00)+ null(0x05)+ String(“read5StreamDemo”)+ String(“live”)RTMP Server >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Flash Player 3) RTMP Server给FlashPlayer发送一个0x14的Invoke包(channel 0x04) //注意根据ChunkSize 分ChunkString(“onStatus”)+ number(0x3f f0 00 00 00 00 00 00)+ null(0x05)+ Object: string ( “level”) ; string (“status”)string(“code”) ; string(“NetStream.Publish.Start”)string ( “description”) ; string (“”)string(“details”); string(“red5StreamDemo ”)string(“clientid”); number(0x3f f0 00 00 00 00 00 00)+ End of Object(0x00 00 09)RTMP Server <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Flash Player 3)FlashPlayer就开始向RTMP server发送AudioData包(channel 0x04) //注意AudioData的ChunkSize为64//TODORuby的握手数据生成算法:## RubyIZUMI## Copyright (C) 2008 Y aacov Akiba Slama <ya@># Copyright (C) 2008 Takuma Mori, SGRA Corporation# <mori@sgra.co.jp> <http://www.sgra.co.jp/en/>## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as# published by the Free Software Foundation, either version 3 of the# License, or 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 Affero General Public License for more details.## Y ou should have received a copy of the GNU Affero General Public License# along with this program. If not, see </licenses/>.#HandshakeServer = [0x00,0x86,0x4f,0x7f,0x00,0x00,0x00,0x00,0x6b,0x04,0x67,0x52,0xa2,0x70,0x5b,0x51, 0xa2,0x89,0xca,0xcc,0x8e,0x70,0xf0,0x06,0x70,0x0e,0xd7,0xb3,0x73,0x7f,0x07,0xc1, 0x72,0xd6,0xcb,0x4c,0xc0,0x45,0x0f,0xf5,0x4f,0xec,0xd0,0x2f,0x46,0x2b,0x76,0x10, 0x92,0x1b,0x0e,0xb6,0xed,0x71,0x73,0x45,0xc1,0xc6,0x26,0x0c,0x69,0x59,0x7b,0xbb, 0x53,0xb9,0x10,0x4d,0xea,0xc1,0xe7,0x7b,0x70,0xde,0xdc,0xf8,0x84,0x90,0xbf,0x80, 0xe8,0x85,0xb2,0x46,0x2c,0x78,0xa1,0x85,0x01,0x8f,0x8b,0x05,0x3f,0xa1,0x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1,0x09,0xa2,0xd0,0x6e,0x39,0x39,0xa5,0x0a,0x3d,0x8a,0x00,0x4b,0x95, 0x6f,0x8c,0x12,0x41,0xc6,0x46,0x10,0x5e,0x9d,0x50,0x85,0x0e,0x6b,0x81,0xa7,0x3b, 0x35,0xa6,0x38,0xf5,0xc2,0xba,0x6c,0x02,0xda,0x27,0x29,0x6e,0xe9,0x54,0x41,0xa4, 0x94,0x75,0xe8,0x55,0xc0,0xe3,0xc2,0x91,0x8a,0x1d,0xfb,0x2b,0xba,0x43,0xe7,0x45,0x85,0xe8,0x13,0x07,0x1d,0x9c,0x37,0xa8,0xf3,0xca,0xf4,0x19,0x77,0xc4,0x65,0xd6, 0x18,0x3e,0x60,0x08,0x74,0x49,0xba,0xc8,0x86,0x37,0x8a,0x0f,0x79,0x91,0x53,0x20, 0x23,0x00,0xb9,0xc5,0x1b,0x01,0xdd,0x10,0x34,0x05,0x42,0xa0,0x64,0xab,0x4d,0x51, 0xf4,0x53,0x35,0x18,0xde,0x20,0x1f,0xaa,0xe2,0x40,0x0d,0x6d,0x77,0x36,0x1f,0xee, 0x3a,0x93,0xdb,0x1d,0xd6,0xa0,0x23,0xcc,0xe6,0xa8,0x44,0x8e,0xae,0x9c,0xd7,0x97, 0x6a,0x99,0xee,0x40,0x15,0xd5,0x5a,0x6d,0xf6,0x9c,0x2c,0x52,0xcd,0xfa,0xf4,0xc8, 0x02,0xee,0xf2,0x76,0x8b,0x49,0x6d,0x66,0x83,0x5f,0xbe,0x05,0x8e,0xf2,0x27,0x73, 0xdb,0x00,0xeb,0x9a,0xb4,0xbf,0x47,0x9a,0xbd,0xf1,0x4f,0x70,0xed,0x33,0xce,0x31, 0x9d,0x9f,0x95,0x80,0x9e,0x73,0x11,0x6c,0x03,0x7b,0x6e,0x62,0x9c,0xd0,0xaa,0xf6, 0x5d,0xe0,0xd8,0x96,0x94,0x46,0xd1,0x10,0x3c,0x1b,0x9d,0x40,0xdd,0xab,0xec,0x8a, 0x5b,0x1a,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0b,0x2b,0xaa,0x57,0x2c,0x27,0xb3,0xa0,0x2a,0x0f,0x85,0x16,0x6c,0xe2,0xe0,0xa1, 0x48,0x8e,0x00,0x8d,0x6d,0xc8,0x10,0xfd,0x43,0x96,0x50,0x07,0x07,0x9a,0xbf,0x50, 0x62,0x76,0x3e,0xe1,0xf7,0x70,0xc1,0xb0,0x79,0x8e,0x61,0xe3,0xfb,0x05,0x5f,0xbb, 0x2d,0x76,0x69,0x89,0xf3,0x1e,0x62,0xf6,0x27,0x3d,0x3e,0x41,0x0f,0xf5,0x0f,0xc7, 0xf3,0x0e,0x3b,0xd5,0xed,0xcf,0xef,0x58,0xfa,0x39,0xdf,0x75,0x85,0x2b,0x8b,0xaa,0x08,0x72,0x52,0xa7,0x98,0x42,0x95,0x7b,0xb7,0xe7,0x10,0xfe,0xdb,0x54,0x34,0xfb, 0x91,0x24,0x1c,0x07,0xfb,0x9c,0xce,0xd0,0x46,0xcf,0xc4,0x9d,0x09,0x49,0x24,0xec, ].pack('C*')KeyServer = [0x47,0x65,0x6e,0x75,0x69,0x6e,0x65,0x20,0x41,0x64,0x6f,0x62,0x65,0x20,0x46,0x6c, 0x61,0x73,0x68,0x20,0x4d,0x65,0x64,0x69,0x61,0x20,0x53,0x65,0x72,0x76,0x65,0x72, 0x20,0x30,0x30,0x31,0xf0,0xee,0xc2,0x4a,0x80,0x68,0xbe,0xe8,0x2e,0x00,0xd0,0xd1, 0x02,0x9e,0x7e,0x57,0x6e,0xec,0x5d,0x2d,0x29,0x80,0x6f,0xab,0x93,0xb8,0xe6,0x36, 0xcf,0xeb,0x31,0xae,].pack('C*')require 'hmac-sha2'module RTMPclass Handshakedef Handshake.get_handshake(client)client_a = client.unpack('C*')if (client_a[4] == 0)return clientelsepart = client_a[(client_a[8] + client_a[9] + client_a[10] + client_a[11]) % 728 + 12,32].pack('C*') a = HMAC::SHA256.digest(KeyServer, part)server_a = Array.new()1504.times doserver_a.push(rand(256))endserver = server_a.pack("C*")server = server + HMAC::SHA256.digest(a, server)return serverendendendendJava的握手数据生成算法:out.put(HandshakeServer);byte[] client_a = new byte[1536];byte[] part = new byte[32];int pos = 0 , num = 0;in.get(client_a);if(client_a[4] == (byte)0x00){out.put(client_a);}else{num += (client_a[8]>0)?client_a[8]:(client_a[8]+256);num += (client_a[9]>0)?client_a[9]:(client_a[9]+256);num += (client_a[10]>0)?client_a[10]:(client_a[10]+256);num += (client_a[11]>0)?client_a[11]:(client_a[11]+256);pos = num % 728 + 12;System.arraycopy(client_a, pos, part, 0, 32);Mac mac = null;try {mac = Mac.getInstance("HmacSHA256");} catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}Random rnd = new Random(256);byte[] server_a = new byte[1504];for(int i = 0; i < 1504; i++ ){server_a[i] = (byte) rnd.nextInt();}byte[] digest = null;SecretKeySpec keySpec1 = new SecretKeySpec(KeyServer, "HmacSHA256");try {mac.init(keySpec1);digest = mac.doFinal(part);SecretKeySpec keySpec2 = new SecretKeySpec(digest, "HmacSHA256");mac.init(keySpec2);digest = mac.doFinal(server_a);out.put(server_a);out.put(digest);//out.put(client_a);} catch (InvalidKeyException e1) {// TODO Auto-generated catch blocke1.printStackTrace();out.putInt(0x01);out.fill((byte)0x00,Constants.HANDSHAKE_SIZE-4);out.put(in);}}。

相关主题