|
| 1 | +/** |
| 2 | + * @file utils/sdp_parser.cpp |
| 3 | + * @author Martin Piatka <[email protected]> |
| 4 | + */ |
| 5 | +/* |
| 6 | + * Copyright (c) 2025 CESNET, zájmové sdružení právnických osob |
| 7 | + * All rights reserved. |
| 8 | + * |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, is permitted provided that the following conditions |
| 11 | + * are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright |
| 14 | + * notice, this list of conditions and the following disclaimer. |
| 15 | + * |
| 16 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | + * notice, this list of conditions and the following disclaimer in the |
| 18 | + * documentation and/or other materials provided with the distribution. |
| 19 | + * |
| 20 | + * 3. Neither the name of CESNET nor the names of its contributors may be |
| 21 | + * used to endorse or promote products derived from this software without |
| 22 | + * specific prior written permission. |
| 23 | + * |
| 24 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS |
| 25 | + * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, |
| 26 | + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 27 | + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 28 | + * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 29 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 30 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 31 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 32 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 33 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 34 | + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 35 | + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | + */ |
| 37 | + |
| 38 | +#include "sdp_parser.hpp" |
| 39 | +#include "utils/string_view_utils.hpp" |
| 40 | + |
| 41 | +Sap_packet_view Sap_packet_view::from_buffer(const void *buf, size_t size){ |
| 42 | + Sap_packet_view ret; |
| 43 | + |
| 44 | + std::string_view sap(static_cast<const char *>(buf), size); |
| 45 | + |
| 46 | + if(sap.empty()){ |
| 47 | + return ret; |
| 48 | + } |
| 49 | + |
| 50 | + ret.flags = sap[0]; |
| 51 | + sap.remove_prefix(1); |
| 52 | + |
| 53 | + ret.version = ret.flags >> 6; |
| 54 | + |
| 55 | + uint8_t auth_len = 0; |
| 56 | + if(sap.empty()) |
| 57 | + return ret; |
| 58 | + |
| 59 | + auth_len = sap[0]; |
| 60 | + sap.remove_prefix(1); |
| 61 | + |
| 62 | + //TODO error check length |
| 63 | + ret.hash = sap[0] << 8 | sap[1]; |
| 64 | + sap.remove_prefix(2); |
| 65 | + |
| 66 | + //TODO error check length |
| 67 | + ret.source = sap.substr(0, 4); |
| 68 | + sap.remove_prefix(4); |
| 69 | + |
| 70 | + //TODO error check length |
| 71 | + sap.remove_prefix(auth_len); |
| 72 | + |
| 73 | + if(sv_is_prefix(sap, "v=0")){ |
| 74 | + //RFC says that the payload type is optional if it's sdp and |
| 75 | + //that it should be detected by the presence of v=0 |
| 76 | + ret.payload_type = "application/sdp"; |
| 77 | + ret.payload = sap; |
| 78 | + } else { |
| 79 | + auto null_idx = sap.find('\0'); |
| 80 | + if(null_idx == sap.npos){ |
| 81 | + return ret; |
| 82 | + } |
| 83 | + ret.payload_type = sap.substr(0, null_idx); |
| 84 | + ret.payload = sap.substr(null_idx + 1); |
| 85 | + } |
| 86 | + |
| 87 | + ret.valid = true; |
| 88 | + return ret; |
| 89 | +} |
| 90 | + |
| 91 | +Sdp_view Sdp_view::from_buffer(const void *buf, size_t size){ |
| 92 | + Sdp_view ret; |
| 93 | + |
| 94 | + std::string_view sdp(static_cast<const char *>(buf), size); |
| 95 | + |
| 96 | + if(sdp.empty()){ |
| 97 | + return ret; |
| 98 | + } |
| 99 | + |
| 100 | + while(!sdp.empty()){ |
| 101 | + auto line = tokenize(sdp, '\n'); |
| 102 | + if(line.empty()) |
| 103 | + continue; |
| 104 | + |
| 105 | + if(line.back() == '\r') |
| 106 | + line.remove_suffix(1); |
| 107 | + |
| 108 | + auto eq_idx = line.find('='); |
| 109 | + if(eq_idx == line.npos){ |
| 110 | + continue; |
| 111 | + } |
| 112 | + auto key = line.substr(0, eq_idx); |
| 113 | + auto val = line.substr(eq_idx + 1); |
| 114 | + |
| 115 | + if(key == "c"){ |
| 116 | + if(ret.media.empty()){ |
| 117 | + ret.connection = val; |
| 118 | + } else { |
| 119 | + ret.media.back().connection = val; |
| 120 | + } |
| 121 | + } else if(key == "a"){ |
| 122 | + auto attrib = tokenize(val, ':'); |
| 123 | + auto attrib_val = tokenize(val, ':'); |
| 124 | + |
| 125 | + if(ret.media.empty()){ |
| 126 | + ret.session_attributes.push_back({attrib, attrib_val}); |
| 127 | + } else { |
| 128 | + ret.media.back().attributes.push_back({attrib, attrib_val}); |
| 129 | + } |
| 130 | + |
| 131 | + } else if(key =="s"){ |
| 132 | + ret.session_name = val; |
| 133 | + } else if(key == "o"){ |
| 134 | + ret.origin = val; |
| 135 | + } else if(key == "m"){ |
| 136 | + ret.media.emplace_back(); |
| 137 | + auto& medium = ret.media.back(); |
| 138 | + |
| 139 | + medium.media_desc = val; |
| 140 | + } |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + ret.valid = true; |
| 145 | + |
| 146 | + return ret; |
| 147 | +} |
| 148 | + |
| 149 | +Rtp_pkt_view Rtp_pkt_view::from_buffer(void *buf, size_t size){ |
| 150 | + Rtp_pkt_view ret{}; |
| 151 | + |
| 152 | + if(size < 12) |
| 153 | + return ret; |
| 154 | + |
| 155 | + auto charbuf = static_cast<unsigned char*>(buf); |
| 156 | + |
| 157 | + uint8_t version = charbuf[0] >> 6; |
| 158 | + bool padding = charbuf[0] & (1 << 5); |
| 159 | + bool extension = charbuf[0] & (1 << 4); |
| 160 | + ret.csrc_count = charbuf[0] & 0x0F; |
| 161 | + ret.marker = charbuf[1] & 0x80; |
| 162 | + ret.payload_type = charbuf[1] & 0x7F; |
| 163 | + ret.seq = charbuf[2] << 8 | charbuf[3]; |
| 164 | + ret.timestamp = charbuf[4] << 24 | charbuf[5] << 16 | charbuf[6] << 8 | charbuf[7]; |
| 165 | + ret.ssrc = charbuf[8] << 24 | charbuf[9] << 16 | charbuf[10] << 8 | charbuf[11]; |
| 166 | + |
| 167 | + |
| 168 | + size_t data_offset = 12 + ret.csrc_count * 4; |
| 169 | + ret.data = &charbuf[data_offset]; |
| 170 | + ret.data_len = size - data_offset; |
| 171 | + |
| 172 | + ret.valid = true; |
| 173 | + return ret; |
| 174 | +} |
0 commit comments