You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
3.1 KiB

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