在SIP通话应用场景中,有时需要在音频和视频通话中进行切换,这个功能实现的需要发送re-INVITE重新协商媒体,即重新协商媒体(SDP)中是否允许视频。
一、实现原理
1.1 音频转视频
音频通话过程中,发起方重新发送INVITE消息,SDP媒体参数中在原有音频参数的基础上增加视频参数,接受方如果支持视频,则在响应的200-OK消息中带上协商的视频参数,然后两端开始收发视频流
1.2 视频转音频
音频通话过程中,发起方重新发送INVITE消息,SDP媒体参数中移除原有的视频参数,接受方则在响应的200-OK消息中只保留音频参数或禁止视频,然后两端停止收发视频流
二、报文交互
2.1 音频转视频
信令交互:
INVITE消息:
INVITE sip:1002@192.168.1.248:5060;transport=UDP SIP/2.0
Via: SIP/2.0/UDP 192.168.1.74:5060;rport;branch=z9hG4bKPjAvimPJxyyYQjxnsp1Py6cvUF4wU-M9LZ
Max-Forwards: 70
From: "1001" sip:1001@192.168.1.248;tag=bUW2K4dpkGZGInsDmLyey16kd4naQzzA
To: sip:1002@192.168.1.248;tag=r9X9UXUNKU1rj
Contact: "1001" sip:1001@192.168.1.74:5060
Call-ID: kpf4FQxDfU6FS77XB.VRIwmHS4fH56T1
CSeq: 3806 INVITE
Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
Supported: replaces, 100rel
User-Agent: VoIP
Content-Type: application/sdp
Content-Length: 666
v=0
o=- 3900455088 3900455090 IN IP4 192.168.1.74
s=SDP
c=IN IP4 192.168.1.74
b=AS:3166
t=0 0
m=audio 10008 RTP/AVP 0 8 9 18 102 97 101
c=IN IP4 192.168.1.74
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:9 G722/8000
a=rtpmap:18 G729/8000
a=fmtp:18 annexb=no
a=rtpmap:102 opus/48000
a=fmtp:102 maxplaybackrate=16000;sprop-maxcapturerate=16000;maxcodedaudiobandwidth=16000
a=rtpmap:97 iLBC/8000
a=fmtp:97 mode=20
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=sendrecv
m=video 10010 RTP/AVP 99
c=IN IP4 192.168.1.74
b=TIAS:3000000
a=rtpmap:99 H264/90000
a=fmtp:99 profile-level-id=42801e;max-br=3000;packetization-mode=1
a=sendrecv
200-OK:
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.1.74:5060;rport=5060;branch=z9hG4bKPjAvimPJxyyYQjxnsp1Py6cvUF4wU-M9LZ
From: "1001" sip:1001@192.168.1.248;tag=bUW2K4dpkGZGInsDmLyey16kd4naQzzA
To: sip:1002@192.168.1.248;tag=r9X9UXUNKU1rj
Call-ID: kpf4FQxDfU6FS77XB.VRIwmHS4fH56T1
CSeq: 3806 INVITE
Contact: sip:1002@192.168.1.248:5060;transport=udp
User-Agent: FreeSWITCH-mod_sofia/1.10.10-dev+git~20230407T063007Z~be3a73af62~64bit
Accept: application/sdp
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY, PUBLISH, SUBSCRIBE
Supported: timer, path, replaces
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 374
v=0
o=- 3900455104 3900455107 IN IP4 192.168.1.222
s=SDP
c=IN IP4 192.168.1.222
t=0 0
m=audio 10008 RTP/AVP 0 101
c=IN IP4 192.168.1.222
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
m=video 10010 RTP/AVP 99
c=IN IP4 192.168.1.222
b=TIAS:768000
a=rtpmap:99 H264/90000
a=fmtp:99 profile-level-id=42801e;max-br=768;packetization-mode=1
2.2 视频转音频
信令交互:
INVITE消息:
INVITE sip:1002@192.168.1.248:5060;transport=UDP SIP/2.0
Via: SIP/2.0/UDP 192.168.1.74:5060;rport;branch=z9hG4bKPjp0vl88Onf20ksGJhJa.YPMabZxhe90sZ
Max-Forwards: 70
From: "1001" sip:1001@192.168.1.248;tag=bUW2K4dpkGZGInsDmLyey16kd4naQzzA
To: sip:1002@192.168.1.248;tag=r9X9UXUNKU1rj
Contact: "1001" sip:1001@192.168.1.74:5060
Call-ID: kpf4FQxDfU6FS77XB.VRIwmHS4fH56T1
CSeq: 3809 INVITE
Allow: PRACK, INVITE, ACK, BYE, CANCEL, UPDATE, INFO, SUBSCRIBE, NOTIFY, REFER, MESSAGE, OPTIONS
Supported: replaces, 100rel
User-Agent: VoIP
Content-Type: application/sdp
Content-Length: 662
v=0
o=- 3900455088 3900455092 IN IP4 192.168.1.74
s=SDP
c=IN IP4 192.168.1.74
b=AS:3166
t=0 0
m=audio 10008 RTP/AVP 0 8 9 18 102 97 101
c=IN IP4 192.168.1.74
a=rtpmap:0 PCMU/8000
a=rtpmap:8 PCMA/8000
a=rtpmap:9 G722/8000
a=rtpmap:18 G729/8000
a=fmtp:18 annexb=no
a=rtpmap:102 opus/48000
a=fmtp:102 maxplaybackrate=16000;sprop-maxcapturerate=16000;maxcodedaudiobandwidth=16000
a=rtpmap:97 iLBC/8000
a=fmtp:97 mode=20
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
a=sendrecv
m=video 0 RTP/AVP 99 //视频接收端口为0
c=IN IP4 192.168.1.74
b=TIAS:3000000
a=rtpmap:99 H264/90000
a=fmtp:99 profile-level-id=42801e;max-br=3000;packetization-mode=1
a=sendrecv
200-OK消息:
SIP/2.0 200 OK
Via: SIP/2.0/UDP 192.168.1.74:5060;rport=5060;branch=z9hG4bKPjp0vl88Onf20ksGJhJa.YPMabZxhe90sZ
From: "1001" sip:1001@192.168.1.248;tag=bUW2K4dpkGZGInsDmLyey16kd4naQzzA
To: sip:1002@192.168.1.248;tag=r9X9UXUNKU1rj
Call-ID: kpf4FQxDfU6FS77XB.VRIwmHS4fH56T1
CSeq: 3809 INVITE
Contact: sip:1002@192.168.1.248:5060;transport=udp
User-Agent: FreeSWITCH-mod_sofia/1.10.10-dev+git~20230407T063007Z~be3a73af62~64bit
Accept: application/sdp
Allow: INVITE, ACK, BYE, CANCEL, OPTIONS, MESSAGE, INFO, UPDATE, REGISTER, REFER, NOTIFY, PUBLISH, SUBSCRIBE
Supported: timer, path, replaces
Content-Type: application/sdp
Content-Disposition: session
Content-Length: 240
v=0
o=- 3900455104 3900455108 IN IP4 192.168.1.222
s=SDP
c=IN IP4 192.168.1.222
t=0 0
m=audio 10008 RTP/AVP 0 101
c=IN IP4 192.168.1.222
a=rtpmap:0 PCMU/8000
a=rtpmap:101 telephone-event/8000
a=fmtp:101 0-15
m=video 0 RTP/AVP 19 //视频接收端口为0
三、关键说明
- 转视频信令实现方式有以下几种:
1. 将视频的接收端口置为0的方式
2. 将视频的媒体属性设置为inactive
html
m=video 10086 RTP/AVP 99
c=IN IP4 192.168.1.74
b=TIAS:3000000
a=rtpmap:99 H264/90000
a=fmtp:99 profile-level-id=42801e;max-br=3000;packetization-mode=1
a=inactive
3. 将视频的媒体接收地址置为0.0.0.0
html
m=video 10086 RTP/AVP 99
c=IN IP4 0.0.0.0
b=TIAS:3000000
a=rtpmap:99 H264/90000
a=fmtp:99 profile-level-id=42801e;max-br=3000;packetization-mode=1
a=inactive
4. 去除视频参数,只保留音频参数
- 不同的服务器实现方式可能不同