ccRTP
sources.h
Go to the documentation of this file.
1 // Copyright (C) 2001,2002,2003,2004 Federico Montesino Pouzols <fedemp@altern.org>.
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // ccRTP. If you copy code from other releases into a copy of GNU
28 // ccRTP, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU ccRTP, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
44 #ifndef CCXX_RTP_SOURCES_H_
45 #define CCXX_RTP_SOURCES_H_
46 
47 #include <string>
48 #include <ccrtp/rtcppkt.h>
49 #include <cstddef>
50 
51 #ifdef CCXX_NAMESPACES
52 namespace ost {
53 #endif
54 
68 class __EXPORT SDESItemsHolder
69 {
70 public:
71  const std::string&
72  getItem(SDESItemType type) const;
73 
74  inline const std::string&
75  getPRIVPrefix() const
76  { return sdesItems[SDESItemTypeEND]; }
77 
78  void
79  setItem(SDESItemType item, const std::string& val);
80 
81  inline void
82  setPRIVPrefix(const std::string& val)
83  { sdesItems[SDESItemTypeEND] = val; }
84 
85 protected:
87  { }
88 
89  inline virtual ~SDESItemsHolder()
90  { }
91 
92 private:
93  // SDES items for a participant.
94  // sdesItems[0] (== sdesItems[SDESItemTypeEND]) holds the prefix
95  // value for the PRIV item. The rest of entries hold the
96  // correponding SDES item value.
97  std::string sdesItems[SDESItemTypeLast + 1];
98 };
99 
128 class __EXPORT Participant : private SDESItemsHolder
129 {
130 public:
143  const std::string&
144  getSDESItem(SDESItemType type) const
145  { return SDESItemsHolder::getItem(type); }
146 
154  inline const std::string&
155  getPRIVPrefix() const
156  { return SDESItemsHolder::getPRIVPrefix(); }
157 
163  Participant(const std::string& cname);
164 
165  ~Participant();
166 
167 private:
168  friend class ParticipantHandler;
169 
173  inline void
174  setSDESItem(SDESItemType item, const std::string& val)
175  { SDESItemsHolder::setItem(item,val); }
176 
180  inline void
181  setPRIVPrefix(const std::string val)
183 };
184 
196 class __EXPORT SyncSource
197 {
198 public:
229  typedef enum {
231  statePrevalid,
232 
233 
234  stateActive,
235 
236  stateInactive,
237 
238 
239  stateLeaving
240 
241  } State;
242 
247  SyncSource(uint32 ssrc);
248 
249  ~SyncSource();
250 
251  State
252  getState() const
253  { return state; }
254 
258  bool isSender() const
259  { return activeSender; }
260 
261  uint32 getID() const
262  { return SSRC; }
263 
271  inline Participant*
272  getParticipant() const
273  { return participant; }
274 
275  tpport_t getDataTransportPort() const
276  { return dataTransportPort; }
277 
278  tpport_t getControlTransportPort() const
279  { return controlTransportPort; }
280 
281  const InetAddress& getNetworkAddress() const
282  { return networkAddress; }
283 
284 protected:
288  SyncSource(const SyncSource& source);
289 
290  SyncSource&
291  operator=(const SyncSource& source);
292 
293 private:
294  friend class SyncSourceHandler;
295 
296  inline void
297  setState(State st)
298  { state = st; }
299 
303  inline void
304  setSender(bool active)
305  { activeSender = active; }
306 
307  inline void
309  { participant = &p; }
310 
311  void setDataTransportPort(tpport_t p)
312  { dataTransportPort = p; }
313 
314  void setControlTransportPort(tpport_t p)
315  { controlTransportPort = p; }
316 
317  void setNetworkAddress(InetAddress addr)
318  { networkAddress = addr; }
319 
320  inline void
321  setLink(void *l)
322  { link = l; }
323 
324  void *getLink() const
325  { return link; }
326 
327  // validity state of this source
328  State state;
329  // 32-bit SSRC identifier.
330  uint32 SSRC;
331  // A valid source not always is active
332  bool activeSender;
333  // The corresponding participant.
334  Participant* participant;
335 
336  // Network protocol address for data and control connection
337  // (both are assumed to be the same).
338  InetAddress networkAddress;
339  tpport_t dataTransportPort;
340  tpport_t controlTransportPort;
341 
342  // Pointer to the SyncSourceLink or similar object in the
343  // service queue. Saves a lot of searches in the membership
344  // table.
345  void* link;
346 };
347 
368 class __EXPORT RTPApplication : private SDESItemsHolder
369 {
370 private:
371  struct ParticipantLink;
372 
373 public:
381  RTPApplication(const std::string& cname);
382 
383  ~RTPApplication();
384 
385  inline void
386  setSDESItem(SDESItemType item, const std::string& val)
387  { SDESItemsHolder::setItem(item,val); }
388 
389  inline void
390  setPRIVPrefix(const std::string& val)
392 
393  const std::string&
394  getSDESItem(SDESItemType item) const
395  { return SDESItemsHolder::getItem(item); }
396 
397  inline const std::string&
398  getPRIVPrefix() const
399  { return SDESItemsHolder::getPRIVPrefix(); }
400 
406  {
407  public:
408  typedef std::forward_iterator_tag iterator_category;
410  typedef ptrdiff_t difference_type;
411  typedef const Participant* pointer;
412  typedef const Participant& reference;
413 
414  ParticipantsIterator(ParticipantLink* p = NULL) :
415  link(p)
416  { }
417 
419  link(pi.link)
420  { }
421 
422  reference operator*() const
423  { return *(link->getParticipant()); }
424 
425  pointer operator->() const
426  { return link->getParticipant(); }
427 
429  link = link->getNext();
430  return *this;
431  }
432 
434  ParticipantsIterator result(*this);
435  ++(*this);
436  return result;
437  }
438  friend bool operator==(const ParticipantsIterator& l,
439  const ParticipantsIterator& r)
440  { return l.link == r.link; }
441 
442  friend bool operator!=(const ParticipantsIterator& l,
443  const ParticipantsIterator& r)
444  { return l.link != r.link; }
445  private:
446  ParticipantLink *link;
447  };
448 
450  { return ParticipantsIterator(firstPart); }
451 
453  { return ParticipantsIterator(NULL); }
454 
455  const Participant*
456  getParticipant(const std::string& cname) const;
457 
458 private:
459  friend class ApplicationHandler;
460 
461  struct ParticipantLink {
462  ParticipantLink(Participant& p,
463  ParticipantLink* l) :
464  participant(&p), next(l)
465  { }
466  inline ~ParticipantLink() { delete participant; }
467  inline Participant* getParticipant() { return participant; }
468  inline ParticipantLink* getPrev() { return prev; }
469  inline ParticipantLink* getNext() { return next; }
470  inline void setPrev(ParticipantLink* l) { prev = l; }
471  inline void setNext(ParticipantLink* l) { next = l; }
472  Participant* participant;
473  ParticipantLink* next, *prev;
474  };
475 
476  void
477  addParticipant(Participant& part);
478 
479  void
480  removeParticipant(ParticipantLink* part);
481 
486  void
487  findCNAME();
488 
490  static const size_t defaultParticipantsNum;
491  Participant** participants;
493  ParticipantLink* firstPart, * lastPart;
494 };
495 
506  // sources
508 
509 #ifdef CCXX_NAMESPACES
510 }
511 #endif
512 
513 #endif //CCXX_RTP_SOURCES_H_
514