需要在音频写入audiotrack的时候在头部插入一个16 byte的avsync header
struct TunnelModeSyncHeader {
// The 32-bit data to identify the sync header (0x55550002)
int32 syncWord;
// The size of the audio data following the sync header before the next sync
// header might be found.
int32 sizeInBytes;
// The presentation timestamp of the first audio sample following the sync
// header.
int64 presentationTimestamp;
// The number of bytes to skip after the beginning of the sync header to find the
// first audio sample (20 bytes for compressed audio, or larger for PCM, aligned
// to the channel count and sample size).
int32 offset;
}
这个header包含一个起始码syncWord,时间戳presentationTimestamp(nano sec),数据大小sizeInBytes,实际数据偏移offset。再写入audio数据前先写入这个header data。
例子:
#pragma pack(1) //字节对齐
typedef struct {
unsigned char syncWord[4];
long long pts;
int sizeInBytes;
unsigned int offset;
} AVSyncHeader;
AVSyncHeader avSyncHeader;
memset(&avSyncHeader, 0, sizeof(AVSyncHeader));
avSyncHeader.syncWord[0] = 0x55;
avSyncHeader.syncWord[1] = 0x55;
avSyncHeader.syncWord[2] = 0x00;
avSyncHeader.syncWord[3] = 0x02;
avSyncHeader.sizeInBytes = dataBufSize;
avSyncHeader.offset = 20;
可能涉及大小端转换
uint64_t htonll(uint64_t value) { //大小端
const int num = 42; // Determine host byte order
if (*(const char *)&num == 42) {
// If it is a small end byte order, perform byte flipping
return ((value & 0xFFULL) << 56) |
((value & 0xFF00ULL) << 40) |
((value & 0xFF0000ULL) << 24) |
((value & 0xFF000000ULL) << 8) |
((value & 0xFF00000000ULL) >> 8) |
((value & 0xFF0000000000ULL) >> 24) |
((value & 0xFF000000000000ULL) >> 40) |
((value & 0xFF00000000000000ULL) >> 56);
}
return value;
}
// If it is a large end byte order, return the original value directly
音频流同步头分两个version,version =1时,为16字节同步头;version =2时,为20字节同步头。
0 - 2字节:0x55 0x55 0x00 (固定)头字节
3字节:version ,为1或者2
4 - 7字节:帧大小,当前帧头所带音频数据帧的大小
8 - 15字节:presentationTimestamp值,单位为ns
16 - 19字节:保留位,只有version 为2时才有