最近一个月一直在研究mdk-sdk音视频组件,这个组件是原qtav作者的最新力作,提供了各种各样的示例demo,不仅限于支持C++,其他各种比如java/flutter/web/android等全部支持,性能上也是杠杠的,目前大概是在V0.23版本,大部分软件发布基本上都是在1.0版本才是比较稳定的,不过目前用下来,感觉还是挺不错的,跨平台,什么windows/linux/macos/ios/android/web等全部支持,底层还支持各种渲染框架。其实mdk底层就是封装的ffmpeg,也可以说核心就是ffmpeg,得益于作者在ffmpeg的使用方面应该是到了如火纯情的境界,主要是三大块,一块是音视频同步、一块是硬解码、一块是渲染到不同的平台。这三大快在音视频领域都是重点也是难点,要想做的稳定做得好性能又好,是很难的。没个十年八年的功力是不行的。
由于mdk-sdk作者也是搞qt开发很多年,所以对qt+mdk的使用提供了非常友好完善的示例。
第一步:下载示例源码 git clone git@github.com:wang-bin/mdk-examples.git
第二步:下载库文件 https://sourceforge.net/projects/mdk-sdk/files/nightly/
第三步:将mdk-sdk目录放到mdk-examples目录下。
第四步:打开projects.pro编译就行,如果本机没有vulkan环境,可以注释vkwindow、qmlrhi 、qmlrhi0项目。
在经历众多音视频组件的各种高强度对比测试验证下,mdk的优点非常多,这里就不多说,缺点就是线程有点多,一个播放基本上就占了30个线程,10个就是300,可能是通过牺牲一部分内存来提升性能,好比谷歌浏览器也是占内存大户,但是性能强悍,目前几乎是一统浏览器江湖。各种播放组件中,对于4K、8K、16K视频文件的解码,开启硬解码模式下,mpv是全宇宙CPU占用最低的,其次就是mdk,vlc的表现最拉垮。可能mpv直接走的屏幕绘制,直接是显卡中的数据绘制的,没有经过内存拷贝。
#include "mdkplayer.h"
#include "videohelper.h"
MdkPlayer::MdkPlayer(QObject *parent) : QObject(parent)
{
//实例化播放对象
player = new mdk::Player;
//默认等比例填充
player->setAspectRatio(mdk::KeepAspectRatio);
//设置缓冲大小
//player->setBufferRange(0, 5000, true);
//渲染回调触发界面更新
player->setRenderCallback([this](void *vo) {
QObject *render = (QObject *)vo;
if (render && render->isWidgetType()) {
QMetaObject::invokeMethod(render, "update");
}
});
//播放状态变化
player->onStateChanged([this](mdk::State state) {
emit stateChanged(state);
});
//媒体状态变化
player->onMediaStatus([this](mdk::MediaStatus oldValue, mdk::MediaStatus newValue) {
emit mediaStatusChanged(newValue);
return true;
});
//各种事件触发
player->onEvent([this](const mdk::MediaEvent & e) {
emit eventChanged(e);
return false;
});
render = NULL;
}
MdkPlayer::~MdkPlayer()
{
//没有设置过渲染窗体释放的时候会崩溃
if (render) {
delete player;
}
}
void MdkPlayer::setMedia(const QString &url)
{
player->setMedia(url.toUtf8().constData());
//设置媒体后立即获取媒体信息/手册说要按照下面这样写
VideoType type = VideoHelper::getVideoType(url);
if (type == VideoType_FileLocal || type == VideoType_FileWeb) {
player->waitFor(mdk::State::Stopped);
player->prepare(0, [this](int64_t, bool *) {
QMetaObject::invokeMethod(parent(), "readMediaInfo");
return true;
});
}
//不做音视频同步
if (type == VideoType_Rtsp) {
player->onSync([] {
return DBL_MAX;
});
}
}
void MdkPlayer::setFilter(const QString &filter)
{
player->setProperty("video.avfilter", filter.toStdString());
}
void MdkPlayer::setDecoders(const QStringList &names)
{
std::vector<std::string> decoders;
foreach (QString name, names) {
decoders.push_back(name.toStdString());
}
player->setDecoders(mdk::MediaType::Video, decoders);
}
void MdkPlayer::setProperty(const std::string &key, const std::string &value)
{
player->setProperty(key, value);
}
void MdkPlayer::play()
{
player->set(mdk::State::Playing);
}
void MdkPlayer::stop()
{
player->set(mdk::State::Stopped);
}
void MdkPlayer::pause()
{
player->set(mdk::State::Paused);
}
void MdkPlayer::next()
{
player->set(mdk::State::Playing);
}
mdk::State MdkPlayer::state()
{
return player->state();
}
void MdkPlayer::setLoop(int count)
{
player->setLoop(count);
}
void MdkPlayer::rotate(int degree, QObject *render)
{
player->rotate(degree, render);
}
void MdkPlayer::setAspect(float value, QObject *render)
{
player->setAspectRatio(value, render);
}
void MdkPlayer::setBackgroundColor(float r, float g, float b, float a, QObject *render)
{
player->setBackgroundColor(r, g, b, a, render);
}
QSize MdkPlayer::getSize(int stream)
{
QSize size;
std::vector<mdk::VideoStreamInfo> video = player->mediaInfo().video;
if (video.size() > stream) {
mdk::VideoCodecParameters para = video.at(stream).codec;
size = QSize(para.width, para.height);
}
return size;
}
qint64 MdkPlayer::duration()
{
return player->mediaInfo().duration;
}
qint64 MdkPlayer::position()
{
return player->position();
}
void MdkPlayer::seek(qint64 position)
{
player->seek(position);
}
void MdkPlayer::seek(bool backward, int frame)
{
mdk::SeekFlag flags = (mdk::SeekFlag::FromNow | mdk::SeekFlag::Frame);
if (backward) {
player->seek(-frame, flags);
} else {
player->seek(frame, flags);
}
}
float MdkPlayer::playbackRate()
{
return player->playbackRate();
}
void MdkPlayer::setPlaybackRate(float value)
{
player->setPlaybackRate(value);
}
float MdkPlayer::volume()
{
return player->volume();
}
void MdkPlayer::setVolume(float value)
{
player->setVolume(value);
}
bool MdkPlayer::isMute()
{
return player->isMute();
}
void MdkPlayer::setMute(bool value)
{
player->setMute(value);
}
void MdkPlayer::snapshot(int rotate, QObject *render)
{
mdk::Player::SnapshotRequest sr{};
player->snapshot(&sr, [this, rotate](mdk::Player::SnapshotRequest * sr2, double) {
QImage image = QImage(sr2->data, sr2->width, sr2->height, Format_RGB);
//如果有旋转角度先要旋转
VideoHelper::rotateImage(rotate, image);
emit imageCaptured(image.copy());
return std::string();
}, render);
}
void MdkPlayer::record(const QString &fileName, const QString &format)
{
player->record(fileName.toUtf8().constData(), format.toUtf8().constData());
}
void MdkPlayer::readMetaData()
{
//标题/艺术家/专辑/专辑封面
QString title, artist, album;
mdk::MediaInfo mediaInfo = player->mediaInfo();
std::unordered_map<std::string, std::string> metadata = mediaInfo.metadata;
for (auto i = metadata.begin(); i != metadata.end(); i++) {
QString key = QString::fromStdString(i->first);
QString value = QString::fromStdString(i->second);
if (key == "title") {
title = value;
} else if (key == "artist") {
artist = value;
} else if (key == "album") {
album = value;
}
}
QString format = mediaInfo.format;
emit receiveMetaData(format, title, artist, album);
}
void MdkPlayer::readAudioInfo(int index)
{
std::vector<mdk::AudioStreamInfo> audios = player->mediaInfo().audio;
if (index >= 0 && audios.size() > index) {
mdk::AudioStreamInfo audio = audios.at(index);
mdk::AudioCodecParameters para = audio.codec;
emit receiveAudioInfo(audio.index, para.sample_rate, para.channels, para.profile, para.bit_rate, para.codec);
}
}
void MdkPlayer::readVideoInfo(int index)
{
std::vector<mdk::VideoStreamInfo> videos = player->mediaInfo().video;
if (index >= 0 && videos.size() > index) {
mdk::VideoStreamInfo video = videos.at(index);
mdk::VideoCodecParameters para = video.codec;
//取出封面
QImage image;
if (video.image_size > 0) {
image = QImage::fromData(video.image_data, video.image_size);
}
emit receiveVideoInfo(video.index, para.width, para.height, para.frame_rate, video.rotation, para.codec, image);
}
}
void MdkPlayer::readTrackInfo(QList<int> &audioTracks, QList<int> &videoTracks)
{
mdk::MediaInfo mediaInfo = player->mediaInfo();
audioTracks.clear();
std::vector<mdk::AudioStreamInfo> audioTrackInfo = mediaInfo.audio;
foreach (mdk::AudioStreamInfo info, audioTrackInfo) {
audioTracks << info.index;
}
videoTracks.clear();
std::vector<mdk::VideoStreamInfo> videoTrackInfo = mediaInfo.video;
foreach (mdk::VideoStreamInfo info, videoTrackInfo) {
videoTracks << info.index;
}
//可能获取到的索引是 音频(3, 4, 5) / 视频(0, 1, 2)
//底层设置节目流按照 0/1/2 这样排列/所以需要强制矫正
int count = videoTracks.count();
audioTracks.clear();
videoTracks.clear();
for (int i = 0; i < count; ++i) {
audioTracks << i;
videoTracks << i;
}
}
void MdkPlayer::setAudioTrack(int track)
{
std::set<int> tracks;
tracks.insert(track);
player->setActiveTracks(mdk::MediaType::Audio, tracks);
}
void MdkPlayer::setVideoTrack(int track)
{
std::set<int> tracks;
tracks.insert(track);
player->setActiveTracks(mdk::MediaType::Video, tracks);
}
void MdkPlayer::renderVideo(QObject *render)
{
player->renderVideo(render);
}
void MdkPlayer::setVideoSurfaceSize(int width, int height, QObject *render)
{
this->render = render;
player->setVideoSurfaceSize(width, height, render);
connect(render, SIGNAL(destroyed(QObject *)), this, SLOT(clear(QObject *)), Qt::ConnectionType(Qt::DirectConnection | Qt::UniqueConnection));
}
void MdkPlayer::clear(QObject *render)
{
player->setVideoSurfaceSize(-1, -1, render);
}