-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXDemux.cpp
More file actions
213 lines (192 loc) · 5.07 KB
/
Copy pathXDemux.cpp
File metadata and controls
213 lines (192 loc) · 5.07 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
208
209
210
211
212
213
#include "XDemux.h"
#include "AVPacketRAII.h"
#include <iostream>
using namespace std;
extern "C" {
#include "libavformat/avformat.h"
}
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avcodec.lib")
static double r2d(AVRational r)
{
return r.den == 0 ? 0 : (double)r.num / (double)r.den;
}
bool XDemux::Open(const char* url)
{
Close();
//参数设置
AVDictionary* opts = NULL;
//设置rtsp流已tcp协议打开
av_dict_set(&opts, "rtsp_transport", "tcp", 0);
//网络延时时间
av_dict_set(&opts, "max_delay", "500", 0);
std::unique_lock<mutex> lck(mux);
int re = avformat_open_input(
&ic,
url,
0, // 0表示自动选择解封器
&opts //参数设置,比如rtsp的延时时间
);
if (re != 0)
{
lck.unlock();
char buf[1024] = { 0 };
av_strerror(re, buf, sizeof(buf) - 1);
cout << "open " << url << " failed! :" << buf << endl;
return false;
}
cout << "open " << url << " success! " << endl;
//获取流信息
re = avformat_find_stream_info(ic, 0);
//总时长 毫秒
totalMs = ic->duration / (AV_TIME_BASE / 1000);
cout << "totalMs = " << totalMs << endl;
//打印视频流详细信息
av_dump_format(ic, 0, url, 0);
//获取视频流
videoStream = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
AVStream* as = ic->streams[videoStream];
width = as->codecpar->width;
height = as->codecpar->height;
cout << "=======================================================" << endl;
cout << videoStream << "视频信息" << endl;
cout << "codec_id = " << as->codecpar->codec_id << endl;
cout << "format = " << as->codecpar->format << endl;
cout << "width=" << as->codecpar->width << endl;
cout << "height=" << as->codecpar->height << endl;
//帧率 fps 分数转换
cout << "video fps = " << r2d(as->avg_frame_rate) << endl;
cout << "=======================================================" << endl;
cout << audioStream << "音频信息" << endl;
//获取音频流
audioStream = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
as = ic->streams[audioStream];
sampleRate = as->codecpar->sample_rate;
channels = as->codecpar->channels;
cout << "codec_id = " << as->codecpar->codec_id << endl;
cout << "format = " << as->codecpar->format << endl;
cout << "sample_rate = " << as->codecpar->sample_rate << endl;
//AVSampleFormat;
cout << "channels = " << as->codecpar->channels << endl;
//一帧数据?? 单通道样本数
cout << "frame_size = " << as->codecpar->frame_size << endl;
//1024 * 2 * 2 = 4096 fps = sample_rate/frame_size
return true;
}
//清空读取缓存
void XDemux::Clear()
{
std::unique_lock<mutex> lck(mux);
if (!ic)
{
return;
}
//清理读取缓冲
avformat_flush(ic);
}
void XDemux::Close()
{
std::unique_lock<mutex> lck(mux);
if (!ic)
{
return;
}
avformat_close_input(&ic);
//媒体总时长(毫秒)
totalMs = 0;
}
//seek 位置 pos 0.0 ~1.0
bool XDemux::Seek(double pos)
{
std::unique_lock<mutex> lck(mux);
if (!ic)
{
return false;
}
//清理读取缓冲
avformat_flush(ic);
long long seekPos = 0;
seekPos = ic->streams[videoStream]->duration * pos;
int re = av_seek_frame(ic, videoStream, seekPos, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME);
if (re < 0) return false;
return true;
}
//获取视频参数 返回的空间需要清理 avcodec_parameters_free
AVCodecParameters* XDemux::CopyVPara()
{
std::unique_lock<mutex> lck(mux);
if (!ic) {
return nullptr;
}
AVCodecParameters* pa = avcodec_parameters_alloc();
avcodec_parameters_copy(pa, ic->streams[videoStream]->codecpar);
return pa;
}
//获取音频参数 返回的空间需要清理 avcodec_parameters_free
AVCodecParameters* XDemux::CopyAPara()
{
std::unique_lock<mutex> lck(mux);
if (!ic) {
return nullptr;
}
AVCodecParameters* pa = avcodec_parameters_alloc();
avcodec_parameters_copy(pa, ic->streams[audioStream]->codecpar);
return pa;
}
shared_ptr<AVPacketRAII> XDemux::ReadVideo() {
shared_ptr<AVPacketRAII> pkt;
// 防止阻塞
for (int i = 0; i < 20; i++) {
pkt = Read();
if (!pkt)break;
if (pkt->get_avpacket()->stream_index == videoStream) break;
}
return pkt;
}
bool XDemux::IsAudio(shared_ptr<AVPacketRAII> pkt)
{
if (!pkt) return false;
if (pkt->get_avpacket()->stream_index == videoStream)
return false;
return true;
}
//空间需要调用者释放 ,释放AVPacket对象空间,和数据空间 av_packet_free
shared_ptr<AVPacketRAII> XDemux::Read()
{
std::unique_lock<mutex> lck(mux);
if (!ic) //容错
{
return 0;
}
shared_ptr<AVPacketRAII> pkt = make_shared<AVPacketRAII>();
//读取一帧,并分配空间
int re = av_read_frame(ic, pkt->get_avpacket());
if (re != 0)
{
return 0;
}
lck.unlock();
//pts转换为毫秒
pkt->get_avpacket()->pts = pkt->get_avpacket()->pts * (1000 * (r2d(ic->streams[pkt->get_avpacket()->stream_index]->time_base)));
pkt->get_avpacket()->dts = pkt->get_avpacket()->dts * (1000 * (r2d(ic->streams[pkt->get_avpacket()->stream_index]->time_base)));
// cout << pkt->get_avpacket()->pts << " " << flush;
return pkt;
}
XDemux::XDemux()
{
static bool isFirst = true;
static std::mutex dmux;
std::unique_lock<mutex> lck(dmux);
if (isFirst)
{
//初始化封装库
av_register_all();
//初始化网络库 (可以打开rtsp rtmp http 协议的流媒体视频)
avformat_network_init();
isFirst = false;
}
}
XDemux::~XDemux()
{
}