thread for sign

This commit is contained in:
AlanRen 2023-07-14 19:09:02 -06:00
parent 732bb17a9a
commit c3c3aae6ce
3 changed files with 91 additions and 14 deletions

View File

@ -37,7 +37,7 @@ using namespace std;
#define TRACEL(x,...) //PRINT(WHITE,x,##__VA_ARGS__)
#define DEBUGL(x,...) //PRINT(LIGHT_GRAY,x,##__VA_ARGS__)
#define INFOL(x,...) //PRINT(BLUE,x,##__VA_ARGS__)
#define NOTICEL(x,...)//PRINT(BROWN,x,##__VA_ARGS__)
#define NOTICEL(x,...) PRINT(BROWN,x,##__VA_ARGS__)
#define WRNGL(x,...) PRINT(YELLOW,x,##__VA_ARGS__)
#define ERROL(x,...) PRINT(RED,x,##__VA_ARGS__)
@ -106,6 +106,13 @@ struct HWsign{
int32_t I_seq;
uint32_t I_len;
CodecId track_type;
uint8_t sign[128];
std::thread * sign_thread;
uint8_t thread_exit;
std::mutex sign_mtx;
std::condition_variable sign_start;
std::condition_variable sign_finish;
};
typedef struct {

View File

@ -1,4 +1,9 @@
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>
#include<unistd.h>
#include <arpa/inet.h>
#include "common.h"
#include "HWcommon.h"
@ -12,7 +17,7 @@
#endif
void send_rtp(HWsign* Handle,const char * buf, const uint32_t len ,int tcp,void * param);
void print_data2(const char * buf, uint32_t len,uint8_t offest){
DEBUGL("\n --%d-- \n",len);
@ -86,6 +91,59 @@ int Hardware_sign(SM2Config * config,const unsigned char * data_in,const size_t
#endif
void ThreadSign(HWsign * HW_ptr){
uint8_t sha[128];
while (1)
{
std::unique_lock<std::mutex> lock(HW_ptr->sign_mtx);
while (HW_ptr->I_seq!=-2)
{
if(HW_ptr->thread_exit){
NOTICEL("sign thread release\n");
sleep(0);
return;
}
HW_ptr->sign_start.wait(lock);
}
if(HW_ptr->thread_exit){
NOTICEL("sign thread release\n");
sleep(0);
return;
}
sm3_update(HW_ptr->sm3_hd,(uint8_t*)HW_ptr->buff->data(),HW_ptr->buff->size());
sm3_final(HW_ptr->sm3_hd,sha);
do_sm2_sign((char*)HW_ptr->sm2_hd->prikey,(char*)HW_ptr->sm2_hd->pubkey,(char *) sha, 32, (char *)HW_ptr->sign);
// Hardware_sign(Handle->sm2_hd,(const unsigned char *)(buf+offset),len-offset,sign,&sign_len);
HW_ptr->I_seq=-5;
DEBUGL("\n&&&&&&&& sign: hash &&&&&&&&&&&&&&&&&&&&&&&&");
print_data2((char*)sha,32,32);
DEBUGL("\n&&&&&&&& sign: sign &&&&&&&&&&&&&&&&&&&&&&&&");
print_data2((char*)HW_ptr->sign,64,64);
// HW_ptr->sign_finish.notify_all();
}
}
void GenSEI(HWsign * HW_ptr,const char * sei_head, const uint16_t sei_len,void * param){
uint8_t sei[2048],sei_race[1024];
uint16_t sign_len=64;
// std::unique_lock<std::mutex> lock(HW_ptr->sign_mtx);
// while (HW_ptr->I_seq!=-5)
// {
// HW_ptr->sign_finish.wait(lock);
// }
HW_ptr->sei_param=nullptr;
auto rtp_len=SecMedia::GeneHWSecritySEI(sei,HW_ptr->track_type,1,HW_ptr->buff->size(),sign_len,HW_ptr->sign,(char *)sei_head,sei_len);
rtp_len=add_racing_code(sei_race,sei+sei_len,rtp_len-sei_len);
memcpy(sei+sei_len,sei_race,rtp_len);
rtp_len+=sei_len;
SUCSL("success sign!!!!!!!!!! sei package\n");
send_rtp(HW_ptr,(char *) sei,rtp_len,0,param);
}
bool MakeFU(uint8_t in, FU &fu) {
fu.S = in >> 7;
@ -209,7 +267,9 @@ void * HWSign_init(){
HWSign_hd->sm2_hd->pubkey_size=64;
memcpy(HWSign_hd->sm2_hd->prikey,sm2fig.prikey,sizeof(sm2fig.prikey));
memcpy(HWSign_hd->sm2_hd->pubkey,sm2fig.pubkey,sizeof(sm2fig.pubkey));
HWSign_hd->thread_exit=0;
HWSign_hd->sign_thread=new std::thread(ThreadSign,HWSign_hd);
HWSign_hd->sign_thread->detach();
return (void*)HWSign_hd;
}
@ -219,6 +279,9 @@ void HWSign_release(void* Handle){
delete HWSign_hd->sm2_hd;
delete HWSign_hd->buff;
delete HWSign_hd->rtp_buff;
HWSign_hd->thread_exit=1;
HWSign_hd->sign_start.notify_all();
delete HWSign_hd->sign_thread;
HWSign_hd->rtp_cb=nullptr;
}
@ -342,18 +405,25 @@ int HWSign_rtp_264(HWsign* HWSign_hd, const char * buf, const uint32_t len,int t
switch (nal_type){
case 6:{
DEBUGL("SEI!!!!!!!!!! package\n");
NOTICEL("SEI!!!!!!!!!! package\n");
if(len<1300){
if (!HWSign_hd->buff->empty())
{
sign_data(HWSign_hd,HWSign_hd->buff->data(),HWSign_hd->buff->size(),0, tcp,param,buf,len);
if(HWSign_hd->I_seq==-5)
GenSEI(HWSign_hd,buf,len,param);
HWSign_hd->buff->clear();
}else
{
WRNGL("Without Sign data\n");
HWSign_hd->rtp_cb(buf,len,param);
}
}
HWSign_hd->I_seq=-1;
return 1;
}
case 28:{
@ -372,13 +442,8 @@ int HWSign_rtp_264(HWsign* HWSign_hd, const char * buf, const uint32_t len,int t
if (fu.S) { //第一个rtp包
if(!HWSign_hd->buff->empty()){
WRNGL("Warning!!!!!!!!!! missing package\n");
// if (HWSign_hd->sei_param!=nullptr)
// {
// none_sign_data(HWSign_hd,offset,tcp, HWSign_hd->sei_param,HWSign_hd->sei_rtp_head);
// }
// flush_all(HWSign_hd);
WRNGL("Warning!!!!!!!!!! missing sei package\n");
HWSign_hd->buff->clear();
}
DEBUGL("!!!!!!!!!! I head\n");
HWSign_hd->I_seq=now_seq;
@ -389,7 +454,8 @@ int HWSign_rtp_264(HWsign* HWSign_hd, const char * buf, const uint32_t len,int t
return 1;
}
if(HWSign_hd->I_seq>=0){
if(HWSign_hd->I_seq+1==now_seq){
HWSign_hd->I_seq=now_seq;
// auto rtp_str=make_shared<string>(buf,len);
// HWSign_hd->rtp_buff->push_back(make_pair(rtp_str,param));
HWSign_hd->buff->append(rtp+2,length-2);
@ -399,7 +465,9 @@ int HWSign_rtp_264(HWsign* HWSign_hd, const char * buf, const uint32_t len,int t
}
// flush_all(HWSign_hd);
HWSign_hd->I_seq=-1;
HWSign_hd->I_seq=-2; //complete
HWSign_hd->sign_start.notify_all();
return 1;
}else{
HWSign_hd->rtp_cb(buf,len,param);

View File

@ -165,6 +165,8 @@ void UDPtest(){
ARRAY(20,42)
ARRAY(21,42)
sleep(1);
ARRAY(1,42)
ARRAY(2,42)
ARRAY(3,42)