Commit 7b7f6141 authored by Max Morin's avatar Max Morin Committed by Commit Bot

Fix Chrome OS audio capture time stamps.

The current code assumes that the time stamp given by CRAS is from the
clock that base::TimeTicks uses. It is not, leading to incorrect time
stamps.

I verified that, in a VM, the new code in apprtc reports ~the same latency
as running "cras_test_client --capture_file /dev/null --show_latency".

Bug: 903213
Change-Id: Ib998414d4c4c8a3df7c7ef62f8386960f7439156
Reviewed-on: https://chromium-review.googlesource.com/c/1326142Reviewed-by: default avatarOlga Sharonova <olka@chromium.org>
Commit-Queue: Max Morin <maxmorin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#606470}
parent 6405b4cf
......@@ -5,6 +5,7 @@
#include "media/audio/cras/cras_input.h"
#include <math.h>
#include <algorithm>
#include "base/logging.h"
#include "base/macros.h"
......@@ -290,13 +291,19 @@ void CrasInputStream::ReadAudio(size_t frames,
double normalized_volume = 0.0;
GetAgcVolume(&normalized_volume);
// Warning: It is generally unsafe to manufacture TimeTicks values; but
// here it is required for interfacing with cras. Assumption: cras
// is providing the timestamp from the CLOCK_MONOTONIC POSIX clock.
const base::TimeTicks capture_time =
base::TimeTicks() + base::TimeDelta::FromTimeSpec(*sample_ts);
DCHECK_EQ(base::TimeTicks::GetClock(),
base::TimeTicks::Clock::LINUX_CLOCK_MONOTONIC);
// Don't just assume sample_ts is from the same clock as base::TimeTicks (it
// is not). Instead, convert it to a latency with a cras utility function
// (guaranteed to use the same clock) and apply that latency to
// TimeTicks::Now().
timespec latency_ts = {0, 0};
cras_client_calc_capture_latency(sample_ts, &latency_ts);
const base::TimeDelta delay =
std::max(base::TimeDelta::FromTimeSpec(latency_ts), base::TimeDelta());
// The delay says how long ago the capture was, so we subtract the delay from
// Now() to find the capture time.
const base::TimeTicks capture_time = base::TimeTicks::Now() - delay;
audio_bus_->FromInterleaved<SignedInt16SampleTypeTraits>(
reinterpret_cast<int16_t*>(buffer), audio_bus_->frames());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment