如何理解stdlib.h里的_countof()宏

本文深入解析了C++模板自动推导原理及_countof宏的实现细节,阐述了如何获取数组元素大小,以及数组引用、数组指针等关键概念在模板函数中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在stdlib.h里有一个宏_countof,如下:
/* _countof helper */ #if !defined(_countof) #if !defined(__cplusplus) #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0])) #else extern "C++" { template <typename _CountofType, size_t _SizeOfArray> char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray]; #define _countof(_Array) sizeof(*__countof_helper(_Array)) } #endif #endif


这个宏的作用就是得到一个数组元素的大小。

如下使用:

#include <stdlib.h> int main() { int a[5]; ...... int arraySize = _countof(a); //得到数组a的大小 }

这个宏使用了一些大家平时都比较少用到的语法。所以理解起来,一下子不是很明白。

所以这里简单说说,如何理解它吧。

为了说明方便,改写如下:
template <typename T, size_t N>
char (* __countof_helper (T (&_Array)[N] ) )[N];

这是一个函数模板声明,没有定义实现体。
__countof_helper是一个函数(红色部分);
其参数是一个“数组引用”T (&)[N](蓝色部分);
而返回值是一个“数组指针”(指向一个数组的指针) char (*)[N](绿色部分);


首先,需要理解3个C++的语法:


1) 数组引用

T (&)[N] (注意:有个括号)。比如:
int a[5] = {0};
int (&ra)[5] = a; 这里ra就是一个对数组a的引用,其这个数组的大小也是5;

数组的信息包括:数组元素的类型,和数组的大小;声明“数组引用”时,类型和大小都要匹配,否则是错误的。比如:
int a[5] = {0};
int (&ra)[4] = a; //编译错误:error C2440: 'initializing' : cannot convert from 'int [5]' to 'int (&)[4]'

注意:C++里没有“引用数组”这个语法概念。"int &ra[5]"这样的语法是通不过编译的。

2) 数组指针

T (*)[N] (注意:有个括号)。比如:
int a[5];
int (*p)[5] = &a;// 是一个指针,该指针指向“一个有5个元素的数组”;简称为“数组指针”

同样,声明时类型和大小都要匹配,如下是错误的。
int a[4];
int (*p)[5] = &a;//error C2440: '=' : cannot convert from 'int (*)[4]' to 'int (*)[5]'

上面说了,C++不支持“引用数组”;但是“指针数组”是绝对支持的,平常都会经常到的。比如如下:
char * names[] = {
"Mike",
"John",
"Tom" };
names就是一个指针数组,该数组有3个元素,每个元素都是一个char类型指针,指向一个字符串。
从上面的例子可以看出,指针数组里面的指针所指向的那块内存区域里的数据大小可以是不同的。
因为指针本身所带的信息只知道被指的内存是什么数据类型,不知道大小信息。比如:
int a[4]; int b[5];
int * p = 0;
p = a; // ok
p = b; // ok

3) 函数返回“数组指针”的声明语法

T (* Fun( param_list ) )[N];
Fun是一个函数,其返回值类型是 T (*)[N];

为什么不可以如下这么声明?
T (*)[N] Fun( param_list ); 这么声明多好理解啊。但是这么是不行的。
C++语法比较复杂,语言设计者对(),[],*,&,&&这些符号所放的位置有特殊的要求。
<<Thinking in C++>>的第3章有一节“Aside: complicated declarations & definitions”,举了一些复杂声明语法的例子。

第二,需要知道的是:sizeof是在编译期完成的。
sizeof是C/C++语言中的keyword,不是函数。对其参数里的表达式是不会在执行期去执行的,而只是在编译期去推算整个表达式的最后的类型信息。
比如: int a = 1; sizeof(a++); cout << a; // a还是1. 不会是2;
因为是在编译期完成,编译器只需要类型信息,不需要函数的实现体。所以就可以不用提供定义体。
下面这个例子可能更能说明这个问题。
int Fun(); // only declare it without definition
sizeof( Fun() ); // 这里Fun()函数不会在运行的时候被执行,仅仅是在编译时,编译器需要知道的是:Fun()函数返回值的类型是什么。


第三,需要理解的是“模板自动推导(template deduction)”
模板推导是由编译器在编译期(compile time)完成的,而不是在执行期(run time)完成的。

这个是理解的关键之一。不涉及内存布局分配的问题。编译期间,编译器只关心声明信息(也就是声明式里面所带的类型信息)。模板推导会自动推导模板参数的各种信息(类型,传递过来的数值等)。以下是一个例子:
template<int x, int y>
struct sum_
{
enum { value = x + y };
};
int sum = sum_<3, 4>::value; // sum的值在编译期间就已经确定下来了,不是在执行期间由CPU运算得到。
这个例子里的模板,仅仅只有数值的传递,没有包含类型。这个可能大家很少见到的。但是模板是支持的。

例子二:
template< typename T, size_t N>
void Fun( T (&)[N] );

这个模板里既有类型T,又有数值N;模板推导时,是根据Fun的实参来推导得到的。比如:
int a[5];
Fun(a); // 编译器经过推导就知道,T=int,N=5。注意这里的N能得到5,是因为Fun的参数声明决定的。这里Fun的参数是:数组引

用。对于“数组引用”,上面说过,编译器能够从实参那里知道数组的类型,又能知道数组的大小。

同时,编译器在进行模板推导时,会对“实参”和“形参”能够匹配的所有情况进行推导,经过所有的尝试都不能匹配,就会报告编译失败信息。比如,对于这个例子,我们将一个指针作为实参传递给它:
int * p = NULL;
Fun( p ); // error C2784: 'void Fun(T (&)[N])' : could not deduce template argument for 'T (&)[N]' from 'int *'
因为一个指针不可能赋值给一个数组引用。所以编译就会出错。


所以对于stdlib.h里的这个_countof(),如果传递一个指针给它,编译就会失败。

同时,需要指出的是,VC6对模板支持的很不好,一些复杂的模板语法就编译不了。根据我的测试,就这里所说的_countof(),在VC6里就编译不了。VC2005/2008就已经支持的很好的。所以这个_countof()是在VC2005中才提供的。在VC6所带的stdlib.h里没有这个_countof()宏的。

【题外话:模板是泛型编程的基础。利用模板自动推导的能力,可以构建出很有技巧性的代码实现。比如:Boost库。更甚者:Loki库<Modern C++ Design>(中文名:C++设计新思维)这本书附带的库。关于模板的语法和自动推导,可以看<C++ Templates the

complete guide>,大陆翻译的中译本编译的不好。侯捷翻译的《C++ Templates全览》比较好,网上有电子的pdf可以找到,但是只

能在电脑上看,不能打印,也不能copy里面的文字。】

第四:注意事项
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) sizeof(*__countof_helper(_Array))

a) #define里的sizeof()的参数是 “*__countof_helper(_Array)”(注意:有个*),而不是“__countof_helper(_Array)”,
如果是“__countof_helper(_Array)”,那么_countof()返回的值永远是一个指针的大小(在32位系统上总是4)。
因为__countof_helper返回的是一个“数组指针”,加上*,就是取得该指针所指向的数组。


sizeof(一个指针)和sizeof(一个数组),是有很大的区别的。看下面的例子:


int a[5];
int *p = a;
int (*pa)[5] = &a;


// p和pa都是指针,但是指针的类型不同:p的类型是int*;pa的类型是int(*)[5]。


sizeof(p) // = 4;
sizeof(a) // = 20 = 5*sizeof(int)
sizeof(*p) // = sizeof(int) = 4;
sizeof(pa) // = 4;
sizeof(*pa) // = sizeof(a) = 20;

