Skip to content

Commit b6c2e2b

Browse files
committed
DO NOT MERGE: More robust parsing of NPT time ranges in RTSP.
Change-Id: If5a00f1e29dbc12956e1fb000dac859706d19791 related-to-bug: 3217210
1 parent 0ef5799 commit b6c2e2b

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

media/libstagefright/rtsp/ASessionDescription.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -254,26 +254,12 @@ bool ASessionDescription::getDurationUs(int64_t *durationUs) const {
254254
return false;
255255
}
256256

257-
if (value == "npt=now-" || value == "npt=0-") {
258-
return false;
259-
}
260-
261257
if (strncmp(value.c_str(), "npt=", 4)) {
262258
return false;
263259
}
264260

265-
const char *s = value.c_str() + 4;
266-
char *end;
267-
double from = strtod(s, &end);
268-
269-
if (end == s || *end != '-') {
270-
return false;
271-
}
272-
273-
s = end + 1;
274-
double to = strtod(s, &end);
275-
276-
if (end == s || *end != '\0' || to < from) {
261+
float from, to;
262+
if (!parseNTPRange(value.c_str() + 4, &from, &to)) {
277263
return false;
278264
}
279265

@@ -307,5 +293,39 @@ void ASessionDescription::ParseFormatDesc(
307293
}
308294
}
309295

296+
// static
297+
bool ASessionDescription::parseNTPRange(
298+
const char *s, float *npt1, float *npt2) {
299+
if (s[0] == '-') {
300+
return false; // no start time available.
301+
}
302+
303+
if (!strncmp("now", s, 3)) {
304+
return false; // no absolute start time available
305+
}
306+
307+
char *end;
308+
*npt1 = strtof(s, &end);
309+
310+
if (end == s || *end != '-') {
311+
// Failed to parse float or trailing "dash".
312+
return false;
313+
}
314+
315+
s = end + 1; // skip the dash.
316+
317+
if (!strncmp("now", s, 3)) {
318+
return false; // no absolute end time available
319+
}
320+
321+
*npt2 = strtof(s, &end);
322+
323+
if (end == s || *end != '\0') {
324+
return false;
325+
}
326+
327+
return *npt2 > *npt1;
328+
}
329+
310330
} // namespace android
311331

media/libstagefright/rtsp/ASessionDescription.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ struct ASessionDescription : public RefBase {
5555

5656
bool findAttribute(size_t index, const char *key, AString *value) const;
5757

58+
// parses strings of the form
59+
// npt := npt-time "-" npt-time? | "-" npt-time
60+
// npt-time := "now" | [0-9]+("." [0-9]*)?
61+
//
62+
// Returns true iff both "npt1" and "npt2" times were available,
63+
// i.e. we have a fixed duration, otherwise this is live streaming.
64+
static bool parseNTPRange(const char *s, float *npt1, float *npt2);
65+
5866
protected:
5967
virtual ~ASessionDescription();
6068

media/libstagefright/rtsp/MyHandler.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -938,13 +938,11 @@ struct MyHandler : public AHandler {
938938

939939
AString val;
940940
CHECK(GetAttribute(range.c_str(), "npt", &val));
941-
float npt1, npt2;
942941

943-
if (val == "now-" || val == "0-") {
942+
float npt1, npt2;
943+
if (!ASessionDescription::parseNTPRange(val.c_str(), &npt1, &npt2)) {
944944
// This is a live stream and therefore not seekable.
945945
return;
946-
} else {
947-
CHECK_EQ(sscanf(val.c_str(), "%f-%f", &npt1, &npt2), 2);
948946
}
949947

950948
i = response->mHeaders.indexOfKey("rtp-info");

0 commit comments

Comments
 (0)