Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Commit 2c7c209

Browse files
committed
Create values for packets with AV_NOPTS_VALUE. Closes #197
1 parent 166e16d commit 2c7c209

File tree

3 files changed

+71
-21
lines changed

3 files changed

+71
-21
lines changed

src/movie.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,28 +770,41 @@ int cls_movie::passthru_pktpts()
770770

771771
if (pkt->pts != AV_NOPTS_VALUE) {
772772
if (pkt->pts < base_pdts) {
773-
ts_interval = 0;
773+
MOTPLS_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "Invalid pkt->pts");
774+
return -1;
774775
} else {
775776
ts_interval = pkt->pts - base_pdts;
776777
}
777778
pkt->pts = av_rescale_q(ts_interval
778779
, netcam_data->transfer_format->streams[indx]->time_base, tmpbase);
780+
} else {
781+
MOTPLS_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "pkt->pts is AV_NOPTS_VALUE");
782+
return -1;
779783
}
780784

781785
if (pkt->dts != AV_NOPTS_VALUE) {
782786
if (pkt->dts < base_pdts) {
783-
ts_interval = 0;
787+
MOTPLS_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "pkt->dts is invalid");
788+
return -1;
784789
} else {
785790
ts_interval = pkt->dts - base_pdts;
786791
}
787792
pkt->dts = av_rescale_q(ts_interval
788793
, netcam_data->transfer_format->streams[indx]->time_base, tmpbase);
794+
} else {
795+
MOTPLS_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "pkt->dts is AV_NOPTS_VALUE");
796+
return -1;
789797
}
790798

791799
ts_interval = pkt->duration;
792800
pkt->duration = av_rescale_q(ts_interval
793801
, netcam_data->transfer_format->streams[indx]->time_base, tmpbase);
794802

803+
if ((pkt->pts == AV_NOPTS_VALUE) || (pkt->dts == AV_NOPTS_VALUE)) {
804+
MOTPLS_LOG(ERR, TYPE_ENCODER, NO_ERRNO, "pkt->dts or pkt->pts is AV_NOPTS_VALUE");
805+
return -1;
806+
}
807+
795808
/*
796809
MOTPLS_LOG(INF, TYPE_ENCODER, NO_ERRNO
797810
,_("base PTS %" PRId64 " new PTS %" PRId64 " srcbase %d-%d newbase %d-%d")

src/netcam.cpp

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,50 @@ int cls_netcam::resize()
13221322
return 0;
13231323
}
13241324

1325+
void cls_netcam::pkt_ts()
1326+
{
1327+
int64_t usec_ltncy;
1328+
AVRational tbase;
1329+
struct timespec tmp_tm;
1330+
1331+
if (connection_pts == -1) {
1332+
if (packet_recv->pts == AV_NOPTS_VALUE) {
1333+
connection_pts = 0;
1334+
} else {
1335+
connection_pts = packet_recv->pts;
1336+
}
1337+
clock_gettime(CLOCK_MONOTONIC, &connection_tm);
1338+
}
1339+
1340+
if (packet_recv->pts == AV_NOPTS_VALUE) {
1341+
clock_gettime(CLOCK_MONOTONIC, &tmp_tm);
1342+
1343+
tbase = format_context->streams[packet_recv->stream_index]->time_base;
1344+
if (tbase.num == 0) {
1345+
tbase.num = 1;
1346+
}
1347+
usec_ltncy = (((tmp_tm.tv_sec - connection_tm.tv_sec) * 1000000) +
1348+
((tmp_tm.tv_nsec - connection_tm.tv_nsec) / 1000));
1349+
if (usec_ltncy < 0) {
1350+
MOTPLS_LOG(ERR, TYPE_NETCAM, NO_ERRNO
1351+
,_("%s:Latency calculation error ")
1352+
, cameratype.c_str());
1353+
usec_ltncy = 0;
1354+
}
1355+
1356+
packet_recv->pts = connection_pts
1357+
+ av_rescale(usec_ltncy, tbase.den/tbase.num, 1000000);
1358+
1359+
}
1360+
1361+
if (packet_recv->dts == AV_NOPTS_VALUE) {
1362+
packet_recv->dts = packet_recv->pts;
1363+
}
1364+
1365+
last_pts = packet_recv->pts;
1366+
1367+
}
1368+
13251369
int cls_netcam::read_image()
13261370
{
13271371
int size_decoded, retcd, errcnt, nodata;
@@ -1403,7 +1447,8 @@ int cls_netcam::read_image()
14031447
clock_gettime(CLOCK_MONOTONIC, &ist_tm);
14041448
clock_gettime(CLOCK_MONOTONIC, &img_recv->image_time);
14051449
last_stream_index = packet_recv->stream_index;
1406-
last_pts = packet_recv->pts;
1450+
1451+
pkt_ts();
14071452

14081453
if (!first_image) {
14091454
status = NETCAM_CONNECTED;
@@ -1672,7 +1717,7 @@ void cls_netcam::set_parms ()
16721717
swsframe_size = 0;
16731718
hw_type = AV_HWDEVICE_TYPE_NONE;
16741719
hw_pix_fmt = AV_PIX_FMT_NONE;
1675-
connection_pts = 0;
1720+
connection_pts = -1;
16761721
last_pts = 0;
16771722
filenbr = 0;
16781723
filelist.clear();
@@ -1901,8 +1946,6 @@ int cls_netcam::open_context()
19011946
return -1;
19021947
}
19031948

1904-
connection_pts = AV_NOPTS_VALUE;
1905-
19061949
return 0;
19071950
}
19081951

@@ -1996,18 +2039,11 @@ void cls_netcam::handler_wait()
19962039

19972040
/* Adjust to clock and pts timer */
19982041
if (pts_adj == true) {
1999-
if (connection_pts == AV_NOPTS_VALUE) {
2000-
connection_pts = last_pts;
2001-
clock_gettime(CLOCK_MONOTONIC, &connection_tm);
2002-
return;
2003-
}
2004-
if (last_pts != AV_NOPTS_VALUE) {
2005-
usec_ltncy +=
2006-
+ (av_rescale(last_pts, 1000000, tbase.den/tbase.num)
2007-
- av_rescale(connection_pts, 1000000, tbase.den/tbase.num))
2008-
-(((tmp_tm.tv_sec - connection_tm.tv_sec) * 1000000) +
2009-
((tmp_tm.tv_nsec - connection_tm.tv_nsec) / 1000));
2010-
}
2042+
usec_ltncy +=
2043+
+ (av_rescale(last_pts, 1000000, tbase.den/tbase.num)
2044+
- av_rescale(connection_pts, 1000000, tbase.den/tbase.num))
2045+
-(((tmp_tm.tv_sec - connection_tm.tv_sec) * 1000000) +
2046+
((tmp_tm.tv_nsec - connection_tm.tv_nsec) / 1000));
20112047
}
20122048

