ZLMediaKit/src/Common/Device.cpp

166 lines
5.3 KiB
C++
Raw Normal View History

2017-10-09 22:11:01 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
2017-09-27 16:20:30 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
2017-09-27 16:20:30 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT-like license that can be found in the
2020-04-04 20:30:09 +08:00
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2017-04-01 16:35:56 +08:00
*/
2020-04-04 20:30:09 +08:00
2017-04-01 16:35:56 +08:00
#include "Device.h"
#include "Util/logger.h"
2018-09-20 15:43:49 +08:00
#include "Util/base64.h"
2023-12-09 16:23:51 +08:00
#include "Extension/Factory.h"
#ifdef ENABLE_FAAC
#include "Codec/AACEncoder.h"
#endif //ENABLE_FAAC
#ifdef ENABLE_X264
#include "Codec/H264Encoder.h"
#endif //ENABLE_X264
2018-10-24 17:17:55 +08:00
using namespace toolkit;
using namespace std;
2017-04-01 16:35:56 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2017-04-01 16:35:56 +08:00
2022-08-08 17:13:39 +08:00
bool DevChannel::inputYUV(char *yuv[3], int linesize[3], uint64_t cts) {
#ifdef ENABLE_X264
2020-03-20 11:51:24 +08:00
//TimeTicker1(50);
if (!_pH264Enc) {
_pH264Enc.reset(new H264Encoder());
2022-05-25 15:11:26 +08:00
if (!_pH264Enc->init(_video->iWidth, _video->iHeight, _video->iFrameRate, _video->iBitRate)) {
2020-03-20 11:51:24 +08:00
_pH264Enc.reset();
WarnL << "H264Encoder init failed!";
}
}
if (_pH264Enc) {
2022-05-25 15:11:26 +08:00
H264Encoder::H264Frame *out_frames;
int frames = _pH264Enc->inputData(yuv, linesize, cts, &out_frames);
2021-09-27 14:34:26 +08:00
bool ret = false;
2022-05-25 15:11:26 +08:00
for (int i = 0; i < frames; i++) {
ret = inputH264((char *) out_frames[i].pucData, out_frames[i].iLength, cts) ? true : ret;
2020-03-20 11:51:24 +08:00
}
2021-09-27 14:34:26 +08:00
return ret;
2020-03-20 11:51:24 +08:00
}
2021-09-27 14:34:26 +08:00
return false;
#else
WarnL << "h264编码未启用,该方法无效,编译时请打开ENABLE_X264选项";
2021-09-27 14:34:26 +08:00
return false;
#endif //ENABLE_X264
}
2017-04-01 16:35:56 +08:00
2022-08-08 17:13:39 +08:00
bool DevChannel::inputPCM(char* pcData, int iDataLen, uint64_t uiStamp) {
#ifdef ENABLE_FAAC
2020-03-20 11:51:24 +08:00
if (!_pAacEnc) {
_pAacEnc.reset(new AACEncoder());
if (!_pAacEnc->init(_audio->iSampleRate, _audio->iChannel, _audio->iSampleBit)) {
_pAacEnc.reset();
WarnL << "AACEncoder init failed!";
}
}
if (_pAacEnc) {
unsigned char *pucOut;
int iRet = _pAacEnc->inputData(pcData, iDataLen, &pucOut);
2020-04-18 23:58:29 +08:00
if (iRet > 7) {
2021-09-27 14:34:26 +08:00
return inputAAC((char *) pucOut + 7, iRet - 7, uiStamp, (char *)pucOut);
2020-03-20 11:51:24 +08:00
}
}
2021-09-27 14:34:26 +08:00
return false;
#else
WarnL << "aac编码未启用,该方法无效,编译时请打开ENABLE_FAAC选项";
2021-09-27 14:34:26 +08:00
return false;
#endif //ENABLE_FAAC
}
2017-04-01 16:35:56 +08:00
2022-08-08 17:13:39 +08:00
bool DevChannel::inputH264(const char *data, int len, uint64_t dts, uint64_t pts) {
if (dts == 0) {
dts = _aTicker[0].elapsedTime();
2020-03-20 11:51:24 +08:00
}
2022-08-08 17:13:39 +08:00
if (pts == 0) {
2020-03-20 11:51:24 +08:00
pts = dts;
2018-03-02 15:00:04 +08:00
}
2020-01-08 12:14:27 +08:00
2023-12-09 16:23:51 +08:00
return inputFrame(Factory::getFrameFromPtr(CodecH264,data, len, dts, pts));
2017-04-01 16:35:56 +08:00
}
2022-08-08 17:13:39 +08:00
bool DevChannel::inputH265(const char *data, int len, uint64_t dts, uint64_t pts) {
if (dts == 0) {
dts = _aTicker[0].elapsedTime();
2020-03-20 11:51:24 +08:00
}
2022-08-08 17:13:39 +08:00
if (pts == 0) {
2020-03-20 11:51:24 +08:00
pts = dts;
}
2023-12-09 16:23:51 +08:00
return inputFrame(Factory::getFrameFromPtr(CodecH265, data, len, dts, pts));
2019-11-01 15:40:21 +08:00
}
2023-12-09 16:23:51 +08:00
#define ADTS_HEADER_LEN 7
2020-04-18 23:56:27 +08:00
2022-08-08 17:13:39 +08:00
bool DevChannel::inputAAC(const char *data_without_adts, int len, uint64_t dts, const char *adts_header){
2020-08-01 10:22:12 +08:00
if (dts == 0) {
2022-08-08 17:13:39 +08:00
dts = _aTicker[1].elapsedTime();
2020-04-18 23:56:27 +08:00
}
2021-09-27 14:34:26 +08:00
if (!adts_header) {
2020-08-01 10:22:12 +08:00
//没有adts头
2023-12-09 16:23:51 +08:00
return inputFrame(std::make_shared<FrameFromPtr>(CodecAAC, (char *) data_without_adts, len, dts, 0, 0));
2021-09-27 14:34:26 +08:00
}
if (adts_header + ADTS_HEADER_LEN == data_without_adts) {
//adts头和帧在一起
2023-12-09 16:23:51 +08:00
return inputFrame(std::make_shared<FrameFromPtr>(CodecAAC, (char *) data_without_adts - ADTS_HEADER_LEN, len + ADTS_HEADER_LEN, dts, 0, ADTS_HEADER_LEN));
2020-03-20 11:51:24 +08:00
}
2021-09-27 14:34:26 +08:00
//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);
2023-12-09 16:23:51 +08:00
return inputFrame(std::make_shared<FrameAutoDelete>(CodecAAC, data_with_adts, len + ADTS_HEADER_LEN, dts, 0, ADTS_HEADER_LEN));
2017-04-01 16:35:56 +08:00
}
2022-08-08 17:13:39 +08:00
bool DevChannel::inputAudio(const char *data, int len, uint64_t dts){
2020-04-18 23:56:27 +08:00
if (dts == 0) {
2022-08-08 17:13:39 +08:00
dts = _aTicker[1].elapsedTime();
}
2023-12-09 16:23:51 +08:00
return inputFrame(Factory::getFrameFromPtr(_audio->codecId, (char *) data, len, dts, dts));
}
2021-09-27 14:34:26 +08:00
bool DevChannel::initVideo(const VideoInfo &info) {
2020-03-20 11:51:24 +08:00
_video = std::make_shared<VideoInfo>(info);
2023-12-09 16:23:51 +08:00
return addTrack(Factory::getTrackByCodecId(info.codecId));
2017-04-01 16:35:56 +08:00
}
2021-09-27 14:34:26 +08:00
bool DevChannel::initAudio(const AudioInfo &info) {
2020-04-18 18:46:20 +08:00
_audio = std::make_shared<AudioInfo>(info);
2023-12-09 16:23:51 +08:00
return addTrack(Factory::getTrackByCodecId(info.codecId, info.iSampleRate, info.iChannel, info.iSampleBit));
2017-04-01 16:35:56 +08:00
}
2018-10-26 16:09:48 +08:00
2023-03-11 11:02:20 +08:00
bool DevChannel::inputFrame(const Frame::Ptr &frame) {
auto cached_frame = Frame::getCacheAbleFrame(frame);
weak_ptr<MultiMediaSourceMuxer> weak_self = shared_from_this();
getOwnerPoller(MediaSource::NullMediaSource())->async([weak_self, cached_frame]() {
if (auto strong_self = weak_self.lock()) {
strong_self->MultiMediaSourceMuxer::inputFrame(cached_frame);
}
});
return true;
}
bool DevChannel::addTrack(const Track::Ptr &track) {
bool ret;
getOwnerPoller(MediaSource::NullMediaSource())->sync([&]() { ret = MultiMediaSourceMuxer::addTrack(track); });
return ret;
}
void DevChannel::addTrackCompleted() {
getOwnerPoller(MediaSource::NullMediaSource())->sync([&]() { MultiMediaSourceMuxer::addTrackCompleted(); });
}
2020-09-27 11:32:49 +08:00
MediaOriginType DevChannel::getOriginType(MediaSource &sender) const {
return MediaOriginType::device_chn;
}
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2017-04-01 16:35:56 +08:00