-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSdpMedia.cpp
More file actions
509 lines (434 loc) · 12.3 KB
/
SdpMedia.cpp
File metadata and controls
509 lines (434 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
* Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "SipPlatformDefine.h"
#include "SdpMedia.h"
#include <stdlib.h>
#include "MemoryDebug.h"
/**
* @ingroup SdpParser
* @brief 생성자
*/
CSdpMedia::CSdpMedia() : m_iPort(-1), m_iNumOfPort(-1)
{
}
/**
* @ingroup SdpParser
* @brief 생성자
* @param pszMedia 미디어 이름
* @param iPort 포트 번호
* @param pszProtocol 전송 프로토콜
*/
CSdpMedia::CSdpMedia( const char * pszMedia, int iPort, const char * pszProtocol ) :
m_strMedia(pszMedia), m_iPort(iPort), m_iNumOfPort(-1), m_strProtocol(pszProtocol)
{
}
/**
* @ingroup SdpParser
* @brief 소멸자
*/
CSdpMedia::~CSdpMedia()
{
}
/**
* @ingroup SdpParser
* @brief SDP media 의 value 문자열을 파싱한다.
* @param pszText SDP media 의 value 문자열
* @param iTextLen SDP media 의 value 문자열 길이
* @returns 성공하면 파싱한 길이를 리턴하고 그렇지 않으면 -1를 리턴한다.
*/
int CSdpMedia::Parse( const char * pszText, int iTextLen )
{
Clear();
if( pszText == NULL || iTextLen <= 0 ) return -1;
int iPos, iStartPos = 0, iType = 0;
for( iPos = 0; iPos < iTextLen; ++iPos )
{
if( pszText[iPos] == ' ' )
{
SetData( pszText + iStartPos, iPos - iStartPos, iType );
++iType;
iStartPos = iPos + 1;
}
}
if( iStartPos < iTextLen )
{
SetData( pszText + iStartPos, iPos - iStartPos, iType );
++iType;
}
if( iType < 3 ) return -1;
return iTextLen;
}
/**
* @ingroup SdpParser
* @brief SDP media 의 value 문자열을 저장한다.
* @param pszText SDP media 의 value 문자열을 저장할 변수
* @param iTextSize pszText 변수의 크기
* @returns 성공하면 저장된 문자열 길이를 리턴하고 그렇지 않으면 -1 를 리턴한다.
*/
int CSdpMedia::ToString( char * pszText, int iTextSize )
{
if( pszText == NULL || iTextSize <= 0 ) return -1;
int iLen, n;
SDP_FMT_LIST::iterator itFmt;
iLen = snprintf( pszText, iTextSize, "%s ", m_strMedia.c_str() );
if( m_iNumOfPort == -1 )
{
iLen += snprintf( pszText + iLen, iTextSize - iLen, "%d ", m_iPort );
}
else
{
iLen += snprintf( pszText + iLen, iTextSize - iLen, "%d/%d ", m_iPort, m_iNumOfPort );
}
iLen += snprintf( pszText + iLen, iTextSize - iLen, "%s", m_strProtocol.c_str() );
for( itFmt = m_clsFmtList.begin(); itFmt != m_clsFmtList.end(); ++itFmt )
{
iLen += snprintf( pszText + iLen, iTextSize - iLen, " %s", itFmt->c_str() );
}
iLen += snprintf( pszText + iLen, iTextSize - iLen, "\r\n" );
if( m_strTitle.empty() == false )
{
iLen += snprintf( pszText + iLen, iTextSize - iLen, "i=%s\r\n", m_strTitle.c_str() );
}
if( m_clsConnection.Empty() == false )
{
iLen += snprintf( pszText + iLen, iTextSize - iLen, "c=" );
n = m_clsConnection.ToString( pszText + iLen, iTextSize - iLen );
if( n == -1 ) return -1;
iLen += n;
iLen += snprintf( pszText + iLen, iTextSize - iLen, "\r\n" );
}
if( m_clsBandWidthList.empty() == false )
{
SDP_BANDWIDTH_LIST::iterator itBL;
for( itBL = m_clsBandWidthList.begin(); itBL != m_clsBandWidthList.end(); ++itBL )
{
iLen += snprintf( pszText + iLen, iTextSize - iLen, "b=" );
n = itBL->ToString( pszText + iLen, iTextSize - iLen );
if( n == -1 ) return -1;
iLen += n;
iLen += snprintf( pszText + iLen, iTextSize - iLen, "\r\n" );
}
}
if( m_clsAttributeList.empty() == false )
{
SDP_ATTRIBUTE_LIST::iterator itAL;
for( itAL = m_clsAttributeList.begin(); itAL != m_clsAttributeList.end(); ++itAL )
{
iLen += snprintf( pszText + iLen, iTextSize - iLen, "a=" );
n = itAL->ToString( pszText + iLen, iTextSize - iLen );
if( n == -1 ) return -1;
iLen += n;
iLen += snprintf( pszText + iLen, iTextSize - iLen, "\r\n" );
}
}
return iLen;
}
/**
* @ingroup SdpParser
* @brief 멤버 변수를 초기화시킨다.
*/
void CSdpMedia::Clear()
{
m_strMedia.clear();
m_iPort = -1;
m_iNumOfPort = -1;
m_strProtocol.clear();
m_clsFmtList.clear();
m_strTitle.clear();
m_clsConnection.Clear();
m_clsBandWidthList.clear();
m_clsAttributeList.clear();
}
/**
* @ingroup SdpParser
* @brief FMT 를 추가한다.
* @param iPayLoadType payload type
*/
void CSdpMedia::AddFmt( int iPayLoadType )
{
if( SelectFmt( iPayLoadType ) ) return;
char szNum[11];
snprintf( szNum, sizeof(szNum), "%d", iPayLoadType );
m_clsFmtList.push_back( szNum );
}
/**
* @ingroup SdpParser
* @brief FMT 리스트에 payload type 이 존재하는지 확인한다.
* @param iPayLoadType payload type
* @returns FMT 리스트에 payload type 이 존재하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CSdpMedia::SelectFmt( int iPayLoadType )
{
char szNum[11];
SDP_FMT_LIST::iterator itList;
snprintf( szNum, sizeof(szNum), "%d", iPayLoadType );
for( itList = m_clsFmtList.begin(); itList != m_clsFmtList.end(); ++itList )
{
if( !strcmp( itList->c_str(), szNum ) )
{
return true;
}
}
return false;
}
/**
* @ingroup SdpParser
* @brief 애트리뷰트를 삭제한다.
* @param pszName 삭제할 애트리뷰트 이름
* @returns 애트리뷰트를 삭제하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CSdpMedia::DeleteAttribute( const char * pszName )
{
SDP_ATTRIBUTE_LIST::iterator itAttr, itNext;
bool bRes = false;
for( itAttr = m_clsAttributeList.begin(); itAttr != m_clsAttributeList.end(); ++itAttr )
{
LOOP_START:
if( !strcmp( itAttr->m_strName.c_str(), pszName ) )
{
bRes = true;
itNext = itAttr;
++itNext;
m_clsAttributeList.erase( itAttr );
if( itNext == m_clsAttributeList.end() ) break;
itAttr = itNext;
goto LOOP_START;
}
}
return bRes;
}
/**
* @ingroup SdpParser
* @brief FMT 리스트에 payload type 이 존재하면 해당 payload type 을 FMT 리스트 및 애트리뷰트에서 삭제한다.
* @param iPayLoadType payload type
* @returns FMT 리스트에 payload type 이 존재하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CSdpMedia::DeleteFmtAttribute( int iPayLoadType )
{
SDP_ATTRIBUTE_LIST::iterator itAttr, itNext;
SDP_FMT_LIST::iterator itFL;
bool bRes = false;
for( itFL = m_clsFmtList.begin(); itFL != m_clsFmtList.end(); ++itFL )
{
int iCodec = atoi( itFL->c_str() );
if( iCodec == iPayLoadType )
{
m_clsFmtList.erase( itFL );
bRes = true;
break;
}
}
if( bRes )
{
for( itAttr = m_clsAttributeList.begin(); itAttr != m_clsAttributeList.end(); ++itAttr )
{
LOOP_START:
if( !strcmp( itAttr->m_strName.c_str(), "rtpmap" ) || !strcmp( itAttr->m_strName.c_str(), "fmtp" ) )
{
int iCodec = atoi( itAttr->m_strValue.c_str() );
if( iCodec == iPayLoadType )
{
itNext = itAttr;
++itNext;
m_clsAttributeList.erase( itAttr );
if( itNext == m_clsAttributeList.end() ) break;
itAttr = itNext;
goto LOOP_START;
}
}
}
}
return bRes;
}
/**
* @ingroup SdpParser
* @brief FMT 리스트에 payload type 이 존재하면 해당 payload type 을 FMT 리스트 및 애트리뷰트의 최상단으로 이동한다.
* @param iPayLoadType payload type
* @returns FMT 리스트에 payload type 이 존재하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CSdpMedia::MakeTopFmtAttribute( int iPayLoadType )
{
SDP_ATTRIBUTE_LIST::iterator itAttr, itBegin;
SDP_FMT_LIST::iterator itFL;
bool bRes = false;
for( itFL = m_clsFmtList.begin(); itFL != m_clsFmtList.end(); ++itFL )
{
int iCodec = atoi( itFL->c_str() );
if( iCodec == iPayLoadType )
{
if( itFL == m_clsFmtList.begin() )
{
// 입력한 payload type 이 top 이면 변경할 필요가 없다.
return true;
}
std::string strFmt = *itFL;
m_clsFmtList.erase( itFL );
m_clsFmtList.push_front( strFmt );
bRes = true;
break;
}
}
if( bRes )
{
itBegin = m_clsAttributeList.end();
for( itAttr = m_clsAttributeList.begin(); itAttr != m_clsAttributeList.end(); ++itAttr )
{
if( itBegin == m_clsAttributeList.end() &&
( !strcmp( itAttr->m_strName.c_str(), "rtpmap" ) || !strcmp( itAttr->m_strName.c_str(), "fmtp" ) ) )
{
itBegin = itAttr;
}
if( !strcmp( itAttr->m_strName.c_str(), "fmtp" ) )
{
int iCodec = atoi( itAttr->m_strValue.c_str() );
if( iCodec == iPayLoadType )
{
CSdpAttribute clsAttr = *itAttr;
m_clsAttributeList.erase( itAttr );
m_clsAttributeList.insert( itBegin, clsAttr );
break;
}
}
}
itBegin = m_clsAttributeList.end();
for( itAttr = m_clsAttributeList.begin(); itAttr != m_clsAttributeList.end(); ++itAttr )
{
if( itBegin == m_clsAttributeList.end() &&
( !strcmp( itAttr->m_strName.c_str(), "rtpmap" ) || !strcmp( itAttr->m_strName.c_str(), "fmtp" ) ) )
{
itBegin = itAttr;
}
if( !strcmp( itAttr->m_strName.c_str(), "rtpmap" ) )
{
int iCodec = atoi( itAttr->m_strValue.c_str() );
if( iCodec == iPayLoadType )
{
CSdpAttribute clsAttr = *itAttr;
m_clsAttributeList.erase( itAttr );
m_clsAttributeList.insert( itBegin, clsAttr );
break;
}
}
}
}
return bRes;
}
/**
* @ingroup SdpParser
* @brief 애트리뷰트를 추가한다. rtpmap 이면 fmt 로 추가한다.
* @param pclsAttr 애트리뷰트 객체
*/
void CSdpMedia::AddAttribute( CSdpAttribute * pclsAttr )
{
if( !strcmp( pclsAttr->m_strName.c_str(), "rtpmap" ) )
{
AddFmt( pclsAttr->GetPayLoadType() );
}
m_clsAttributeList.push_back( *pclsAttr );
}
/**
* @ingroup SdpParser
* @brief 애트리뷰트를 추가한다. rtpmap 이면 fmt 로 추가한다.
* @param pszName 애트리뷰트 이름
* @param pszValue 애트리뷰트 값
*/
void CSdpMedia::AddAttribute( const char * pszName, const char * pszValue )
{
CSdpAttribute clsAttr( pszName, pszValue );
if( !strcmp( clsAttr.m_strName.c_str(), "rtpmap" ) )
{
AddFmt( clsAttr.GetPayLoadType() );
}
m_clsAttributeList.push_back( clsAttr );
}
/**
* @ingroup SdpParser
* @brief direction 애트리뷰를 수정한다.
* @param pszDirection direction
*/
void CSdpMedia::SetDirection( const char * pszDirection )
{
SDP_ATTRIBUTE_LIST::iterator itAttr;
bool bFound = false;
for( itAttr = m_clsAttributeList.begin(); itAttr != m_clsAttributeList.end(); ++itAttr )
{
if( !strcmp( itAttr->m_strName.c_str(), "sendrecv" ) ||
!strcmp( itAttr->m_strName.c_str(), "sendonly" ) ||
!strcmp( itAttr->m_strName.c_str(), "recvonly" ) ||
!strcmp( itAttr->m_strName.c_str(), "inactive" ) )
{
if( strcmp( itAttr->m_strName.c_str(), pszDirection ) )
{
itAttr->m_strName = pszDirection;
}
bFound = true;
break;
}
}
if( bFound == false )
{
AddAttribute( pszDirection, NULL );
}
}
/**
* @ingroup SdpParser
* @brief SDP media 문자열을 파싱한 각 항목을 저장한다.
* @param pszData 문자열
* @param iLen 문자열 길이
* @param iType SDP media 문자열을 파싱한 문자열 배열의 인덱스
*/
void CSdpMedia::SetData( const char * pszData, int iLen, int iType )
{
switch( iType )
{
case 0:
m_strMedia.append( pszData, iLen );
break;
case 1:
for( int i = 0; i < iLen; ++i )
{
if( pszData[i] == '/' )
{
std::string strTemp;
strTemp.append( pszData, i );
m_iPort = atoi( strTemp.c_str() );
strTemp.clear();
strTemp.append( pszData + i + 1, iLen - ( i + 1 ) );
m_iNumOfPort = atoi( strTemp.c_str() );
break;
}
}
if( m_iPort == -1 )
{
std::string strTemp;
strTemp.append( pszData, iLen );
m_iPort = atoi( strTemp.c_str() );
}
break;
case 2:
m_strProtocol.append( pszData, iLen );
break;
default:
{
std::string strTemp;
strTemp.append( pszData, iLen );
m_clsFmtList.push_back( strTemp );
}
break;
}
}