Publish: Preflight & Permissions
Use this guide to verify permissions and capabilities before starting to publish, so users have a smooth experience. You’ll preview the local video, check camera/mic permission state, and confirm the client can publish.
Preview before streaming
By default, the Publisher view shows local preview when you start streaming. You can also explicitly render a preview before publishing:
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).
Check camera access
We recommend checking camera permissions prior to publishing and guiding users to enable access if needed.
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.
}
}
You may control when this prompt is shown by calling [AVCaptureDevice requestAccessForMediaType:completionHandler:] with AVMediaTypeVideo, which will give the prompt on first run. Subsequent calls will execute the completion handler with the user's stored preference.
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){
// Access to the camera is granted. You can publish.
} else {
// Access to the camera is not granted.
}
}];
Check publish capability
After connecting 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.
Check the value of the capabilities.publish property of the Session object. If it is set to 1, the client can publish:
if (session.capabilities.publish == 1) {
// The client can publish. See the next section.
} else {
// The client cannot publish.
// You may want to notify the user.
}
To publish, the client must connect to the session with a token that is assigned a role that supports publishing. There must be a connected camera and microphone. Also, the client environment must support publishing (see Browser support).
Also, publishing is only supported on HTTPS pages.
Check the value of the OTSession capablilites.canPublish property. If it is set to true, the client can publish:
if let capabilities = session.capabilities, capabilities.canPublish {
// The client can publish.
} else {
// The client cannot publish.
// You may want to notify the user.
}
To publish, the client must connect to the session with a token that is assigned a role that supports publishing. See the Token Creation Overview.
Check the value of the [OTSession.capablilites.canPublish:] property. If it is set to YES, the client can publish:
if (session.capabilities.canPublish) {
// The client can publish.
} else {
// The client cannot publish.
// You may want to notify the user.
}
To publish, the client must connect to the session with a token that is assigned a role that supports publishing. See the Token Creation Overview.
To configure devices and quality once preflight passes, continue with Publish: Settings & Devices.