C/C++对于数组和指针是明确区分其类型的。数组的信息包括(类型+大小),而指针则没有大小信息。
在这里附带要说一下,数组经过函数参数传递后,数组的大小信息就丢失了。比如:
void Fun( int a[5] )
{
int n = sizeof(a); // n为4,相对于就是sizeof(int*).因为编译器只传递过来了数组的地址,大小信息没有了。
}


所以,上面Fun的参数声明里面的数组的具体数字是被忽略掉的。其等价声明式可以是:
void Fun( int a[10] );
void Fun( int a[] );
void Fun( int *a );


为了需要把大小信息传递到函数里面去,需要额外加一个参数来表示大小信息,这个大小信息需要调用者来提供,并且有调用者来保证大小信息的正确性。
void Fun( int a[], int size );
或者使用stl里的vector模板类来代替:void Fun( vector<int>& v); 因为vector是一个类,它有size()方法能得到大小信息。

b)__countof_helper函数的类型是char,而不是_CountofType。因为sizeof( * (char(*)[N]) )【注:这么写只是为了示范说明,

在代码里不能这么写,通不过编译的】 刚好等于N,因为sizeof(char)在任何操作系统上都是=1;

c) 这个模板函数只有声明(declaration),没有定义(definition)。没有定义体的原因是:sizeof是在编译期完成的,不需要具体

的定义体出现(上面第二点里说了)。同时,模板推导也是在编译期完成的。所以_countof()整个宏在编译期就完成了。不需要函

数的具体实现体。
如果定义了实现体,_countof依然能工作,但是那些实现体都是不会被用到的,而这些实现体会在程序运行起来之后,加载到内存

里。这样就浪费内存了。
因为__countof_helper()没有定义体,所以使用的时候只能使用_countof宏本身,可不要像下面那样调用__countof_helper()。
int a[5];
__countof_helper(a); // 能编译通过,但是链接时,链接器会告诉你找不到实现体。


d) google chrome浏览器源代码里也有一个类似的宏如下:
template <typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))

跟_countof的区别是:
1) ArraySizeHelper函数返回的是 char (&)[N],而不是char (*)[N];
2) #define里没有*号。因为sizeof( char (&)[N] ) = N;

为什么stdlib.h里的_countof宏,不用&,而要用*?
在winnt.h里其实也有个同样功能的宏RTL_NUMBER_OF_V2【注:VC6里没有,VC2005才提供的】,其中有

一句注释是这样的:
“We could return a reference instead of a pointer but older compilers do not accept that.”:)
That's it.

winnt.h 里的注释说明的很好,值得看看。摘抄如下:
//
// RtlpNumberOf is a function that takes a reference to an array of N Ts.
//
// typedef T array_of_T[N];
// typedef array_of_T &reference_to_array_of_T;
//
// RtlpNumberOf returns a pointer to an array of N chars.
// We could return a reference instead of a pointer but older compilers do not accept that.
//
// typedef char array_of_char[N];
// typedef array_of_char *pointer_to_array_of_char;
//
// sizeof(array_of_char) == N
// sizeof(*pointer_to_array_of_char) == N
//
// pointer_to_array_of_char RtlpNumberOf(reference_to_array_of_T);
//
// We never even call RtlpNumberOf, we just take the size of dereferencing its return type.
// We do not even implement RtlpNumberOf, we just decare it.
//
// Attempts to pass pointers instead of arrays to this macro result in compile time errors.
// That is the point.
//
extern "C++" // templates cannot be declared to have 'C' linkage
template <typename T, size_t N>
char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N];

#define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A)))

P.S. 下面这个链接值得看。其中的解释相当好。
http://blogs.msdn.com/the1/archive/2004/05/07/128242.aspx

附图:一些语法

/* _countof helper */ #if !defined(_countof) #if !defined(__cplusplus) #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0])) #else extern "C++" { template <typename _CountofType, size_t _SizeOfArray> char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray]; #define _countof(_Array) sizeof(*__countof_helper(_Array)) } #endif #endif


这个宏的作用就是得到一个数组元素的大小。

如下使用:

#include <stdlib.h> int main() { int a[5]; ...... int arraySize = _countof(a); //得到数组a的大小 }

这个宏使用了一些大家平时都比较少用到的语法。所以理解起来,一下子不是很明白。

所以这里简单说说,如何理解它吧。

为了说明方便,改写如下:
template <typename T, size_t N>
char (* __countof_helper (T (&_Array)[N] ) )[N];

这是一个函数模板声明,没有定义实现体。
__countof_helper是一个函数(红色部分);
其参数是一个“数组引用”T (&)[N](蓝色部分);
而返回值是一个“数组指针”(指向一个数组的指针) char (*)[N](绿色部分);


首先,需要理解3个C++的语法:


1) 数组引用

T (&)[N] (注意:有个括号)。比如:
int a[5] = {0};
int (&ra)[5] = a; 这里ra就是一个对数组a的引用,其这个数组的大小也是5;

数组的信息包括:数组元素的类型,和数组的大小;声明“数组引用”时,类型和大小都要匹配,否则是错误的。比如:
int a[5] = {0};
int (&ra)[4] = a; //编译错误:error C2440: 'initializing' : cannot convert from 'int [5]' to 'int (&)[4]'

注意:C++里没有“引用数组”这个语法概念。"int &ra[5]"这样的语法是通不过编译的。

2) 数组指针

T (*)[N] (注意:有个括号)。比如:
int a[5];
int (*p)[5] = &a;// 是一个指针,该指针指向“一个有5个元素的数组”;简称为“数组指针”

同样,声明时类型和大小都要匹配,如下是错误的。
int a[4];
int (*p)[5] = &a;//error C2440: '=' : cannot convert from 'int (*)[4]' to 'int (*)[5]'

上面说了,C++不支持“引用数组”;但是“指针数组”是绝对支持的,平常都会经常到的。比如如下:
char * names[] = {
"Mike",
"John",
"Tom" };
names就是一个指针数组,该数组有3个元素,每个元素都是一个char类型指针,指向一个字符串。
从上面的例子可以看出,指针数组里面的指针所指向的那块内存区域里的数据大小可以是不同的。
因为指针本身所带的信息只知道被指的内存是什么数据类型,不知道大小信息。比如:
int a[4]; int b[5];
int * p = 0;
p = a; // ok
p = b; // ok

3) 函数返回“数组指针”的声明语法

T (* Fun( param_list ) )[N];
Fun是一个函数,其返回值类型是 T (*)[N];

为什么不可以如下这么声明?
T (*)[N] Fun( param_list ); 这么声明多好理解啊。但是这么是不行的。
C++语法比较复杂,语言设计者对(),[],*,&,&&这些符号所放的位置有特殊的要求。
<<Thinking in C++>>的第3章有一节“Aside: complicated declarations & definitions”,举了一些复杂声明语法的例子。

第二,需要知道的是:sizeof是在编译期完成的。
sizeof是C/C++语言中的keyword,不是函数。对其参数里的表达式是不会在执行期去执行的,而只是在编译期去推算整个表达式的最后的类型信息。
比如: int a = 1; sizeof(a++); cout << a; // a还是1. 不会是2;
因为是在编译期完成,编译器只需要类型信息,不需要函数的实现体。所以就可以不用提供定义体。
下面这个例子可能更能说明这个问题。
int Fun(); // only declare it without definition
sizeof( Fun() ); // 这里Fun()函数不会在运行的时候被执行,仅仅是在编译时,编译器需要知道的是:Fun()函数返回值的类型是什么。


