-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXDemux.cpp
More file actions
272 lines (238 loc) · 5.8 KB
/
XDemux.cpp
File metadata and controls
272 lines (238 loc) · 5.8 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "XDemux.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);
//解封装上下文
//AVFormatContext* ic = NULL;
mux.lock();
int re = avformat_open_input(
&ic,
url,
0, // 0表示自动选择解封器
&opts //参数设置,比如rtsp的延时时间
);
if (re != 0)
{
mux.unlock();
char buf[1024] = { 0 };
cout << "open " << url << " failed! :" << buf << endl;
//getchar();
return false;
}
cout << "open " << url << " success! " << endl;
//获取流信息
re = avformat_find_stream_info(ic, 0);
//总时长 毫秒
this->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 << "codec_id = " << as->codecpar->codec_id << endl;
cout << "format = " << as->codecpar->format << endl;
cout << videoStream << "视频信息" << 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;
//获取音频流
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
mux.unlock();
return true;
}
//清空读取缓存
void XDemux::Clear()
{
mux.lock();
if (!ic) {
mux.unlock();
return;
}
int re = avformat_flush(ic);
mux.unlock();
if (re < 0) {
return;
}
}
void XDemux::Close() {
mux.lock();
if (!ic) {
mux.unlock();
return;
}
avformat_close_input(&ic);//这里会清空所有缓存
totalMs = 0;
mux.unlock();
}
//seek 位置 0.0~1.0
bool XDemux::Seek(double pos)
{
mux.lock();
if (!ic) {
mux.unlock();
return false;
}
//考虑ic->streams[videoStream]->duration 不存在的情况
//if(!ic->streams[videoStream]->duration) {
// //pos = (double)ms / (double)1000 * r2d(ic->streams[pkt->stream_index]->time_base);
//}
//清理读取缓冲,读取到了新的位置,防止网络粘包现象
int re = avformat_flush(ic);
if (re < 0) {
mux.unlock();
return false;
}
long long seekPos = 0;
seekPos = ic->streams[videoStream]->duration * pos;
//int ms = 3000; //三秒位置 根据时间基数(分数)转换
//long long pos = (double)ms / (double)1000 * r2d(ic->streams[pkt->stream_index]->time_base);
//seek只是往后跳到关键帧,实际帧还是需要业务来做的,这里需要跟解码模块关联
re = av_seek_frame(ic, videoStream, seekPos, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME);
mux.unlock();
if (re < 0) return false;
return true;
};
//获取视频参数,返回的空间需要清理 avcodec_parameters_free
AVCodecParameters *XDemux::CopyVPara()
{
mux.lock();
if (!ic) {
mux.unlock();
return NULL;
}
AVCodecParameters *pa = avcodec_parameters_alloc();
int re = avcodec_parameters_copy(pa,ic->streams[videoStream]->codecpar);
if (re != 0) {
mux.unlock();
return NULL;
}
mux.unlock();
return pa;
};
//获取音频参数,返回的空间需要清理 avcodec_parameters_free
AVCodecParameters *XDemux::CopyAPara()
{
mux.lock();
if (!ic) {
mux.unlock();
return NULL;
}
AVCodecParameters* pa = avcodec_parameters_alloc();
int re = avcodec_parameters_copy(pa, ic->streams[audioStream]->codecpar);
if (re != 0) {
mux.unlock();
return NULL;
}
mux.unlock();
return pa;
}
//判断音视频
bool XDemux::IsAudio(AVPacket *pkt)
{
if (!pkt) return false;
if (pkt->stream_index == videoStream)
return false;
return true;
};
//只读视频,音频丢弃,空间释放
AVPacket* XDemux::ReadVideo()
{
mux.lock();
if (!ic) {
mux.unlock();
return 0;
}
mux.unlock();
AVPacket* pkt = NULL;
//防止阻塞,读取20帧如果读不到就默认读不到
for (int i = 0; i < 20;i++) {
pkt = Read();
if (!pkt) {
break;
}
//只读video帧
if (pkt->stream_index == videoStream) {
break;
}
//读取到其他帧(音频)释放掉
av_packet_free(&pkt);
}
return pkt;
}
//空间需要调用者释放,释放AVPacket对象空间,和数据空间 av_packet_free
AVPacket *XDemux::Read()
{
mux.lock();
if (!ic) {
mux.unlock();
return false;
}
//分配对象空间
AVPacket *pkt = av_packet_alloc();
//读取一帧,并分配空间
int re = av_read_frame(ic,pkt);
if (re != 0) {
mux.unlock();
av_packet_free(&pkt);
return false;
}
//pts转换成毫秒
pkt->pts = pkt->pts * (1000 * (r2d(ic->streams[pkt->stream_index]->time_base)));
pkt->dts = pkt->dts * (1000 * (r2d(ic->streams[pkt->stream_index]->time_base)));
mux.unlock();
//cout << "pkt->pts:" << pkt->pts << " " << flush;
return pkt;
}
XDemux::XDemux()
{
static bool isFirst = true;
static std::mutex dmux;
dmux.lock();
if (isFirst) {
// 初始化封装库
av_register_all();
//初始化网络库(可以打开rtsp rtmp http 协议的流媒体视频)
avformat_network_init();
isFirst = false;
}
dmux.unlock();
}
XDemux::~XDemux()
{
}