ccRTP 2.1.2
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
source.cpp
Go to the documentation of this file.
1 // Copyright (C) 2001-2015 Federico Montesino <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 #include "private.h"
45 #include <ccrtp/sources.h>
46 
47 NAMESPACE_COMMONCPP
48 
49 static void
50 findusername(std::string &username);
51 
52 #ifndef _MSWINDOWS_
53 static void
54 findusername(std::string &username)
55 {
56  // LOGNAME environment var has two advantages:
57  // 1) avoids problems of getlogin(3) and cuserid(3)
58  // 2) unlike getpwuid, takes into account user
59  // customization of the environment.
60  // Try both LOGNAME and USER env. var.
61  const char *user = Process::getEnv("LOGNAME");
62  if ( !user || !strcmp(user,"") )
63  user = Process::getEnv("USER");
64  if ( !user || !strcmp(user,"") )
65  username = Process::getUser();
66 
67  if ( user )
68  username = user;
69  else
70  username = "";
71 }
72 
73 #else
74 
75 static void
76 findusername(std::string &username)
77 {
78  unsigned long len = 0;
79  if ( GetUserName(NULL,&len) && (len > 0) ) {
80  char *n = new char[len];
81  GetUserName(n,&len);
82  username = n;
83  delete [] n;
84  } else {
85  username = "";
86  }
87 }
88 #endif // #ifndef WIN32
89 
90 void
91 SDESItemsHolder::setItem(SDESItemType item, const std::string& val)
92 {
93  if ( item > SDESItemTypeEND && item <= SDESItemTypeH323CADDR ) {
94  sdesItems[item] = val;
95  }
96 }
97 
98 const std::string&
100 {
101  if ( type > SDESItemTypeEND && type <= SDESItemTypeH323CADDR ) {
102  return sdesItems[type];
103  } else
105 }
106 
108 state(stateUnknown), SSRC(ssrc), participant(NULL),
109 networkAddress("0"), dataTransportPort(0), controlTransportPort(0)
110 {}
111 
113 {
114  activeSender = false;
116 }
117 
118 Participant::Participant(const std::string& cname) : SDESItemsHolder()
119 {
121 }
122 
124 {}
125 
127 
129 {
130  // default application CNAME is automatically assigned.
131  static RTPApplication defApp("");
132 
133  return defApp;
134 }
135 
136 RTPApplication::RTPApplication(const std::string& cname) :
137 SDESItemsHolder(), participants( new Participant* [defaultParticipantsNum] ),
138 firstPart(NULL), lastPart(NULL)
139 {
140  // guess CNAME, in the form of user@host_fqn
141  if ( cname.length() > 0 )
143  else
144  findCNAME();
145 }
146 
148 {
149  ParticipantLink *p;
150  while ( NULL != firstPart ) {
151  p = firstPart;
153 #ifdef CCXX_EXCEPTIONS
154  try {
155 #endif
156  delete p;
157 #ifdef CCXX_EXCEPTIONS
158  } catch (...) {}
159 #endif
160  }
161  lastPart = NULL;
162 #ifdef CCXX_EXCEPTIONS
163  try {
164 #endif
165  delete [] participants;
166 #ifdef CCXX_EXCEPTIONS
167  } catch (...) {}
168 #endif
169 }
170 
171 // TODO: it should be implemented using the participant iterators
172 const Participant*
173 RTPApplication::getParticipant(const std::string& cname) const
174 {
176  while ( (NULL != pl) &&
178  != cname) ) {
179  pl = pl->getNext();
180  }
181  if ( pl ) {
182  return pl->getParticipant();
183  } else {
184  return NULL;
185  }
186 }
187 
188 void
190 {
191  ParticipantLink* pl = new ParticipantLink(part,NULL);
192  if ( NULL == firstPart )
193  firstPart = lastPart = pl;
194  else
195  lastPart->setNext(pl);
196  lastPart = pl;
197 }
198 
199 void
201 {
202  if ( NULL == pl )
203  return;
204  if ( pl->getPrev() )
205  pl->getPrev()->setNext(pl->getNext());
206  if ( pl->getNext() )
207  pl->getNext()->setPrev(pl->getPrev());
208  delete pl;
209 }
210 
211 void
213 {
214  // build string username@host_fqn
215  std::string username;
216  findusername(username);
217 
218  // First create an InetHostAddress object, otherwise the
219  // object will be destructed and the hostname corrupted.
220  InetHostAddress iha;
221  const char *p = iha.getHostname();
222  // Returned hostname can be NULL
223  std::string hname;
224  if (p) hname = p;
225 
227  username + "@" + hname);
228 }
229 
230 END_NAMESPACE
231 
ParticipantLink * lastPart
Definition: sources.h:489
const std::string & getItem(SDESItemType type) const
Definition: source.cpp:99
An RTP application, holding identifying RTCP SDES item values.
Definition: sources.h:364
ParticipantLink * firstPart
List of participants, ordered from older to newer.
Definition: sources.h:489
Some packets have been.
Definition: sources.h:227
Canonical end-point identifier.
Definition: rtcppkt.h:67
Sources of synchronization and participants related clases.
Declaration of ccRTP internal stuff.
std::string sdesItems[SDESItemTypeLast+1]
Definition: sources.h:93
A class of objects representing remote participants (RTP applications) in a multimedia session...
Definition: sources.h:124
void setItem(SDESItemType item, const std::string &val)
Definition: source.cpp:91
Participant(const std::string &cname)
Construct a new participant.
Definition: source.cpp:118
void findCNAME()
Find out the local CNAME as user@host and store it as part of the internal state of this class...
Definition: source.cpp:212
void setSDESItem(SDESItemType item, const std::string &val)
Definition: sources.h:382
END of SDES item list.
Definition: rtcppkt.h:66
void addParticipant(Participant &part)
Definition: source.cpp:189
void removeParticipant(ParticipantLink *part)
Definition: source.cpp:200
RTPApplication(const std::string &cname)
Create a new RTP application.
Definition: source.cpp:136
H323 callable address.
Definition: rtcppkt.h:75
Holds the SDES items and related information from a participant in an RTP application.
Definition: sources.h:64
SyncSource(uint32 ssrc)
Definition: source.cpp:107
static const size_t defaultParticipantsNum
Hash table with sources of RTP and RTCP packets.
Definition: sources.h:486
~SyncSource()
Definition: source.cpp:112
State state
Definition: sources.h:324
Participant ** participants
Definition: sources.h:487
const Participant * getParticipant(const std::string &cname) const
Definition: source.cpp:173
RTPApplication & defaultApplication()
Get the RTPApplication object for the "default" application (the only one used by common applications...
Definition: source.cpp:128
bool activeSender
Definition: sources.h:328
~Participant()
Definition: source.cpp:123
SDESItemType
SDES items that may be carried in a Source DEScription RTCP packet.
Definition: rtcppkt.h:64
const std::string & getSDESItem(SDESItemType type) const
Get the value of an SDES item.
Definition: sources.h:140