提交 9ee1230c authored 作者: blu's avatar blu

init

上级 ea6b9c79
...@@ -110,14 +110,13 @@ protected: ...@@ -110,14 +110,13 @@ protected:
// serialize packet to raw bytes // serialize packet to raw bytes
char * data = NULL; char * data = NULL;
//av_log(NULL, AV_LOG_WARNING, "chkpt1: %d\n", pktCnt); //av_log(NULL, AV_LOG_WARNING, "chkpt1: %d\n", pktCnt);
int size = PacketSerializer::encode(packet, &data); int size = AVPacketSerializer::encode(packet, &data);
//av_log(NULL, AV_LOG_WARNING, "chkpt2: %d\n", pktCnt); //av_log(NULL, AV_LOG_WARNING, "chkpt2: %d\n", pktCnt);
zmq_msg_init_data(&msg, (void*)data, size, mqPacketFree, NULL); zmq_msg_init_data(&msg, (void*)data, size, mqPacketFree, NULL);
//av_log(NULL, AV_LOG_WARNING, "chkpt3: %d\n", pktCnt); //av_log(NULL, AV_LOG_WARNING, "chkpt3: %d\n", pktCnt);
zmq_send_const(pPublisher, zmq_msg_data(&msg), size, 0); zmq_send_const(pPublisher, zmq_msg_data(&msg), size, 0);
//av_log(NULL, AV_LOG_WARNING, "chkpt4: %d\n", pktCnt); //av_log(NULL, AV_LOG_WARNING, "chkpt4: %d\n", pktCnt);
av_packet_unref(&packet); av_packet_unref(&packet);
} }
......
...@@ -83,6 +83,8 @@ private: ...@@ -83,6 +83,8 @@ private:
logThrow(NULL, AV_LOG_FATAL, "Failed to retrieve input stream information"); logThrow(NULL, AV_LOG_FATAL, "Failed to retrieve input stream information");
} }
//avformat_close_input(&pAVFormatInput);
pAVFormatInput->flags = AVFMT_FLAG_NOBUFFER | AVFMT_FLAG_FLUSH_PACKETS; pAVFormatInput->flags = AVFMT_FLAG_NOBUFFER | AVFMT_FLAG_FLUSH_PACKETS;
ret = avformat_alloc_output_context2(&pAVFormatRemux, NULL, "rtsp", urlOut); ret = avformat_alloc_output_context2(&pAVFormatRemux, NULL, "rtsp", urlOut);
...@@ -104,8 +106,7 @@ private: ...@@ -104,8 +106,7 @@ private:
AVStream *in_stream = pAVFormatInput->streams[i]; AVStream *in_stream = pAVFormatInput->streams[i];
AVCodecParameters *in_codecpar = in_stream->codecpar; AVCodecParameters *in_codecpar = in_stream->codecpar;
if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO && if (in_codecpar->codec_type != AVMEDIA_TYPE_AUDIO &&
in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO && in_codecpar->codec_type != AVMEDIA_TYPE_VIDEO) {
in_codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE) {
streamList[i] = -1; streamList[i] = -1;
continue; continue;
} }
...@@ -122,6 +123,10 @@ private: ...@@ -122,6 +123,10 @@ private:
} }
} }
for(int i = 0; i < pAVFormatInput->nb_streams; i++ ) {
av_log(NULL, AV_LOG_INFO, "streamList[%d]: %d\n", i, streamList[i]);
}
av_dump_format(pAVFormatRemux, 0, urlOut, 1); av_dump_format(pAVFormatRemux, 0, urlOut, 1);
if (!(pAVFormatRemux->oformat->flags & AVFMT_NOFILE)) { if (!(pAVFormatRemux->oformat->flags & AVFMT_NOFILE)) {
...@@ -150,11 +155,8 @@ protected: ...@@ -150,11 +155,8 @@ protected:
bool bStopSig = false; bool bStopSig = false;
zmq_msg_t msg; zmq_msg_t msg;
av_log_set_level(AV_LOG_DEBUG); av_log_set_level(AV_LOG_DEBUG);
bool firstPkt = true;
double oldPos = 0;
AVPacket packet; AVPacket packet;
uint64_t pktCnt = 0; uint64_t pktCnt = 0;
int64_t start_time = av_gettime();
while (true) { while (true) {
if(checkStop() == true) { if(checkStop() == true) {
bStopSig = true; bStopSig = true;
...@@ -172,79 +174,32 @@ protected: ...@@ -172,79 +174,32 @@ protected:
continue; continue;
} }
av_log(NULL, AV_LOG_DEBUG, "msg size: %d, %d\n", ret, zmq_msg_size(&msg));
// decode // decode
pktCnt++; pktCnt++;
ret = PacketSerializer::decode((char*)zmq_msg_data(&msg), ret, &packet); { ret = AVPacketSerializer::decode((char*)zmq_msg_data(&msg), ret, &packet); {
if (ret < 0) { if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "packet decode failed."); av_log(NULL, AV_LOG_ERROR, "packet decode failed.");
continue; continue;
} }
} }
zmq_msg_close(&msg); zmq_msg_close(&msg);
av_log(NULL, AV_LOG_ERROR, "packet stream indx: %d\n", packet.stream_index);
// relay // relay
AVStream *in_stream =NULL, *out_stream = NULL; AVStream *in_stream =NULL, *out_stream = NULL;
in_stream = pAVFormatInput->streams[packet.stream_index]; in_stream = pAVFormatInput->streams[packet.stream_index];
packet.stream_index = streamList[packet.stream_index]; packet.stream_index = streamList[packet.stream_index];
out_stream = pAVFormatRemux->streams[packet.stream_index]; out_stream = pAVFormatRemux->streams[packet.stream_index];
//calc pts
{ {
//Simple Write PTS av_log(NULL, AV_LOG_WARNING, "seq: %lld, pts: %lld, dts: %lld, dur: %lld, idx:%d\n", pktCnt, packet.pts, packet.dts, packet.duration, packet.stream_index);
if (packet.pts == AV_NOPTS_VALUE) {
//Write PTS
AVRational time_base1 = pAVFormatInput->streams[packet.stream_index]->time_base;
//Duration between 2 frames (us)
int64_t calc_duration = (double)AV_TIME_BASE / av_q2d(pAVFormatInput->streams[packet.stream_index]->r_frame_rate);
//Parameters
packet.pts = (double)(pktCnt*calc_duration) / (double)(av_q2d(time_base1)*AV_TIME_BASE);
packet.dts = packet.pts;
packet.duration = (double)calc_duration / (double)(av_q2d(time_base1)*AV_TIME_BASE);
}
//Important:Delay
if (packet.stream_index == packet.stream_index)
{
AVRational time_base = pAVFormatInput->streams[packet.stream_index]->time_base;
AVRational time_base_q = { 1,AV_TIME_BASE };
int64_t pts_time = av_rescale_q(packet.dts, time_base, time_base_q);
int64_t now_time = av_gettime() - start_time;
if (pts_time > now_time)
av_usleep(pts_time - now_time);
}
else
{
AVRational time_base1 = pAVFormatInput->streams[packet.stream_index]->time_base;
AVRational time_base_q = { 1, AV_TIME_BASE };
double frame_size = pAVFormatInput->streams[packet.stream_index]->codec->frame_size;
double sample_rate = pAVFormatInput->streams[packet.stream_index]->codec->sample_rate;
//Duration between 2 frames (us)
int64_t calc_duration = (double)(AV_TIME_BASE) * (frame_size / sample_rate);
packet.pts = av_rescale_q(pktCnt*calc_duration, time_base_q, time_base1);
packet.dts = packet.pts;
packet.duration = av_rescale_q(calc_duration, time_base_q, time_base1);
packet.pos = -1;
//_sleep(26);
//AVRational time_base1 = pAVFormatInput->streams[packet.stream_index]->time_base;
double currentPos = packet.pts * av_q2d(time_base1);
if (firstPkt == true)
{
oldPos = currentPos;
firstPkt = false;
}
double duration = currentPos - oldPos;
oldPos = currentPos;
cout << duration << endl;
av_usleep(duration * 1000 * 1000);
}
in_stream = pAVFormatInput->streams[packet.stream_index];
out_stream = pAVFormatRemux->streams[packet.stream_index];
/* copy packet */ /* copy packet */
//转换PTS/DTS(Convert PTS/DTS)
packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); packet.pts = av_rescale_q_rnd(packet.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX)); packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base); packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);
packet.pos = -1; packet.pos = -1;
av_log(NULL, AV_LOG_WARNING, "chkpt0.4: %lld\n", pktCnt);
} }
//av_log(NULL, AV_LOG_WARNING, "chkpt1: %d\n", pktCnt); //av_log(NULL, AV_LOG_WARNING, "chkpt1: %d\n", pktCnt);
......
...@@ -20,13 +20,13 @@ void logThrow(void * avcl, int lvl, const char *fmt, ...) ...@@ -20,13 +20,13 @@ void logThrow(void * avcl, int lvl, const char *fmt, ...)
throw fmt; throw fmt;
} }
namespace PacketSerializer { namespace AVPacketSerializer {
int encode(AVPacket &pkt, char **bytes) { int encode(AVPacket &pkt, char **bytes) {
int cnt = 0; int cnt = 0;
//data //data
int wholeSize = 4 + pkt.size; int wholeSize = sizeof(pkt.size) + pkt.size;
//side data //side data
wholeSize +=4; wholeSize +=sizeof(pkt.side_data_elems);
if(pkt.side_data_elems != 0) { if(pkt.side_data_elems != 0) {
for(int i = 0; i < pkt.side_data_elems; i++) { for(int i = 0; i < pkt.side_data_elems; i++) {
wholeSize += pkt.side_data[i].size + sizeof(AVPacketSideData); wholeSize += pkt.side_data[i].size + sizeof(AVPacketSideData);
...@@ -34,43 +34,45 @@ namespace PacketSerializer { ...@@ -34,43 +34,45 @@ namespace PacketSerializer {
} }
// 4 + 8: wholeSize + DEADBEAF // 4 + 8: wholeSize + DEADBEAF
wholeSize += 8 * 5 + 4 + 4 + 8; wholeSize += sizeof(pkt.pts) * 5 + sizeof(pkt.flags) + sizeof(pkt.stream_index) + sizeof(wholeSize) + 8;
*bytes = (char*)malloc(wholeSize); *bytes = (char*)malloc(wholeSize);
// data // data
memcpy((*bytes)+cnt, &(pkt.size), 4); memcpy((*bytes)+cnt, &(pkt.size), sizeof(pkt.size));
cnt +=4; cnt +=sizeof(pkt.size);
memcpy((*bytes )+cnt, pkt.data, pkt.size); memcpy((*bytes )+cnt, pkt.data, pkt.size);
cnt += pkt.size; cnt += pkt.size;
//side data //side data
memcpy((*bytes )+cnt, &(pkt.side_data_elems), 4); memcpy((*bytes )+cnt, &(pkt.side_data_elems), sizeof(pkt.side_data_elems));
cnt +=4; cnt += sizeof(pkt.side_data_elems);
if(pkt.side_data_elems != 0) { if(pkt.side_data_elems != 0) {
for(int i = 0; i < pkt.side_data_elems; i++) { for(int i = 0; i < pkt.side_data_elems; i++) {
memcpy((*bytes )+cnt, &(pkt.side_data[i].size), 4); memcpy((*bytes )+cnt, &(pkt.side_data[i].size), sizeof(pkt.side_data[i].size));
cnt+=4; cnt+=sizeof(pkt.side_data[i].size);
memcpy((*bytes )+cnt, pkt.side_data[i].data, pkt.side_data[i].size); memcpy((*bytes )+cnt, pkt.side_data[i].data, pkt.side_data[i].size);
cnt+=pkt.side_data[i].size; cnt+=pkt.side_data[i].size;
memcpy((*bytes )+cnt, &(pkt.side_data[i].type), 4); memcpy((*bytes )+cnt, &(pkt.side_data[i].type), sizeof(pkt.side_data[i].type));
cnt+=4; cnt+=sizeof(pkt.side_data[i].type);
} }
} }
// other properties // other properties
memcpy((*bytes )+cnt, &(pkt.pts), 8); memcpy((*bytes )+cnt, &(pkt.pts), sizeof(pkt.pts));
cnt+=sizeof(pkt.pts);
memcpy((*bytes )+cnt, &(pkt.dts), sizeof(pkt.dts));
cnt+=8; cnt+=8;
memcpy((*bytes )+cnt, &(pkt.dts), 8); memcpy((*bytes )+cnt, &(pkt.pos), sizeof(pkt.pos));
cnt+=8; cnt+=sizeof(pkt.pos);
memcpy((*bytes )+cnt, &(pkt.pos), 8); memcpy((*bytes )+cnt, &(pkt.duration), sizeof(pkt.duration));
cnt+=8; cnt+=sizeof(pkt.duration);
memcpy((*bytes )+cnt, &(pkt.duration), 8); memcpy((*bytes )+cnt, &(pkt.convergence_duration), sizeof(pkt.convergence_duration));
cnt+=8; cnt+=sizeof(pkt.convergence_duration);
memcpy((*bytes )+cnt, &(pkt.convergence_duration), 8); memcpy((*bytes )+cnt, &(pkt.flags), sizeof(pkt.flags));
cnt+=8; cnt+=sizeof(pkt.flags);
memcpy((*bytes )+cnt, &(pkt.flags), 4); memcpy((*bytes )+cnt, &(pkt.stream_index), sizeof(pkt.stream_index));
cnt+=4; cnt+=sizeof(pkt.stream_index);
memcpy((*bytes )+cnt,&wholeSize, 4); memcpy((*bytes )+cnt,&wholeSize, sizeof(wholeSize));
cnt+=4; cnt+=sizeof(wholeSize);
memcpy((*bytes )+cnt, (char*)"DEADBEEF", 8); memcpy((*bytes )+cnt, (char*)"DEADBEEF", 8);
cnt+=8; cnt+=8;
av_log_set_level(AV_LOG_DEBUG); av_log_set_level(AV_LOG_DEBUG);
...@@ -88,40 +90,42 @@ namespace PacketSerializer { ...@@ -88,40 +90,42 @@ namespace PacketSerializer {
av_log(NULL, AV_LOG_ERROR, "invalid packet"); av_log(NULL, AV_LOG_ERROR, "invalid packet");
return -1; return -1;
} }
memcpy(&(pkt->size), bytes, 4); memcpy(&(pkt->size), bytes, sizeof(pkt->size));
got += 4; got += sizeof(pkt->size);
av_new_packet(pkt, pkt->size); av_new_packet(pkt, pkt->size);
memcpy(pkt->data, bytes + got, pkt->size); memcpy(pkt->data, bytes + got, pkt->size);
got += pkt->size; got += pkt->size;
memcpy(&pkt->side_data_elems, bytes + got, 4); memcpy(&pkt->side_data_elems, bytes + got, sizeof(pkt->side_data_elems));
got += 4; got += sizeof(pkt->side_data_elems);
for(int i = 0; i < pkt->side_data_elems; i++) { for(int i = 0; i < pkt->side_data_elems; i++) {
memcpy(&(pkt->side_data[i].size), bytes+got, 4); memcpy(&(pkt->side_data[i].size), bytes+got, sizeof(pkt->side_data[i].size));
got += 4; got += sizeof(pkt->side_data[i].size);
memcpy(pkt->side_data[i].data,bytes + got ,pkt->side_data[i].size); memcpy(pkt->side_data[i].data,bytes + got ,pkt->side_data[i].size);
got += pkt->side_data[i].size; got += pkt->side_data[i].size;
memcpy(&(pkt->side_data[i].type), bytes + got, 4); memcpy(&(pkt->side_data[i].type), bytes + got, sizeof(pkt->side_data[i].type));
got += 4; got += sizeof(pkt->side_data[i].type);
} }
// props // props
memcpy(&(pkt->pts), bytes + got, 8); memcpy(&(pkt->pts), bytes + got, sizeof(pkt->pts));
got += 8; got += sizeof(pkt->pts);
memcpy(&(pkt->dts), bytes + got, 8); memcpy(&(pkt->dts), bytes + got, sizeof(pkt->dts));
got += 8; got += sizeof(pkt->dts);
memcpy(&(pkt->pos), bytes + got, 8); memcpy(&(pkt->pos), bytes + got, sizeof(pkt->pos));
got += 8; got += sizeof(pkt->pos);
memcpy(&(pkt->duration), bytes + got, 8); memcpy(&(pkt->duration), bytes + got, sizeof(pkt->duration));
got += 8; got += sizeof(pkt->duration);
memcpy(&(pkt->convergence_duration), bytes + got, 8); memcpy(&(pkt->convergence_duration), bytes + got, sizeof(pkt->convergence_duration));
got += 8; got += sizeof(pkt->convergence_duration);
memcpy(&(pkt->flags), bytes + got, 4); memcpy(&(pkt->flags), bytes + got, sizeof(pkt->flags));
got += 4; got += sizeof(pkt->flags);
memcpy(&(pkt->stream_index), bytes + got, sizeof(pkt->stream_index));
got += sizeof(pkt->stream_index);
int wholeSize = 0; int wholeSize = 0;
memcpy(&wholeSize, bytes + got, 4); memcpy(&wholeSize, bytes + got, sizeof(wholeSize));
got +=4; got += sizeof(wholeSize);
got+=8; got += 8;
av_log(NULL, AV_LOG_WARNING, "wholeSize: %d, %d\n", wholeSize, got); av_log(NULL, AV_LOG_WARNING, "wholeSize: %d, %d\n", wholeSize, got);
return ret; return ret;
...@@ -132,5 +136,18 @@ void mqPacketFree(void *data, void*hint) { ...@@ -132,5 +136,18 @@ void mqPacketFree(void *data, void*hint) {
free(data); free(data);
} }
namespace AVFormatSerializer {
int encode(AVFormatContext &context, char **bytes) {
int ret = 0;
int wholeSize = 0;
int got = 0;
// num streams
wholeSize += sizeof(context.nb_streams);
for(int i = 0; i < wholeSize; i++) {
}
}
}
#endif #endif
...@@ -144,6 +144,8 @@ int main(int argc, char **argv) ...@@ -144,6 +144,8 @@ int main(int argc, char **argv)
av_packet_unref(&packet); av_packet_unref(&packet);
continue; continue;
} }
av_log(NULL, AV_LOG_ERROR, "pts: %lld, dts:%lld, dur: %lld\n", packet.pts, packet.dts, packet.duration);
packet.stream_index = streamList[packet.stream_index]; packet.stream_index = streamList[packet.stream_index];
out_stream = pAVFormatRemux->streams[packet.stream_index]; out_stream = pAVFormatRemux->streams[packet.stream_index];
/* copy packet */ /* copy packet */
...@@ -151,6 +153,8 @@ int main(int argc, char **argv) ...@@ -151,6 +153,8 @@ int main(int argc, char **argv)
packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX)); packet.dts = av_rescale_q_rnd(packet.dts, in_stream->time_base, out_stream->time_base, AVRounding(AV_ROUND_NEAR_INF|AV_ROUND_PASS_MINMAX));
packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base); packet.duration = av_rescale_q(packet.duration, in_stream->time_base, out_stream->time_base);
packet.pos = -1; packet.pos = -1;
av_log(NULL, AV_LOG_WARNING, "pts: %lld, dts:%lld, dur: %lld, idx: %d\n", packet.pts, packet.dts, packet.duration, packet.stream_index);
ret = av_interleaved_write_frame(pAVFormatRemux, &packet); ret = av_interleaved_write_frame(pAVFormatRemux, &packet);
if (ret < 0) { if (ret < 0) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论