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.
}
}
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 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.
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.