RTPSession
對于大多數的RTP應用程序,RTPSession類可能是JRTPLIB唯一使用的類。它能完全處理RTCP部份的數據包,所以用戶可以把精力集中在真正的數據收發。
要知道RTPSession類在多線程下并不是安全的,因此,用戶要通過某些鎖同步機制來保證不會出現在不同線程當中調用同一個RTPSession實例。
RTPSession類有如下的接口。
• RTPSession(RTPTransmitter::TransmissionProtocol proto = RTPTransmitter::IPv4UDPProto)
使用proto類型傳輸層創建一個PRTSession實例。如果proto使用用戶自定義(user-defined)傳輸層,則相應的NewUserDefinedTransmitter()函數必須實現。
ps:這里默認就行了,默認就是IPV4網絡。 • int Create(const RTPSessionParams &sessparams, const RTPTransmissionParams*transparams = 0)
使用RTPSession參數sessparams和RTPTransmission參數transparams 真正創建一個RTP會話。如果transparams 為NULL,則使用默認的參數。
ps:RTPSessionParams 我們可能要設得比較多,RTPTransmissionParams參數就只要設置其中的端口就行了,端口一定要設對,不然進行組播時,這個進程將不接收數據。設置方式可以看example.cpp。
• void Destroy()
離開一個會話但不向其它組成員發送BYE包。
ps:我不推薦用這個函數除非是錯誤處理,正常離開我們應該用ByeDestroy()。
• void BYEDestroy(const RTPTime &maxwaittime, const void *reason,size t reasonlength)
發送一個BYE包并且離開會話。在發送BYE包前等待maxwaittime,如果超時,會不發送BYE包直接離開,BYE包會包含你的離開原因reason。相應的reasonlength表示reason長度。
ps:因為BYE包是一個RTCP包,RTCP不是要發就發的,它的發送時間是為了平衡帶寬通過計算得出來的,那就很有可能到了要發的時候以經超過了maxwaittime時間了,作者可能認一直保留個這會話這么久沒意義。當然,我常常把maxwaittime設得很大。
• bool IsActive()
看看這個RTPSession實例是否以經通過Create建立了真實的會話。
• uint32 t GetLocalSSRC()
返回我們的SSRC。
ps:至于什么是SSRC,去看看RFC3550吧。我說過JRTPLIB只是RTP協議的包裝,并沒有做任何應用的事情。 • int AddDestination(const RTPAddress &addr)
添加一個發送目標。
ps: 當然,如果我們使用組播,這里只用調用一次,把我們的組播地址寫進去。這樣,這組的全部人都能收到你發的包。但是組播可因特網的上設置很煩。而且用組播測試也很煩(組播必須BIND一個端口,如果你想在同一臺機器上運行兩個軟件實例來沒試,你就會發現同一個端口BIND兩次,當然,后面那次會失敗,也就是說測試不了,要測?找兩臺機器,或用虛擬機
),如果組播不滿足,我們就要把組播變在單播,這時就要返復調用這個函數把其它組成員的IP都加進來了。具體可以看看example3.cpp。
• int DeleteDestination(const RTPAddress &addr)
從發送地址列表中刪除一下地址。
• void ClearDestinations()
清除發送地址列表。
• bool SupportsMulticasting()
返回JRTPLIB是否支持組播。ps:這里指JRTPLIB本身,不是你的真實網絡。編譯JRTPLIB庫時可能指定。
.
• int JoinMulticastGroup(const RTPAddress &addr)
加入一個組播組addr。
• int LeaveMulticastGroup(const RTPAddress &addr)
離開一個組播組addr。
• void LeaveAllMulticastGroups()
離開所有組播組。
ps:我們可以同時加入多個組播組。.
• 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個函數都是發送數據包的,我想如果你沒有看RTP協議,我說了你也暈。如果你RTP協議看了,再看看RTPSession.h的注識,你就懂了。 • int SetDefaultPayloadType(uint8 t pt)
設定默認的RTP PayloadType為PT。
ps:和上面的第一個和第三個發送函數配套。至于應該設個什么數,如果你看BAIDU上亂七八糟的文章,當然的亂設就可能了。其實應該按RFC3551,根據你要傳輸的媒體類型來設。 • int SetDefaultMark(bool m)
這設RTP數據包的Mark標識。
ps:設為什么值好?這個,呵呵,連RFC3550也不能確定了。要看具體的RTP Payload規范,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)
設置默認的時間戳的增量。
ps:也是和上的第一和第三個函數配套的。每發一個RTP數據包timestamp就會自動增加
• int IncrementTimestamp(uint32 t inc)
這個函數用來手工增加Timestamp。有時我這很好用,例如,一個RTP數據包因為只含有靜音數據,我們沒有發送,這是我們就應手工增加Timestamp以便發下一個RTP數據包時它的Timestamp是正確的。
• int IncrementTimestampDefault()
這個函數用于增加由SetDefaultTimestampIncrement設定的值。有時候這很有用,例如,一個RTP數據包因為只含有靜音數據,我們沒有發送。這時,這個函數就會被調用用來設置Timestamp以便下一個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()
下面的函數(直到EndDataAccess)要在BeginDataAccess 和EndDataAccess之間被調用,BeginDataAccess確保輪詢(poll)線程不會在這期間訪問source table 。EndDataAccess 調用完成后,輪詢(poll)線程會得到鎖而繼續訪問。
ps:首先,你里的source table中的每一個source表示參與會議中的每一個參與者的每一個獨立的媒體流。我們會在下面用到他們,但同時,poll線程也會輪詢它們以正確處理和RTCP有關的內容。
• bool GotoFirstSource()
開始遞歸參與者的第一個流,如果找到了,就返回tree,否則返回false。
ps:我們通過這個函數和下面的GotoNextSource遍歷source table中的每一個source。
• bool GotoNextSource()
設置當前的源(source)為source table中的下一個源。如果已經到尾部了就返回false.
• bool GotoPreviousSource()
設置當前的源(source)為source table中上一個源。如果已經到頭部了就返回false.
• bool GotoFirstSourceWithData()
開始遞歸參與者中第一個有RTP數據的流,如果找到了,就返回tree,否則返回false。
PS:在接收數據是我們常用的是這套函數,因為如果沒有數據要來都沒用。
• bool GotoNextSourceWithData()
設置當前的源(source)為source table中有RTP數據的下一個源。如果已經到尾部了就返回false.
• bool GotoPreviousSourceWithData()
設置當前的源(source)為source table中有RTP數據的上一個源。如果已經到頭部了就返回false.
• RTPSourceData *GetCurrentSourceInfo()
返回當前參與者的當前源(source)的RTPSourceData 實列。
ps:返回的這個RTPSourceData 就是本進程從期它參與者的RTCP數據包中收集得到的信息,對我們來說其實很有用,只是作者的例程沒有用上,國內的網絡也沒有提到。在RFC3550中有關RTCP的東西都在這了,看過RFC3550的人都知到,里頭談得最多的就是RTCP。這個類我們以后會專門說。
• RTPSourceData *GetSourceInfo(uint32 t ssrc)
返回由ssrc指定的RTPSourceData ,或都NULL(當這個條目不存在)。
ps:這個函數也很有用。因為GetCurrentSourceInfo只有在GotoFirstSource等上下文當中才能用。如果我們是在RTPSource子類的成員函數中,我們沒有這個上下文,就只能用這個函數。 • RTPPacket *GetNextPacket()
得到當前參與者當前媒體流的下一個RTP數據包。
• int EndDataAccess()
請看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(我們不用理這個內部的東西)會檢查是否有其它(non-CNAME)SDES項目要發送。如果count為零或負數,則不發送,如果count為正數,則在sources table處理count次后會把SDES name item加到當前RTCP包中。
ps: 其實每次處理sources table都會伴隨都SDES RTCP數據包的發送,在這個數據包當中CNAME是必須的,但其它的項目不是必須的,這就函數確定了NAME項目發送的頻度,如果為1,則表不每個 SDES RTCP數據包都帶著它,如果為2則每兩個SDES數據包就發送一次NAME項目,下面的SetEMailInterval、 SetLocationInterval、SetPhoneInterval、SetToolInterval、SetNoteInterval都是同一原理。關于這個ITEM的描述,請看RFC3550.老版本的JRTPLIB沒有使用這套函數,而是用EnableSendName()等函數。
• 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)
設置NAME SDES項目,以遍會議的其它人員看到你的名稱。下同。
• 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數據包來到,會調用這個函數處理。
ps:這個函數在我們繼承RTPSession類時很可能重載,這是獲取RTP數據包除了上面所說的方法以外的另外一種方法,這個方法比較適合異步的情況。默認這個是一個空虛函數。除了這個函數以外,下面的幾個函數了會經常重載。 • 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)
當有一個新的條目加到source table時,調用這個函數。
ps: 這也是一個比較重要的函數,因為這意味著很有可能有一個新的與會者加入。但令我很不高興的是,這時候的RTPSourceData 里頭的CNAME和NAME等字段都還是無效的,這不是RTCP的責任,因為在這個SDES RTCP數據包中所有的信息都以經有了(通過抓包證實了這一點)。我們的函數被調用后,需要延時一會才能得到有關這個Source的CNAME和NAME 等相關的信息。當然,如果你不想軟件死掉,不能在這個函數體內以阻塞的方式延時。
• void OnRemoveSource(RTPSourceData *srcdat)
當有一個條目從source table中移除時調用這個函數。
ps:這通常意味著有一個與會者離開了,和OnNewSource不一樣,這時的CNAME和NAME等都是有效的。用這個函數要注意,我們的“意味著兩個字” 因為“加入”的可能不是一個新的與會者,而是一個現有與會者的一個新的媒體流。“離開”的也可能不是一個與會者,而只是其中一個與會者的其中一個媒體流,這兩個函數只能給我們更新與會者提供一個觸發條件而已。當OnNewSource調用時,我們要看看這個CNAME是不是以經在我們與會者名單中,如果不是,那就是一個新與會者。同時,如果OnRemoveSource被調用,則我們要看看這個CNAME的與會者還有沒有其它的Source,如果沒有了,這個與會者才是真正離開。這么很麻煩??那就對了,那就是現在的H323和SIP要做的事情--會話管理。
• 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.
接收者報告(RR)
V| P| RC| PT=SR=201| LEN|
SSRC1(第一個接收者報告塊所關聯的發送者) (已關聯)
分組丟失率 | 丟失分組總數|
擴展的最高序號
間隔抖動
最新的發送者報告時間戳(LSR)
SR最新間隔(DLSR)
附加信息:
這個源是否有發送接收者報告
接收者報告接收時間
以及以RR_Prev_開頭的,獲得倒數第二個接收者報告的信息。
|
posted @
2009-06-26 13:13 黑色天使 閱讀(1740) |
評論 (0) |
編輯 收藏