完善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 width
* @param height * @param height
* @param fps fps * @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 channel
* @param sample_bit 16 * @param sample_bit 16
* @param sample_rate * @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 len H264数据字节数
* @param dts * @param dts
* @param pts * @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 * 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 len H265数据字节数
* @param dts * @param dts
* @param pts * @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头) * 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 len AAC数据字节数
* @param dts * @param dts
* @param adts adts头null * @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编译时 * 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 data PCM数据
* @param len PCM数据字节数 * @param len PCM数据字节数
* @param dts * @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音频帧 * 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 data
* @param len * @param len
* @param dts * @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() * MediaSource.close()

View File

@ -174,7 +174,7 @@ API_EXPORT void API_CALL mk_media_release(mk_media ctx) {
delete obj; 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); assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
VideoInfo info; 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.iFrameRate = fps;
info.iWidth = width; info.iWidth = width;
info.iHeight = height; 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); assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
AudioInfo info; 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.iSampleRate = sample_rate;
info.iChannel = channels; info.iChannel = channels;
info.iSampleBit = sample_bit; 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) { 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(); (*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); assert(ctx && data && len > 0);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; 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); assert(ctx && data && len > 0);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; 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); assert(ctx && data && len > 0 && adts);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; 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); assert(ctx && data && len > 0);
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx; 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); assert(ctx && data && len > 0);
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx; 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){ 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() {} 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 #ifdef ENABLE_X264
//TimeTicker1(50); //TimeTicker1(50);
if (!_pH264Enc) { if (!_pH264Enc) {
@ -46,16 +46,20 @@ void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) {
if (_pH264Enc) { if (_pH264Enc) {
H264Encoder::H264Frame *pOut; H264Encoder::H264Frame *pOut;
int iFrames = _pH264Enc->inputData(apcYuv, aiYuvLen, uiStamp, &pOut); int iFrames = _pH264Enc->inputData(apcYuv, aiYuvLen, uiStamp, &pOut);
bool ret = false;
for (int i = 0; i < iFrames; i++) { 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 #else
WarnL << "h264编码未启用,该方法无效,编译时请打开ENABLE_X264选项"; WarnL << "h264编码未启用,该方法无效,编译时请打开ENABLE_X264选项";
return false;
#endif //ENABLE_X264 #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 #ifdef ENABLE_FAAC
if (!_pAacEnc) { if (!_pAacEnc) {
_pAacEnc.reset(new AACEncoder()); _pAacEnc.reset(new AACEncoder());
@ -68,15 +72,17 @@ void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) {
unsigned char *pucOut; unsigned char *pucOut;
int iRet = _pAacEnc->inputData(pcData, iDataLen, &pucOut); int iRet = _pAacEnc->inputData(pcData, iDataLen, &pucOut);
if (iRet > 7) { 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 #else
WarnL << "aac编码未启用,该方法无效,编译时请打开ENABLE_FAAC选项"; WarnL << "aac编码未启用,该方法无效,编译时请打开ENABLE_FAAC选项";
return false;
#endif //ENABLE_FAAC #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){ if(dts == 0){
dts = (uint32_t)_aTicker[0].elapsedTime(); 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->_pts = pts;
frame->_buffer.assign(data, len); frame->_buffer.assign(data, len);
frame->_prefix_size = prefixSize(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){ if(dts == 0){
dts = (uint32_t)_aTicker[0].elapsedTime(); 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->_pts = pts;
frame->_buffer.assign(data, len); frame->_buffer.assign(data, len);
frame->_prefix_size = prefixSize(data,len); frame->_prefix_size = prefixSize(data,len);
inputFrame(frame); return inputFrame(frame);
} }
class FrameAutoDelete : public FrameFromPtr{ 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) { if (dts == 0) {
dts = (uint32_t) _aTicker[1].elapsedTime(); dts = (uint32_t) _aTicker[1].elapsedTime();
} }
if (adts_header) { if (!adts_header) {
//没有adts头
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) { if (adts_header + ADTS_HEADER_LEN == data_without_adts) {
//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)); return 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头和帧不在一起 //adts头和帧不在一起
char *data_with_adts = new char[len + ADTS_HEADER_LEN]; 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, ADTS_HEADER_LEN);
memcpy(data_with_adts + ADTS_HEADER_LEN, data_without_adts, 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)); return inputFrame(std::make_shared<FrameAutoDelete>(_audio->codecId, data_with_adts, len + ADTS_HEADER_LEN, dts, 0, ADTS_HEADER_LEN));
}
} else {
//没有adts头
inputFrame(std::make_shared<FrameFromPtr>(_audio->codecId, (char *) data_without_adts, len, dts, 0, 0));
}
} }
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) { if (dts == 0) {
dts = (uint32_t) _aTicker[1].elapsedTime(); 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); _video = std::make_shared<VideoInfo>(info);
switch (info.codecId){ switch (info.codecId){
case CodecH265 : addTrack(std::make_shared<H265Track>()); break; case CodecH265 : return addTrack(std::make_shared<H265Track>());
case CodecH264 : addTrack(std::make_shared<H264Track>()); break; case CodecH264 : return addTrack(std::make_shared<H264Track>());
default: WarnL << "不支持该类型的视频编码类型:" << info.codecId; break; default: WarnL << "不支持该类型的视频编码类型:" << info.codecId; return false;
} }
} }
void DevChannel::initAudio(const AudioInfo &info) { bool DevChannel::initAudio(const AudioInfo &info) {
_audio = std::make_shared<AudioInfo>(info); _audio = std::make_shared<AudioInfo>(info);
switch (info.codecId) { switch (info.codecId) {
case CodecAAC : addTrack(std::make_shared<AACTrack>()); break; case CodecAAC : return addTrack(std::make_shared<AACTrack>());
case CodecG711A : case CodecG711A :
case CodecG711U : addTrack(std::make_shared<G711Track>(info.codecId, info.iSampleRate, info.iChannel, info.iSampleBit)); break; case CodecG711U : return addTrack(std::make_shared<G711Track>(info.codecId, info.iSampleRate, info.iChannel, info.iSampleBit));
case CodecOpus : addTrack(std::make_shared<OpusTrack>()); break; case CodecOpus : return addTrack(std::make_shared<OpusTrack>());
default: WarnL << "不支持该类型的音频编码类型:" << info.codecId; break; default: WarnL << "不支持该类型的音频编码类型:" << info.codecId; return false;
} }
} }

View File

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

View File

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