435 lines
11 KiB
C++
435 lines
11 KiB
C++
|
|
/***************************************************************************************
|
||
|
|
*
|
||
|
|
* IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||
|
|
*
|
||
|
|
* By downloading, copying, installing or using the software you agree to this license.
|
||
|
|
* If you do not agree to this license, do not download, install,
|
||
|
|
* copy or use the software.
|
||
|
|
*
|
||
|
|
* Copyright (C) 2014-2024, Happytimesoft Corporation, all rights reserved.
|
||
|
|
*
|
||
|
|
* Redistribution and use in binary forms, with or without modification, are permitted.
|
||
|
|
*
|
||
|
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||
|
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||
|
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
|
||
|
|
* language governing permissions and limitations under the License.
|
||
|
|
*
|
||
|
|
****************************************************************************************/
|
||
|
|
|
||
|
|
#include "rtmp_player.h"
|
||
|
|
#include "utils.h"
|
||
|
|
|
||
|
|
/***************************************************************************************/
|
||
|
|
|
||
|
|
int rtmp_notify_callback(int evt, void * puser)
|
||
|
|
{
|
||
|
|
CRtmpPlayer * pPlayer = (CRtmpPlayer *) puser;
|
||
|
|
|
||
|
|
pPlayer->onNotify(evt);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
int rtmp_audio_callback(uint8 * pdata, int len, uint32 ts, void *puser)
|
||
|
|
{
|
||
|
|
CRtmpPlayer * pPlayer = (CRtmpPlayer *) puser;
|
||
|
|
|
||
|
|
pPlayer->onAudio(pdata, len, ts);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
int rtmp_video_callback(uint8 * pdata, int len, uint32 ts, void *puser)
|
||
|
|
{
|
||
|
|
CRtmpPlayer * pPlayer = (CRtmpPlayer *) puser;
|
||
|
|
|
||
|
|
pPlayer->onVideo(pdata, len, ts);
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
/***************************************************************************************/
|
||
|
|
|
||
|
|
CRtmpPlayer::CRtmpPlayer(QObject * parent)
|
||
|
|
: CVideoPlayer(parent)
|
||
|
|
{
|
||
|
|
memset(m_ip, 0, sizeof(m_ip));
|
||
|
|
}
|
||
|
|
|
||
|
|
CRtmpPlayer::~CRtmpPlayer()
|
||
|
|
{
|
||
|
|
close();
|
||
|
|
}
|
||
|
|
|
||
|
|
BOOL CRtmpPlayer::open(QString fileName, WId hWnd)
|
||
|
|
{
|
||
|
|
close();
|
||
|
|
|
||
|
|
CVideoPlayer::open(fileName, hWnd);
|
||
|
|
|
||
|
|
char host[100];
|
||
|
|
|
||
|
|
url_split(fileName.toStdString().c_str(), NULL, 0, NULL, 0, NULL, 0, host, sizeof(host), NULL, NULL, 0);
|
||
|
|
|
||
|
|
if (host[0] == '\0')
|
||
|
|
{
|
||
|
|
return FALSE;
|
||
|
|
}
|
||
|
|
|
||
|
|
strncpy(m_ip, host, sizeof(m_ip) - 1);
|
||
|
|
|
||
|
|
m_rtmp.set_notify_cb(rtmp_notify_callback, this);
|
||
|
|
m_rtmp.set_audio_cb(rtmp_audio_callback);
|
||
|
|
m_rtmp.set_video_cb(rtmp_video_callback);
|
||
|
|
|
||
|
|
return TRUE;
|
||
|
|
}
|
||
|
|
|
||
|
|
void CRtmpPlayer::close()
|
||
|
|
{
|
||
|
|
// stop rtmp connection
|
||
|
|
m_rtmp.rtmp_stop();
|
||
|
|
m_rtmp.rtmp_close();
|
||
|
|
|
||
|
|
m_bPlaying = FALSE;
|
||
|
|
m_bPaused = FALSE;
|
||
|
|
|
||
|
|
CVideoPlayer::close();
|
||
|
|
}
|
||
|
|
|
||
|
|
BOOL CRtmpPlayer::play()
|
||
|
|
{
|
||
|
|
if (m_rtmp.rtmp_start(m_sFileName.toStdString().c_str(), m_acct.toStdString().c_str(), m_pass.toStdString().c_str()))
|
||
|
|
{
|
||
|
|
m_bPlaying = TRUE;
|
||
|
|
}
|
||
|
|
|
||
|
|
return m_bPlaying;
|
||
|
|
}
|
||
|
|
|
||
|
|
void CRtmpPlayer::stop()
|
||
|
|
{
|
||
|
|
if (m_bPlaying || m_bPaused)
|
||
|
|
{
|
||
|
|
m_rtmp.rtmp_stop();
|
||
|
|
}
|
||
|
|
|
||
|
|
m_bPlaying = FALSE;
|
||
|
|
m_bPaused = FALSE;
|
||
|
|
}
|
||
|
|
|
||
|
|
BOOL CRtmpPlayer::pause()
|
||
|
|
{
|
||
|
|
if (m_bPlaying)
|
||
|
|
{
|
||
|
|
if (m_rtmp.rtmp_pause())
|
||
|
|
{
|
||
|
|
m_bPaused = TRUE;
|
||
|
|
m_bPlaying = FALSE;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else if (m_bPaused)
|
||
|
|
{
|
||
|
|
if (m_rtmp.rtmp_play())
|
||
|
|
{
|
||
|
|
m_bPaused = FALSE;
|
||
|
|
m_bPlaying = TRUE;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return m_bPaused;
|
||
|
|
}
|
||
|
|
|
||
|
|
BOOL CRtmpPlayer::seek(int pos)
|
||
|
|
{
|
||
|
|
return FALSE;
|
||
|
|
}
|
||
|
|
|
||
|
|
int64 CRtmpPlayer::getElapse()
|
||
|
|
{
|
||
|
|
uint32 cur_ts;
|
||
|
|
uint32 init_ts;
|
||
|
|
int frequency;
|
||
|
|
|
||
|
|
if (m_videoClock.SyncTime.tv_sec > m_audioClock.SyncTime.tv_sec)
|
||
|
|
{
|
||
|
|
cur_ts = m_videoClock.SyncTimestamp;
|
||
|
|
init_ts = m_rtmp.get_video_init_ts();
|
||
|
|
frequency = getVideoClock();
|
||
|
|
}
|
||
|
|
else if (m_videoClock.SyncTime.tv_sec == m_audioClock.SyncTime.tv_sec &&
|
||
|
|
m_videoClock.SyncTime.tv_usec > m_audioClock.SyncTime.tv_usec)
|
||
|
|
{
|
||
|
|
cur_ts = m_videoClock.SyncTimestamp;
|
||
|
|
init_ts = m_rtmp.get_video_init_ts();
|
||
|
|
frequency = getVideoClock();
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
cur_ts = m_audioClock.SyncTimestamp;
|
||
|
|
init_ts = m_rtmp.get_audio_init_ts();
|
||
|
|
frequency = getAudioClock();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (init_ts == 0)
|
||
|
|
{
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
int64 elapse;
|
||
|
|
int timestampDiff = cur_ts - init_ts;
|
||
|
|
|
||
|
|
// Divide this by the timestamp frequency to get real time:
|
||
|
|
double timeDiff = timestampDiff / (double)frequency;
|
||
|
|
|
||
|
|
if (timeDiff >= 0.0)
|
||
|
|
{
|
||
|
|
elapse = timeDiff * 1000;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
timeDiff = -timeDiff;
|
||
|
|
elapse = timeDiff * 1000;
|
||
|
|
}
|
||
|
|
|
||
|
|
return elapse;
|
||
|
|
}
|
||
|
|
|
||
|
|
int64 CRtmpPlayer::getDuration()
|
||
|
|
{
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
int CRtmpPlayer::getVideoCodec()
|
||
|
|
{
|
||
|
|
return m_rtmp.video_codec();
|
||
|
|
}
|
||
|
|
|
||
|
|
int CRtmpPlayer::getAudioCodec()
|
||
|
|
{
|
||
|
|
return m_rtmp.audio_codec();
|
||
|
|
}
|
||
|
|
|
||
|
|
void CRtmpPlayer::onNotify(int evt)
|
||
|
|
{
|
||
|
|
if (evt == RTMP_EVE_VIDEOREADY)
|
||
|
|
{
|
||
|
|
int videoCodec = m_rtmp.video_codec();
|
||
|
|
|
||
|
|
if (VIDEO_CODEC_NONE != videoCodec)
|
||
|
|
{
|
||
|
|
BOOL ret;
|
||
|
|
uint8 extradata[1024];
|
||
|
|
int extradata_size = 0;
|
||
|
|
|
||
|
|
if (VIDEO_CODEC_H264 == videoCodec)
|
||
|
|
{
|
||
|
|
ret = m_rtmp.get_h264_params(m_h26XParamSets.sps, &m_h26XParamSets.sps_size,
|
||
|
|
m_h26XParamSets.pps, &m_h26XParamSets.pps_size);
|
||
|
|
if (ret)
|
||
|
|
{
|
||
|
|
memcpy(extradata, m_h26XParamSets.sps, m_h26XParamSets.sps_size);
|
||
|
|
extradata_size += m_h26XParamSets.sps_size;
|
||
|
|
memcpy(extradata+extradata_size, m_h26XParamSets.pps, m_h26XParamSets.pps_size);
|
||
|
|
extradata_size += m_h26XParamSets.pps_size;
|
||
|
|
|
||
|
|
// init video decoder
|
||
|
|
openVideo(videoCodec, extradata, extradata_size);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else if (VIDEO_CODEC_H265 == videoCodec)
|
||
|
|
{
|
||
|
|
ret = m_rtmp.get_h265_params(m_h26XParamSets.sps, &m_h26XParamSets.sps_size,
|
||
|
|
m_h26XParamSets.pps, &m_h26XParamSets.pps_size,
|
||
|
|
m_h26XParamSets.vps, &m_h26XParamSets.vps_size);
|
||
|
|
if (ret)
|
||
|
|
{
|
||
|
|
memcpy(extradata, m_h26XParamSets.vps, m_h26XParamSets.vps_size);
|
||
|
|
extradata_size += m_h26XParamSets.vps_size;
|
||
|
|
memcpy(extradata+extradata_size, m_h26XParamSets.sps, m_h26XParamSets.sps_size);
|
||
|
|
extradata_size += m_h26XParamSets.sps_size;
|
||
|
|
memcpy(extradata+extradata_size, m_h26XParamSets.pps, m_h26XParamSets.pps_size);
|
||
|
|
extradata_size += m_h26XParamSets.pps_size;
|
||
|
|
|
||
|
|
// init video decoder
|
||
|
|
openVideo(videoCodec, extradata, extradata_size);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
openVideo(videoCodec);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else if (evt == RTMP_EVE_AUDIOREADY)
|
||
|
|
{
|
||
|
|
int audioCodec = m_rtmp.audio_codec();
|
||
|
|
|
||
|
|
if (AUDIO_CODEC_NONE != audioCodec)
|
||
|
|
{
|
||
|
|
openAudio(audioCodec, m_rtmp.get_audio_samplerate(), m_rtmp.get_audio_channels());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
emit notify(evt);
|
||
|
|
}
|
||
|
|
|
||
|
|
void CRtmpPlayer::onAudio(uint8 * pdata, int len, uint32 ts)
|
||
|
|
{
|
||
|
|
playAudio(pdata, len, ts, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
void CRtmpPlayer::onVideo(uint8 * pdata, int len, uint32 ts)
|
||
|
|
{
|
||
|
|
playVideo(pdata, len, ts, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
BOOL CRtmpPlayer::onRecord()
|
||
|
|
{
|
||
|
|
CRtmpClient * p_rtmp = &m_rtmp;
|
||
|
|
AVICTX * p_avictx = m_pAviCtx;
|
||
|
|
|
||
|
|
int vcodec = p_rtmp->video_codec();
|
||
|
|
int fps = p_rtmp->get_video_framerate();
|
||
|
|
int v_extra_len = 0;
|
||
|
|
uint8 v_extra[1024];
|
||
|
|
|
||
|
|
if (VIDEO_CODEC_H264 == vcodec)
|
||
|
|
{
|
||
|
|
avi_set_video_info(p_avictx, fps, 0, 0, "H264");
|
||
|
|
|
||
|
|
uint8 sps[512], pps[512];
|
||
|
|
int sps_len = 0, pps_len = 0;
|
||
|
|
|
||
|
|
if (p_rtmp->get_h264_params(sps, &sps_len, pps, &pps_len))
|
||
|
|
{
|
||
|
|
memcpy(v_extra, sps, sps_len);
|
||
|
|
memcpy(v_extra + sps_len, pps, pps_len);
|
||
|
|
|
||
|
|
v_extra_len = sps_len + pps_len;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (v_extra_len > 0)
|
||
|
|
{
|
||
|
|
avi_set_video_extra_info(p_avictx, v_extra, v_extra_len);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else if (VIDEO_CODEC_H265 == vcodec)
|
||
|
|
{
|
||
|
|
avi_set_video_info(p_avictx, fps, 0, 0, "H265");
|
||
|
|
|
||
|
|
uint8 vps[512], sps[512], pps[512];
|
||
|
|
int vps_len = 0, sps_len = 0, pps_len = 0;
|
||
|
|
|
||
|
|
if (p_rtmp->get_h265_params(sps, &sps_len, pps, &pps_len, vps, &vps_len))
|
||
|
|
{
|
||
|
|
memcpy(v_extra, vps, vps_len);
|
||
|
|
memcpy(v_extra + vps_len, sps, sps_len);
|
||
|
|
memcpy(v_extra + vps_len + sps_len, pps, pps_len);
|
||
|
|
|
||
|
|
v_extra_len = vps_len + sps_len + pps_len;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (v_extra_len > 0)
|
||
|
|
{
|
||
|
|
avi_set_video_extra_info(p_avictx, v_extra, v_extra_len);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else if (VIDEO_CODEC_JPEG == vcodec)
|
||
|
|
{
|
||
|
|
avi_set_video_info(p_avictx, fps, 0, 0, "JPEG");
|
||
|
|
}
|
||
|
|
else if (VIDEO_CODEC_MP4 == vcodec)
|
||
|
|
{
|
||
|
|
avi_set_video_info(p_avictx, fps, 0, 0, "MP4V");
|
||
|
|
}
|
||
|
|
|
||
|
|
int acodec = p_rtmp->audio_codec();
|
||
|
|
int sr = p_rtmp->get_audio_samplerate();
|
||
|
|
int ch = p_rtmp->get_audio_channels();
|
||
|
|
|
||
|
|
if (AUDIO_CODEC_G711A == acodec)
|
||
|
|
{
|
||
|
|
avi_set_audio_info(p_avictx, ch, sr, AUDIO_FORMAT_ALAW);
|
||
|
|
}
|
||
|
|
else if (AUDIO_CODEC_G711U == acodec)
|
||
|
|
{
|
||
|
|
avi_set_audio_info(p_avictx, ch, sr, AUDIO_FORMAT_MULAW);
|
||
|
|
}
|
||
|
|
else if (AUDIO_CODEC_G726 == acodec)
|
||
|
|
{
|
||
|
|
avi_set_audio_info(p_avictx, ch, sr, AUDIO_FORMAT_G726);
|
||
|
|
}
|
||
|
|
else if (AUDIO_CODEC_G722 == acodec)
|
||
|
|
{
|
||
|
|
avi_set_audio_info(p_avictx, ch, sr, AUDIO_FORMAT_G722);
|
||
|
|
}
|
||
|
|
else if (AUDIO_CODEC_AAC == acodec)
|
||
|
|
{
|
||
|
|
avi_set_audio_info(p_avictx, ch, sr, AUDIO_FORMAT_AAC);
|
||
|
|
avi_set_audio_extra_info(p_avictx, p_rtmp->get_audio_config(), p_rtmp->get_audio_config_len());
|
||
|
|
}
|
||
|
|
|
||
|
|
avi_update_header(p_avictx);
|
||
|
|
|
||
|
|
if (p_avictx->ctxf_video)
|
||
|
|
{
|
||
|
|
if (v_extra_len > 0)
|
||
|
|
{
|
||
|
|
recordVideo(v_extra, v_extra_len, 0, 0);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return TRUE;
|
||
|
|
}
|
||
|
|
|
||
|
|
void CRtmpPlayer::onRecordFileSwitch()
|
||
|
|
{
|
||
|
|
AVICTX * p_ctx;
|
||
|
|
AVICTX * p_oldctx = m_pAviCtx;
|
||
|
|
|
||
|
|
QString path = getRecordPath();
|
||
|
|
QString file = path + "/" + getTempFile(m_ip, ".avi");
|
||
|
|
|
||
|
|
p_ctx = avi_write_open(file.toLocal8Bit().toStdString().c_str());
|
||
|
|
if (NULL == p_ctx)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
p_ctx->ctxf_video = p_oldctx->ctxf_video;
|
||
|
|
p_ctx->ctxf_audio = p_oldctx->ctxf_audio;
|
||
|
|
|
||
|
|
if (p_ctx->ctxf_video)
|
||
|
|
{
|
||
|
|
avi_calc_fps(p_oldctx);
|
||
|
|
avi_set_video_info(p_ctx, p_oldctx->v_fps, p_oldctx->v_width, p_oldctx->v_height, p_oldctx->v_fcc);
|
||
|
|
avi_set_video_extra_info(p_ctx, p_oldctx->v_extra, p_oldctx->v_extra_len);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (p_ctx->ctxf_audio)
|
||
|
|
{
|
||
|
|
avi_set_audio_info(p_ctx, p_oldctx->a_chns, p_oldctx->a_rate, p_oldctx->a_fmt);
|
||
|
|
avi_set_audio_extra_info(p_ctx, p_oldctx->a_extra, p_oldctx->a_extra_len);
|
||
|
|
}
|
||
|
|
|
||
|
|
avi_write_close(p_oldctx);
|
||
|
|
|
||
|
|
avi_update_header(p_ctx);
|
||
|
|
|
||
|
|
m_pAviCtx = p_ctx;
|
||
|
|
|
||
|
|
if (m_h26XParamSets.vps_size > 0 ||
|
||
|
|
m_h26XParamSets.sps_size > 0 ||
|
||
|
|
m_h26XParamSets.pps_size > 0)
|
||
|
|
{
|
||
|
|
avi_write_nalu(m_pAviCtx,
|
||
|
|
m_h26XParamSets.vps, m_h26XParamSets.vps_size,
|
||
|
|
m_h26XParamSets.sps, m_h26XParamSets.sps_size,
|
||
|
|
m_h26XParamSets.pps, m_h26XParamSets.pps_size);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|