1*d9f75844SAndroid Build Coastguard Worker<?% config.freshness.owner = 'hta' %?> 2*d9f75844SAndroid Build Coastguard Worker<?% config.freshness.reviewed = '2021-04-12' %?> 3*d9f75844SAndroid Build Coastguard Worker# API Threading Design considerations 4*d9f75844SAndroid Build Coastguard Worker 5*d9f75844SAndroid Build Coastguard WorkerThe header files in this directory form the API to the WebRTC library 6*d9f75844SAndroid Build Coastguard Workerthat is intended for client applications' use. 7*d9f75844SAndroid Build Coastguard Worker 8*d9f75844SAndroid Build Coastguard WorkerThis API is designed to be used on top of a multithreaded runtime. 9*d9f75844SAndroid Build Coastguard Worker 10*d9f75844SAndroid Build Coastguard WorkerThe public API functions are designed to be called from a single thread* 11*d9f75844SAndroid Build Coastguard Worker(the "client thread"), and can do internal dispatching to the thread 12*d9f75844SAndroid Build Coastguard Workerwhere activity needs to happen. Those threads can be passed in by the 13*d9f75844SAndroid Build Coastguard Workerclient, typically as arguments to factory constructors, or they can be 14*d9f75844SAndroid Build Coastguard Workercreated by the library if factory constructors that don't take threads 15*d9f75844SAndroid Build Coastguard Workerare used. 16*d9f75844SAndroid Build Coastguard Worker 17*d9f75844SAndroid Build Coastguard WorkerMany of the functions are designed to be used in an asynchronous manner, 18*d9f75844SAndroid Build Coastguard Workerwhere a function is called to initiate an activity, and a callback will 19*d9f75844SAndroid Build Coastguard Workerbe called when the activity is completed, or a handler function will 20*d9f75844SAndroid Build Coastguard Workerbe called on an observer object when interesting events happen. 21*d9f75844SAndroid Build Coastguard Worker 22*d9f75844SAndroid Build Coastguard WorkerNote: Often, even functions that look like simple functions (such as 23*d9f75844SAndroid Build Coastguard Workerinformation query functions) will need to jump between threads to perform 24*d9f75844SAndroid Build Coastguard Workertheir function - which means that things may happen on other threads 25*d9f75844SAndroid Build Coastguard Workerbetween calls; writing "increment(x); increment(x)" is not a safe 26*d9f75844SAndroid Build Coastguard Workerway to increment X by exactly two, since the increment function may have 27*d9f75844SAndroid Build Coastguard Workerjumped to another thread that already had a queue of things to handle, 28*d9f75844SAndroid Build Coastguard Workercausing large amounts of other activity to have intervened between 29*d9f75844SAndroid Build Coastguard Workerthe two calls. 30*d9f75844SAndroid Build Coastguard Worker 31*d9f75844SAndroid Build Coastguard Worker(*) The term "thread" is used here to denote any construct that guarantees 32*d9f75844SAndroid Build Coastguard Workersequential execution - other names for such constructs are task runners 33*d9f75844SAndroid Build Coastguard Workerand sequenced task queues. 34*d9f75844SAndroid Build Coastguard Worker 35*d9f75844SAndroid Build Coastguard Worker## Client threads and callbacks 36*d9f75844SAndroid Build Coastguard Worker 37*d9f75844SAndroid Build Coastguard WorkerAt the moment, the API does not give any guarantee on which thread* the 38*d9f75844SAndroid Build Coastguard Workercallbacks and events are called on. So it's best to write all callback 39*d9f75844SAndroid Build Coastguard Workerand event handlers like this (pseudocode): 40*d9f75844SAndroid Build Coastguard Worker``` 41*d9f75844SAndroid Build Coastguard Workervoid ObserverClass::Handler(event) { 42*d9f75844SAndroid Build Coastguard Worker if (!called_on_client_thread()) { 43*d9f75844SAndroid Build Coastguard Worker dispatch_to_client_thread(bind(handler(event))); 44*d9f75844SAndroid Build Coastguard Worker return; 45*d9f75844SAndroid Build Coastguard Worker } 46*d9f75844SAndroid Build Coastguard Worker // Process event, we're now on the right thread 47*d9f75844SAndroid Build Coastguard Worker} 48*d9f75844SAndroid Build Coastguard Worker``` 49*d9f75844SAndroid Build Coastguard WorkerIn the future, the implementation may change to always call the callbacks 50*d9f75844SAndroid Build Coastguard Workerand event handlers on the client thread. 51*d9f75844SAndroid Build Coastguard Worker 52*d9f75844SAndroid Build Coastguard Worker## Implementation considerations 53*d9f75844SAndroid Build Coastguard Worker 54*d9f75844SAndroid Build Coastguard WorkerThe C++ classes that are part of the public API are also used to derive 55*d9f75844SAndroid Build Coastguard Workerclasses that form part of the implementation. 56*d9f75844SAndroid Build Coastguard Worker 57*d9f75844SAndroid Build Coastguard WorkerThis should not directly concern users of the API, but may matter if one 58*d9f75844SAndroid Build Coastguard Workerwants to look at how the WebRTC library is implemented, or for legacy code 59*d9f75844SAndroid Build Coastguard Workerthat directly accesses internal APIs. 60*d9f75844SAndroid Build Coastguard Worker 61*d9f75844SAndroid Build Coastguard WorkerMany APIs are defined in terms of a "proxy object", which will do a blocking 62*d9f75844SAndroid Build Coastguard Workerdispatch of the function to another thread, and an "implementation object" 63*d9f75844SAndroid Build Coastguard Workerwhich will do the actual 64*d9f75844SAndroid Build Coastguard Workerwork, but can only be created, invoked and destroyed on its "home thread". 65*d9f75844SAndroid Build Coastguard Worker 66*d9f75844SAndroid Build Coastguard WorkerUsually, the classes are named "xxxInterface" (in api/), "xxxProxy" and 67*d9f75844SAndroid Build Coastguard Worker"xxx" (not in api/). WebRTC users should only need to depend on the files 68*d9f75844SAndroid Build Coastguard Workerin api/. In many cases, the "xxxProxy" and "xxx" classes are subclasses 69*d9f75844SAndroid Build Coastguard Workerof "xxxInterface", but this property is an implementation feature only, 70*d9f75844SAndroid Build Coastguard Workerand should not be relied upon. 71*d9f75844SAndroid Build Coastguard Worker 72*d9f75844SAndroid Build Coastguard WorkerThe threading properties of these internal APIs are NOT documented in 73*d9f75844SAndroid Build Coastguard Workerthis note, and need to be understood by inspecting those classes. 74