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 
50 NAMESPACE_COMMONCPP
51 
65 class __EXPORT SDESItemsHolder
66 {
67 public:
68  const std::string&
69  getItem(SDESItemType type) const;
70 
71  inline const std::string&
72  getPRIVPrefix() const
73  { return sdesItems[SDESItemTypeEND]; }
74 
75  void
76  setItem(SDESItemType item, const std::string& val);
77 
78  inline void
79  setPRIVPrefix(const std::string& val)
80  { sdesItems[SDESItemTypeEND] = val; }
81 
82 protected:
84  { }
85 
86  inline virtual ~SDESItemsHolder()
87  { }
88 
89 private:
90  // SDES items for a participant.
91  // sdesItems[0] (== sdesItems[SDESItemTypeEND]) holds the prefix
92  // value for the PRIV item. The rest of entries hold the
93  // correponding SDES item value.
94  std::string sdesItems[SDESItemTypeLast + 1];
95 };
96 
125 class __EXPORT Participant : private SDESItemsHolder
126 {
127 public:
140  const std::string&
141  getSDESItem(SDESItemType type) const
142  { return SDESItemsHolder::getItem(type); }
143 
151  inline const std::string&
153  { return SDESItemsHolder::getPRIVPrefix(); }
154 
160  Participant(const std::string& cname);
161 
162  ~Participant();
163 
164 private:
165  friend class ParticipantHandler;
166 
170  inline void
171  setSDESItem(SDESItemType item, const std::string& val)
172  { SDESItemsHolder::setItem(item,val); }
173 
177  inline void
178  setPRIVPrefix(const std::string val)
180 };
181 
193 class __EXPORT SyncSource
194 {
195 public:
226  typedef enum {
229 
230 
232 
234 
235 
236  stateLeaving
237 
238  } State;
239 
244  SyncSource(uint32 ssrc);
245 
246  ~SyncSource();
247 
248  State
249  getState() const
250  { return state; }
251 
255  bool isSender() const
256  { return activeSender; }
257 
258  uint32 getID() const
259  { return SSRC; }
260 
268  inline Participant*
269  getParticipant() const
270  { return participant; }
271 
272  tpport_t getDataTransportPort() const
273  { return dataTransportPort; }
274 
275  tpport_t getControlTransportPort() const
276  { return controlTransportPort; }
277 
278  const InetAddress& getNetworkAddress() const
279  { return networkAddress; }
280 
281 protected:
285  SyncSource(const SyncSource& source);
286 
287  SyncSource&
288  operator=(const SyncSource& source);
289 
290 private:
291  friend class SyncSourceHandler;
292 
293  inline void
294  setState(State st)
295  { state = st; }
296 
300  inline void
301  setSender(bool active)
302  { activeSender = active; }
303 
304  inline void
306  { participant = &p; }
307 
308  void setDataTransportPort(tpport_t p)
309  { dataTransportPort = p; }
310 
311  void setControlTransportPort(tpport_t p)
312  { controlTransportPort = p; }
313 
314  void setNetworkAddress(InetAddress addr)
315  { networkAddress = addr; }
316 
317  inline void
318  setLink(void *l)
319  { link = l; }
320 
321  void *getLink() const
322  { return link; }
323 
324  // validity state of this source
325  State state;
326  // 32-bit SSRC identifier.
327  uint32 SSRC;
328  // A valid source not always is active
329  bool activeSender;
330  // The corresponding participant.
331  Participant* participant;
332 
333  // Network protocol address for data and control connection
334  // (both are assumed to be the same).
335  InetAddress networkAddress;
336  tpport_t dataTransportPort;
337  tpport_t controlTransportPort;
338 
339  // Pointer to the SyncSourceLink or similar object in the
340  // service queue. Saves a lot of searches in the membership
341  // table.
342  void* link;
343 };
344 
365 class __EXPORT RTPApplication : private SDESItemsHolder
366 {
367 private:
368  struct ParticipantLink;
369 
370 public:
378  RTPApplication(const std::string& cname);
379 
380  ~RTPApplication();
381 
382  inline void
383  setSDESItem(SDESItemType item, const std::string& val)
384  { SDESItemsHolder::setItem(item,val); }
385 
386  inline void
387  setPRIVPrefix(const std::string& val)
389 
390  const std::string&
391  getSDESItem(SDESItemType item) const
392  { return SDESItemsHolder::getItem(item); }
393 
394  inline const std::string&
396  { return SDESItemsHolder::getPRIVPrefix(); }
397 
403  {
404  public:
405  typedef std::forward_iterator_tag iterator_category;
407  typedef std::ptrdiff_t difference_type;
408  typedef const Participant* pointer;
409  typedef const Participant& reference;
410 
411  ParticipantsIterator(ParticipantLink* p = NULL) :
412  link(p)
413  { }
414 
416  link(pi.link)
417  { }
418 
419  reference operator*() const
420  { return *(link->getParticipant()); }
421 
422  pointer operator->() const
423  { return link->getParticipant(); }
424 
426  link = link->getNext();
427  return *this;
428  }
429 
431  ParticipantsIterator result(*this);
432  ++(*this);
433  return result;
434  }
435  friend bool operator==(const ParticipantsIterator& l,
436  const ParticipantsIterator& r)
437  { return l.link == r.link; }
438 
439  friend bool operator!=(const ParticipantsIterator& l,
440  const ParticipantsIterator& r)
441  { return l.link != r.link; }
442  private:
443  ParticipantLink *link;
444  };
445 
447  { return ParticipantsIterator(firstPart); }
448 
450  { return ParticipantsIterator(NULL); }
451 
452  const Participant*
453  getParticipant(const std::string& cname) const;
454 
455 private:
456  friend class ApplicationHandler;
457 
458  struct ParticipantLink {
459  ParticipantLink(Participant& p,
460  ParticipantLink* l) :
461  participant(&p), next(l)
462  { }
463  inline ~ParticipantLink() { delete participant; }
464  inline Participant* getParticipant() { return participant; }
465  inline ParticipantLink* getPrev() { return prev; }
466  inline ParticipantLink* getNext() { return next; }
467  inline void setPrev(ParticipantLink* l) { prev = l; }
468  inline void setNext(ParticipantLink* l) { next = l; }
469  Participant* participant;
470  ParticipantLink* next, *prev;
471  };
472 
473  void
474  addParticipant(Participant& part);
475 
476  void
477  removeParticipant(ParticipantLink* part);
478 
483  void
484  findCNAME();
485 
487  static const size_t defaultParticipantsNum;
488  Participant** participants;
490  ParticipantLink* firstPart, * lastPart;
491 };
492 
503  // sources
505 
506 END_NAMESPACE
507 
508 #endif //CCXX_RTP_SOURCES_H_
509