第三,需要理解的是“模板自动推导(template deduction)”
模板推导是由编译器在编译期(compile time)完成的,而不是在执行期(run time)完成的。

这个是理解的关键之一。不涉及内存布局分配的问题。编译期间,编译器只关心声明信息(也就是声明式里面所带的类型信息)。模板推导会自动推导模板参数的各种信息(类型,传递过来的数值等)。以下是一个例子:
template<int x, int y>
struct sum_
{
enum { value = x + y };
};
int sum = sum_<3, 4>::value; // sum的值在编译期间就已经确定下来了,不是在执行期间由CPU运算得到。
这个例子里的模板,仅仅只有数值的传递,没有包含类型。这个可能大家很少见到的。但是模板是支持的。

例子二:
template< typename T, size_t N>
void Fun( T (&)[N] );

这个模板里既有类型T,又有数值N;模板推导时,是根据Fun的实参来推导得到的。比如:
int a[5];
Fun(a); // 编译器经过推导就知道,T=int,N=5。注意这里的N能得到5,是因为Fun的参数声明决定的。这里Fun的参数是:数组引

用。对于“数组引用”,上面说过,编译器能够从实参那里知道数组的类型,又能知道数组的大小。

同时,编译器在进行模板推导时,会对“实参”和“形参”能够匹配的所有情况进行推导,经过所有的尝试都不能匹配,就会报告编译失败信息。比如,对于这个例子,我们将一个指针作为实参传递给它:
int * p = NULL;
Fun( p ); // error C2784: 'void Fun(T (&)[N])' : could not deduce template argument for 'T (&)[N]' from 'int *'
因为一个指针不可能赋值给一个数组引用。所以编译就会出错。


所以对于stdlib.h里的这个_countof(),如果传递一个指针给它,编译就会失败。

同时,需要指出的是,VC6对模板支持的很不好,一些复杂的模板语法就编译不了。根据我的测试,就这里所说的_countof(),在VC6里就编译不了。VC2005/2008就已经支持的很好的。所以这个_countof()是在VC2005中才提供的。在VC6所带的stdlib.h里没有这个_countof()宏的。

【题外话:模板是泛型编程的基础。利用模板自动推导的能力,可以构建出很有技巧性的代码实现。比如:Boost库。更甚者:Loki库<Modern C++ Design>(中文名:C++设计新思维)这本书附带的库。关于模板的语法和自动推导,可以看<C++ Templates the

complete guide>,大陆翻译的中译本编译的不好。侯捷翻译的《C++ Templates全览》比较好,网上有电子的pdf可以找到,但是只

能在电脑上看,不能打印,也不能copy里面的文字。】

第四:注意事项
template <typename _CountofType, size_t _SizeOfArray>
char (*__countof_helper(UNALIGNED _CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray];
#define _countof(_Array) sizeof(*__countof_helper(_Array))

a) #define里的sizeof()的参数是 “*__countof_helper(_Array)”(注意:有个*),而不是“__countof_helper(_Array)”,
如果是“__countof_helper(_Array)”,那么_countof()返回的值永远是一个指针的大小(在32位系统上总是4)。
因为__countof_helper返回的是一个“数组指针”,加上*,就是取得该指针所指向的数组。


sizeof(一个指针)和sizeof(一个数组),是有很大的区别的。看下面的例子:


int a[5];
int *p = a;
int (*pa)[5] = &a;


// p和pa都是指针,但是指针的类型不同:p的类型是int*;pa的类型是int(*)[5]。


sizeof(p) // = 4;
sizeof(a) // = 20 = 5*sizeof(int)
sizeof(*p) // = sizeof(int) = 4;
sizeof(pa) // = 4;
sizeof(*pa) // = sizeof(a) = 20;

C/C++对于数组和指针是明确区分其类型的。数组的信息包括(类型+大小),而指针则没有大小信息。
在这里附带要说一下,数组经过函数参数传递后,数组的大小信息就丢失了。比如:
void Fun( int a[5] )
{
int n = sizeof(a); // n为4,相对于就是sizeof(int*).因为编译器只传递过来了数组的地址,大小信息没有了。
}


所以,上面Fun的参数声明里面的数组的具体数字是被忽略掉的。其等价声明式可以是:
void Fun( int a[10] );
void Fun( int a[] );
void Fun( int *a );


为了需要把大小信息传递到函数里面去,需要额外加一个参数来表示大小信息,这个大小信息需要调用者来提供,并且有调用者来保证大小信息的正确性。
void Fun( int a[], int size );
或者使用stl里的vector模板类来代替:void Fun( vector<int>& v); 因为vector是一个类,它有size()方法能得到大小信息。

b)__countof_helper函数的类型是char,而不是_CountofType。因为sizeof( * (char(*)[N]) )【注:这么写只是为了示范说明,

在代码里不能这么写,通不过编译的】 刚好等于N,因为sizeof(char)在任何操作系统上都是=1;

c) 这个模板函数只有声明(declaration),没有定义(definition)。没有定义体的原因是:sizeof是在编译期完成的,不需要具体

的定义体出现(上面第二点里说了)。同时,模板推导也是在编译期完成的。所以_countof()整个宏在编译期就完成了。不需要函

数的具体实现体。
如果定义了实现体,_countof依然能工作,但是那些实现体都是不会被用到的,而这些实现体会在程序运行起来之后,加载到内存

里。这样就浪费内存了。
因为__countof_helper()没有定义体,所以使用的时候只能使用_countof宏本身,可不要像下面那样调用__countof_helper()。
int a[5];
__countof_helper(a); // 能编译通过,但是链接时,链接器会告诉你找不到实现体。


d) google chrome浏览器源代码里也有一个类似的宏如下:
template <typename T, size_t N>
char (&ArraySizeHelper(T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array)))

跟_countof的区别是:
1) ArraySizeHelper函数返回的是 char (&)[N],而不是char (*)[N];
2) #define里没有*号。因为sizeof( char (&)[N] ) = N;

为什么stdlib.h里的_countof宏,不用&,而要用*?
在winnt.h里其实也有个同样功能的宏RTL_NUMBER_OF_V2【注:VC6里没有,VC2005才提供的】,其中有

一句注释是这样的:
“We could return a reference instead of a pointer but older compilers do not accept that.”:)
That's it.

winnt.h 里的注释说明的很好,值得看看。摘抄如下:
//
// RtlpNumberOf is a function that takes a reference to an array of N Ts.
//
// typedef T array_of_T[N];
// typedef array_of_T &reference_to_array_of_T;
//
// RtlpNumberOf returns a pointer to an array of N chars.
// We could return a reference instead of a pointer but older compilers do not accept that.
//
// typedef char array_of_char[N];
// typedef array_of_char *pointer_to_array_of_char;
//
// sizeof(array_of_char) == N
// sizeof(*pointer_to_array_of_char) == N
//
// pointer_to_array_of_char RtlpNumberOf(reference_to_array_of_T);
//
// We never even call RtlpNumberOf, we just take the size of dereferencing its return type.
// We do not even implement RtlpNumberOf, we just decare it.
//
// Attempts to pass pointers instead of arrays to this macro result in compile time errors.
// That is the point.
//
extern "C++" // templates cannot be declared to have 'C' linkage
template <typename T, size_t N>
char (*RtlpNumberOf( UNALIGNED T (&)[N] ))[N];

#define RTL_NUMBER_OF_V2(A) (sizeof(*RtlpNumberOf(A)))

P.S. 下面这个链接值得看。其中的解释相当好。
http://blogs.msdn.com/the1/archive/2004/05/07/128242.aspx

