-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathUriParser.java
More file actions
207 lines (175 loc) · 7.46 KB
/
UriParser.java
File metadata and controls
207 lines (175 loc) · 7.46 KB
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
/*
* Copyright (C) 2011-2014 GUIGUI Simon, [email protected]
*
* This file is part of libstreaming (https://github.com/fyhertz/libstreaming)
*
* Spydroid 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
* (at your option) any later version.
*
* This source code 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 source code; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package net.majorkernelpanic.streaming.rtsp;
import static net.majorkernelpanic.streaming.SessionBuilder.AUDIO_AAC;
import static net.majorkernelpanic.streaming.SessionBuilder.AUDIO_AMRNB;
import static net.majorkernelpanic.streaming.SessionBuilder.AUDIO_NONE;
import static net.majorkernelpanic.streaming.SessionBuilder.VIDEO_H263;
import static net.majorkernelpanic.streaming.SessionBuilder.VIDEO_H264;
import static net.majorkernelpanic.streaming.SessionBuilder.VIDEO_NONE;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.List;
import net.majorkernelpanic.streaming.MediaStream;
import net.majorkernelpanic.streaming.Session;
import net.majorkernelpanic.streaming.SessionBuilder;
import net.majorkernelpanic.streaming.audio.AudioQuality;
import net.majorkernelpanic.streaming.video.VideoQuality;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import android.hardware.Camera.CameraInfo;
/**
* This class parses URIs received by the RTSP server and configures a Session accordingly.
*/
public class UriParser {
public final static String TAG = "UriParser";
/**
* Configures a Session according to the given URI.
* Here are some examples of URIs that can be used to configure a Session:
* <ul><li>rtsp://xxx.xxx.xxx.xxx:8086?h264&flash=on</li>
* <li>rtsp://xxx.xxx.xxx.xxx:8086?h263&camera=front&flash=on</li>
* <li>rtsp://xxx.xxx.xxx.xxx:8086?h264=200-20-320-240</li>
* <li>rtsp://xxx.xxx.xxx.xxx:8086?aac</li></ul>
* @param uri The URI
* @throws IllegalStateException
* @throws IOException
* @return A Session configured according to the URI
*/
public static Session parse(String uri) throws IllegalStateException, IOException {
SessionBuilder builder = SessionBuilder.getInstance().clone();
byte audioApi = 0, videoApi = 0;
List<NameValuePair> params = URLEncodedUtils.parse(URI.create(uri),"UTF-8");
if (params.size()>0) {
builder.setAudioEncoder(AUDIO_NONE).setVideoEncoder(VIDEO_NONE);
// Those parameters must be parsed first or else they won't necessarily be taken into account
for (Iterator<NameValuePair> it = params.iterator();it.hasNext();) {
NameValuePair param = it.next();
// FLASH ON/OFF
if (param.getName().equalsIgnoreCase("flash")) {
if (param.getValue().equalsIgnoreCase("on"))
builder.setFlashEnabled(true);
else
builder.setFlashEnabled(false);
}
// CAMERA -> the client can choose between the front facing camera and the back facing camera
else if (param.getName().equalsIgnoreCase("camera")) {
if (param.getValue().equalsIgnoreCase("back"))
builder.setCamera(CameraInfo.CAMERA_FACING_BACK);
else if (param.getValue().equalsIgnoreCase("front"))
builder.setCamera(CameraInfo.CAMERA_FACING_FRONT);
}
// MULTICAST -> the stream will be sent to a multicast group
// The default mutlicast address is 228.5.6.7, but the client can specify another
else if (param.getName().equalsIgnoreCase("multicast")) {
if (param.getValue()!=null) {
try {
InetAddress addr = InetAddress.getByName(param.getValue());
if (!addr.isMulticastAddress()) {
throw new IllegalStateException("Invalid multicast address !");
}
builder.setDestination(param.getValue());
} catch (UnknownHostException e) {
throw new IllegalStateException("Invalid multicast address !");
}
}
else {
// Default multicast address
builder.setDestination("228.5.6.7");
}
}
// UNICAST -> the client can use this to specify where he wants the stream to be sent
else if (param.getName().equalsIgnoreCase("unicast")) {
if (param.getValue()!=null) {
builder.setDestination(param.getValue());
}
}
// VIDEOAPI -> can be used to specify what api will be used to encode video (the MediaRecorder API or the MediaCodec API)
else if (param.getName().equalsIgnoreCase("videoapi")) {
if (param.getValue()!=null) {
if (param.getValue().equalsIgnoreCase("mr")) {
videoApi = MediaStream.MODE_MEDIARECORDER_API;
} else if (param.getValue().equalsIgnoreCase("mc")) {
videoApi = MediaStream.MODE_MEDIACODEC_API;
}
}
}
// AUDIOAPI -> can be used to specify what api will be used to encode audio (the MediaRecorder API or the MediaCodec API)
else if (param.getName().equalsIgnoreCase("audioapi")) {
if (param.getValue()!=null) {
if (param.getValue().equalsIgnoreCase("mr")) {
audioApi = MediaStream.MODE_MEDIARECORDER_API;
} else if (param.getValue().equalsIgnoreCase("mc")) {
audioApi = MediaStream.MODE_MEDIACODEC_API;
}
}
}
// TTL -> the client can modify the time to live of packets
// By default ttl=64
else if (param.getName().equalsIgnoreCase("ttl")) {
if (param.getValue()!=null) {
try {
int ttl = Integer.parseInt(param.getValue());
if (ttl<0) throw new IllegalStateException();
builder.setTimeToLive(ttl);
} catch (Exception e) {
throw new IllegalStateException("The TTL must be a positive integer !");
}
}
}
// H.264
else if (param.getName().equalsIgnoreCase("h264")) {
VideoQuality quality = VideoQuality.parseQuality(param.getValue());
builder.setVideoQuality(quality).setVideoEncoder(VIDEO_H264);
}
// H.263
else if (param.getName().equalsIgnoreCase("h263")) {
VideoQuality quality = VideoQuality.parseQuality(param.getValue());
builder.setVideoQuality(quality).setVideoEncoder(VIDEO_H263);
}
// AMR
else if (param.getName().equalsIgnoreCase("amrnb") || param.getName().equalsIgnoreCase("amr")) {
AudioQuality quality = AudioQuality.parseQuality(param.getValue());
builder.setAudioQuality(quality).setAudioEncoder(AUDIO_AMRNB);
}
// AAC
else if (param.getName().equalsIgnoreCase("aac")) {
AudioQuality quality = AudioQuality.parseQuality(param.getValue());
builder.setAudioQuality(quality).setAudioEncoder(AUDIO_AAC);
}
}
}
if (builder.getVideoEncoder()==VIDEO_NONE && builder.getAudioEncoder()==AUDIO_NONE) {
SessionBuilder b = SessionBuilder.getInstance();
builder.setVideoEncoder(b.getVideoEncoder());
builder.setAudioEncoder(b.getAudioEncoder());
}
Session session = builder.build();
if (videoApi>0 && session.getVideoTrack() != null) {
session.getVideoTrack().setStreamingMethod(videoApi);
}
if (audioApi>0 && session.getAudioTrack() != null) {
session.getAudioTrack().setStreamingMethod(audioApi);
}
return session;
}
}