• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "api/metronome/test/fake_metronome.h"
12 
13 #include <utility>
14 #include <vector>
15 
16 #include "api/priority.h"
17 #include "api/sequence_checker.h"
18 #include "api/task_queue/task_queue_base.h"
19 #include "api/task_queue/task_queue_factory.h"
20 #include "api/units/time_delta.h"
21 #include "rtc_base/event.h"
22 #include "rtc_base/task_utils/repeating_task.h"
23 
24 namespace webrtc::test {
25 
ForcedTickMetronome(TimeDelta tick_period)26 ForcedTickMetronome::ForcedTickMetronome(TimeDelta tick_period)
27     : tick_period_(tick_period) {}
28 
RequestCallOnNextTick(absl::AnyInvocable<void ()&&> callback)29 void ForcedTickMetronome::RequestCallOnNextTick(
30     absl::AnyInvocable<void() &&> callback) {
31   callbacks_.push_back(std::move(callback));
32 }
33 
TickPeriod() const34 TimeDelta ForcedTickMetronome::TickPeriod() const {
35   return tick_period_;
36 }
37 
NumListeners()38 size_t ForcedTickMetronome::NumListeners() {
39   return callbacks_.size();
40 }
41 
Tick()42 void ForcedTickMetronome::Tick() {
43   std::vector<absl::AnyInvocable<void() &&>> callbacks;
44   callbacks_.swap(callbacks);
45   for (auto& callback : callbacks)
46     std::move(callback)();
47 }
48 
FakeMetronome(TimeDelta tick_period)49 FakeMetronome::FakeMetronome(TimeDelta tick_period)
50     : tick_period_(tick_period) {}
51 
RequestCallOnNextTick(absl::AnyInvocable<void ()&&> callback)52 void FakeMetronome::RequestCallOnNextTick(
53     absl::AnyInvocable<void() &&> callback) {
54   TaskQueueBase* current = TaskQueueBase::Current();
55   callbacks_.push_back(std::move(callback));
56   if (callbacks_.size() == 1) {
57     current->PostDelayedTask(
58         [this] {
59           std::vector<absl::AnyInvocable<void() &&>> callbacks;
60           callbacks_.swap(callbacks);
61           for (auto& callback : callbacks)
62             std::move(callback)();
63         },
64         tick_period_);
65   }
66 }
67 
TickPeriod() const68 TimeDelta FakeMetronome::TickPeriod() const {
69   return tick_period_;
70 }
71 
72 }  // namespace webrtc::test
73