RTPSession
對(duì)于大多數(shù)的RTP應(yīng)用程序,RTPSession類可能是JRTPLIB唯一使用的類。它能完全處理RTCP部份的數(shù)據(jù)包,所以用戶可以把精力集中在真正的數(shù)據(jù)收發(fā)。
要知道RTPSession類在多線程下并不是安全的,因此,用戶要通過某些鎖同步機(jī)制來保證不會(huì)出現(xiàn)在不同線程當(dāng)中調(diào)用同一個(gè)RTPSession實(shí)例。
RTPSession類有如下的接口。
• RTPSession(RTPTransmitter::TransmissionProtocol proto = RTPTransmitter::IPv4UDPProto)
使用proto類型傳輸層創(chuàng)建一個(gè)PRTSession實(shí)例。如果proto使用用戶自定義(user-defined)傳輸層,則相應(yīng)的NewUserDefinedTransmitter()函數(shù)必須實(shí)現(xiàn)。
ps:這里默認(rèn)就行了,默認(rèn)就是IPV4網(wǎng)絡(luò)。 • int Create(const RTPSessionParams &sessparams, const RTPTransmissionParams*transparams = 0)
使用RTPSession參數(shù)sessparams和RTPTransmission參數(shù)transparams 真正創(chuàng)建一個(gè)RTP會(huì)話。如果transparams 為NULL,則使用默認(rèn)的參數(shù)。
ps:RTPSessionParams 我們可能要設(shè)得比較多,RTPTransmissionParams參數(shù)就只要設(shè)置其中的端口就行了,端口一定要設(shè)對(duì),不然進(jìn)行組播時(shí),這個(gè)進(jìn)程將不接收數(shù)據(jù)。設(shè)置方式可以看example.cpp。
• void Destroy()
離開一個(gè)會(huì)話但不向其它組成員發(fā)送BYE包。
ps:我不推薦用這個(gè)函數(shù)除非是錯(cuò)誤處理,正常離開我們應(yīng)該用ByeDestroy()。
• void BYEDestroy(const RTPTime &maxwaittime, const void *reason,size t reasonlength)
發(fā)送一個(gè)BYE包并且離開會(huì)話。在發(fā)送BYE包前等待maxwaittime,如果超時(shí),會(huì)不發(fā)送BYE包直接離開,BYE包會(huì)包含你的離開原因reason。相應(yīng)的reasonlength表示reason長度。
ps:因?yàn)锽YE包是一個(gè)RTCP包,RTCP不是要發(fā)就發(fā)的,它的發(fā)送時(shí)間是為了平衡帶寬通過計(jì)算得出來的,那就很有可能到了要發(fā)的時(shí)候以經(jīng)超過了maxwaittime時(shí)間了,作者可能認(rèn)一直保留個(gè)這會(huì)話這么久沒意義。當(dāng)然,我常常把maxwaittime設(shè)得很大。
• bool IsActive()
看看這個(gè)RTPSession實(shí)例是否以經(jīng)通過Create建立了真實(shí)的會(huì)話。
• uint32 t GetLocalSSRC()
返回我們的SSRC。
ps:至于什么是SSRC,去看看RFC3550吧。我說過JRTPLIB只是RTP協(xié)議的包裝,并沒有做任何應(yīng)用的事情。 • int AddDestination(const RTPAddress &addr)
添加一個(gè)發(fā)送目標(biāo)。
ps: 當(dāng)然,如果我們使用組播,這里只用調(diào)用一次,把我們的組播地址寫進(jìn)去。這樣,這組的全部人都能收到你發(fā)的包。但是組播可因特網(wǎng)的上設(shè)置很煩。而且用組播測試也很煩(組播必須BIND一個(gè)端口,如果你想在同一臺(tái)機(jī)器上運(yùn)行兩個(gè)軟件實(shí)例來沒試,你就會(huì)發(fā)現(xiàn)同一個(gè)端口BIND兩次,當(dāng)然,后面那次會(huì)失敗,也就是說測試不了,要測?找兩臺(tái)機(jī)器,或用虛擬機(jī)
),如果組播不滿足,我們就要把組播變?cè)趩尾ィ@時(shí)就要返復(fù)調(diào)用這個(gè)函數(shù)把其它組成員的IP都加進(jìn)來了。具體可以看看example3.cpp。
• int DeleteDestination(const RTPAddress &addr)
從發(fā)送地址列表中刪除一下地址。
• void ClearDestinations()
清除發(fā)送地址列表。
• bool SupportsMulticasting()
返回JRTPLIB是否支持組播。ps:這里指JRTPLIB本身,不是你的真實(shí)網(wǎng)絡(luò)。編譯JRTPLIB庫時(shí)可能指定。
.
• int JoinMulticastGroup(const RTPAddress &addr)
加入一個(gè)組播組addr。
• int LeaveMulticastGroup(const RTPAddress &addr)
離開一個(gè)組播組addr。
• void LeaveAllMulticastGroups()
離開所有組播組。
ps:我們可以同時(shí)加入多個(gè)組播組。.
• int SendPacket(const void *data, size t len)
• int SendPacket(const void *data, size t len, uint8 t pt, bool mark,uint32 t timestampinc)
• int SendPacketEx(const void *data, size t len, uint16 t hdrextID,const void *hdrextdata, size t numhdrextwords)
• int SendPacketEx(const void *data, size t len, uint8 t pt, boolmark, uint32 t timestampinc, uint16 t hdrextID, const void *hdrextdata,size t numhdrextwords)
上面的4個(gè)函數(shù)都是發(fā)送數(shù)據(jù)包的,我想如果你沒有看RTP協(xié)議,我說了你也暈。如果你RTP協(xié)議看了,再看看RTPSession.h的注識(shí),你就懂了。 • int SetDefaultPayloadType(uint8 t pt)
設(shè)定默認(rèn)的RTP PayloadType為PT。
ps:和上面的第一個(gè)和第三個(gè)發(fā)送函數(shù)配套。至于應(yīng)該設(shè)個(gè)什么數(shù),如果你看BAIDU上亂七八糟的文章,當(dāng)然的亂設(shè)就可能了。其實(shí)應(yīng)該按RFC3551,根據(jù)你要傳輸?shù)拿襟w類型來設(shè)。 • int SetDefaultMark(bool m)
這設(shè)RTP數(shù)據(jù)包的Mark標(biāo)識(shí)。
ps:設(shè)為什么值好?這個(gè),呵呵,連RFC3550也不能確定了。要看具體的RTP Payload規(guī)范,MPEG的,H263的都不一樣。
MPEG2 www.ietf.org/rfc/rfc2250.txt
MPEG4 www.rfc-editor.org/rfc/rfc3016.txt
H263 www.ietf.org/rfc/rfc2190.txt
• int SetDefaultTimestampIncrement(uint32 t timestampinc)
設(shè)置默認(rèn)的時(shí)間戳的增量。
ps:也是和上的第一和第三個(gè)函數(shù)配套的。每發(fā)一個(gè)RTP數(shù)據(jù)包timestamp就會(huì)自動(dòng)增加
• int IncrementTimestamp(uint32 t inc)
這個(gè)函數(shù)用來手工增加Timestamp。有時(shí)我這很好用,例如,一個(gè)RTP數(shù)據(jù)包因?yàn)橹缓徐o音數(shù)據(jù),我們沒有發(fā)送,這是我們就應(yīng)手工增加Timestamp以便發(fā)下一個(gè)RTP數(shù)據(jù)包時(shí)它的Timestamp是正確的。
• int IncrementTimestampDefault()
這個(gè)函數(shù)用于增加由SetDefaultTimestampIncrement設(shè)定的值。有時(shí)候這很有用,例如,一個(gè)RTP數(shù)據(jù)包因?yàn)橹缓徐o音數(shù)據(jù),我們沒有發(fā)送。這時(shí),這個(gè)函數(shù)就會(huì)被調(diào)用用來設(shè)置Timestamp以便下一個(gè)RTP包的Timestamp是正確的。
• int SetPreTransmissionDelay(const RTPTime &delay)
This function allows you to inform the library about the delay between
sampling the first sample of a packet and sending the packet. This delay is
taken into account when calculating the relation between RTP timestamp
and wallclock time, used for inter-media synchronization.
• RTPTransmissionInfo *GetTransmissionInfo()
This function returns an instance of a subclass of RTPTransmissionInfo
which will give some additional information about the transmitter (a list
of local IP addresses for example). The user has to delete the returned
instance when it is no longer needed.
• int Poll()
If you’re not using the poll thread, this function must be called regularly
to process incoming data and to send RTCP data when necessary.
61
• int WaitForIncomingData(const RTPTime &delay,bool *dataavailable= 0)
Waits at most a time delay until incoming data has been detected. Only
works when you’re not using the poll thread. If dataavailable is not NULL,
it should be set to true if data was actually read and to false otherwise.
• int AbortWait()
If the previous function has been called, this one aborts the waiting. Only
works when you’re not using the poll thread.
• RTPTime GetRTCPDelay()
Returns the time interval after which an RTCP compound packet may have
to be sent. Only works when you’re not using the poll thread.
• int BeginDataAccess()
下面的函數(shù)(直到EndDataAccess)要在BeginDataAccess 和EndDataAccess之間被調(diào)用,BeginDataAccess確保輪詢(poll)線程不會(huì)在這期間訪問source table 。EndDataAccess 調(diào)用完成后,輪詢(poll)線程會(huì)得到鎖而繼續(xù)訪問。
ps:首先,你里的source table中的每一個(gè)source表示參與會(huì)議中的每一個(gè)參與者的每一個(gè)獨(dú)立的媒體流。我們會(huì)在下面用到他們,但同時(shí),poll線程也會(huì)輪詢它們以正確處理和RTCP有關(guān)的內(nèi)容。
• bool GotoFirstSource()
開始遞歸參與者的第一個(gè)流,如果找到了,就返回tree,否則返回false。
ps:我們通過這個(gè)函數(shù)和下面的GotoNextSource遍歷source table中的每一個(gè)source。
• bool GotoNextSource()
設(shè)置當(dāng)前的源(source)為source table中的下一個(gè)源。如果已經(jīng)到尾部了就返回false.
• bool GotoPreviousSource()
設(shè)置當(dāng)前的源(source)為source table中上一個(gè)源。如果已經(jīng)到頭部了就返回false.
• bool GotoFirstSourceWithData()
開始遞歸參與者中第一個(gè)有RTP數(shù)據(jù)的流,如果找到了,就返回tree,否則返回false。
PS:在接收數(shù)據(jù)是我們常用的是這套函數(shù),因?yàn)槿绻麤]有數(shù)據(jù)要來都沒用。
• bool GotoNextSourceWithData()
設(shè)置當(dāng)前的源(source)為source table中有RTP數(shù)據(jù)的下一個(gè)源。如果已經(jīng)到尾部了就返回false.
• bool GotoPreviousSourceWithData()
設(shè)置當(dāng)前的源(source)為source table中有RTP數(shù)據(jù)的上一個(gè)源。如果已經(jīng)到頭部了就返回false.
• RTPSourceData *GetCurrentSourceInfo()
返回當(dāng)前參與者的當(dāng)前源(source)的RTPSourceData 實(shí)列。
ps:返回的這個(gè)RTPSourceData 就是本進(jìn)程從期它參與者的RTCP數(shù)據(jù)包中收集得到的信息,對(duì)我們來說其實(shí)很有用,只是作者的例程沒有用上,國內(nèi)的網(wǎng)絡(luò)也沒有提到。在RFC3550中有關(guān)RTCP的東西都在這了,看過RFC3550的人都知到,里頭談得最多的就是RTCP。這個(gè)類我們以后會(huì)專門說。
• RTPSourceData *GetSourceInfo(uint32 t ssrc)
返回由ssrc指定的RTPSourceData ,或都NULL(當(dāng)這個(gè)條目不存在)。
ps:這個(gè)函數(shù)也很有用。因?yàn)镚etCurrentSourceInfo只有在GotoFirstSource等上下文當(dāng)中才能用。如果我們是在RTPSource子類的成員函數(shù)中,我們沒有這個(gè)上下文,就只能用這個(gè)函數(shù)。 • RTPPacket *GetNextPacket()
得到當(dāng)前參與者當(dāng)前媒體流的下一個(gè)RTP數(shù)據(jù)包。
• int EndDataAccess()
請(qǐng)看BeginDataAccess
• int SetReceiveMode(RTPTransmitter::ReceiveMode m)
Sets the receive mode to m, which can be one of the following:
– RTPTransmitter::AcceptAll
All incoming data is accepted, no matter where it originated from.
– RTPTransmitter::AcceptSome
Only data coming from specific sources will be accepted.
– RTPTransmitter::IgnoreSome
All incoming data is accepted, except for data coming from a specificset of sources.
Note that when the receive mode is changed, the list of addressed to be ignored or accepted will be cleared.
• int AddToIgnoreList(const RTPAddress &addr)
Adds addr to the list of addresses to ignore.
• int DeleteFromIgnoreList(const RTPAddress &addr)
Deletes addr from the list of addresses to ignore.
• void ClearIgnoreList()
Clears the list of addresses to ignore.
• int AddToAcceptList(const RTPAddress &addr)
Adds addr to the list of addresses to accept.
• int DeleteFromAcceptList(const RTPAddress &addr)
Deletes addr from the list of addresses to accept.
• void ClearAcceptList()
Clears the list of addresses to accept.
• int SetMaximumPacketSize(size t s)
Sets the maximum allowed packet size to s.
• int SetSessionBandwidth(double bw)
Sets the session bandwidth to bw, which is specified in bytes per second.
• int SetTimestampUnit(double u)
Sets our own timestamp unit to u. The timestamp unit is defined as a time
interval divided by the number of samples in that interval: for 8000Hz
audio this would be 1.0/8000.0.
• void SetNameInterval(int count)
在處理source table中的sourcese后,RTCP packet builder(我們不用理這個(gè)內(nèi)部的東西)會(huì)檢查是否有其它(non-CNAME)SDES項(xiàng)目要發(fā)送。如果count為零或負(fù)數(shù),則不發(fā)送,如果count為正數(shù),則在sources table處理count次后會(huì)把SDES name item加到當(dāng)前RTCP包中。
ps: 其實(shí)每次處理sources table都會(huì)伴隨都SDES RTCP數(shù)據(jù)包的發(fā)送,在這個(gè)數(shù)據(jù)包當(dāng)中CNAME是必須的,但其它的項(xiàng)目不是必須的,這就函數(shù)確定了NAME項(xiàng)目發(fā)送的頻度,如果為1,則表不每個(gè) SDES RTCP數(shù)據(jù)包都帶著它,如果為2則每兩個(gè)SDES數(shù)據(jù)包就發(fā)送一次NAME項(xiàng)目,下面的SetEMailInterval、 SetLocationInterval、SetPhoneInterval、SetToolInterval、SetNoteInterval都是同一原理。關(guān)于這個(gè)ITEM的描述,請(qǐng)看RFC3550.老版本的JRTPLIB沒有使用這套函數(shù),而是用EnableSendName()等函數(shù)。
• void SetEMailInterval(int count)
After all possible sources in the source table have been processed, the RTCP
packet builder will check if other (non-CNAME) SDES items need to be
sent. If count is zero or negative, nothing will happen. If count is positive,
an SDES e-mail item will be added after the sources in the source table
have been processed count times.
• void SetLocationInterval(int count)
After all possible sources in the source table have been processed, the RTCP
packet builder will check if other (non-CNAME) SDES items need to be
sent. If count is zero or negative, nothing will happen. If count is positive,
an SDES location item will be added after the sources in the source table
have been processed count times.
• void SetPhoneInterval(int count)
After all possible sources in the source table have been processed, the RTCP
packet builder will check if other (non-CNAME) SDES items need to be
sent. If count is zero or negative, nothing will happen. If count is positive,
an SDES phone item will be added after the sources in the source table
have been processed count times.
• void SetToolInterval(int count)
After all possible sources in the source table have been processed, the RTCP
packet builder will check if other (non-CNAME) SDES items need to be
sent. If count is zero or negative, nothing will happen. If count is positive,
an SDES tool item will be added after the sources in the source table have
been processed count times.
• void SetNoteInterval(int count)
After all possible sources in the source table have been processed, the RTCP
packet builder will check if other (non-CNAME) SDES items need to be
sent. If count is zero or negative, nothing will happen. If count is positive,
an SDES note item will be added after the sources in the source table have
been processed count times.
• int SetLocalName(const void *s, size t len)
設(shè)置NAME SDES項(xiàng)目,以遍會(huì)議的其它人員看到你的名稱。下同。
• int SetLocalEMail(const void *s, size t len)
Sets the SDES e-mail item for the local participant to the value s with
length len.
• int SetLocalLocation(const void *s, size t len)
Sets the SDES location item for the local participant to the value s with
length len.
• int SetLocalPhone(const void *s, size t len)
Sets the SDES phone item for the local participant to the value s with
length len.
• int SetLocalTool(const void *s, size t len)
Sets the SDES tool item for the local participant to the value s with length
len.
• int SetLocalNote(const void *s, size t len)
Sets the SDES note item for the local participant to the value s with length
len.
In case you specified in the constructor that you want to use your own transmission
component, you should override the following function:
• RTPTransmitter *NewUserDefinedTransmitter()
The RTPTransmitter instance returned by this function will then be used to send
and receive RTP and RTCP packets. Note that when the session is destroyed,
this RTPTransmitter instance will be destroyed with a delete call.
By inheriting your own class from RTPSession and overriding one or more of the
functions below, certain events can be detected:
• void OnRTPPacket(RTPPacket *pack, const RTPTime &receivetime, const RTPAddress *senderaddress)
如果有RTPPacket數(shù)據(jù)包來到,會(huì)調(diào)用這個(gè)函數(shù)處理。
ps:這個(gè)函數(shù)在我們繼承RTPSession類時(shí)很可能重載,這是獲取RTP數(shù)據(jù)包除了上面所說的方法以外的另外一種方法,這個(gè)方法比較適合異步的情況。默認(rèn)這個(gè)是一個(gè)空虛函數(shù)。除了這個(gè)函數(shù)以外,下面的幾個(gè)函數(shù)了會(huì)經(jīng)常重載。 • void OnRTCPCompoundPacket(RTCPCompoundPacket *pack, const RTPTime &receivetime, const RTPAddress *senderaddress)
Is called when an incoming RTCP packet is about to be processed.
• void OnSSRCCollision(RTPSourceData *srcdat, const RTPAddress *senderaddress, bool isrtp)
Is called when an SSRC collision was detected. The instance srcdat is the
one present in the table, the address senderaddress is the one that collided
with one of the addresses and isrtp indicates against which address
of srcdat the check failed.
• void OnCNAMECollision(RTPSourceData *srcdat, const RTPAddress *senderaddress, const uint8 t *cname, size t cnamelength)
Is called when another CNAME was received than the one already present for source srcdat.
• void OnNewSource(RTPSourceData *srcdat)
當(dāng)有一個(gè)新的條目加到source table時(shí),調(diào)用這個(gè)函數(shù)。
ps: 這也是一個(gè)比較重要的函數(shù),因?yàn)檫@意味著很有可能有一個(gè)新的與會(huì)者加入。但令我很不高興的是,這時(shí)候的RTPSourceData 里頭的CNAME和NAME等字段都還是無效的,這不是RTCP的責(zé)任,因?yàn)樵谶@個(gè)SDES RTCP數(shù)據(jù)包中所有的信息都以經(jīng)有了(通過抓包證實(shí)了這一點(diǎn))。我們的函數(shù)被調(diào)用后,需要延時(shí)一會(huì)才能得到有關(guān)這個(gè)Source的CNAME和NAME 等相關(guān)的信息。當(dāng)然,如果你不想軟件死掉,不能在這個(gè)函數(shù)體內(nèi)以阻塞的方式延時(shí)。
• void OnRemoveSource(RTPSourceData *srcdat)
當(dāng)有一個(gè)條目從source table中移除時(shí)調(diào)用這個(gè)函數(shù)。
ps:這通常意味著有一個(gè)與會(huì)者離開了,和OnNewSource不一樣,這時(shí)的CNAME和NAME等都是有效的。用這個(gè)函數(shù)要注意,我們的“意味著兩個(gè)字” 因?yàn)?#8220;加入”的可能不是一個(gè)新的與會(huì)者,而是一個(gè)現(xiàn)有與會(huì)者的一個(gè)新的媒體流。“離開”的也可能不是一個(gè)與會(huì)者,而只是其中一個(gè)與會(huì)者的其中一個(gè)媒體流,這兩個(gè)函數(shù)只能給我們更新與會(huì)者提供一個(gè)觸發(fā)條件而已。當(dāng)OnNewSource調(diào)用時(shí),我們要看看這個(gè)CNAME是不是以經(jīng)在我們與會(huì)者名單中,如果不是,那就是一個(gè)新與會(huì)者。同時(shí),如果OnRemoveSource被調(diào)用,則我們要看看這個(gè)CNAME的與會(huì)者還有沒有其它的Source,如果沒有了,這個(gè)與會(huì)者才是真正離開。這么很麻煩??那就對(duì)了,那就是現(xiàn)在的H323和SIP要做的事情--會(huì)話管理。
• void OnTimeout(RTPSourceData *srcdat)
Is called when participant srcdat is timed out.
• void OnBYETimeout(RTPSourceData *srcdat)
Is called when participant srcdat is timed after having sent a BYE packet.
• void OnBYEPacket(RTPSourceData *srcdat)
Is called when a BYE packet has been processed for source srcdat.
• void OnAPPPacket(RTCPAPPPacket *apppacket, const RTPTime &receivetime,
const RTPAddress *senderaddress)
In called when an RTCP APP packet apppacket has been received at time
receivetime from address senderaddress.
• void OnUnknownPacketType(RTCPPacket *rtcppack, const RTPTime &receivetime,
const RTPAddress *senderaddress)
Is called when an unknown RTCP packet type was detected.
• void OnUnknownPacketFormat(RTCPPacket *rtcppack, const RTPTime &receivetime,
const RTPAddress *senderaddress)
Is called when an unknown packet format for a known packet type was
detected.
• void OnNoteTimeout(RTPSourceData *srcdat)
Is called when the SDES NOTE item for source srcdat has been timed out.
• void OnSendRTCPCompoundPacket(RTCPCompoundPacket *pack)
Is called when an RTCP compound packet has just been sent. Useful to
inspect outgoing RTCP data.
• void OnPollThreadError(int errcode)
Is called when error errcode was detected in the poll thread.
• void OnPollThreadStep()
Is called each time the poll thread loops. This happens when incoming data
was detected or when its time to send an RTCP compound packet.
接收者報(bào)告(RR)
V| P| RC| PT=SR=201| LEN|
SSRC1(第一個(gè)接收者報(bào)告塊所關(guān)聯(lián)的發(fā)送者) (已關(guān)聯(lián))
分組丟失率 | 丟失分組總數(shù)|
擴(kuò)展的最高序號(hào)
間隔抖動(dòng)
最新的發(fā)送者報(bào)告時(shí)間戳(LSR)
SR最新間隔(DLSR)
附加信息:
這個(gè)源是否有發(fā)送接收者報(bào)告
接收者報(bào)告接收時(shí)間
以及以RR_Prev_開頭的,獲得倒數(shù)第二個(gè)接收者報(bào)告的信息。
|