@Override
public void ntsOnInviteTalk(String deviceId, SessionDescription sessionDescription) {
handler_.postDelayed(new Runnable() {
@Override
public void run() {
gb28181_agent_.respondTalkInvite(180, device_id_);
MediaSessionDescription audio_description = null;
SDPRtpMapAttribute rtp_map_attribute = null;
Vector<MediaSessionDescription> audio_des_list = session_description_.getAudioDescriptions();
if (audio_des_list != null && !audio_des_list.isEmpty()) {
for(MediaSessionDescription m : audio_des_list) {
if (m != null && m.isValidAddressType() && m.isHasAddress()) {
rtp_map_attribute = m.getRtpMapAttribute(SDPRtpMapAttribute.PCMA_ENCODING_NAME);
if (rtp_map_attribute != null) {
audio_description = m;
break;
}
}
}
if (null == rtp_map_attribute) {
for(MediaSessionDescription m : audio_des_list) {
if (m != null && m.isValidAddressType() && m.isHasAddress()) {
rtp_map_attribute = m.getRtpMapAttribute(SDPRtpMapAttribute.PS_ENCODING_NAME);
if (rtp_map_attribute != null) {
audio_description = m;
break;
}
}
}
}
}
if (null == audio_description) {
gb28181_agent_.respondTalkInvite(488, device_id_);
Log.i(TAG, "ntsOnInviteTalk get audio description is null, response 488, device_id:" + device_id_);
return;
}
if (null == rtp_map_attribute ) {
gb28181_agent_.respondTalkInvite(488, device_id_);
Log.i(TAG, "ntsOnInviteTalk get rtp map attribute is null, response 488, device_id:" + device_id_);
return;
}
Log.i(TAG,"ntsOnInviteTalk, device_id:" +device_id_+", is_tcp:" + audio_description.isRTPOverTCP()
+ " rtp_port:" + audio_description.getPort() + " ssrc:" + audio_description.getSSRC()
+ " address_type:" + audio_description.getAddressType() + " address:" + audio_description.getAddress()
+ " payload_type:" + rtp_map_attribute.getPayloadType() + " encoding_name:" + rtp_map_attribute.getEncodingName());
long rtp_sender_handle = libPublisher.CreateRTPSender(0);
if (0 == rtp_sender_handle) {
gb28181_agent_.respondTalkInvite(488, device_id_);
Log.i(TAG, "ntsOnInviteTalk CreateRTPSender failed, response 488, device_id:" + device_id_);
return;
}
gb_talk_rtp_payload_type_ = rtp_map_attribute.getPayloadType();
gb_talk_rtp_encoding_name_ = rtp_map_attribute.getEncodingName();
libPublisher.SetRTPSenderTransportProtocol(rtp_sender_handle, audio_description.isRTPOverUDP()?0:1);
libPublisher.SetRTPSenderIPAddressType(rtp_sender_handle, audio_description.isIPv4()?0:1);
libPublisher.SetRTPSenderLocalPort(rtp_sender_handle, 0);
libPublisher.SetRTPSenderSSRC(rtp_sender_handle, audio_description.getSSRC());
libPublisher.SetRTPSenderSocketSendBuffer(rtp_sender_handle, 256*1024); // 音频配置到256KB
libPublisher.SetRTPSenderClockRate(rtp_sender_handle, rtp_map_attribute.getClockRate());
libPublisher.SetRTPSenderDestination(rtp_sender_handle, audio_description.getAddress(), audio_description.getPort());
gb_talk_is_receive_ = audio_description.isHasAttribute("sendrecv");
if (gb_talk_is_receive_) {
libPublisher.EnableRTPSenderReceive(rtp_sender_handle, 1);
// libPublisher.SetRTPSenderReceiveSSRC(rtp_sender_handle, audio_description.getSSRC());
libPublisher.SetRTPSenderReceivePayloadType(rtp_sender_handle, gb_talk_rtp_payload_type_, gb_talk_rtp_encoding_name_, 2, rtp_map_attribute.getClockRate());
// 目前发现某些平台 PS-PCMA 是8000, 不建议设置
//if (gb_talk_rtp_encoding_name_.equals("PS")) {
// libPublisher.SetRTPSenderReceivePSClockFrequency(rtp_sender_handle, 8000);
// }
// 如果是PCMA编码, 采样率和通道可以先不设置
// libPublisher.SetRTPSenderReceiveAudioSamplingRate(rtp_sender_handle, 8000);
// libPublisher.SetRTPSenderReceiveAudioChannels(rtp_sender_handle, 1);
}
if (libPublisher.InitRTPSender(rtp_sender_handle) != 0 ) {
gb28181_agent_.respondTalkInvite(488, device_id_);
libPublisher.DestoryRTPSender(rtp_sender_handle);
return;
}
int local_port = libPublisher.GetRTPSenderLocalPort(rtp_sender_handle);
if (0==local_port) {
gb28181_agent_.respondTalkInvite(488, device_id_);
libPublisher.DestoryRTPSender(rtp_sender_handle);
return;
}
Log.i(TAG,"ntsOnInviteTalk get local_port:" + local_port);
String local_ip_addr = IPAddrUtils.getIpAddress(context_);
MediaSessionDescription main_local_audio_des = new MediaSessionDescription(audio_description.getType());
main_local_audio_des.addFormat(String.valueOf(rtp_map_attribute.getPayloadType()));
main_local_audio_des.addRtpMapAttribute(rtp_map_attribute);
main_local_audio_des.addAttribute(new SDPAttribute("sendonly"));
if (audio_description.isRTPOverTCP()) {
// tcp主动链接服务端
main_local_audio_des.addAttribute(new SDPAttribute("setup", "active"));
main_local_audio_des.addAttribute(new SDPAttribute("connection", "new"));
}
main_local_audio_des.setPort(local_port);
main_local_audio_des.setTransportProtocol(audio_description.getTransportProtocol());
main_local_audio_des.setSSRC(audio_description.getSSRC());
MediaSessionDescription sub_local_audio_des = null;
if (gb_talk_is_receive_) {
sub_local_audio_des = new MediaSessionDescription(audio_description.getType());
sub_local_audio_des.addFormat(String.valueOf(rtp_map_attribute.getPayloadType()));
sub_local_audio_des.addRtpMapAttribute(rtp_map_attribute);
sub_local_audio_des.addAttribute(new SDPAttribute("recvonly"));
if (audio_description.isRTPOverTCP()) {
// tcp主动链接服务端
sub_local_audio_des.addAttribute(new SDPAttribute("setup", "active"));
sub_local_audio_des.addAttribute(new SDPAttribute("connection", "new"));
}
sub_local_audio_des.setPort(local_port);
sub_local_audio_des.setTransportProtocol(audio_description.getTransportProtocol());
sub_local_audio_des.setSSRC(audio_description.getSSRC());
}
if (!gb28181_agent_.respondTalkInviteOK(device_id_, audio_description.getAddressType(), local_ip_addr, main_local_audio_des, sub_local_audio_des) ) {
libPublisher.DestoryRTPSender(rtp_sender_handle);
Log.e(TAG, "ntsOnInviteTalk call respondPlayInviteOK failed.");
return;
}
gb_talk_rtp_sender_handle_ = rtp_sender_handle;
}
private String device_id_;
private SessionDescription session_description_;
public Runnable set(String device_id, SessionDescription session_des) {
this.device_id_ = device_id;
this.session_description_ = session_des;
return this;
}
}.set(deviceId, sessionDescription),0);
}