panlinlin
2021-01-15 503f891c9e443551309a91b38493ac49413dbe35
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
/*
    This file is part of Peers, a java SIP softphone.
 
    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
    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, see <http://www.gnu.org/licenses/>.
    
    Copyright 2007, 2008, 2009, 2010 Yohann Martineau 
*/
 
package com.genersoft.iot.vmp.gb28181.sdp;
 
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
 
public class SdpParser {
 
    public SessionDescription parse(byte[] body) throws IOException {
        if (body == null || body.length == 0) {
            return null;
        }
        ByteArrayInputStream in = new ByteArrayInputStream(body);
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        BufferedReader reader = new BufferedReader(inputStreamReader);
        SessionDescription sessionDescription = new SessionDescription();
        
        //version
        
        String line = reader.readLine();
        if (line.length() < 3) {
            return null;
        }
        if (line.charAt(0) != RFC4566_28181.TYPE_VERSION
                || line.charAt(1) != RFC4566_28181.SEPARATOR
                || line.charAt(2) != RFC4566_28181.VERSION) {
            return null;
        }
 
        //origin
        
        line = reader.readLine();
        if (line.length() < 3) {
            return null;
        }
        if (line.charAt(0) != RFC4566_28181.TYPE_ORIGIN
                || line.charAt(1) != RFC4566_28181.SEPARATOR) {
            return null;
        }
        line = line.substring(2);
        String[] originArr = line.split(" ");
        if (originArr == null || originArr.length != 6) {
            return null;
        }
        sessionDescription.setUsername(originArr[0]);
        sessionDescription.setId(Long.parseLong(originArr[1]));
        sessionDescription.setVersion(Long.parseLong(originArr[2]));
        sessionDescription.setIpAddress(InetAddress.getByName(originArr[5]));
 
        //name
        
        line = reader.readLine();
        if (line.length() < 3) {
            return null;
        }
        if (line.charAt(0) != RFC4566_28181.TYPE_SUBJECT
                || line.charAt(1) != RFC4566_28181.SEPARATOR) {
            return null;
        }
        sessionDescription.setName(line.substring(2));
        
        //session connection and attributes
        Hashtable<String, String> sessionAttributes = new Hashtable<String, String>();
        sessionDescription.setAttributes(sessionAttributes);
        
        while ((line = reader.readLine()) != null
                && line.charAt(0) != RFC4566_28181.TYPE_MEDIA) {
            if (line.length() > 3
                    && line.charAt(0) == RFC4566_28181.TYPE_CONNECTION
                    && line.charAt(1) == RFC4566_28181.SEPARATOR) {
                String connection = parseConnection(line.substring(2));
                if (connection == null) {
                    continue;
                }
                sessionDescription.setIpAddress(InetAddress.getByName(connection));
            } else if (line.length() > 3
                    && line.charAt(0) == RFC4566_28181.TYPE_ATTRIBUTE
                    && line.charAt(1) == RFC4566_28181.SEPARATOR) {
                String value = line.substring(2);
                int pos = value.indexOf(RFC4566_28181.ATTR_SEPARATOR);
                if (pos > -1) {
                    sessionAttributes.put(value.substring(0, pos),
                            value.substring(pos + 1));
                } else {
                    sessionAttributes.put(value, "");
                }
            }
        }
        if (line == null) {
            return null;
        }
        //we are at the first media line
        
        ArrayList<SdpLine> mediaLines = new ArrayList<SdpLine>();
        do {
            if (line.length() < 2) {
                return null;
            }
            if (line.charAt(1) != RFC4566_28181.SEPARATOR) {
                return null;
            }
            if (line.charAt(0) == RFC4566_28181.TYPE_SSRC) {
                sessionDescription.setSsrc(line.length() >=2 ?line.substring(2):"");
            }else if (line.charAt(0) == RFC4566_28181.TYPE_MEDIA_DES) {
                sessionDescription.setGbMediaDescriptions(line.length() >=2 ?line.substring(2):"");
            }else {
                SdpLine mediaLine = new SdpLine();
                mediaLine.setType(line.charAt(0));
                mediaLine.setValue(line.substring(2));
                mediaLines.add(mediaLine);
            }
 
        }
        while ((line = reader.readLine()) != null );
        
        ArrayList<MediaDescription> mediaDescriptions = new ArrayList<MediaDescription>();
        sessionDescription.setMediaDescriptions(mediaDescriptions);
        
        for (SdpLine sdpLine : mediaLines) {
            MediaDescription mediaDescription;
            if (sdpLine.getType() == RFC4566_28181.TYPE_MEDIA) {
                String[] mediaArr = sdpLine.getValue().split(" ");
                if (mediaArr == null || mediaArr.length < 4) {
                    return null;
                }
                mediaDescription = new MediaDescription();
                mediaDescription.setType(mediaArr[0]);
                //TODO manage port range
                mediaDescription.setPort(Integer.parseInt(mediaArr[1]));
                mediaDescription.setAttributes(new Hashtable<String, String>());
                List<Codec> codecs = new ArrayList<Codec>();
                for (int i = 3; i < mediaArr.length; ++i) {
                    int payloadType = Integer.parseInt(mediaArr[i]);
                    Codec codec = new Codec();
                    codec.setPayloadType(payloadType);
                    codec.setName("unsupported");
                    codecs.add(codec);
                }
                mediaDescription.setCodecs(codecs);
                mediaDescriptions.add(mediaDescription);
            } else {
                mediaDescription = mediaDescriptions.get(mediaDescriptions.size() - 1);
                String sdpLineValue = sdpLine.getValue();
                if (sdpLine.getType() == RFC4566_28181.TYPE_CONNECTION) {
                    String ipAddress = parseConnection(sdpLineValue);
                    mediaDescription.setIpAddress(InetAddress.getByName(ipAddress));
                } else if (sdpLine.getType() == RFC4566_28181.TYPE_ATTRIBUTE) {
                    Hashtable<String, String> attributes = mediaDescription.getAttributes();
                    int pos = sdpLineValue.indexOf(RFC4566_28181.ATTR_SEPARATOR);
                    if (pos > -1) {
                        String name = sdpLineValue.substring(0, pos);
                        String value = sdpLineValue.substring(pos + 1);
                        pos = value.indexOf(" ");
                        if (pos > -1) {
                            int payloadType;
                            try {
                                payloadType = Integer.parseInt(value.substring(0, pos));
                                List<Codec> codecs = mediaDescription.getCodecs();
                                for (Codec codec: codecs) {
                                    if (codec.getPayloadType() == payloadType) {
                                        value = value.substring(pos + 1);
                                        pos = value.indexOf("/");
                                        if (pos > -1) {
                                            value = value.substring(0, pos);
                                            codec.setName(value);
                                        }
                                        break;
                                    }
                                }
                            } catch (NumberFormatException e) {
                                attributes.put(name, value);
                            }
                        } else {
                            attributes.put(name, value);
                        }
                    } else {
                        attributes.put(sdpLineValue, "");
                    }
                }
            }
        }
        sessionDescription.setMediaDescriptions(mediaDescriptions);
 
        for (MediaDescription description : mediaDescriptions) {
            if (description.getIpAddress() == null) {
                InetAddress sessionAddress = sessionDescription.getIpAddress();
                if (sessionAddress == null) {
                    return null;
                }
                description.setIpAddress(sessionAddress);
            }
        }
 
 
        return sessionDescription;
    }
    
    private String parseConnection(String line) {
        String[] connectionArr = line.split(" ");
        if (connectionArr == null || connectionArr.length != 3) {
            return null;
        }
        return connectionArr[2];
    }
    
}