jitsi-meet/react/features/app/middleware.js
Saúl Ibarra Corretgé 9bca0e3b3d [RN] Create tracks right when they are required
When do we need tracks?

- Welcome page (only the video track)
- Conference (depends if starting with audio / video muted is requested)

When do we need to destroy the tracks?

- When we are not in a conference and there is no welcome page

In order to accommodate all the above use cases, a new component is introduced:
BlankWelcomePage. Its purpose is to take the place of the welcome page when it
is disabled. When this component is mounted local tracks are destroyed.

Analogously, a video track is created when the (real) welcome page is created,
and all the desired tracks are created then the Conference component is created.
What are desired tracks? These are the tracks we'd like to use for the
conference that is about to happen. By default both audio and video are desired.
It's possible, however, the user requested to start the call with no
video/audio, in which case it's muted in base/media and a track is not created.

The first time the app starts (with the welcome page) it will request permission
for video only, since there is no need for audio in the welcome page. Later,
when a conference is joined permission for audio will be requested when an audio
track is to be created. The audio track is not destroyed when the conference
ends. Yours truly thinks this is not needed since it's a stopped track which is
not using system resources.
2017-08-22 07:28:19 -05:00

122 lines
4.0 KiB
JavaScript

import { SET_ROOM } from '../base/conference';
import {
CONNECTION_ESTABLISHED,
getURLWithoutParams,
SET_LOCATION_URL
} from '../base/connection';
import { MiddlewareRegistry } from '../base/redux';
MiddlewareRegistry.register(store => next => action => {
switch (action.type) {
case CONNECTION_ESTABLISHED:
return _connectionEstablished(store, next, action);
case SET_LOCATION_URL:
return _setLocationURL(store, next, action);
case SET_ROOM:
return _setRoom(store, next, action);
}
return next(action);
});
/**
* Notifies the feature app that the action {@link CONNECTION_ESTABLISHED} is
* being dispatched within a specific redux {@code store}.
*
* @param {Store} store - The redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
* which is being dispatched in the specified {@code store}.
* @private
* @returns {Object} The new state that is the result of the reduction of the
* specified {@code action}.
*/
function _connectionEstablished(store, next, action) {
const result = next(action);
// In the Web app we explicitly do not want to display the hash and
// query/search URL params. Unfortunately, window.location and, more
// importantly, its params are used not only in jitsi-meet but also in
// lib-jitsi-meet. Consequenlty, the time to remove the params is
// determined by when no one needs them anymore.
const { history, location } = window;
if (history
&& location
&& history.length
&& typeof history.replaceState === 'function') {
const replacement = getURLWithoutParams(location);
if (location !== replacement) {
history.replaceState(
history.state,
(document && document.title) || '',
replacement);
}
}
return result;
}
/**
* Navigates to a route in accord with a specific redux state.
*
* @param {Store} store - The redux store which determines/identifies the route
* to navigate to.
* @private
* @returns {void}
*/
function _navigate({ getState }) {
const state = getState();
const { app, getRouteToRender } = state['features/app'];
const routeToRender = getRouteToRender && getRouteToRender(state);
return app._navigate(routeToRender);
}
/**
* Notifies the feature app that the action {@link SET_LOCATION_URL} is being
* dispatched within a specific redux {@code store}.
*
* @param {Store} store - The redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The redux action, {@code SET_LOCATION_URL}, which is
* being dispatched in the specified {@code store}.
* @private
* @returns {Object} The new state that is the result of the reduction of the
* specified {@code action}.
*/
function _setLocationURL({ getState }, next, action) {
return (
getState()['features/app'].app._navigate(undefined)
.then(() => next(action)));
}
/**
* Notifies the feature app that the action {@link SET_ROOM} is being dispatched
* within a specific redux {@code store}.
*
* @param {Store} store - The redux store in which the specified {@code action}
* is being dispatched.
* @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
* specified {@code action} to the specified {@code store}.
* @param {Action} action - The redux action, {@code SET_ROOM}, which is being
* dispatched in the specified {@code store}.
* @private
* @returns {Object} The new state that is the result of the reduction of the
* specified {@code action}.
*/
function _setRoom(store, next, action) {
const result = next(action);
_navigate(store);
return result;
}