0
votes

I am Developing a VOIP softphone, I need to put RTP port number in SDP part in my INVITE Request. how can I find a free UDP port number to accept RTP packets. I have found 2 solutions but don't know if they are correct way to do this.

Solution 1 : start from a UDP port number (say 7000) and see if its free , if not increase by 1 and continue until a free port is found. then open a UDP socket on that port , so that other calls can't choose my calls RTP port. then send the request.

Solution 2 : start from a UDP port number (say 7000) and see if it's free, put it in SDP and send the request. but when I get OK response from other party (after a while), there is no guarantee that the port number I announced for RTP is still available. maybe other call has captured that.

I would like to know what is the best way to do this.

2

2 Answers

0
votes

Solution 1 is the only way to reserve a port number within a specific range of port.

If you do not care about being close to a specific port number, just open a port with value 0 in order to get a random port which will of course be free. Then, retrieve the real opened port with socket's API and use it in your sdp!

0
votes

As AymericM suggested, you should stick to your solution 1.

You need to use the bind call to bind a socket to a port.

Additionally, the RTP specification states that the RTP port should typically be even, with the RTCP port being the rtp_port + 1.

For UDP and similar protocols, RTP SHOULD use an even destination port number and the corresponding RTCP stream SHOULD use the next higher (odd) destination port number.

Even in the case where you support RTP/RTCP multiplexing over a single port, the answerer might not, so it might be a good idea to bind both the RTP and RTCP ports when generating the offer.

So to summarise, try to bind two consecutive ports starting on an even number and once you've found two suitable ports, generate the offer/INVITE.