本程序实现通过UDP接受原始AAC数据,并且打包后通过UDP发送示例,里面主要介绍了AAC打包PS的关键流程,希望给到小伙伴们启示。
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
// 计算校验和
unsigned short checksum(const unsigned char *buf, int len) {
unsigned int sum = 0;
while (len > 1) {
sum += (*buf << 8) | *(buf + 1);
len -= 2;
buf += 2;
}
if (len == 1) {
sum += *buf << 8;
}
while (sum >> 16) {
sum = (sum & 0xFFFF) + (sum >> 16);
}
return ~sum;
}
// 将AAC数据打包成PS流
int PackAAC(const unsigned char *aac_data, int aac_len, unsigned char *ps_buffer) {
// 用于存放PES数据的buffer
unsigned char pes_buffer[65536];
// 初始化PES头部信息
PESHeader pes_header = {0};
pes_header.packet_start_code_prefix = 0x000001;
pes_header.stream_id = 192;
pes_header.PES_packet_length = htons(6 + aac_len);
pes_header.PTS_DTS_flags = 0x80;
pes_header.PTS = CalculatePTS();
// 将PES头部信息写入到buffer中
memcpy(pes_buffer, &pes_header, sizeof(pes_header));
// 添加AAC数据到PES负载中
memcpy(pes_buffer + sizeof(pes_header), "\x0f\xff\xf1", 3);
memcpy(pes_buffer + sizeof(pes_header) + 3, aac_data, aac_len);
// 计算PES校验和
unsigned short pes_cksum = checksum(pes_buffer, sizeof(pes_header) + 3 + aac_len);
// 初始化PS头部信息
PSHeader ps_header = {0};
ps_header.sync_byte = 0x47;
ps_header.id = 0x40;
ps_header.PES_start_flag = 1;
ps_header.pid = htons(256); // TODO: 根据需求修改
ps_header.transport_scrambling_control = 0;
ps_header.adaptation_field_control = 1;
ps_header.continuity_counter = continuity_counter++;
// 将PS头部信息写入到buffer中
memcpy(ps_buffer, &ps_header, sizeof(ps_header));
// 添加PES数据到PS负载中
memcpy(ps_buffer + sizeof(ps_header), &pes_header, sizeof(pes_header));
memcpy(ps_buffer + sizeof(ps_header) + sizeof(pes_header), "\x0f\xff\xf1", 3);
memcpy(ps_buffer + sizeof(ps_header) + sizeof(pes_header) + 3, aac_data, aac_len);
*(ps_buffer + sizeof(ps_header) + sizeof(pes_header) + 3 + aac_len) = pes_cksum >> 8;
*(ps_buffer + sizeof(ps_header) + sizeof(pes_header) + 3 + aac_len + 1) = pes_cksum & 0xff;
return sizeof(ps_header) + sizeof(pes_header) + 3 + aac_len + 2;
}
int main() {
// 初始化socket连接和PS流发送参数等信息
int socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in remote_addr = {0};
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(9000); // TODO: 根据需求修改
remote_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // TODO: 根据需求修改
unsigned char ps_buffer[65536];
// 每次接收到AAC数据时,使用PackAAC函数将其打包成PS格式,并通过UDP发送到远程主机
while (1) {
unsigned char aac_data[2048]; // 假设每次接收到的AAC数据长度不超过2048字节
int aac_len = recvfrom(socket_fd, aac_data, sizeof(aac_data), 0, NULL, NULL);
int ps_len = PackAAC(aac_data, aac_len, ps_buffer);
sendto(socket_fd, ps_buffer, ps_len, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
}
return 0;
}