Philipp Hancke | 37e5080 | 2025-05-12 23:09:11 | [diff] [blame] | 1 | // Copyright 2025 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Reduced copy of |
| 6 | // third_party/blink/web_tests/external/wpt/webrtc/third_party/sdp/sdp.js |
| 7 | |
| 8 | // Splits SDP into lines, dealing with both CRLF and LF. |
| 9 | export function splitLines(blob) { |
| 10 | return blob.trim().split('\n').map(line => line.trim()); |
| 11 | } |
| 12 | |
| 13 | // Splits SDP into sections, including the sessionpart. |
| 14 | export function splitSections(blob) { |
| 15 | const parts = blob.split('\nm='); |
| 16 | return parts.map((part, index) => (index > 0 ? |
| 17 | 'm=' + part : part).trim() + '\r\n'); |
| 18 | } |
| 19 | |
| 20 | // Gets the direction from the mediaSection or the sessionpart. |
| 21 | export function getDirection(mediaSection, sessionpart) { |
| 22 | // Look for sendrecv, sendonly, recvonly, inactive, default to sendrecv. |
| 23 | const lines = splitLines(mediaSection); |
| 24 | for (let i = 0; i < lines.length; i++) { |
| 25 | switch (lines[i]) { |
| 26 | case 'a=sendrecv': |
| 27 | case 'a=sendonly': |
| 28 | case 'a=recvonly': |
| 29 | case 'a=inactive': |
| 30 | return lines[i].substring(2); |
| 31 | } |
| 32 | } |
| 33 | if (sessionpart) { |
| 34 | return getDirection(sessionpart); |
| 35 | } |
| 36 | return 'sendrecv'; |
| 37 | } |
| 38 | |
| 39 | // Parses a m= line. |
| 40 | export function parseMLine(mediaSection) { |
| 41 | const lines = splitLines(mediaSection); |
| 42 | const parts = lines[0].substring(2).split(' '); |
| 43 | return { |
| 44 | kind: parts[0], |
| 45 | port: parseInt(parts[1], 10), |
| 46 | protocol: parts[2], |
| 47 | fmt: parts.slice(3).join(' '), |
| 48 | }; |
| 49 | } |