-
Notifications
You must be signed in to change notification settings - Fork 0
/
demuxutil.cpp
220 lines (208 loc) · 6.48 KB
/
demuxutil.cpp
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
#include "demuxutil.h"
static double fraction2double(AVRational r)
{
return r.den == 0 ? 0 : (double)r.num / r.den;
}
DemuxUtil::DemuxUtil()
{
static bool isFirst = true;
lock.lock();
if (isFirst)
{
// 初始化网络库
avformat_network_init();
isFirst = false;
}
lock.unlock();
}
DemuxUtil::~DemuxUtil() {}
// 打开本地媒体文件或者流媒体(rtmp HTTP rstp)
bool DemuxUtil::Open(const char* url)
{
Close();
int result;
// 参数设置
AVDictionary* options = nullptr;
// 设置rtsp流以tcp协议打开
av_dict_set(&options, "rtsp_transport", "tcp", 0);
// 设置网络延时
av_dict_set(&options, "max_delay", "500", 0);
lock.lock();
result = avformat_open_input(&fmt_ctx, url, nullptr, &options);
if (result != 0)
{
lock.unlock();
char errbuf[64] = {0};
qDebug() << "Can't open the file!" << av_make_error_string(errbuf, 64, result);
return false;
}
// 获取流信息
result = avformat_find_stream_info(fmt_ctx, nullptr);
// 获取总时长(毫秒)
totalMilliseconds = fmt_ctx->duration / (AV_TIME_BASE / 1000);
// 获取总时长(秒)
int totalSeconds = fmt_ctx->duration / AV_TIME_BASE;
qDebug() << QString::fromLocal8Bit("获取媒体总时长(秒)") << totalSeconds;
// 打印视频流信息
av_dump_format(fmt_ctx, 0, url, 0);
// 获取音视频流信息
videoStream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0);
AVStream* vStream = fmt_ctx->streams[videoStream];
qDebug() << QString::fromLocal8Bit("------------------------------------------------------------");
qDebug() << QString::fromLocal8Bit("视频信息(videoStream)") << videoStream;
qDebug() << QString::fromLocal8Bit("视频宽度") << vStream->codecpar->width;
qDebug() << QString::fromLocal8Bit("视频高度") << vStream->codecpar->height;
qDebug() << QString::fromLocal8Bit("视频帧率FPS") << fraction2double(vStream->avg_frame_rate);
qDebug() << QString::fromLocal8Bit("视频像素格式") << vStream->codecpar->format;
qDebug() << QString::fromLocal8Bit("------------------------------------------------------------");
audioStream = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, nullptr, 0);
AVStream* aStream = fmt_ctx->streams[audioStream];
qDebug() << QString::fromLocal8Bit("音频信息(audioStream)") << audioStream;
qDebug() << QString::fromLocal8Bit("样本率") << aStream->codecpar->sample_rate;
qDebug() << QString::fromLocal8Bit("音频采样格式") << aStream->codecpar->format;
qDebug() << QString::fromLocal8Bit("声道数") << aStream->codecpar->channels;
qDebug() << QString::fromLocal8Bit("编码格式") << aStream->codecpar->codec_id;
qDebug() << QString::fromLocal8Bit("音频FPS") << fraction2double(aStream->avg_frame_rate);
qDebug() << QString::fromLocal8Bit("一帧音频的大小") << aStream->codecpar->frame_size;
qDebug() << QString::fromLocal8Bit("------------------------------------------------------------");
lock.unlock();
return true;
}
// 调用者使用av_packet_free()释放对象空间和数据空间
AVPacket* DemuxUtil::Read()
{
lock.lock();
if(!fmt_ctx)
{
lock.unlock();
return nullptr;
}
AVPacket* pkt = av_packet_alloc();
// 读取一帧并分配空间
int result = av_read_frame(fmt_ctx, pkt);
if (result != 0)
{
lock.unlock();
av_packet_free(&pkt);
char errbuf[64] = {0};
qDebug() << QString::fromLocal8Bit("读取AVPacket失败") << av_make_error_string(errbuf, 64, result);
return nullptr;
}
// 转换为毫秒
pkt->pts = pkt->pts * (1000 * (fraction2double(fmt_ctx->streams[pkt->stream_index]->time_base)));
pkt->dts = pkt->dts * (1000 * (fraction2double(fmt_ctx->streams[pkt->stream_index]->time_base)));
// qDebug() << QString::fromLocal8Bit("当前stream_index") << pkt->stream_index << QString::fromLocal8Bit("pts") << pkt->pts;
lock.unlock();
return pkt;
}
// 只读视频,释放音频
AVPacket* DemuxUtil::ReadOnlyVideo()
{
lock.lock();
if (!fmt_ctx)
{
lock.unlock();
return nullptr;
}
lock.unlock();
AVPacket* pkt = nullptr;
for (int i = 0; i < 40; i++)
{
pkt = Read();
if (pkt->stream_index == videoStream)
{
break;
}
av_packet_free(&pkt);
}
return pkt;
}
// 判断是否是音频
bool DemuxUtil::IsAudio(AVPacket* pkt)
{
if (!pkt)
{
return false;
}
if (pkt->stream_index == videoStream)
{
return false;
}
return true;
}
// 获取视频参数,使用avcodec_parameters_free()清理空间
AVCodecParameters* DemuxUtil::getVideoParameters()
{
lock.lock();
if (!fmt_ctx)
{
lock.unlock();
return nullptr;
}
AVCodecParameters* videoParameters = avcodec_parameters_alloc();
avcodec_parameters_copy(videoParameters, fmt_ctx->streams[videoStream]->codecpar);
lock.unlock();
return videoParameters;
}
// 获取音频参数,使用avcodec_parameters_free()清理空间
AVCodecParameters* DemuxUtil::getAudioParameters()
{
lock.lock();
if (!fmt_ctx)
{
lock.unlock();
return nullptr;
}
AVCodecParameters* audioParameters = avcodec_parameters_alloc();
avcodec_parameters_copy(audioParameters, fmt_ctx->streams[audioStream]->codecpar);
lock.unlock();
return audioParameters;
}
// seek 位置 pos 0.0
bool DemuxUtil::Seek(double pos)
{
lock.lock();
if (!fmt_ctx)
{
lock.unlock();
return false;
}
avformat_flush(fmt_ctx);
long long seekPos = 0;
seekPos = fmt_ctx->streams[videoStream]->duration * pos;
int result = av_seek_frame(fmt_ctx, videoStream, seekPos, AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_FRAME);
lock.unlock();
if (result < 0)
{
char errbuf[64] = {0};
qDebug() << QString::fromLocal8Bit("Seek失败") << av_make_error_string(errbuf, 64, result);
return false;
}
return true;
}
// 清空读取缓存
void DemuxUtil::Clear()
{
lock.lock();
if (!fmt_ctx)
{
lock.unlock();
return;
}
// 清理读取缓存
avformat_flush(fmt_ctx);
lock.unlock();
}
// 关闭
void DemuxUtil::Close()
{
lock.lock();
if (!fmt_ctx)
{
lock.unlock();
return;
}
avformat_close_input(&fmt_ctx);
totalMilliseconds = 0;
lock.unlock();
}