VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvsdp.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // gvsdp.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gvsdp.h"
23 #include "gstr.h"
24 #include "gdebug.h"
25 
26 Gv::Sdp::Sdp( const std::vector<std::string> & lines )
27 {
28  //if( lines.size() <= 1 ) throw Error( "no data" ) ;
29  //if( lines.at(0U) != "v=0" ) throw Error( "invalid version" ) ;
30 
31  typedef std::vector<std::string> StringArray ;
32  for( StringArray::const_iterator p = lines.begin() ; p != lines.end() ; ++p )
33  {
34  G_DEBUG( "Sdp::ctor: [" << *p << "]" ) ;
35  if( (*p).length() <= 2U ) throw Error() ;
36  if( (*p).at(1U) != '=' ) throw Error() ;
37 
38  if( (*p).at(0U) == 't' || (*p).at(0U) == 'r' )
39  {
40  if( (*p).at(0U) == 't' )
41  m_time.push_back( Map() ) ;
42  m_time.back().insert( pair(*p) ) ;
43  }
44  else if( (*p).at(0U) == 'm' )
45  {
46  m_media.push_back( Map() ) ;
47  m_media.back().insert( pair(*p) ) ;
48  }
49  else if( m_media.empty() )
50  {
51  m_session.insert( pair(*p) ) ;
52  }
53  else
54  {
55  m_media.back().insert( pair(*p) ) ;
56  }
57  }
58 }
59 
60 Gv::Sdp::Map::value_type Gv::Sdp::pair( const std::string & line )
61 {
62  return Map::value_type( line.substr(0U,1U) , line.substr(2U) ) ;
63 }
64 
65 std::string Gv::Sdp::originator() const
66 {
67  return value( "o" ) ;
68 }
69 
70 std::string Gv::Sdp::name() const
71 {
72  return value( "s" ) ;
73 }
74 
75 Gv::Sdp::Map Gv::Sdp::attributes() const
76 {
77  return attributes( m_session ) ;
78 }
79 
80 std::string Gv::Sdp::attributes( const std::string & sep ) const
81 {
82  return str( attributes(m_session) , sep ) ;
83 }
84 
85 size_t Gv::Sdp::timeCount() const
86 {
87  return m_time.size() ;
88 }
89 
90 size_t Gv::Sdp::mediaCount() const
91 {
92  return m_media.size() ;
93 }
94 
95 std::string Gv::Sdp::mediaType( size_t index ) const
96 {
97  return value( m_media.at(index) , "m" ) ;
98 }
99 
100 std::string Gv::Sdp::mediaTitle( size_t index ) const
101 {
102  return value( m_media.at(index) , "i" , "undefined" ) ;
103 }
104 
105 std::string Gv::Sdp::mediaConnection( size_t index ) const
106 {
107  return value( m_media.at(index) , m_session , "c" ) ;
108 }
109 
110 Gv::Sdp::Map Gv::Sdp::mediaAttributes( size_t index ) const
111 {
112  return attributes( m_media.at(index) ) ;
113 }
114 
115 std::string Gv::Sdp::mediaAttributes( size_t index , const std::string & sep ) const
116 {
117  return str( attributes(m_media.at(index)) , sep ) ;
118 }
119 
120 std::string Gv::Sdp::mediaAttribute( size_t index , const std::string & key ) const
121 {
122  return value( mediaAttributes(index) , key ) ;
123 }
124 
125 std::string Gv::Sdp::mediaAttribute( size_t index , const std::string & key , const std::string & default_ ) const
126 {
127  return value( mediaAttributes(index) , key , default_ ) ;
128 }
129 
130 // ==
131 
132 Gv::Sdp::Map Gv::Sdp::attributes( const Map & map )
133 {
134  Map result ;
135  const std::string a( "a" ) ;
136  for( Map::const_iterator p = map.find(a) ; p != map.end() && (*p).first == a ; ++p )
137  {
138  std::string v = (*p).second ;
139  std::string::size_type pos = v.find(":") ;
140  result.insert( Map::value_type( G::Str::head(v,pos,v) , G::Str::tail(v,pos) ) ) ;
141  }
142  return result ;
143 }
144 
145 std::string Gv::Sdp::str( const Map & map , const std::string & sep )
146 {
147  std::string result ;
148  for( Map::const_iterator p = map.begin() ; p != map.end() ; ++p )
149  {
150  if( p != map.begin() ) result.append( sep ) ;
151  result.append( (*p).first ) ;
152  if( !(*p).second.empty() )
153  {
154  result.append( ":" ) ;
155  result.append( (*p).second ) ;
156  }
157  }
158  return result ;
159 }
160 
161 std::string Gv::Sdp::value( const std::string & key ) const
162 {
163  return value( m_session , key ) ;
164 }
165 
166 std::string Gv::Sdp::value( const Map & map_1 , const Map & map_2 , const std::string & key )
167 {
168  Map::const_iterator p = map_1.find( key ) ;
169  if( p == map_1.end() )
170  {
171  p = map_2.find( key ) ;
172  if( p == map_2.end() )
173  throw Error( "no such value" ) ;
174  }
175  return (*p).second ;
176 }
177 
178 std::string Gv::Sdp::value( const Map & map , const std::string & key , const std::string & default_ )
179 {
180  Map::const_iterator p = map.find( key ) ;
181  return p == map.end() ? default_ : (*p).second ;
182 }
183 
184 std::string Gv::Sdp::value( const Map & map , const std::string & key )
185 {
186  Map::const_iterator p = map.find( key ) ;
187  if( p == map.end() )
188  throw Error( "no such value" ) ;
189  return (*p).second ;
190 }
191 
192 std::string Gv::Sdp::fmtp() const
193 {
194  std::string result ;
195  for( size_t i = 0 ; i < m_media.size() ; i++ )
196  {
197  if( mediaType(i).find("video") == 0U && mediaType(i).find("RTP/AVP") != std::string::npos )
198  {
199  std::string value = mediaAttribute( i , "fmtp" , std::string() ) ;
200  if( !value.empty() )
201  {
202  result = G::Str::printable( G::Str::tail(value,value.find(" "),value) ) ; // without the initial payload type number
203  break ;
204  }
205  }
206  }
207  return result ;
208 }
209 
210 /// \file gvsdp.cpp
static std::string printable(const std::string &in, char escape= '\\')
Returns a printable represention of the given input string.
Definition: gstr.cpp:663
std::string mediaAttribute(size_t index, const std::string &key) const
Returns the specified attribute of the index-th media ("a=key").
Definition: gvsdp.cpp:120
std::string name() const
Returns the session name ("s=").
Definition: gvsdp.cpp:70
Map attributes() const
Returns the session attributes as a multimap ("a=key:value").
Definition: gvsdp.cpp:75
std::string originator() const
Returns the session originator ("o=").
Definition: gvsdp.cpp:65
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
Sdp(const std::vector< std::string > &sdp_lines)
Constructor taking a list of text lines.
Definition: gvsdp.cpp:26
static std::string tail(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the last part of the string after the given position.
Definition: gstr.cpp:1051
std::string mediaTitle(size_t index=0U) const
Returns the title of the index-th media ("i=").
Definition: gvsdp.cpp:100
std::string mediaConnection(size_t index=0U) const
Returns the connection of the index-th media ("c=").
Definition: gvsdp.cpp:105
std::string fmtp() const
A convenience method that returns the value of the "fmtp" attribute of the first video "RTP/AVP" medi...
Definition: gvsdp.cpp:192
static std::string head(const std::string &in, std::string::size_type pos, const std::string &default_=std::string())
Returns the first part of the string up to just before the given position.
Definition: gstr.cpp:1037
std::string mediaType(size_t index=0U) const
Returns the type of the index-th media ("m=").
Definition: gvsdp.cpp:95
Map mediaAttributes(size_t index=0U) const
Returns the attributes of the index-th media as a multimap ("a=").
Definition: gvsdp.cpp:110
size_t mediaCount() const
Returns the number of mediaType()s.
Definition: gvsdp.cpp:90
size_t timeCount() const
Returns the number of timeValue()s.
Definition: gvsdp.cpp:85