完善mk_media接口

This commit is contained in:
ziyue 2021-09-27 14:34:26 +08:00
parent c493f0da1b
commit 3bc0769ccb
5 changed files with 77 additions and 64 deletions

View File

@ -46,8 +46,9 @@ API_EXPORT void API_CALL mk_media_release(mk_media ctx);
* @param width
* @param height
* @param fps fps
* @return 10
*/
API_EXPORT void API_CALL mk_media_init_video(mk_media ctx, int codec_id, int width, int height, float fps);
API_EXPORT int API_CALL mk_media_init_video(mk_media ctx, int codec_id, int width, int height, float fps);
/**
*
@ -56,8 +57,9 @@ API_EXPORT void API_CALL mk_media_init_video(mk_media ctx, int codec_id, int wid
* @param channel
* @param sample_bit 16
* @param sample_rate
* @return 10
*/
API_EXPORT void API_CALL mk_media_init_audio(mk_media ctx, int codec_id, int sample_rate, int channels, int sample_bit);
API_EXPORT int API_CALL mk_media_init_audio(mk_media ctx, int codec_id, int sample_rate, int channels, int sample_bit);
/**
*
@ -80,8 +82,9 @@ API_EXPORT void API_CALL mk_media_init_complete(mk_media ctx);
* @param len H264数据字节数
* @param dts
* @param pts
* @return 10
*/
API_EXPORT void API_CALL mk_media_input_h264(mk_media ctx, const void *data, int len, uint32_t dts, uint32_t pts);
API_EXPORT int API_CALL mk_media_input_h264(mk_media ctx, const void *data, int len, uint32_t dts, uint32_t pts);
/**
* H265视频00 00 01,00 00 00 01
@ -90,8 +93,9 @@ API_EXPORT void API_CALL mk_media_input_h264(mk_media ctx, const void *data, int
* @param len H265数据字节数
* @param dts
* @param pts
* @return 10
*/
API_EXPORT void API_CALL mk_media_input_h265(mk_media ctx, const void *data, int len, uint32_t dts, uint32_t pts);
API_EXPORT int API_CALL mk_media_input_h265(mk_media ctx, const void *data, int len, uint32_t dts, uint32_t pts);
/**
* AAC音频(adts头)
@ -100,8 +104,9 @@ API_EXPORT void API_CALL mk_media_input_h265(mk_media ctx, const void *data, int
* @param len AAC数据字节数
* @param dts
* @param adts adts头null
* @return 10
*/
API_EXPORT void API_CALL mk_media_input_aac(mk_media ctx, const void *data, int len, uint32_t dts, void *adts);
API_EXPORT int API_CALL mk_media_input_aac(mk_media ctx, const void *data, int len, uint32_t dts, void *adts);
/**
* PCM音频,ENABLE_FAAC编译时
@ -109,8 +114,9 @@ API_EXPORT void API_CALL mk_media_input_aac(mk_media ctx, const void *data, int
* @param data PCM数据
* @param len PCM数据字节数
* @param dts
* @return 10
*/
API_EXPORT void API_CALL mk_media_input_pcm(mk_media ctx, void *data, int len, uint32_t pts);
API_EXPORT int API_CALL mk_media_input_pcm(mk_media ctx, void *data, int len, uint32_t pts);
/**
* OPUS/G711音频帧
@ -118,8 +124,9 @@ API_EXPORT void API_CALL mk_media_input_pcm(mk_media ctx, void *data, int len, u
* @param data
* @param len
* @param dts
* @return 10
*/
API_EXPORT void API_CALL mk_media_input_audio(mk_media ctx, const void* data, int len, uint32_t dts);
API_EXPORT int API_CALL mk_media_input_audio(mk_media ctx, const void* data, int len, uint32_t dts);
/**
* MediaSource.close()

View File

@ -174,7 +174,7 @@ API_EXPORT void API_CALL mk_media_release(mk_media ctx) {
delete obj;
}
API_EXPORT void API_CALL mk_media_init_video(mk_media ctx, int codec_id, int width, int height, float fps){
API_EXPORT int API_CALL mk_media_init_video(mk_media ctx, int codec_id, int width, int height, float fps){
assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
VideoInfo info;
@ -182,10 +182,10 @@ API_EXPORT void API_CALL mk_media_init_video(mk_media ctx, int codec_id, int wid
info.iFrameRate = fps;
info.iWidth = width;
info.iHeight = height;
(*obj)->getChannel()->initVideo(info);
return (*obj)->getChannel()->initVideo(info);
}
API_EXPORT void API_CALL mk_media_init_audio(mk_media ctx, int codec_id, int sample_rate, int channels, int sample_bit){
API_EXPORT int API_CALL mk_media_init_audio(mk_media ctx, int codec_id, int sample_rate, int channels, int sample_bit){
assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
AudioInfo info;
@ -193,7 +193,7 @@ API_EXPORT void API_CALL mk_media_init_audio(mk_media ctx, int codec_id, int sam
info.iSampleRate = sample_rate;
info.iChannel = channels;
info.iSampleBit = sample_bit;
(*obj)->getChannel()->initAudio(info);
return (*obj)->getChannel()->initAudio(info);
}
API_EXPORT void API_CALL mk_media_init_mute_audio(mk_media ctx) {
@ -208,34 +208,34 @@ API_EXPORT void API_CALL mk_media_init_complete(mk_media ctx){
(*obj)->getChannel()->addTrackCompleted();
}
API_EXPORT void API_CALL mk_media_input_h264(mk_media ctx, const void *data, int len, uint32_t dts, uint32_t pts) {
API_EXPORT int API_CALL mk_media_input_h264(mk_media ctx, const void *data, int len, uint32_t dts, uint32_t pts) {
assert(ctx && data && len > 0);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
(*obj)->getChannel()->inputH264((const char *) data, len, dts, pts);
return (*obj)->getChannel()->inputH264((const char *) data, len, dts, pts);
}
API_EXPORT void API_CALL mk_media_input_h265(mk_media ctx, const void *data, int len, uint32_t dts, uint32_t pts) {
API_EXPORT int API_CALL mk_media_input_h265(mk_media ctx, const void *data, int len, uint32_t dts, uint32_t pts) {
assert(ctx && data && len > 0);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
(*obj)->getChannel()->inputH265((const char *) data, len, dts, pts);
return (*obj)->getChannel()->inputH265((const char *) data, len, dts, pts);
}
API_EXPORT void API_CALL mk_media_input_aac(mk_media ctx, const void *data, int len, uint32_t dts, void *adts) {
API_EXPORT int API_CALL mk_media_input_aac(mk_media ctx, const void *data, int len, uint32_t dts, void *adts) {
assert(ctx && data && len > 0 && adts);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
(*obj)->getChannel()->inputAAC((const char *) data, len, dts, (char *) adts);
return (*obj)->getChannel()->inputAAC((const char *) data, len, dts, (char *) adts);
}
API_EXPORT void API_CALL mk_media_input_pcm(mk_media ctx, void *data , int len, uint32_t pts){
API_EXPORT int API_CALL mk_media_input_pcm(mk_media ctx, void *data , int len, uint32_t pts){
assert(ctx && data && len > 0);
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
(*obj)->getChannel()->inputPCM((char*)data, len, pts);
return (*obj)->getChannel()->inputPCM((char*)data, len, pts);
}
API_EXPORT void API_CALL mk_media_input_audio(mk_media ctx, const void* data, int len, uint32_t dts){
API_EXPORT int API_CALL mk_media_input_audio(mk_media ctx, const void* data, int len, uint32_t dts){
assert(ctx && data && len > 0);
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
(*obj)->getChannel()->inputAudio((const char*)data, len, dts);
return (*obj)->getChannel()->inputAudio((const char*)data, len, dts);
}
API_EXPORT void API_CALL mk_media_start_send_rtp(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data){

View File

@ -33,7 +33,7 @@ DevChannel::DevChannel(const string &vhost, const string &app, const string &str
DevChannel::~DevChannel() {}
void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) {
bool DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) {
#ifdef ENABLE_X264
//TimeTicker1(50);
if (!_pH264Enc) {
@ -46,16 +46,20 @@ void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) {
if (_pH264Enc) {
H264Encoder::H264Frame *pOut;
int iFrames = _pH264Enc->inputData(apcYuv, aiYuvLen, uiStamp, &pOut);
bool ret = false;
for (int i = 0; i < iFrames; i++) {
inputH264((char *) pOut[i].pucData, pOut[i].iLength, uiStamp);
ret = inputH264((char *) pOut[i].pucData, pOut[i].iLength, uiStamp) ? true : ret;
}
return ret;
}
return false;
#else
WarnL << "h264编码未启用,该方法无效,编译时请打开ENABLE_X264选项";
return false;
#endif //ENABLE_X264
}
void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
bool DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
#ifdef ENABLE_FAAC
if (!_pAacEnc) {
_pAacEnc.reset(new AACEncoder());
@ -68,15 +72,17 @@ void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
unsigned char *pucOut;
int iRet = _pAacEnc->inputData(pcData, iDataLen, &pucOut);
if (iRet > 7) {
inputAAC((char *) pucOut + 7, iRet - 7, uiStamp, (char *)pucOut);
return inputAAC((char *) pucOut + 7, iRet - 7, uiStamp, (char *)pucOut);
}
}
return false;
#else
WarnL << "aac编码未启用,该方法无效,编译时请打开ENABLE_FAAC选项";
return false;
#endif //ENABLE_FAAC
}
void DevChannel::inputH264(const char *data, int len, uint32_t dts, uint32_t pts) {
bool DevChannel::inputH264(const char *data, int len, uint32_t dts, uint32_t pts) {
if(dts == 0){
dts = (uint32_t)_aTicker[0].elapsedTime();
}
@ -92,10 +98,10 @@ void DevChannel::inputH264(const char *data, int len, uint32_t dts, uint32_t pts
frame->_pts = pts;
frame->_buffer.assign(data, len);
frame->_prefix_size = prefixSize(data,len);
inputFrame(frame);
return inputFrame(frame);
}
void DevChannel::inputH265(const char *data, int len, uint32_t dts, uint32_t pts) {
bool DevChannel::inputH265(const char *data, int len, uint32_t dts, uint32_t pts) {
if(dts == 0){
dts = (uint32_t)_aTicker[0].elapsedTime();
}
@ -111,7 +117,7 @@ void DevChannel::inputH265(const char *data, int len, uint32_t dts, uint32_t pts
frame->_pts = pts;
frame->_buffer.assign(data, len);
frame->_prefix_size = prefixSize(data,len);
inputFrame(frame);
return inputFrame(frame);
}
class FrameAutoDelete : public FrameFromPtr{
@ -128,52 +134,53 @@ public:
}
};
void DevChannel::inputAAC(const char *data_without_adts, int len, uint32_t dts, const char *adts_header){
bool DevChannel::inputAAC(const char *data_without_adts, int len, uint32_t dts, const char *adts_header){
if (dts == 0) {
dts = (uint32_t) _aTicker[1].elapsedTime();
}
if (adts_header) {
if (adts_header + ADTS_HEADER_LEN == data_without_adts) {
//adts头和帧在一起
inputFrame(std::make_shared<FrameFromPtr>(_audio->codecId, (char *) data_without_adts - ADTS_HEADER_LEN, len + ADTS_HEADER_LEN, dts, 0, ADTS_HEADER_LEN));
} else {
//adts头和帧不在一起
char *data_with_adts = new char[len + ADTS_HEADER_LEN];
memcpy(data_with_adts, adts_header, ADTS_HEADER_LEN);
memcpy(data_with_adts + ADTS_HEADER_LEN, data_without_adts, len);
inputFrame(std::make_shared<FrameAutoDelete>(_audio->codecId, data_with_adts, len + ADTS_HEADER_LEN, dts, 0, ADTS_HEADER_LEN));
}
} else {
if (!adts_header) {
//没有adts头
inputFrame(std::make_shared<FrameFromPtr>(_audio->codecId, (char *) data_without_adts, len, dts, 0, 0));
return inputFrame(std::make_shared<FrameFromPtr>(_audio->codecId, (char *) data_without_adts, len, dts, 0, 0));
}
if (adts_header + ADTS_HEADER_LEN == data_without_adts) {
//adts头和帧在一起
return inputFrame(std::make_shared<FrameFromPtr>(_audio->codecId, (char *) data_without_adts - ADTS_HEADER_LEN, len + ADTS_HEADER_LEN, dts, 0, ADTS_HEADER_LEN));
}
//adts头和帧不在一起
char *data_with_adts = new char[len + ADTS_HEADER_LEN];
memcpy(data_with_adts, adts_header, ADTS_HEADER_LEN);
memcpy(data_with_adts + ADTS_HEADER_LEN, data_without_adts, len);
return inputFrame(std::make_shared<FrameAutoDelete>(_audio->codecId, data_with_adts, len + ADTS_HEADER_LEN, dts, 0, ADTS_HEADER_LEN));
}
void DevChannel::inputAudio(const char *data, int len, uint32_t dts){
bool DevChannel::inputAudio(const char *data, int len, uint32_t dts){
if (dts == 0) {
dts = (uint32_t) _aTicker[1].elapsedTime();
}
inputFrame(std::make_shared<FrameFromPtr>(_audio->codecId, (char *) data, len, dts, 0));
return inputFrame(std::make_shared<FrameFromPtr>(_audio->codecId, (char *) data, len, dts, 0));
}
void DevChannel::initVideo(const VideoInfo &info) {
bool DevChannel::initVideo(const VideoInfo &info) {
_video = std::make_shared<VideoInfo>(info);
switch (info.codecId){
case CodecH265 : addTrack(std::make_shared<H265Track>()); break;
case CodecH264 : addTrack(std::make_shared<H264Track>()); break;
default: WarnL << "不支持该类型的视频编码类型:" << info.codecId; break;
case CodecH265 : return addTrack(std::make_shared<H265Track>());
case CodecH264 : return addTrack(std::make_shared<H264Track>());
default: WarnL << "不支持该类型的视频编码类型:" << info.codecId; return false;
}
}
void DevChannel::initAudio(const AudioInfo &info) {
bool DevChannel::initAudio(const AudioInfo &info) {
_audio = std::make_shared<AudioInfo>(info);
switch (info.codecId) {
case CodecAAC : addTrack(std::make_shared<AACTrack>()); break;
case CodecAAC : return addTrack(std::make_shared<AACTrack>());
case CodecG711A :
case CodecG711U : addTrack(std::make_shared<G711Track>(info.codecId, info.iSampleRate, info.iChannel, info.iSampleBit)); break;
case CodecOpus : addTrack(std::make_shared<OpusTrack>()); break;
default: WarnL << "不支持该类型的音频编码类型:" << info.codecId; break;
case CodecG711U : return addTrack(std::make_shared<G711Track>(info.codecId, info.iSampleRate, info.iChannel, info.iSampleBit));
case CodecOpus : return addTrack(std::make_shared<OpusTrack>());
default: WarnL << "不支持该类型的音频编码类型:" << info.codecId; return false;
}
}

View File

@ -58,14 +58,14 @@ public:
* MultiMediaSourceMuxer::addTrack(VideoTrack::Ptr );
* @param info
*/
void initVideo(const VideoInfo &info);
bool initVideo(const VideoInfo &info);
/**
* Track
* MultiMediaSourceMuxer::addTrack(AudioTrack::Ptr );
* @param info
*/
void initAudio(const AudioInfo &info);
bool initAudio(const AudioInfo &info);
/**
* 264
@ -74,7 +74,7 @@ public:
* @param dts 0
* @param pts 0dts
*/
void inputH264(const char *data, int len, uint32_t dts, uint32_t pts = 0);
bool inputH264(const char *data, int len, uint32_t dts, uint32_t pts = 0);
/**
* 265
@ -83,7 +83,7 @@ public:
* @param dts 0
* @param pts 0dts
*/
void inputH265(const char *data, int len, uint32_t dts, uint32_t pts = 0);
bool inputH265(const char *data, int len, uint32_t dts, uint32_t pts = 0);
/**
* aac帧
@ -92,7 +92,7 @@ public:
* @param dts
* @param adts_header adts头
*/
void inputAAC(const char *data_without_adts, int len, uint32_t dts, const char *adts_header);
bool inputAAC(const char *data_without_adts, int len, uint32_t dts, const char *adts_header);
/**
* OPUS/G711音频帧
@ -100,7 +100,7 @@ public:
* @param len
* @param dts
*/
void inputAudio(const char *data, int len, uint32_t dts);
bool inputAudio(const char *data, int len, uint32_t dts);
/**
* yuv420p视频帧inputH264方法
@ -108,8 +108,7 @@ public:
* @param aiYuvLen
* @param uiStamp
*/
void inputYUV(char *apcYuv[3], int aiYuvLen[3], uint32_t uiStamp);
bool inputYUV(char *apcYuv[3], int aiYuvLen[3], uint32_t uiStamp);
/**
* pcm数据inputAAC方法
@ -117,7 +116,7 @@ public:
* @param iDataLen
* @param uiStamp
*/
void inputPCM(char *pcData, int iDataLen, uint32_t uiStamp);
bool inputPCM(char *pcData, int iDataLen, uint32_t uiStamp);
private:
MediaOriginType getOriginType(MediaSource &sender) const override;

View File

@ -80,7 +80,7 @@ bool PSEncoder::addTrack(const Track::Ptr &track) {
break;
}
default: WarnL << "mpeg-ps 不支持该编码格式,已忽略:" << track->getCodecName(); return false;;
default: WarnL << "mpeg-ps 不支持该编码格式,已忽略:" << track->getCodecName(); return false;
}
//尝试音视频同步
stampSync();