xref: /aosp_15_r20/external/webrtc/g3doc/implementation_basics.md (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker<?% config.freshness.owner = 'hta' %?>
2*d9f75844SAndroid Build Coastguard Worker<?% config.freshness.reviewed = '2021-05-31' %?>
3*d9f75844SAndroid Build Coastguard Worker
4*d9f75844SAndroid Build Coastguard Worker# Basic concepts and primitives
5*d9f75844SAndroid Build Coastguard Worker
6*d9f75844SAndroid Build Coastguard Worker## Time
7*d9f75844SAndroid Build Coastguard Worker
8*d9f75844SAndroid Build Coastguard WorkerInternally, time is represent using the [webrtc::Timestamp][1] class. This
9*d9f75844SAndroid Build Coastguard Workerrepresents
10*d9f75844SAndroid Build Coastguard Workertime with a resolution of one microsecond, using a 64-bit integer, and provides
11*d9f75844SAndroid Build Coastguard Workerconverters to milliseconds or seconds as needed.
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard WorkerAll timestamps need to be measured from the system monotonic time.
14*d9f75844SAndroid Build Coastguard Worker
15*d9f75844SAndroid Build Coastguard WorkerThe epoch is not specified (because we can't always know if the system clock is
16*d9f75844SAndroid Build Coastguard Workercorrect), but whenever an absolute epoch is needed, the Unix time
17*d9f75844SAndroid Build Coastguard Workerepoch (Jan 1, 1970 at 0:00 GMT) is used.
18*d9f75844SAndroid Build Coastguard Worker
19*d9f75844SAndroid Build Coastguard WorkerConversion from/to other formats (for example milliseconds, NTP times,
20*d9f75844SAndroid Build Coastguard Workertimestamp strings) should happen as close to the interface requiring that
21*d9f75844SAndroid Build Coastguard Workerformat as possible.
22*d9f75844SAndroid Build Coastguard Worker
23*d9f75844SAndroid Build Coastguard WorkerNOTE: There are parts of the codebase that don't use Timestamp, parts of the
24*d9f75844SAndroid Build Coastguard Workercodebase that use the NTP epoch, and parts of the codebase that don't use the
25*d9f75844SAndroid Build Coastguard Workermonotonic clock. They need to
26*d9f75844SAndroid Build Coastguard Workerbe updated.
27*d9f75844SAndroid Build Coastguard Worker
28*d9f75844SAndroid Build Coastguard Worker## Threads
29*d9f75844SAndroid Build Coastguard Worker
30*d9f75844SAndroid Build Coastguard WorkerAll execution happens on a TaskQueue instance. How a TaskQueue is implemented
31*d9f75844SAndroid Build Coastguard Workervaries by platform, but they all have the [webrtc::TaskQueueBase][3] API.
32*d9f75844SAndroid Build Coastguard Worker
33*d9f75844SAndroid Build Coastguard WorkerThis API offers primitives for posting tasks, with or without delay.
34*d9f75844SAndroid Build Coastguard Worker
35*d9f75844SAndroid Build Coastguard WorkerSome core parts use the [rtc::Thread][2], which is a subclass of TaskQueueBase.
36*d9f75844SAndroid Build Coastguard WorkerThis may contain a SocketServer for processing I/O, and is used for policing
37*d9f75844SAndroid Build Coastguard Workercertain calling pattern between a few core threads (the NetworkThread cannot
38*d9f75844SAndroid Build Coastguard Workerdo Invoke on the Worker thread, for instance).
39*d9f75844SAndroid Build Coastguard Worker
40*d9f75844SAndroid Build Coastguard Worker## Reserved class suffixes
41*d9f75844SAndroid Build Coastguard Worker
42*d9f75844SAndroid Build Coastguard WorkerC++ classes with names ending in the suffixes "Factory", "Builder" and "Manager" are supposed to behave
43*d9f75844SAndroid Build Coastguard Workerin certain well known ways.
44*d9f75844SAndroid Build Coastguard Worker
45*d9f75844SAndroid Build Coastguard WorkerFor a particular class name Foo, the following classes, if they exist, should
46*d9f75844SAndroid Build Coastguard Workerbehave as follows:
47*d9f75844SAndroid Build Coastguard Worker
48*d9f75844SAndroid Build Coastguard Worker* FooFactory: Has a Create function that creates a Foo object and returns the
49*d9f75844SAndroid Build Coastguard Worker  object or an owning reference to it (for instance std::unique_ptr or
50*d9f75844SAndroid Build Coastguard Worker  rtc::scoped_refptr<Foo>). The Create function should NOT alter the factory
51*d9f75844SAndroid Build Coastguard Worker  state; ideally, it is marked const. Ownership of the returned object is only
52*d9f75844SAndroid Build Coastguard Worker  with the caller.
53*d9f75844SAndroid Build Coastguard Worker
54*d9f75844SAndroid Build Coastguard Worker* FooBuilder: Has a Build function that returns ownership of a Foo object (as
55*d9f75844SAndroid Build Coastguard Worker  above). The Builder can only be used once, and resources given to the Builder
56*d9f75844SAndroid Build Coastguard Worker  before the Build function is called are either released or owned by the Foo
57*d9f75844SAndroid Build Coastguard Worker  object. The Create function may be reference-qualified (declared as ```Foo
58*d9f75844SAndroid Build Coastguard Worker  Build() &&```), which means it is invoked as ```std::move(builder).Build()```,
59*d9f75844SAndroid Build Coastguard Worker  and C++ will ensure that it is not used again.
60*d9f75844SAndroid Build Coastguard Worker
61*d9f75844SAndroid Build Coastguard Worker* FooManager: Has a Create function that returns an rtc::scoped_refptr<Foo> (if
62*d9f75844SAndroid Build Coastguard Worker  shared ownership) or a Foo* (if the Manager retains sole ownership). If
63*d9f75844SAndroid Build Coastguard Worker  Create() cannot fail, consider returning a Foo&. The Manager is responsible
64*d9f75844SAndroid Build Coastguard Worker  for keeping track of the object; if the Create function returns a Foo*, the
65*d9f75844SAndroid Build Coastguard Worker  Foo object is guaranteed to be destroyed when the FooManager is destroyed.
66*d9f75844SAndroid Build Coastguard Worker
67*d9f75844SAndroid Build Coastguard WorkerIf a Manager class manages multiple classes of objects, the Create functions
68*d9f75844SAndroid Build Coastguard Workershould be appropriately named (the FooAndBarManager would have CreateFoo() and
69*d9f75844SAndroid Build Coastguard WorkerCreateBar() functions), and the class will have a suitable name for the group of
70*d9f75844SAndroid Build Coastguard Workerobjects it is managing.
71*d9f75844SAndroid Build Coastguard Worker
72*d9f75844SAndroid Build Coastguard WorkerFooFactory is mainly useful for the case where preparation for producing Foo
73*d9f75844SAndroid Build Coastguard Workerobjects is complex. If Foo can be created with just an argument list, consider
74*d9f75844SAndroid Build Coastguard Workerexposing its constructor instead; if Foo creation can fail, consider having
75*d9f75844SAndroid Build Coastguard Workera free function called CreateFoo instead of a factory.
76*d9f75844SAndroid Build Coastguard Worker
77*d9f75844SAndroid Build Coastguard WorkerNote that classes with these names exist that do not follow these conventions.
78*d9f75844SAndroid Build Coastguard WorkerWhen they are detected, they need to be marked with TODO statements and bugs
79*d9f75844SAndroid Build Coastguard Workerfiled on them to get them into a conformant state.
80*d9f75844SAndroid Build Coastguard Worker
81*d9f75844SAndroid Build Coastguard Worker## Synchronization primitives
82*d9f75844SAndroid Build Coastguard Worker
83*d9f75844SAndroid Build Coastguard Worker### PostTask and thread-guarded variables
84*d9f75844SAndroid Build Coastguard Worker
85*d9f75844SAndroid Build Coastguard WorkerThe preferred method for synchronization is to post tasks between threads,
86*d9f75844SAndroid Build Coastguard Workerand to let each thread take care of its own variables (lock-free programming).
87*d9f75844SAndroid Build Coastguard WorkerAll variables in
88*d9f75844SAndroid Build Coastguard Workerclasses intended to be used with multiple threads should therefore be
89*d9f75844SAndroid Build Coastguard Workerannotated with RTC_GUARDED_BY(thread).
90*d9f75844SAndroid Build Coastguard Worker
91*d9f75844SAndroid Build Coastguard WorkerFor classes used with only one thread, the recommended pattern is to let
92*d9f75844SAndroid Build Coastguard Workerthem own a webrtc::SequenceChecker (conventionally named sequence_checker_)
93*d9f75844SAndroid Build Coastguard Workerand let all variables be RTC_GUARDED_BY(sequence_checker_).
94*d9f75844SAndroid Build Coastguard Worker
95*d9f75844SAndroid Build Coastguard WorkerMember variables marked const do not need to be guarded, since they never
96*d9f75844SAndroid Build Coastguard Workerchange. (But note that they may point to objects that can change!)
97*d9f75844SAndroid Build Coastguard Worker
98*d9f75844SAndroid Build Coastguard WorkerWhen posting tasks with callbacks, it is the duty of the caller to check
99*d9f75844SAndroid Build Coastguard Workerthat the object one is calling back into still exists when the callback
100*d9f75844SAndroid Build Coastguard Workeris made. A helper for this task is the [webrtc::ScopedTaskSafety][5]
101*d9f75844SAndroid Build Coastguard Workerflag, which can automatically drop callbacks in this situation, and
102*d9f75844SAndroid Build Coastguard Workerassociated classes.
103*d9f75844SAndroid Build Coastguard Worker
104*d9f75844SAndroid Build Coastguard Worker### Synchronization primitives to be used when needed
105*d9f75844SAndroid Build Coastguard Worker
106*d9f75844SAndroid Build Coastguard WorkerWhen it is absolutely necessary to let one thread wait for another thread
107*d9f75844SAndroid Build Coastguard Workerto do something, Thread::Invoke can be used. This function is DISCOURAGED,
108*d9f75844SAndroid Build Coastguard Workersince it leads to performance issues, but is currently still widespread.
109*d9f75844SAndroid Build Coastguard Worker
110*d9f75844SAndroid Build Coastguard WorkerWhen it is absolutely necessary to access one variable from multiple threads,
111*d9f75844SAndroid Build Coastguard Workerthe webrtc::Mutex can be used. Such variables MUST be marked up with
112*d9f75844SAndroid Build Coastguard WorkerRTC_GUARDED_BY(mutex), to allow static analysis that lessens the chance of
113*d9f75844SAndroid Build Coastguard Workerdeadlocks or unintended consequences.
114*d9f75844SAndroid Build Coastguard Worker
115*d9f75844SAndroid Build Coastguard Worker### Synchronization primitives that are being removed
116*d9f75844SAndroid Build Coastguard WorkerThe following non-exhaustive list of synchronization primitives are
117*d9f75844SAndroid Build Coastguard Workerin the (slow) process of being removed from the codebase.
118*d9f75844SAndroid Build Coastguard Worker
119*d9f75844SAndroid Build Coastguard Worker* sigslot. Use [webrtc::CallbackList][4] instead, or, when there's only one
120*d9f75844SAndroid Build Coastguard Worker  signal consumer, a single std::function.
121*d9f75844SAndroid Build Coastguard Worker
122*d9f75844SAndroid Build Coastguard Worker* AsyncInvoker.
123*d9f75844SAndroid Build Coastguard Worker
124*d9f75844SAndroid Build Coastguard Worker* RecursiveCriticalSection. Try to use [webrtc::Mutex][6] instead, and don't recurse.
125*d9f75844SAndroid Build Coastguard Worker
126*d9f75844SAndroid Build Coastguard Worker## Enum-To-String functions
127*d9f75844SAndroid Build Coastguard WorkerIf there is a need to convert an enum to a string representation, such as for
128*d9f75844SAndroid Build Coastguard Workerenums exposed at the Javascript API interface, the recommended way is to write
129*d9f75844SAndroid Build Coastguard Workera function named AsString, declared "static constexpr" and returning an
130*d9f75844SAndroid Build Coastguard Workerabsl::string_view. The declaration should be right after the enum declaration,
131*d9f75844SAndroid Build Coastguard Workerin the same scope; the implementation (which must be marked "inline") should
132*d9f75844SAndroid Build Coastguard Workerbe at the end of the same header file.
133*d9f75844SAndroid Build Coastguard Worker
134*d9f75844SAndroid Build Coastguard WorkerIf the enum is not defined within a class, the "static" keyword is not needed.
135*d9f75844SAndroid Build Coastguard Worker
136*d9f75844SAndroid Build Coastguard Worker[1]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/api/units/timestamp.h;drc=b95d90b78a3491ef8e8aa0640dd521515ec881ca;l=29
137*d9f75844SAndroid Build Coastguard Worker[2]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/rtc_base/thread.h;drc=1107751b6f11c35259a1c5c8a0f716e227b7e3b4;l=194
138*d9f75844SAndroid Build Coastguard Worker[3]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/api/task_queue/task_queue_base.h;drc=1107751b6f11c35259a1c5c8a0f716e227b7e3b4;l=25
139*d9f75844SAndroid Build Coastguard Worker[4]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/rtc_base/callback_list.h;drc=54b91412de3f579a2d5ccdead6e04cc2cc5ca3a1;l=162
140*d9f75844SAndroid Build Coastguard Worker[5]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/rtc_base/task_utils/pending_task_safety_flag.h;drc=86ee89f73e4f4799b3ebcc0b5c65837c9601fe6d;l=117
141*d9f75844SAndroid Build Coastguard Worker[6]: https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/rtc_base/synchronization/mutex.h;drc=0d3c09a8fe5f12dfbc9f1bcd5790fda8830624ec;l=40
142