20132049
if ((usec_ltncy > 0) && (usec_ltncy < 1000000L)) {

src/netcam.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ class cls_netcam {
9191
AVFormatContext *transfer_format; /* Format context just for transferring to pass-through */
9292
ctx_packet_item *pktarray; /* Pointer to array of packets for passthru processing */
9393
int pktarray_size; /* The number of packets in array. 1 based */
94-
int video_stream_index; /* Stream index associated with video from camera */
95-
int audio_stream_index; /* Stream index associated with video from camera */
94+
int video_stream_index; /* Stream index associated with video from camera */
95+
int audio_stream_index; /* Stream index associated with audio from camera */
9696

9797
bool handler_stop;
9898
bool handler_running;
@@ -118,7 +118,6 @@ class cls_netcam {
118118
int64_t idnbr; /* A ID number to track the packet vs image */
119119
AVDictionary *opts; /* AVOptions when opening the format context */
120120
int swsframe_size; /* The size of the image after resizing */
121-
int last_stream_index; /* Index of the last packet read */
122121

123122
enum AVHWDeviceType hw_type;
124123
enum AVPixelFormat hw_pix_fmt;
@@ -145,6 +144,7 @@ class cls_netcam {
145144
struct timespec connection_tm; /* Time when camera was connected*/
146145
int64_t connection_pts; /* PTS from the connection */
147146
int64_t last_pts; /* PTS from the last packet read */
147+
int last_stream_index; /* Stream index for last packet */
148148
bool pts_adj; /* Bool for whether to use pts for timing */
149149

150150
struct timespec frame_prev_tm; /* The time set before calling the av functions */
@@ -191,6 +191,7 @@ class cls_netcam {
191191
int open_codec();
192192
int open_sws();
193193
int resize();
194+
void pkt_ts();
194195
int read_image();
195196
int ntc();
196197
void set_options();

0 commit comments

Comments
 (0)