【发布时间】:2018-11-25 18:42:32
【问题描述】:
我正在查看 Chromium 的源代码,以研究他们如何实现 MediaRecorder API,该 API 将原始麦克风输入流编码/记录为特定格式。
我遇到了interesting codes from their source。简而言之:
bool DoEncode(float* data_in, std::string* data_out) {
...
data_out->resize(MAX_DATA_BTYES_OR_SOMETHING);
opus_encode_float(
data_in,
reinterpret_cast<uint8_t*>(base::data(*data_out))
);
...
}
所以DoEncode(C++方法)这里接受一个float数组并将其转换为编码字节流,实际操作在opus_encode_float()(这是一个纯C函数)中完成。
有趣的是,Google Chromium 团队使用std::string 代替std::vector<uint_8> 作为字节数组,他们甚至手动转换为uint8_t 缓冲区。
为什么 Google Chromium 团队的人会这样做,是否存在使用 std::string 对通用字节缓冲区比使用像 std::vector<uint8_t> 等其他缓冲区更有用的情况?
【问题讨论】:
-
改用
std::vectot<uint8_t>怎么样? -
@πάνταῥεῖ 我不是在寻找建议。提到的代码由 Google Chromium 团队完成。我有点想知道他们为什么要这样编码。
-
真的only differences 是API 的外观以及操作优化。充其量,我们可以推测 Chromium 团队决定他们希望对数据使用字符串操作而不是收集操作。但事情就是这样——这纯粹是猜测。还有一点要记住:仅仅因为它像 Chromium 或 Google 一样多产并不意味着它是完美的。有时,这些项目会做一些奇怪或明显不正确的事情。
-
@Qix 感谢您的评论。所以毕竟没有理由不使用
std::vector的东西。
标签: c++