1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 /*
8 
9          ,-----.                  ,--.  ,--.
10         '  .--./ ,--,--.,--.,--.,-'  '-.`--' ,---. ,--,--,
11         |  |    ' ,-.  ||  ||  |'-.  .-',--.| .-. ||      `
12         '  '--'\\ '-'  |'  ''  '  |  |  |  |' '-' '|  ||  |
13          `-----' `--`--' `----'   `--'  `--' `---' `--''--'
14 
15                         :+o+-
16                       -dNNNNNd.
17                       yNNNNNNNs
18                       :mNNNNNm-
19                        `/sso/``-://-
20                         .:+sydNNNNNNms:                      `://`
21                  `-/+shmNNNNNNNNNNNNNNNms-                  :mNNNm/
22            `-/oydmNNNNNNNNNNNNNNNNNNNNNNNNdo-              +NNNNNN+
23        .shmNNNNNNNNNNNmdyo/:dNNNNNNNNNNNNNNNNdo.         `sNNNNNm+
24        hNNNNNNNNmhs+:-`   .dNNNNNNNNNNNNNNNNNNNNh+-`    `hNNNNNm:
25        -yddyo/:.         -dNNNNm::ymNNNNNNNNNNNNNNNmdy+/dNNNNNd.
26                         :mNNNNd.   `/ymNNNNNNNNNNNNNNNNNNNNNNh`
27                        +NNNNNh`       `+hNNNNNNNNNNNNNNNNNNNs
28                       sNNNNNy`           .yNNNNNm`-/oymNNNm+
29                     `yNNNNNo              oNNNNNm`     `-.
30                    .dNNNNm/               oNNNNNm`
31                    oNNNNm:                +NNNNNm`
32                    `+yho.                 +NNNNNm`
33                                           +NNNNNNs.
34                                           `yNNNNNNmy-
35                                             -smNNNNNNh:
36                                               .smNNNNNNh/
37                                                 `omNNNNNNd:
38                                                   `+dNNNNNd
39                              ````......````          /hmdy-
40             `.:/+osyhddmNNMMMMMMMMMMMMMMMMMMMMNNmddhyso+/:.`
41      `-+shmNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNmhs+-`
42   -smMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMds-
43  hMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMh
44  yMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMs
45   .ohNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNh+.
46       ./oydmMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMmhyo:.
47              `.:/+osyyhddmmNNMMMMMMMMMMMMMMNNmmddhyyso+/:.`
48 
49             ,--------.,--.     ,--.           ,--.
50             '--.  .--'|  ,---. `--' ,---.     |  | ,---.
51                |  |   |  .-.  |,--.(  .-'     |  |(  .-'
52                |  |   |  | |  ||  |.-'  `)    |  |.-'  `)
53                `--'   `--' `--'`--'`----'     `--'`----'
54                                                                 ,--.
55        ,---.  ,------.  ,------.                  ,--.          |  |
56       '   .-' |  .-.  \ |  .--. ' ,--,--.,--.--.,-'  '-. ,--,--.|  |
57       `.  `-. |  |  \  :|  '--' |' ,-.  ||  .--''-.  .-'' ,-.  ||  |
58       .-'    ||  '--'  /|  | --' \ '-'  ||  |     |  |  \ '-'  |`--'
59       `-----' `-------' `--'      `--`--'`--'     `--'   `--`--'.--.
60                                                                 '__'
61 */
62 
63 #ifndef _SDP_H_
64 #define _SDP_H_
65 
66 #include <ostream>
67 #include <vector>
68 #include <sstream>
69 #include "mozilla/UniquePtr.h"
70 #include "mozilla/Maybe.h"
71 #include "sdp/SdpMediaSection.h"
72 #include "sdp/SdpAttributeList.h"
73 #include "sdp/SdpEnum.h"
74 
75 namespace mozilla {
76 
77 class SdpOrigin;
78 class SdpEncryptionKey;
79 class SdpMediaSection;
80 
81 /**
82  * Base class for an SDP
83  */
84 class Sdp {
85  public:
86   Sdp() = default;
87   virtual ~Sdp() = default;
88 
89   virtual const SdpOrigin& GetOrigin() const = 0;
90   // Note: connection information is always retrieved from media sections
91   virtual uint32_t GetBandwidth(const std::string& type) const = 0;
92 
93   virtual const SdpAttributeList& GetAttributeList() const = 0;
94   virtual SdpAttributeList& GetAttributeList() = 0;
95 
96   virtual size_t GetMediaSectionCount() const = 0;
97   virtual const SdpMediaSection& GetMediaSection(size_t level) const = 0;
98   virtual SdpMediaSection& GetMediaSection(size_t level) = 0;
99 
100   virtual SdpMediaSection& AddMediaSection(SdpMediaSection::MediaType media,
101                                            SdpDirectionAttribute::Direction dir,
102                                            uint16_t port,
103                                            SdpMediaSection::Protocol proto,
104                                            sdp::AddrType addrType,
105                                            const std::string& addr) = 0;
106 
107   virtual void Serialize(std::ostream&) const = 0;
108 
109   std::string ToString() const;
110 };
111 
112 inline std::ostream& operator<<(std::ostream& os, const Sdp& sdp) {
113   sdp.Serialize(os);
114   return os;
115 }
116 
ToString()117 inline std::string Sdp::ToString() const {
118   std::stringstream s;
119   s << *this;
120   return s.str();
121 }
122 
123 class SdpOrigin {
124  public:
SdpOrigin(const std::string & username,uint64_t sessId,uint64_t sessVer,sdp::AddrType addrType,const std::string & addr)125   SdpOrigin(const std::string& username, uint64_t sessId, uint64_t sessVer,
126             sdp::AddrType addrType, const std::string& addr)
127       : mUsername(username),
128         mSessionId(sessId),
129         mSessionVersion(sessVer),
130         mAddrType(addrType),
131         mAddress(addr) {}
132 
GetUsername()133   const std::string& GetUsername() const { return mUsername; }
134 
GetSessionId()135   uint64_t GetSessionId() const { return mSessionId; }
136 
GetSessionVersion()137   uint64_t GetSessionVersion() const { return mSessionVersion; }
138 
GetAddrType()139   sdp::AddrType GetAddrType() const { return mAddrType; }
140 
GetAddress()141   const std::string& GetAddress() const { return mAddress; }
142 
Serialize(std::ostream & os)143   void Serialize(std::ostream& os) const {
144     sdp::NetType netType = sdp::kInternet;
145     os << "o=" << mUsername << " " << mSessionId << " " << mSessionVersion
146        << " " << netType << " " << mAddrType << " " << mAddress << "\r\n";
147   }
148 
149  private:
150   std::string mUsername;
151   uint64_t mSessionId;
152   uint64_t mSessionVersion;
153   sdp::AddrType mAddrType;
154   std::string mAddress;
155 };
156 
157 inline std::ostream& operator<<(std::ostream& os, const SdpOrigin& origin) {
158   origin.Serialize(os);
159   return os;
160 }
161 
162 }  // namespace mozilla
163 
164 #endif
165