Previewing the publisher's video before streaming

Note that, by default, the view of the Publisher contains the video of the publisher when the publisher starts streaming to the session.

To see a preview of the video before the Publisher starts streaming, call the startPreview() method of the Publisher object:

mPublisher.startPreview();

If you call the startPreview() method, you must call the destroy() method of the Publisher to remove the Publisher's view (and the video), when the publisher stops streaming (when the onStreamDestroyed(PublisherKit publisher, Stream stream) method of the PublisherListener is called).

Checking for camera access

We recommend that you check for camera access app permissions prior to publishing and, if access is denied, give feedback to the end-user about setting app access permissions. (An end-user can enable access to the camera via the iOS Settings: Privacy > Camera.)

The end-user is prompted to grant access to the camera for any app requesting it.

You may control when this prompt is shown by calling AVCaptureDevice requestAccess(for:completionHandler:) with AVMediaType.Video, which will give the prompt on first run. Subsequent calls will execute the completion handler with the user's stored preference.

AVCaptureDevice.requestAccess(for: .video) { granted in
	if granted {
		// Access to the camera is granted. You can publish.
	} else {
		// Access to the camera is not granted.
	}
}

Check whether a client has publish capabilities

Once you have connected to a session, you can check if the client can publish.

Once you have connected to a session, you can check if the client can publish. Set a reference to the OTSession object, and call its getCapabilites() method in the sessionConnected event handler. This method returns a promise with an object that includes a canPublish property. You can then conditionally publish based on that value:

import React, {Component} from 'react';
import {View} from 'react-native';
import {OTSession, OTPublisher, OTSubscriber} from 'opentok-react-native';

class App extends Component {
  constructor(props) {
    super(props);
    this.apiKey = 'your API key';
    this.sessionId = 'a session ID';
    this.token = 'a valid token';
    this.state = {canPublish: false};

    this.sessionEventHandlers = {
      sessionConnected: event => {
        this.connectionCount++;
        this.session.getCapabilities().then(capabilities => {
          this.setState({canPublish: capabilities.canPublish});
        });
      },
    };
  }

  render() {
    return (
      <View
        <OTSession
          applicationId={this.apiKey}
          sessionId={this.sessionId}
          token={this.token}
          ref={instance => {
            this.session = instance;
          }}
          eventHandlers={this.sessionEventHandlers}>
          {this.state.canPublish ? (
            <OTPublisher/>
          ) : null}
          <OTSubscriber/>
        </OTSession>
      </View>
    );
  }
}

export default App;

To publish, the client must connect to the session with a token that is assigned a role that supports publishing.