# 安装

在容器中进行安装前需要以下前置需求:

1. 开发容器启动使用```-v /usr/include/GL:/usr/include/GL```与```-v /usr/include/X11:/usr/include/X11```将宿主机的相关头文件挂载进容器内,部署容器经过CI后可以不需要宿主机头文件挂载
2. 确认容器内的DISPLAY环境变量为```:0```,使用```env | grep DISPLAY```进行确认;
3. 确认```~/.Xauthority```存在,如果不存在使用```ln -s /root/auth/:0 /root/.Xauthority```
4. 确认宿主机的X服务正常,已执行```xhost +```允许所有用户链接X服务
5. 容器内需执行过```ulimit -c unlimited```或添加该行至```/etc/profile```文件

开发环境容器启动：
```
#注意替换SSH端口号
sudo docker run --privileged -d --gpus all --pid=host -e CNAMA_GPU=true -v /var/run/lightdm/root/:/root/auth/ -v /tmp/.X11-unix:/tmp/.X11-unix -p xxxx:22 harbor.faceunity.com/cnama/base:gpu-1.1 sh -c "apt install ssh -y&&service ssh start&&sleep infinity"
```

生产环境容器启动：
```
#注意替换目标镜像
sudo docker run --privileged -d --gpus all --pid=host -v /var/run/lightdm/root/:/root/auth/ -v /tmp/.X11-unix:/tmp/.X11-unix xxxxx
```

```
# GPU环境需要在安装前设置环境变量CNAMA_GPU为true,初始GPU容器内部已经内置了该变量不需要主动设置
# 需要硬解码的话需要安装前设置环境变量HW_DECODE为true,默认为CPU软解
npm install cnama -S --registry=http://47.96.88.190:4873

# CPU环境需要在安装前设置环境变量CPU_LIBRARY_PREFIX路径,包含OSMesa和libGL等库,初始CPU容器内部已经内置了该变量不需要主动设置
npm install cnama -S --registry=http://47.96.88.190:4873
```

# ！！注意

目前本项目仅为CNAMASDK的API子集

# 使用
写图片
```
const {Setup,CreateItemFromPackage,BindItem,RenderBundles} = require('cnama');
const auth_pack = require("auth_pack.json").auth_pack;
Setup(Buffer.from(auth_pack));
const controller = CreateItemFromPackage(readFileSync(resolve(__dirname, '../assets/controller_cpp.bundle')));
const avatar = CreateItemFromPackage(readFileSync(resolve(__dirname, '../assets/STA_avatar_kt_def_book.bundle')));
const controller_config = CreateItemFromPackage(readFileSync(resolve(__dirname, '../assets/controller_config.bundle')));
BindItem(controller, controller_config);
BindItem(controller, avatar);
const frame = RenderBundles(1920, 1080, new Int32Array([controller]), {
            p_translation: Buffer.from([0, 0, 350]),
            p_rotation: Buffer.from([0, 0, 0, 1]),
            pupil_pos: Buffer.from([0, 0, 0]),
            p_expression: Buffer.from(new Array(57).fill(0)),
            rotation_mode: Buffer.from([0]),
            is_valid: 1,
        });
frame.write("./out.png");
```

写视频
```
const {Setup,CreateItemFromPackage,BindItem,RenderBundles,VideoWriter} = require('cnama');
const auth_pack = require("auth_pack.json").auth_pack;
Setup(Buffer.from(auth_pack));
const controller = CreateItemFromPackage(readFileSync(resolve(__dirname, '../assets/controller_cpp.bundle')));
const avatar = CreateItemFromPackage(readFileSync(resolve(__dirname, '../assets/STA_avatar_kt_def_book.bundle')));
const controller_config = CreateItemFromPackage(readFileSync(resolve(__dirname, '../assets/controller_config.bundle')));
BindItem(controller, controller_config);
BindItem(controller, avatar);

const videowriter = new VideoWriter("out.mp4",{ fps:25,width:1920,height:1080 });

for(let i=0;i<100;i++){
    const frame = RenderBundles(1920, 1080, new Int32Array([controller]), {
            p_translation: Buffer.from([0, 0, 350]),
            p_rotation: Buffer.from([0, 0, 0, 1]),
            pupil_pos: Buffer.from([0, 0, 0]),
            p_expression: Buffer.from(new Array(57).fill(0)),
            rotation_mode: Buffer.from([0]),
            is_valid: 1,
        });
    videowriter.write(frame);   
}

videowriter.release();
```
