Commit 25ee193
committed
[Proposal] Solution 1: PlaybackObserver is moved in
Root issue
----------
While working on code refactoring for better taking into account the new
potentially multithread nature of the RxPlayer code (with some files
only running in a WebWorker environment and others only running in a
main thread environment), we recently refactored our file hierarchy
(#1365) to better reflect that new situation. The idea is to make
RxPlayer developpers more aware of what code is intended to run where.
In that work, we had a remaining issue concerning the
`PlaybackObserver`.
This is the part of the code that is monitoring and advertising playback
conditions to the rest of the RxPlayer code. Most core RxPlayer modules
rely on it, in both main thread and WebWorker environments.
The root issue is that the `PlaybackObserver` is a class and thus cannot
be easily transmitted in-between environments (as the main thread and
WebWorker only exchanges between one another through `postMessage`
calls, which follows some rules preventing from doing that).
As such, and only on a multithreaded scenario, the RxPlayer is
serializing in some way the constructed source PlaybackObserver in the
main thread to reconstruct one (the `WorkerPlaybackObserver`) on the
worker.
Because a whole complex PlaybackObserver-compatible structure has to
both be constructed in the main thread and in the worker (yet only the
main thread can act as the "true" source one, because the media element
can only be accessed in main thread), we were asking ourselves where
should we put the common utils (required by both the main thread and
worker) needed to construct one (like the `ObservationPosition` class
and the `generateReadOnlyObserver` util).
Solution 1
----------
This is a first solution proposal, which moves the `PlaybackObserver`
directory outside of the `main_thread` and `core` directories. Instead,
its code is now directly in `src/playback_observer`.
This solution takes inspiration from the `src/mse` directory, which also
exports both:
1. files intended to be imported when MSE API are present in the
current environment (when in main thread or when in a WebWorker
with the MSE-in-worker feature) and
2. files intended to be imported in environments without MSE.
For example the `src/mse/main_media_source_interface.ts` should __ONLY__
be imported in environments with MSE capabilities. If you do not, you
should import `src/mse/worker_media_source_interface.ts`.
Likewise, I here added a
`src/playback_observer/media_element_playback_observer.ts` file exporting
a `MediaElementPlaybackObserver` structure (new name of the
`PlaybackObserver`) which can only be imported in environements where the
media element is available (so, only on main thread) and a
`src/playback_observer/worker_playback_observer.ts` file which should only
be imported in other environments.
Doing this allows to easily share common utils, in a
`src/playback_observer/utils` directory, without involving the rest of
the RxPlayer code.
Result
------
I'm quite happy with the result, though I get it may seem weird to have
a complex core frequently-running logic part directly in `src` (and not
in `main_thread` or `core` as was the norm) - though it is also the case
of directories like `mse` or `transports`.
RxPlayer code very rarely imported files inside other directories (which
it does here with paths like
`../playback_observer/media_element_playback_observer`) as a way to
push better modularization. Though here it may seem like a feature
because its unusual-ness forces the developper to double check if this
is the right file that is imported.src
1 parent 1d732f4 commit 25ee193
File tree
40 files changed
+726
-661
lines changed- src
- core
- adaptive
- main
- common
- worker
- segment_sinks
- stream
- adaptation
- orchestrator
- period
- utils
- representation
- utils
- main_thread
- api
- init
- utils
- stream_events_emitter
- playback_observer
- utils
40 files changed
+726
-661
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | 19 | | |
24 | 20 | | |
25 | 21 | | |
26 | 22 | | |
27 | 23 | | |
28 | 24 | | |
29 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
604 | 604 | | |
605 | 605 | | |
606 | 606 | | |
607 | | - | |
| 607 | + | |
608 | 608 | | |
609 | 609 | | |
610 | 610 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
22 | | - | |
23 | 22 | | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
28 | 27 | | |
| 28 | + | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| |||
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | 8 | | |
11 | 9 | | |
12 | 10 | | |
| |||
24 | 22 | | |
25 | 23 | | |
26 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
27 | 30 | | |
28 | 31 | | |
29 | 32 | | |
| |||
60 | 63 | | |
61 | 64 | | |
62 | 65 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | 66 | | |
68 | 67 | | |
69 | 68 | | |
| |||
97 | 96 | | |
98 | 97 | | |
99 | 98 | | |
100 | | - | |
| 99 | + | |
101 | 100 | | |
102 | 101 | | |
103 | 102 | | |
| |||
161 | 160 | | |
162 | 161 | | |
163 | 162 | | |
164 | | - | |
| 163 | + | |
165 | 164 | | |
166 | 165 | | |
167 | 166 | | |
| |||
475 | 474 | | |
476 | 475 | | |
477 | 476 | | |
478 | | - | |
| 477 | + | |
479 | 478 | | |
480 | 479 | | |
481 | 480 | | |
| |||
544 | 543 | | |
545 | 544 | | |
546 | 545 | | |
| 546 | + | |
547 | 547 | | |
548 | 548 | | |
549 | 549 | | |
| |||
This file was deleted.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
Lines changed: 2 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 18 | + | |
| 19 | + | |
23 | 20 | | |
24 | 21 | | |
25 | 22 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
0 commit comments