附图:一些语法

/* * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ #include "api/neteq/neteq.h" #include <math.h> #include <stdlib.h> #include <string.h> // memset #include <algorithm> #include <memory> #include <set> #include <string> #include <vector> #include "absl/flags/flag.h" #include "api/audio/audio_frame.h" #include "api/audio_codecs/builtin_audio_decoder_factory.h" #include "modules/audio_coding/codecs/pcm16b/pcm16b.h" #include "modules/audio_coding/neteq/test/neteq_decoding_test.h" #include "modules/audio_coding/neteq/tools/audio_loop.h" #include "modules/audio_coding/neteq/tools/neteq_rtp_dump_input.h" #include "modules/audio_coding/neteq/tools/neteq_test.h" #include "modules/include/module_common_types_public.h" #include "modules/rtp_rtcp/include/rtcp_statistics.h" #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" #include "rtc_base/ignore_wundef.h" #include "rtc_base/message_digest.h" #include "rtc_base/numerics/safe_conversions.h" #include "rtc_base/strings/string_builder.h" #include "rtc_base/system/arch.h" #include "test/field_trial.h" #include "test/gtest.h" #include "test/testsupport/file_utils.h" ABSL_FLAG(bool, gen_ref, false, "Generate reference files."); namespace webrtc { #if defined(WEBRTC_LINUX) && defined(WEBRTC_ARCH_X86_64) && \ defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && \ (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX)) && \ defined(WEBRTC_CODEC_ILBC) #define MAYBE_TestBitExactness TestBitExactness #else #define MAYBE_TestBitExactness DISABLED_TestBitExactness #endif TEST_F(NetEqDecodingTest, MAYBE_TestBitExactness) { const std::string input_rtp_file = webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"); const std::string output_checksum = "dee7a10ab92526876a70a85bc48a4906901af3df"; const std::string network_stats_checksum = "911dbf5fd97f48d25b8f0967286eb73c9d6f6158"; DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum, absl::GetFlag(FLAGS_gen_ref)); } #if defined(WEBRTC_LINUX) && defined(WEBRTC_ARCH_X86_64) && \ defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && defined(WEBRTC_CODEC_OPUS) #define MAYBE_TestOpusBitExactness TestOpusBitExactness #else #define MAYBE_TestOpusBitExactness DISABLED_TestOpusBitExactness #endif TEST_F(NetEqDecodingTest, MAYBE_TestOpusBitExactness) { const std::string input_rtp_file = webrtc::test::ResourcePath("audio_coding/neteq_opus", "rtp"); const std::string output_checksum = "fec6827bb9ee0b21770bbbb4a3a6f8823bf537dc|" "3610cc7be4b3407b9c273b1299ab7f8f47cca96b"; const std::string network_stats_checksum = "3d043e47e5f4bb81d37e7bce8c44bf802965c853|" "076662525572dba753b11578330bd491923f7f5e"; DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum, absl::GetFlag(FLAGS_gen_ref)); } #if defined(WEBRTC_LINUX) && defined(WEBRTC_ARCH_X86_64) && \ defined(WEBRTC_NETEQ_UNITTEST_BITEXACT) && defined(WEBRTC_CODEC_OPUS) #define MAYBE_TestOpusDtxBitExactness TestOpusDtxBitExactness #else #define MAYBE_TestOpusDtxBitExactness DISABLED_TestOpusDtxBitExactness #endif TEST_F(NetEqDecodingTest, MAYBE_TestOpusDtxBitExactness) { const std::string input_rtp_file = webrtc::test::ResourcePath("audio_coding/neteq_opus_dtx", "rtp"); const std::string output_checksum = "b3c4899eab5378ef5e54f2302948872149f6ad5e|" "589e975ec31ea13f302457fea1425be9380ffb96"; const std::string network_stats_checksum = "dc8447b9fee1a21fd5d1f4045d62b982a3fb0215"; DecodeAndCompare(input_rtp_file, output_checksum, network_stats_checksum, absl::GetFlag(FLAGS_gen_ref)); } // Use fax mode to avoid time-scaling. This is to simplify the testing of // packet waiting times in the packet buffer. class NetEqDecodingTestFaxMode : public NetEqDecodingTest { protected: NetEqDecodingTestFaxMode() : NetEqDecodingTest() { config_.for_test_no_time_stretching = true; } void TestJitterBufferDelay(bool apply_packet_loss); }; TEST_F(NetEqDecodingTestFaxMode, TestFrameWaitingTimeStatistics) { // Insert 30 dummy packets at once. Each packet contains 10 ms 16 kHz audio. size_t num_frames = 30; const size_t kSamples = 10 * 16; const size_t kPayloadBytes = kSamples * 2; for (size_t i = 0; i < num_frames; ++i) { const uint8_t payload[kPayloadBytes] = {0}; RTPHeader rtp_info; rtp_info.sequenceNumber = rtc::checked_cast<uint16_t>(i); rtp_info.timestamp = rtc::checked_cast<uint32_t>(i * kSamples); rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC. rtp_info.payloadType = 94; // PCM16b WB codec. rtp_info.markerBit = 0; ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload)); } // Pull out all data. for (size_t i = 0; i < num_frames; ++i) { bool muted; ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); } NetEqNetworkStatistics stats; EXPECT_EQ(0, neteq_->NetworkStatistics(&stats)); // Since all frames are dumped into NetEQ at once, but pulled out with 10 ms // spacing (per definition), we expect the delay to increase with 10 ms for // each packet. Thus, we are calculating the statistics for a series from 10 // to 300, in steps of 10 ms. EXPECT_EQ(155, stats.mean_waiting_time_ms); EXPECT_EQ(155, stats.median_waiting_time_ms); EXPECT_EQ(10, stats.min_waiting_time_ms); EXPECT_EQ(300, stats.max_waiting_time_ms); // Check statistics again and make sure it's been reset. EXPECT_EQ(0, neteq_->NetworkStatistics(&stats)); EXPECT_EQ(-1, stats.mean_waiting_time_ms); EXPECT_EQ(-1, stats.median_waiting_time_ms); EXPECT_EQ(-1, stats.min_waiting_time_ms); EXPECT_EQ(-1, stats.max_waiting_time_ms); } TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDrift) { // Apply a clock drift of -25 ms / s (sender faster than receiver). const double kDriftFactor = 1000.0 / (1000.0 + 25.0); const double kNetworkFreezeTimeMs = 0.0; const bool kGetAudioDuringFreezeRecovery = false; const int kDelayToleranceMs = 20; const int kMaxTimeToSpeechMs = 100; LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs, kGetAudioDuringFreezeRecovery, kDelayToleranceMs, kMaxTimeToSpeechMs); } TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDrift) { // Apply a clock drift of +25 ms / s (sender slower than receiver). const double kDriftFactor = 1000.0 / (1000.0 - 25.0); const double kNetworkFreezeTimeMs = 0.0; const bool kGetAudioDuringFreezeRecovery = false; const int kDelayToleranceMs = 40; const int kMaxTimeToSpeechMs = 100; LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs, kGetAudioDuringFreezeRecovery, kDelayToleranceMs, kMaxTimeToSpeechMs); } TEST_F(NetEqDecodingTest, LongCngWithNegativeClockDriftNetworkFreeze) { // Apply a clock drift of -25 ms / s (sender faster than receiver). const double kDriftFactor = 1000.0 / (1000.0 + 25.0); const double kNetworkFreezeTimeMs = 5000.0; const bool kGetAudioDuringFreezeRecovery = false; const int kDelayToleranceMs = 60; const int kMaxTimeToSpeechMs = 200; LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs, kGetAudioDuringFreezeRecovery, kDelayToleranceMs, kMaxTimeToSpeechMs); } TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreeze) { // Apply a clock drift of +25 ms / s (sender slower than receiver). const double kDriftFactor = 1000.0 / (1000.0 - 25.0); const double kNetworkFreezeTimeMs = 5000.0; const bool kGetAudioDuringFreezeRecovery = false; const int kDelayToleranceMs = 40; const int kMaxTimeToSpeechMs = 100; LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs, kGetAudioDuringFreezeRecovery, kDelayToleranceMs, kMaxTimeToSpeechMs); } TEST_F(NetEqDecodingTest, LongCngWithPositiveClockDriftNetworkFreezeExtraPull) { // Apply a clock drift of +25 ms / s (sender slower than receiver). const double kDriftFactor = 1000.0 / (1000.0 - 25.0); const double kNetworkFreezeTimeMs = 5000.0; const bool kGetAudioDuringFreezeRecovery = true; const int kDelayToleranceMs = 40; const int kMaxTimeToSpeechMs = 100; LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs, kGetAudioDuringFreezeRecovery, kDelayToleranceMs, kMaxTimeToSpeechMs); } TEST_F(NetEqDecodingTest, LongCngWithoutClockDrift) { const double kDriftFactor = 1.0; // No drift. const double kNetworkFreezeTimeMs = 0.0; const bool kGetAudioDuringFreezeRecovery = false; const int kDelayToleranceMs = 10; const int kMaxTimeToSpeechMs = 50; LongCngWithClockDrift(kDriftFactor, kNetworkFreezeTimeMs, kGetAudioDuringFreezeRecovery, kDelayToleranceMs, kMaxTimeToSpeechMs); } TEST_F(NetEqDecodingTest, UnknownPayloadType) { const size_t kPayloadBytes = 100; uint8_t payload[kPayloadBytes] = {0}; RTPHeader rtp_info; PopulateRtpInfo(0, 0, &rtp_info); rtp_info.payloadType = 1; // Not registered as a decoder. EXPECT_EQ(NetEq::kFail, neteq_->InsertPacket(rtp_info, payload)); } #if defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX) #define MAYBE_DecoderError DecoderError #else #define MAYBE_DecoderError DISABLED_DecoderError #endif TEST_F(NetEqDecodingTest, MAYBE_DecoderError) { const size_t kPayloadBytes = 100; uint8_t payload[kPayloadBytes] = {0}; RTPHeader rtp_info; PopulateRtpInfo(0, 0, &rtp_info); rtp_info.payloadType = 103; // iSAC, but the payload is invalid. EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload)); // Set all of `out_data_` to 1, and verify that it was set to 0 by the call // to GetAudio. int16_t* out_frame_data = out_frame_.mutable_data(); for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) { out_frame_data[i] = 1; } bool muted; EXPECT_EQ(NetEq::kFail, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_FALSE(muted); // Verify that the first 160 samples are set to 0. static const int kExpectedOutputLength = 160; // 10 ms at 16 kHz sample rate. const int16_t* const_out_frame_data = out_frame_.data(); for (int i = 0; i < kExpectedOutputLength; ++i) { rtc::StringBuilder ss; ss << "i = " << i; SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. EXPECT_EQ(0, const_out_frame_data[i]); } } TEST_F(NetEqDecodingTest, GetAudioBeforeInsertPacket) { // Set all of `out_data_` to 1, and verify that it was set to 0 by the call // to GetAudio. int16_t* out_frame_data = out_frame_.mutable_data(); for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; ++i) { out_frame_data[i] = 1; } bool muted; EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_FALSE(muted); // Verify that the first block of samples is set to 0. static const int kExpectedOutputLength = kInitSampleRateHz / 100; // 10 ms at initial sample rate. const int16_t* const_out_frame_data = out_frame_.data(); for (int i = 0; i < kExpectedOutputLength; ++i) { rtc::StringBuilder ss; ss << "i = " << i; SCOPED_TRACE(ss.str()); // Print out the parameter values on failure. EXPECT_EQ(0, const_out_frame_data[i]); } // Verify that the sample rate did not change from the initial configuration. EXPECT_EQ(config_.sample_rate_hz, neteq_->last_output_sample_rate_hz()); } class NetEqBgnTest : public NetEqDecodingTest { protected: void CheckBgn(int sampling_rate_hz) { size_t expected_samples_per_channel = 0; uint8_t payload_type = 0xFF; // Invalid. if (sampling_rate_hz == 8000) { expected_samples_per_channel = kBlockSize8kHz; payload_type = 93; // PCM 16, 8 kHz. } else if (sampling_rate_hz == 16000) { expected_samples_per_channel = kBlockSize16kHz; payload_type = 94; // PCM 16, 16 kHZ. } else if (sampling_rate_hz == 32000) { expected_samples_per_channel = kBlockSize32kHz; payload_type = 95; // PCM 16, 32 kHz. } else { ASSERT_TRUE(false); // Unsupported test case. } AudioFrame output; test::AudioLoop input; // We are using the same 32 kHz input file for all tests, regardless of // `sampling_rate_hz`. The output may sound weird, but the test is still // valid. ASSERT_TRUE(input.Init( webrtc::test::ResourcePath("audio_coding/testfile32kHz", "pcm"), 10 * sampling_rate_hz, // Max 10 seconds loop length. expected_samples_per_channel)); // Payload of 10 ms of PCM16 32 kHz. uint8_t payload[kBlockSize32kHz * sizeof(int16_t)]; RTPHeader rtp_info; PopulateRtpInfo(0, 0, &rtp_info); rtp_info.payloadType = payload_type; bool muted; for (int n = 0; n < 10; ++n) { // Insert few packets and get audio. auto block = input.GetNextBlock(); ASSERT_EQ(expected_samples_per_channel, block.size()); size_t enc_len_bytes = WebRtcPcm16b_Encode(block.data(), block.size(), payload); ASSERT_EQ(enc_len_bytes, expected_samples_per_channel * 2); ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>( payload, enc_len_bytes))); output.Reset(); ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); ASSERT_EQ(1u, output.num_channels_); ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_); ASSERT_EQ(AudioFrame::kNormalSpeech, output.speech_type_); // Next packet. rtp_info.timestamp += rtc::checked_cast<uint32_t>(expected_samples_per_channel); rtp_info.sequenceNumber++; } output.Reset(); // Get audio without inserting packets, expecting PLC and PLC-to-CNG. Pull // one frame without checking speech-type. This is the first frame pulled // without inserting any packet, and might not be labeled as PLC. ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); ASSERT_EQ(1u, output.num_channels_); ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_); // To be able to test the fading of background noise we need at lease to // pull 611 frames. const int kFadingThreshold = 611; // Test several CNG-to-PLC packet for the expected behavior. The number 20 // is arbitrary, but sufficiently large to test enough number of frames. const int kNumPlcToCngTestFrames = 20; bool plc_to_cng = false; for (int n = 0; n < kFadingThreshold + kNumPlcToCngTestFrames; ++n) { output.Reset(); // Set to non-zero. memset(output.mutable_data(), 1, AudioFrame::kMaxDataSizeBytes); ASSERT_EQ(0, neteq_->GetAudio(&output, &muted)); ASSERT_FALSE(muted); ASSERT_EQ(1u, output.num_channels_); ASSERT_EQ(expected_samples_per_channel, output.samples_per_channel_); if (output.speech_type_ == AudioFrame::kPLCCNG) { plc_to_cng = true; double sum_squared = 0; const int16_t* output_data = output.data(); for (size_t k = 0; k < output.num_channels_ * output.samples_per_channel_; ++k) sum_squared += output_data[k] * output_data[k]; EXPECT_EQ(0, sum_squared); } else { EXPECT_EQ(AudioFrame::kPLC, output.speech_type_); } } EXPECT_TRUE(plc_to_cng); // Just to be sure that PLC-to-CNG has occurred. } }; TEST_F(NetEqBgnTest, RunTest) { CheckBgn(8000); CheckBgn(16000); CheckBgn(32000); } TEST_F(NetEqDecodingTest, SequenceNumberWrap) { // Start with a sequence number that will soon wrap. std::set<uint16_t> drop_seq_numbers; // Don't drop any packets. WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false); } TEST_F(NetEqDecodingTest, SequenceNumberWrapAndDrop) { // Start with a sequence number that will soon wrap. std::set<uint16_t> drop_seq_numbers; drop_seq_numbers.insert(0xFFFF); drop_seq_numbers.insert(0x0); WrapTest(0xFFFF - 10, 0, drop_seq_numbers, true, false); } TEST_F(NetEqDecodingTest, TimestampWrap) { // Start with a timestamp that will soon wrap. std::set<uint16_t> drop_seq_numbers; WrapTest(0, 0xFFFFFFFF - 3000, drop_seq_numbers, false, true); } TEST_F(NetEqDecodingTest, TimestampAndSequenceNumberWrap) { // Start with a timestamp and a sequence number that will wrap at the same // time. std::set<uint16_t> drop_seq_numbers; WrapTest(0xFFFF - 10, 0xFFFFFFFF - 5000, drop_seq_numbers, true, true); } TEST_F(NetEqDecodingTest, DiscardDuplicateCng) { uint16_t seq_no = 0; uint32_t timestamp = 0; const int kFrameSizeMs = 10; const int kSampleRateKhz = 16; const int kSamples = kFrameSizeMs * kSampleRateKhz; const size_t kPayloadBytes = kSamples * 2; const int algorithmic_delay_samples = std::max(algorithmic_delay_ms_ * kSampleRateKhz, 5 * kSampleRateKhz / 8); // Insert three speech packets. Three are needed to get the frame length // correct. uint8_t payload[kPayloadBytes] = {0}; RTPHeader rtp_info; bool muted; for (int i = 0; i < 3; ++i) { PopulateRtpInfo(seq_no, timestamp, &rtp_info); ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload)); ++seq_no; timestamp += kSamples; // Pull audio once. ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); } // Verify speech output. EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_); // Insert same CNG packet twice. const int kCngPeriodMs = 100; const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz; size_t payload_len; PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); // This is the first time this CNG packet is inserted. ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>( payload, payload_len))); // Pull audio once and make sure CNG is played. ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_); EXPECT_FALSE( neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG. EXPECT_EQ(timestamp - algorithmic_delay_samples, out_frame_.timestamp_ + out_frame_.samples_per_channel_); // Insert the same CNG packet again. Note that at this point it is old, since // we have already decoded the first copy of it. ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>( payload, payload_len))); // Pull audio until we have played `kCngPeriodMs` of CNG. Start at 10 ms since // we have already pulled out CNG once. for (int cng_time_ms = 10; cng_time_ms < kCngPeriodMs; cng_time_ms += 10) { ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_); EXPECT_FALSE( neteq_->GetPlayoutTimestamp()); // Returns empty value during CNG. EXPECT_EQ(timestamp - algorithmic_delay_samples, out_frame_.timestamp_ + out_frame_.samples_per_channel_); } ++seq_no; timestamp += kCngPeriodSamples; uint32_t first_speech_timestamp = timestamp; // Insert speech again. for (int i = 0; i < 3; ++i) { PopulateRtpInfo(seq_no, timestamp, &rtp_info); ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload)); ++seq_no; timestamp += kSamples; } // Pull audio once and verify that the output is speech again. ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_); absl::optional<uint32_t> playout_timestamp = neteq_->GetPlayoutTimestamp(); ASSERT_TRUE(playout_timestamp); EXPECT_EQ(first_speech_timestamp + kSamples - algorithmic_delay_samples, *playout_timestamp); } TEST_F(NetEqDecodingTest, CngFirst) { uint16_t seq_no = 0; uint32_t timestamp = 0; const int kFrameSizeMs = 10; const int kSampleRateKhz = 16; const int kSamples = kFrameSizeMs * kSampleRateKhz; const int kPayloadBytes = kSamples * 2; const int kCngPeriodMs = 100; const int kCngPeriodSamples = kCngPeriodMs * kSampleRateKhz; size_t payload_len; uint8_t payload[kPayloadBytes] = {0}; RTPHeader rtp_info; PopulateCng(seq_no, timestamp, &rtp_info, payload, &payload_len); ASSERT_EQ(NetEq::kOK, neteq_->InsertPacket( rtp_info, rtc::ArrayView<const uint8_t>(payload, payload_len))); ++seq_no; timestamp += kCngPeriodSamples; // Pull audio once and make sure CNG is played. bool muted; ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_); // Insert some speech packets. const uint32_t first_speech_timestamp = timestamp; int timeout_counter = 0; do { ASSERT_LT(timeout_counter++, 20) << "Test timed out"; PopulateRtpInfo(seq_no, timestamp, &rtp_info); ASSERT_EQ(0, neteq_->InsertPacket(rtp_info, payload)); ++seq_no; timestamp += kSamples; // Pull audio once. ASSERT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_EQ(kBlockSize16kHz, out_frame_.samples_per_channel_); } while (!IsNewerTimestamp(out_frame_.timestamp_, first_speech_timestamp)); // Verify speech output. EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_); } class NetEqDecodingTestWithMutedState : public NetEqDecodingTest { public: NetEqDecodingTestWithMutedState() : NetEqDecodingTest() { config_.enable_muted_state = true; } protected: static constexpr size_t kSamples = 10 * 16; static constexpr size_t kPayloadBytes = kSamples * 2; void InsertPacket(uint32_t rtp_timestamp) { uint8_t payload[kPayloadBytes] = {0}; RTPHeader rtp_info; PopulateRtpInfo(0, rtp_timestamp, &rtp_info); EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload)); } void InsertCngPacket(uint32_t rtp_timestamp) { uint8_t payload[kPayloadBytes] = {0}; RTPHeader rtp_info; size_t payload_len; PopulateCng(0, rtp_timestamp, &rtp_info, payload, &payload_len); EXPECT_EQ(NetEq::kOK, neteq_->InsertPacket(rtp_info, rtc::ArrayView<const uint8_t>( payload, payload_len))); } bool GetAudioReturnMuted() { bool muted; EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); return muted; } void GetAudioUntilMuted() { while (!GetAudioReturnMuted()) { ASSERT_LT(counter_++, 1000) << "Test timed out"; } } void GetAudioUntilNormal() { bool muted = false; while (out_frame_.speech_type_ != AudioFrame::kNormalSpeech) { EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_LT(counter_++, 1000) << "Test timed out"; } EXPECT_FALSE(muted); } int counter_ = 0; }; // Verifies that NetEq goes in and out of muted state as expected. TEST_F(NetEqDecodingTestWithMutedState, MutedState) { // Insert one speech packet. InsertPacket(0); // Pull out audio once and expect it not to be muted. EXPECT_FALSE(GetAudioReturnMuted()); // Pull data until faded out. GetAudioUntilMuted(); EXPECT_TRUE(out_frame_.muted()); // Verify that output audio is not written during muted mode. Other parameters // should be correct, though. AudioFrame new_frame; int16_t* frame_data = new_frame.mutable_data(); for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) { frame_data[i] = 17; } bool muted; EXPECT_EQ(0, neteq_->GetAudio(&new_frame, &muted)); EXPECT_TRUE(muted); EXPECT_TRUE(out_frame_.muted()); for (size_t i = 0; i < AudioFrame::kMaxDataSizeSamples; i++) { EXPECT_EQ(17, frame_data[i]); } EXPECT_EQ(out_frame_.timestamp_ + out_frame_.samples_per_channel_, new_frame.timestamp_); EXPECT_EQ(out_frame_.samples_per_channel_, new_frame.samples_per_channel_); EXPECT_EQ(out_frame_.sample_rate_hz_, new_frame.sample_rate_hz_); EXPECT_EQ(out_frame_.num_channels_, new_frame.num_channels_); EXPECT_EQ(out_frame_.speech_type_, new_frame.speech_type_); EXPECT_EQ(out_frame_.vad_activity_, new_frame.vad_activity_); // Insert new data. Timestamp is corrected for the time elapsed since the last // packet. Verify that normal operation resumes. InsertPacket(kSamples * counter_); GetAudioUntilNormal(); EXPECT_FALSE(out_frame_.muted()); NetEqNetworkStatistics stats; EXPECT_EQ(0, neteq_->NetworkStatistics(&stats)); // NetEqNetworkStatistics::expand_rate tells the fraction of samples that were // concealment samples, in Q14 (16384 = 100%) .The vast majority should be // concealment samples in this test. EXPECT_GT(stats.expand_rate, 14000); // And, it should be greater than the speech_expand_rate. EXPECT_GT(stats.expand_rate, stats.speech_expand_rate); } // Verifies that NetEq goes out of muted state when given a delayed packet. TEST_F(NetEqDecodingTestWithMutedState, MutedStateDelayedPacket) { // Insert one speech packet. InsertPacket(0); // Pull out audio once and expect it not to be muted. EXPECT_FALSE(GetAudioReturnMuted()); // Pull data until faded out. GetAudioUntilMuted(); // Insert new data. Timestamp is only corrected for the half of the time // elapsed since the last packet. That is, the new packet is delayed. Verify // that normal operation resumes. InsertPacket(kSamples * counter_ / 2); GetAudioUntilNormal(); } // Verifies that NetEq goes out of muted state when given a future packet. TEST_F(NetEqDecodingTestWithMutedState, MutedStateFuturePacket) { // Insert one speech packet. InsertPacket(0); // Pull out audio once and expect it not to be muted. EXPECT_FALSE(GetAudioReturnMuted()); // Pull data until faded out. GetAudioUntilMuted(); // Insert new data. Timestamp is over-corrected for the time elapsed since the // last packet. That is, the new packet is too early. Verify that normal // operation resumes. InsertPacket(kSamples * counter_ * 2); GetAudioUntilNormal(); } // Verifies that NetEq goes out of muted state when given an old packet. TEST_F(NetEqDecodingTestWithMutedState, MutedStateOldPacket) { // Insert one speech packet. InsertPacket(0); // Pull out audio once and expect it not to be muted. EXPECT_FALSE(GetAudioReturnMuted()); // Pull data until faded out. GetAudioUntilMuted(); EXPECT_NE(AudioFrame::kNormalSpeech, out_frame_.speech_type_); // Insert a few packets which are older than the first packet. for (int i = 0; i < 5; ++i) { InsertPacket(kSamples * (i - 1000)); } EXPECT_FALSE(GetAudioReturnMuted()); EXPECT_EQ(AudioFrame::kNormalSpeech, out_frame_.speech_type_); } // Verifies that NetEq doesn't enter muted state when CNG mode is active and the // packet stream is suspended for a long time. TEST_F(NetEqDecodingTestWithMutedState, DoNotMuteExtendedCngWithoutPackets) { // Insert one CNG packet. InsertCngPacket(0); // Pull 10 seconds of audio (10 ms audio generated per lap). for (int i = 0; i < 1000; ++i) { bool muted; EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); ASSERT_FALSE(muted); } EXPECT_EQ(AudioFrame::kCNG, out_frame_.speech_type_); } // Verifies that NetEq goes back to normal after a long CNG period with the // packet stream suspended. TEST_F(NetEqDecodingTestWithMutedState, RecoverAfterExtendedCngWithoutPackets) { // Insert one CNG packet. InsertCngPacket(0); // Pull 10 seconds of audio (10 ms audio generated per lap). for (int i = 0; i < 1000; ++i) { bool muted; EXPECT_EQ(0, neteq_->GetAudio(&out_frame_, &muted)); } // Insert new data. Timestamp is corrected for the time elapsed since the last // packet. Verify that normal operation resumes. InsertPacket(kSamples * counter_); GetAudioUntilNormal(); } namespace { ::testing::AssertionResult AudioFramesEqualExceptData(const AudioFrame& a, const AudioFrame& b) { if (a.timestamp_ != b.timestamp_) return ::testing::AssertionFailure() << "timestamp_ diff (" << a.timestamp_ << " != " << b.timestamp_ << ")"; if (a.sample_rate_hz_ != b.sample_rate_hz_) return ::testing::AssertionFailure() << "sample_rate_hz_ diff (" << a.sample_rate_hz_ << " != " << b.sample_rate_hz_ << ")"; if (a.samples_per_channel_ != b.samples_per_channel_) return ::testing::AssertionFailure() << "samples_per_channel_ diff (" << a.samples_per_channel_ << " != " << b.samples_per_channel_ << ")"; if (a.num_channels_ != b.num_channels_) return ::testing::AssertionFailure() << "num_channels_ diff (" << a.num_channels_ << " != " << b.num_channels_ << ")"; if (a.speech_type_ != b.speech_type_) return ::testing::AssertionFailure() << "speech_type_ diff (" << a.speech_type_ << " != " << b.speech_type_ << ")"; if (a.vad_activity_ != b.vad_activity_) return ::testing::AssertionFailure() << "vad_activity_ diff (" << a.vad_activity_ << " != " << b.vad_activity_ << ")"; return ::testing::AssertionSuccess(); } ::testing::AssertionResult AudioFramesEqual(const AudioFrame& a, const AudioFrame& b) { ::testing::AssertionResult res = AudioFramesEqualExceptData(a, b); if (!res) return res; if (memcmp(a.data(), b.data(), a.samples_per_channel_ * a.num_channels_ * sizeof(*a.data())) != 0) { return ::testing::AssertionFailure() << "data_ diff"; } return ::testing::AssertionSuccess(); } } // namespace TEST_F(NetEqDecodingTestTwoInstances, CompareMutedStateOnOff) { ASSERT_FALSE(config_.enable_muted_state); config2_.enable_muted_state = true; CreateSecondInstance(); // Insert one speech packet into both NetEqs. const size_t kSamples = 10 * 16; const size_t kPayloadBytes = kSamples * 2; uint8_t payload[kPayloadBytes] = {0}; RTPHeader rtp_info; PopulateRtpInfo(0, 0, &rtp_info); EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload)); EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload)); AudioFrame out_frame1, out_frame2; bool muted; for (int i = 0; i < 1000; ++i) { rtc::StringBuilder ss; ss << "i = " << i; SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure. EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted)); EXPECT_FALSE(muted); EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted)); if (muted) { EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2)); } else { EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2)); } } EXPECT_TRUE(muted); // Insert new data. Timestamp is corrected for the time elapsed since the last // packet. for (int i = 0; i < 5; ++i) { PopulateRtpInfo(0, kSamples * 1000 + kSamples * i, &rtp_info); EXPECT_EQ(0, neteq_->InsertPacket(rtp_info, payload)); EXPECT_EQ(0, neteq2_->InsertPacket(rtp_info, payload)); } int counter = 0; while (out_frame1.speech_type_ != AudioFrame::kNormalSpeech) { ASSERT_LT(counter++, 1000) << "Test timed out"; rtc::StringBuilder ss; ss << "counter = " << counter; SCOPED_TRACE(ss.str()); // Print out the loop iterator on failure. EXPECT_EQ(0, neteq_->GetAudio(&out_frame1, &muted)); EXPECT_FALSE(muted); EXPECT_EQ(0, neteq2_->GetAudio(&out_frame2, &muted)); if (muted) { EXPECT_TRUE(AudioFramesEqualExceptData(out_frame1, out_frame2)); } else { EXPECT_TRUE(AudioFramesEqual(out_frame1, out_frame2)); } } EXPECT_FALSE(muted); } TEST_F(NetEqDecodingTest, TestConcealmentEvents) { const int kNumConcealmentEvents = 19; const size_t kSamples = 10 * 16; const size_t kPayloadBytes = kSamples * 2; int seq_no = 0; RTPHeader rtp_info; rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC. rtp_info.payloadType = 94; // PCM16b WB codec. rtp_info.markerBit = 0; const uint8_t payload[kPayloadBytes] = {0}; bool muted; for (int i = 0; i < kNumConcealmentEvents; i++) { // Insert some packets of 10 ms size. for (int j = 0; j < 10; j++) { rtp_info.sequenceNumber = seq_no++; rtp_info.timestamp = rtp_info.sequenceNumber * kSamples; neteq_->InsertPacket(rtp_info, payload); neteq_->GetAudio(&out_frame_, &muted); } // Lose a number of packets. int num_lost = 1 + i; for (int j = 0; j < num_lost; j++) { seq_no++; neteq_->GetAudio(&out_frame_, &muted); } } // Check number of concealment events. NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics(); EXPECT_EQ(kNumConcealmentEvents, static_cast<int>(stats.concealment_events)); } // Test that the jitter buffer delay stat is computed correctly. void NetEqDecodingTestFaxMode::TestJitterBufferDelay(bool apply_packet_loss) { const int kNumPackets = 10; const int kDelayInNumPackets = 2; const int kPacketLenMs = 10; // All packets are of 10 ms size. const size_t kSamples = kPacketLenMs * 16; const size_t kPayloadBytes = kSamples * 2; RTPHeader rtp_info; rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC. rtp_info.payloadType = 94; // PCM16b WB codec. rtp_info.markerBit = 0; const uint8_t payload[kPayloadBytes] = {0}; bool muted; int packets_sent = 0; int packets_received = 0; int expected_delay = 0; int expected_target_delay = 0; uint64_t expected_emitted_count = 0; while (packets_received < kNumPackets) { // Insert packet. if (packets_sent < kNumPackets) { rtp_info.sequenceNumber = packets_sent++; rtp_info.timestamp = rtp_info.sequenceNumber * kSamples; neteq_->InsertPacket(rtp_info, payload); } // Get packet. if (packets_sent > kDelayInNumPackets) { neteq_->GetAudio(&out_frame_, &muted); packets_received++; // The delay reported by the jitter buffer never exceeds // the number of samples previously fetched with GetAudio // (hence the min()). int packets_delay = std::min(packets_received, kDelayInNumPackets + 1); // The increase of the expected delay is the product of // the current delay of the jitter buffer in ms * the // number of samples that are sent for play out. int current_delay_ms = packets_delay * kPacketLenMs; expected_delay += current_delay_ms * kSamples; expected_target_delay += neteq_->TargetDelayMs() * kSamples; expected_emitted_count += kSamples; } } if (apply_packet_loss) { // Extra call to GetAudio to cause concealment. neteq_->GetAudio(&out_frame_, &muted); } // Check jitter buffer delay. NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics(); EXPECT_EQ(expected_delay, rtc::checked_cast<int>(stats.jitter_buffer_delay_ms)); EXPECT_EQ(expected_emitted_count, stats.jitter_buffer_emitted_count); EXPECT_EQ(expected_target_delay, rtc::checked_cast<int>(stats.jitter_buffer_target_delay_ms)); } TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithoutLoss) { TestJitterBufferDelay(false); } TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithLoss) { TestJitterBufferDelay(true); } TEST_F(NetEqDecodingTestFaxMode, TestJitterBufferDelayWithAcceleration) { const int kPacketLenMs = 10; // All packets are of 10 ms size. const size_t kSamples = kPacketLenMs * 16; const size_t kPayloadBytes = kSamples * 2; RTPHeader rtp_info; rtp_info.ssrc = 0x1234; // Just an arbitrary SSRC. rtp_info.payloadType = 94; // PCM16b WB codec. rtp_info.markerBit = 0; const uint8_t payload[kPayloadBytes] = {0}; int expected_target_delay = neteq_->TargetDelayMs() * kSamples; neteq_->InsertPacket(rtp_info, payload); bool muted; neteq_->GetAudio(&out_frame_, &muted); rtp_info.sequenceNumber += 1; rtp_info.timestamp += kSamples; neteq_->InsertPacket(rtp_info, payload); rtp_info.sequenceNumber += 1; rtp_info.timestamp += kSamples; neteq_->InsertPacket(rtp_info, payload); expected_target_delay += neteq_->TargetDelayMs() * 2 * kSamples; // We have two packets in the buffer and kAccelerate operation will // extract 20 ms of data. neteq_->GetAudio(&out_frame_, &muted, nullptr, NetEq::Operation::kAccelerate); // Check jitter buffer delay. NetEqLifetimeStatistics stats = neteq_->GetLifetimeStatistics(); EXPECT_EQ(10 * kSamples * 3, stats.jitter_buffer_delay_ms); EXPECT_EQ(kSamples * 3, stats.jitter_buffer_emitted_count); EXPECT_EQ(expected_target_delay, rtc::checked_cast<int>(stats.jitter_buffer_target_delay_ms)); } namespace test { TEST(NetEqNoTimeStretchingMode, RunTest) { NetEq::Config config; config.for_test_no_time_stretching = true; auto codecs = NetEqTest::StandardDecoderMap(); std::map<int, RTPExtensionType> rtp_ext_map = { {1, kRtpExtensionAudioLevel}, {3, kRtpExtensionAbsoluteSendTime}, {5, kRtpExtensionTransportSequenceNumber}, {7, kRtpExtensionVideoContentType}, {8, kRtpExtensionVideoTiming}}; std::unique_ptr<NetEqInput> input = CreateNetEqRtpDumpInput( webrtc::test::ResourcePath("audio_coding/neteq_universal_new", "rtp"), rtp_ext_map, absl::nullopt /*No SSRC filter*/); std::unique_ptr<TimeLimitedNetEqInput> input_time_limit( new TimeLimitedNetEqInput(std::move(input), 20000)); std::unique_ptr<AudioSink> output(new VoidAudioSink); NetEqTest::Callbacks callbacks; NetEqTest test(config, CreateBuiltinAudioDecoderFactory(), codecs, /*text_log=*/nullptr, /*neteq_factory=*/nullptr, /*input=*/std::move(input_time_limit), std::move(output), callbacks); test.Run(); const auto stats = test.SimulationStats(); EXPECT_EQ(0, stats.accelerate_rate); EXPECT_EQ(0, stats.preemptive_rate); } } // namespace test } // namespace webrtc
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值