ZLMediaKit/c_wrapper/src/media.cpp

106 lines
3.6 KiB
C++
Raw Normal View History

2017-12-13 18:15:22 +08:00
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "media.h"
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Util/onceToken.h"
#include "Device/Device.h"
#include "cleaner.h"
using namespace std;
using namespace ZL::DEV;
using namespace ZL::Util;
static recursive_mutex s_mtxMapMedia;
static unordered_map<void *, DevChannel::Ptr> s_mapMedia;
static onceToken s_token([](){
cleaner::Instance().push_front([](){
lock_guard<recursive_mutex> lck(s_mtxMapMedia);
s_mapMedia.clear();
DebugL << "clear media" << endl;
});
},nullptr);
//////////////////////////Rtsp media///////////////////////////
2017-12-19 10:34:52 +08:00
API_EXPORT MediaContext API_CALL createMedia(const char *appName,const char *mediaName) {
DevChannel::Ptr ret(new DevChannel(DEFAULT_VHOST,appName,mediaName));
2017-12-13 18:15:22 +08:00
lock_guard<recursive_mutex> lck(s_mtxMapMedia);
s_mapMedia.emplace((void *) (ret.get()), ret);
return ret.get();
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL releaseMedia(MediaContext ctx) {
2017-12-13 18:15:22 +08:00
lock_guard<recursive_mutex> lck(s_mtxMapMedia);
s_mapMedia.erase(ctx);
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL media_initVideo(MediaContext ctx, int width, int height, int frameRate) {
2017-12-13 18:15:22 +08:00
DevChannel *ptr = (DevChannel *) ctx;
VideoInfo info;
info.iFrameRate = frameRate;
info.iWidth = width;
info.iHeight = height;
ptr->initVideo(info);
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL media_initAudio(MediaContext ctx, int channel, int sampleBit, int sampleRate) {
2017-12-13 18:15:22 +08:00
DevChannel *ptr = (DevChannel *) ctx;
AudioInfo info;
info.iSampleRate = sampleRate;
info.iChannel = channel;
info.iSampleBit = sampleBit;
ptr->initAudio(info);
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL media_inputH264(MediaContext ctx, void *data, int len, unsigned long stamp) {
2017-12-13 18:15:22 +08:00
//TimeTicker();
DevChannel *ptr = (DevChannel *) ctx;
ptr->inputH264((char *) data, len, stamp);
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL media_inputAAC(MediaContext ctx, void *data, int len,unsigned long stamp) {
2017-12-13 18:15:22 +08:00
//TimeTicker();
DevChannel *ptr = (DevChannel *) ctx;
ptr->inputAAC((char *) data, len, stamp);
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL media_inputAAC1(MediaContext ctx, void *data, int len, unsigned long stamp,void *adts){
DevChannel *ptr = (DevChannel *) ctx;
ptr->inputAAC((char *) data, len, stamp,(char *)adts);
}
API_EXPORT void API_CALL media_inputAAC2(MediaContext ctx, void *data, int len, unsigned long stamp,void *aac_cfg){
DevChannel *ptr = (DevChannel *) ctx;
AdtsFrame frame;
makeAdtsHeader((char*)aac_cfg, frame);
char adts[8];
writeAdtsHeader(frame, (uint8_t*)adts);
ptr->inputAAC((char *) data, len, stamp,(char *)adts);
}
2017-12-13 18:15:22 +08:00