Skip to content

Commit

Permalink
1.fixed some problem
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Dec 9, 2021
1 parent 0fb26b0 commit 1dfdb40
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 41 deletions.
3 changes: 3 additions & 0 deletions video_player_core/module/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#include <functional>
#include "Log/Log.h"

//#define _DESKTOP_

extern "C"
{
#include "libavdevice/avdevice.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/time.h"
Expand Down
3 changes: 2 additions & 1 deletion video_player_core/module/decoder/core_decoder_hardware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ void core_decoder_hardware::calcClock()
}

pts_video *= av_q2d(stream->time_base);
_clock = pts_video;
//_clock = pts_video;
setClock(pts_video);
}

AVPixelFormat core_decoder_hardware::getFormatCUDA(AVCodecContext *ctx, const AVPixelFormat *srcFormat)
Expand Down
28 changes: 27 additions & 1 deletion video_player_core/module/media/core_media.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,21 @@ void av_log_call(void* /*ptr*/, int level, const char* fmt, va_list vl)
}
}

int core_media::interruptCallback(void* para)
{
if(!para)
return 0;
auto pThis = reinterpret_cast<core_media*>(para);
if(pThis->testFlag(flag_bit_Stop))
{
return 1;
}
else
{
return 0;
}
}

bool core_media::open(int index)
{
av_log_set_callback(&av_log_call);
Expand All @@ -229,10 +244,20 @@ bool core_media::open(int index)
_cb->startCall(index);
// allocate context
_format_ctx = avformat_alloc_context();
_format_ctx->interrupt_callback.callback = interruptCallback;
_format_ctx->interrupt_callback.opaque = this;
auto& pFormatCtx = _format_ctx;
Log(Log_Info, "start open input. thread:%d", core_util::getThreadId());
// open file
if((ret = avformat_open_input(&pFormatCtx, _src.c_str(), nullptr, nullptr)) < 0)
#ifdef _DESKTOP_
// AVInputFormat *ifmt=av_find_input_format("gdigrab");
// ret = avformat_open_input(&pFormatCtx, "desktop", ifmt, nullptr);
AVInputFormat *ifmt=av_find_input_format("dshow");
ret = avformat_open_input(&pFormatCtx, "video=Full HD webcam", ifmt, nullptr);
#else
ret = avformat_open_input(&pFormatCtx, _src.c_str(), nullptr, nullptr);
#endif
if(ret < 0)
{
char buf[1024] = {};
av_strerror(ret, buf, 1024);
Expand Down Expand Up @@ -329,6 +354,7 @@ bool core_media::open(int index)
_audio_thread->start(this);

decodeloop();
Log(Log_Info, "down.");
return true;
media_start_end:
Log(Log_Info, "finished.");
Expand Down
1 change: 1 addition & 0 deletions video_player_core/module/media/core_media.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class core_media
int audioIndex();
void decodeloop();
private:
static int interruptCallback(void* ctx);
void setFlag(int bit, bool value = true);
bool testFlag(int bit);
void setState(int state);
Expand Down
8 changes: 5 additions & 3 deletions video_player_core/module/preview/core_preview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void core_preview::preview(const std::string& src, int64_t ms, video_interface*
avcodec_flush_buffers(ctx);
AVPacket pk;
int ret = 0;
bool bFirst = false;
bool bFirstIFrame = false;
while(1)
{
ret = av_read_frame(pFormatCtx, &pk);
Expand All @@ -76,13 +76,13 @@ void core_preview::preview(const std::string& src, int64_t ms, video_interface*
continue;
}

if(!bFirst && !(pk.flags & AV_PKT_FLAG_KEY))
if(!bFirstIFrame && !(pk.flags & AV_PKT_FLAG_KEY))
{
av_packet_unref(&pk);
continue;
}

bFirst = true;
bFirstIFrame = true;

ret = avcodec_send_packet(ctx, &pk);
if( ret != 0 && ret != AVERROR(EAGAIN) )
Expand All @@ -103,6 +103,8 @@ void core_preview::preview(const std::string& src, int64_t ms, video_interface*
}
}

std::string sData = "seek:" + std::to_string(pk.pts) + "\n";
OutputDebugStringA(sData.c_str());
av_packet_unref(&pk);
outFrame->scalePreview(frame, cb);
}
Expand Down
64 changes: 42 additions & 22 deletions video_player_core/module/sdl/core_sdl_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ bool core_sdl_op::initResample(AVCodecContext* ctx)
bool core_sdl_op::init(audioCallback callback, void* userdata)
{
int nRet = 0;
if(SDL_WasInit(nRet))
if(SDL_WasInit(SDL_INIT_AUDIO))
{
return true;
}
Expand Down Expand Up @@ -130,14 +130,24 @@ int core_sdl_op::initSDL()

int core_sdl_op::openSDL()
{
auto num = SDL_GetNumAudioDevices(0);
for(int i = 0; i <= num; ++i)
if (SDL_OpenAudio(&target, NULL)<0)
{
auto name = SDL_GetAudioDeviceName(i, 0);
nAudioId = SDL_OpenAudioDevice(name, false, &target, &spec, 0);
Log(Log_Info, "audio device id=%d name%d=%s!", nAudioId, i, name);
if(nAudioId > 0) return nAudioId;
return -1;
}
else
{
return 0;
}

// auto num = SDL_GetNumAudioDevices(0);
// for(int i = 0; i <= num; ++i)
// {
// auto name = SDL_GetAudioDeviceName(i, 0);
// Log(Log_Info, "audio device name[%d]=%s start!", i, name);
// nAudioId = SDL_OpenAudioDevice(name, false, &target, &spec, SDL_AUDIO_ALLOW_FREQUENCY_CHANGE);
// Log(Log_Info, "audio device id=%d name%d=%s!", nAudioId, i, name);
// if(nAudioId > 0) return nAudioId;
// }

// Log(Log_Warning, "sdl open failed, audio devices count:%d, %s", num, SDL_GetError());
return -1;
Expand All @@ -149,30 +159,40 @@ void core_sdl_op::closeSDL()
if(nAudioId > 0)
{
Log(Log_Info, "sdl close:%d, thread:%d, 0x%p", nAudioId, std::this_thread::get_id(), this);
SDL_LockAudioDevice(nAudioId);
SDL_CloseAudioDevice(nAudioId);
SDL_UnlockAudioDevice(nAudioId);
// SDL_LockAudioDevice(nAudioId);
// SDL_CloseAudioDevice(nAudioId);
// SDL_UnlockAudioDevice(nAudioId);

SDL_LockAudio();
SDL_CloseAudio();
SDL_UnlockAudio();
Log(Log_Info, "sdl close:successed, thread: %d", std::this_thread::get_id());
nAudioId = -1;
}
}

void core_sdl_op::startSDL()
{
if(nAudioId > 0)
{
SDL_LockAudioDevice(nAudioId);
SDL_PauseAudioDevice(nAudioId, 0);
SDL_UnlockAudioDevice(nAudioId);
}
SDL_LockAudio();
SDL_PauseAudio(0);
SDL_UnlockAudio();
// if(nAudioId > 0)
// {
// SDL_LockAudioDevice(nAudioId);
// SDL_PauseAudioDevice(nAudioId, 0);
// SDL_UnlockAudioDevice(nAudioId);
// }
}

void core_sdl_op::pauseSDL()
{
if(nAudioId > 0)
{
SDL_LockAudioDevice(nAudioId);
SDL_PauseAudioDevice(nAudioId, 1);
SDL_UnlockAudioDevice(nAudioId);
}
SDL_LockAudio();
SDL_PauseAudio(1);
SDL_UnlockAudio();
// if(nAudioId > 0)
// {
// SDL_LockAudioDevice(nAudioId);
// SDL_PauseAudioDevice(nAudioId, 1);
// SDL_UnlockAudioDevice(nAudioId);
// }
}
4 changes: 4 additions & 0 deletions video_player_core/video_player_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ video_player_core::~video_player_core()
int video_player_core::_init()
{
#ifndef AVFORMAT_STATIC_REGISTER
#ifdef _DESKTOP_
avdevice_register_all();
#endif
avcodec_register_all();
av_register_all();
#endif
Log(Log_Info, "avformat_network_init()=%d", avformat_network_init());
Expand Down
1 change: 1 addition & 0 deletions video_player_ui/platform/model/qegamemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void QEGameModel::loadGameRoomPage(const QString &url, int pg)
auto liveData = data1.value("live_data").toObject();
auto arr = liveData.value("live_list").toArray();
waitClearRooms();

for(auto it : arr)
{
auto arrObj = it.toObject();
Expand Down
41 changes: 34 additions & 7 deletions video_player_ui/platform/model/qhuyamodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include <QScriptEngine>
#include <QCryptographicHash>
#include <QEventLoop>

#define _OUTPUT_DEBUG

QHuYaModel::QHuYaModel(QObject *parent)
: QLiveDataModel(parent)
{
Expand Down Expand Up @@ -130,23 +133,47 @@ void QHuYaModel::loadGameRoomPage(const QString &url, int nPage)
appendRequest(request, func);
}

#include <QFile>
void QHuYaModel::getPreviewUrl(const QString &sRid)
{
auto func = [=](QNetworkReply *response)
{
QString text = QString::fromUtf8(response->readAll());
QRegExp re("\"stream\": \"(.*)/(.*)==");


QRegExp re("\"stream\": \"(.*)/(.*)\"");
re.setMinimal(true);
re.indexIn(text);
QString data = QString::fromUtf8(QByteArray::fromBase64(re.cap(2).toUtf8()));
QRegExp re1("\"sStreamName\":\"(.*)\"");
re1.setMinimal(true);
re1.indexIn(data);
auto sLiveUrl = QString("rtmp://tx.flv.huya.com/src/%1").arg(re1.cap(1));
emit play(sLiveUrl);
int n = 0;
for(auto it : re.capturedTexts())
{
QString data = QString::fromUtf8(QByteArray::fromBase64(it.toUtf8()));

#ifdef _OUTPUT_DEBUG
QFile f;
f.setFileName(QString("D://%1.html").arg(n++));
f.open(QFile::ReadWrite | QFile::Truncate);
f.write(data.toUtf8());
f.close();
qDebug() << "[preview]" << data.length();
#endif

QRegExp re1("\"sStreamName\":\"(.*)\"");
re1.setMinimal(true);
if(-1 == re1.indexIn(data))
continue;

auto realUrl = re1.cap(1);
auto sLiveUrl = QString("rtmp://tx.flv.huya.com/src/%1").arg(realUrl);
emit play(sLiveUrl);
return;
}
};

auto url = QString("https://www.huya.com/%1").arg(sRid);
#ifdef _OUTPUT_DEBUG
qDebug() << "start get preview url:" << url;
#endif
auto request = new QNetworkRequest();
request->setUrl(QUrl(url));
appendRequest(request, func);
Expand Down
2 changes: 2 additions & 0 deletions video_player_ui/platform/model/qnetworkqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ void QNetWorkQueue::threadFunc()
else
reply = m_net->post(*req->req, req->sData);
auto error = reply->error();
// qDebug() << req->req->url();
// qDebug() << reply->error();
if(error != QNetworkReply::NoError)
qDebug() << reply->errorString() << error;
loop.exec();
Expand Down
1 change: 0 additions & 1 deletion video_player_ui/platform/room/qroomlistdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ void QRoomListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt
painter->setRenderHints(QPainter::SmoothPixmapTransform, true);
auto rc = viewOption.rect;
auto info = reinterpret_cast<_DyRoom_*>(index.data(QRoomListViewModel::room_info_role).value<void*>());
// qDebug() << index << rc;
int borderWidth = 2;
int offset = borderWidth;
int padding = 3;
Expand Down
2 changes: 1 addition & 1 deletion video_player_ui/platform/room/qroomlistviewmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ QRoomListViewModel::QRoomListViewModel(QObject *parent)
: QAbstractListModel(parent)
, m_room(nullptr)
{
m_itemSize = CALC_SIZE(300.0f / 1920, 260.0f / 1080);
m_itemSize = CALC_SIZE(300, 260);
}

QVariant QRoomListViewModel::headerData(int /*section*/, Qt::Orientation /*orientation*/, int /*role*/) const
Expand Down
8 changes: 4 additions & 4 deletions video_player_ui/res.rc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ IDI_ICON1 ICON DISCARDABLE "Resources/app.ico"
#endif

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,17
PRODUCTVERSION 1,0,0,17
FILEVERSION 1,0,0,18
PRODUCTVERSION 1,0,0,18
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS VS_FF_DEBUG
Expand All @@ -27,8 +27,8 @@ IDI_ICON1 ICON DISCARDABLE "Resources/app.ico"
BEGIN
VALUE "CompanyName", ""
VALUE "FileDescription", "vPlay"
VALUE "FileVersion", "1.0.0.2\0"
VALUE "ProductVersion", "1.0.0.2\0"
VALUE "FileVersion", "1.0.0.3\0"
VALUE "ProductVersion", "1.0.0.3\0"
VALUE "LegalCopyright", "\0"
VALUE "LegalTrademarks", ""
VALUE "OriginalFilename", "vPlay.exe\0"
Expand Down
4 changes: 4 additions & 0 deletions video_player_ui/ui.pri
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ win32
{
INCLUDEPATH += F:/DX11/Include
LIBS += -LF:/DX11/lib/x86 -ld3d11 -ld3dx11 -ld3dcompiler

# Tips: admin need administrator startup QtCreator
# QMAKE_LFLAGS += /MANIFESTUAC:\"level=\'requireAdministrator\' uiAccess=\'false\'\"
# QMAKE_LFLAGS += /SUBSYSTEM:WINDOWS,\"5.01\"
}

# ui
Expand Down
4 changes: 3 additions & 1 deletion video_player_ui/ui/qtoolwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ void QToolWidgets::onFull()

void QToolWidgets::onSubtitle(const QString &str, unsigned int /*index*/)
{
// qDebug() << str << index;
enum enum_sub_title
{
Sub_Titl_Time_Begin = 1,
Expand Down Expand Up @@ -600,7 +601,7 @@ QWidget *QToolWidgets::CreateToolbar(QWidget *parent)
connect(this, &QToolWidgets::selectMode, [stop, prev, play, next](int mode)
{
bool bShow = (mode == QPlayFileListModel::play_mode_local);
stop->setVisible(bShow);
// stop->setVisible(bShow);
prev->setVisible(bShow);
// play->setVisible(bShow);
next->setVisible(bShow);
Expand Down Expand Up @@ -835,6 +836,7 @@ QWidget *QToolWidgets::CreateSubTitle(QWidget *parent)
// }
// });

connect(m_timerDisplay, &QTimer::timeout, this, [=]{ ch->clear(); other->clear(); });
return widget;
}

Expand Down

0 comments on commit 1dfdb40

Please sign in to comment.