| This API is designed to be used on top of a multithreaded runtime. |
| 7 |
| 8 | The public API functions are designed to be called from a single thread* |
| 9 | (the "client thread"), and can do internal dispatching to the thread |
| 10 | where activity needs to happen. Those threads can be passed in by the |
| 11 | client, typically as arguments to factory constructors, or they can be |
| 12 | created by the library if factory constructors that don't take threads |
| 13 | are used. |
| 14 |
| 15 | Many of the functions are designed to be used in an asynchronous manner, |
| 16 | where a function is called to initiate an activity, and a callback will |
| 17 | be called when the activity is completed, or a handler function will |
| 18 | be called on an observer object when interesting events happen. |
| 19 |
| 20 | Note: Often, even functions that look like simple functions (such as |
| 21 | information query functions) will need to jump between threads to perform |
| 22 | their function - which means that things may happen on other threads |
| 23 | between calls; writing "increment(x); increment(x)" is not a safe |
| 24 | way to increment X by exactly two, since the increment function may have |
| 25 | jumped to another thread that already had a queue of things to handle, |
| 26 | causing large amounts of other activity to have intervened between |
| 27 | the two calls. |
| 28 |
| 29 | (*) The term "thread" is used here to denote any construct that guarantees |
| 30 | sequential execution - other names for such constructs are task runners |
| 31 | and sequenced task queues. |
| 32 |
| 33 | # Client threads and callbacks |
| 34 |
| 35 | At the moment, the API does not give any guarantee on which thread* the |
| 36 | callbacks and events are called on. So it's best to write all callback |
| 37 | and event handlers like this (pseudocode): |
| 38 | <pre> |
| 39 | void ObserverClass::Handler(event) { |
| 40 | if (!called_on_client_thread()) { |
| 41 | dispatch_to_client_thread(bind(handler(event))); |
| 42 | return; |
| 43 | } |
| 44 | // Process event, we're now on the right thread |
| 45 | } |
| 46 | </pre> |
| 47 | In the future, the implementation may change to always call the callbacks |
| 48 | and event handlers on the client thread. |
| 49 |
| 50 | # Implementation considerations |
| 51 |
| 52 | The C++ classes that are part of the public API are also used to derive |
| 53 | classes that form part of the implementation. |
| 54 |
| 55 | This should not directly concern users of the API, but may matter if one |
| 56 | wants to look at how the WebRTC library is implemented, or for legacy code |
| 57 | that directly accesses internal APIs. |
| 58 |
| 59 | Many APIs are defined in terms of a "proxy object", which will do a blocking |
| 60 | dispatch of the function to another thread, and an "implementation object" |
| 61 | which will do the actual |
| 62 | work, but can only be created, invoked and destroyed on its "home thread". |
| 63 |
| 64 | Usually, the classes are named "xxxInterface" (in api/), "xxxProxy" and |
| 65 | "xxx" (not in api/). WebRTC users should only need to depend on the files |
| 66 | in api/. In many cases, the "xxxProxy" and "xxx" classes are subclasses |
| 67 | of "xxxInterface", but this property is an implementation feature only, |
| 68 | and should not be relied upon. |
| 69 |
| 70 | The threading properties of these internal APIs are NOT documented in |
| 71 | this note, and need to be understood by inspecting those classes. |
| 72 |