Initializing a publisher object
A Publisher object is used to capture an audio-video stream from the systems's microphone and camera for use in a Vonage Video session.
You can also use a Publisher to publish a screen-sharing video stream.
The OT.initPublisher() method initializes and returns a Publisher object. The Publisher object represents the view of a video you publish:
var publisher;
var targetElement = 'publisherContainer';
publisher = OT.initPublisher(targetElement, null, function(error) {
if (error) {
// The client cannot publish.
// You may want to notify the user.
} else {
console.log('Publisher initialized.');
}
});
The OT.initPublisher() method takes three parameters:
targetElement— (Optional) Defines the DOM element that the Publisher video replaces.properties— (Optional) A set of properties that customize the Publisher. Thepropertiesparameter also includes options to specify an audio and video input device used by the publisher. Thepropertiesparameter also includes options for customizing the appearance of view in the HTML page (see Customizing the UI) and select whether to publish audio and video (see Publishing audio or video only).For more publisher options, see the documentation of thepropertiesparameter of the OT.initPublisher() method.completionHandler— (Optional) A completion handler that specifies whether the publisher instantiated successfully or with an error.
You can pass this Publisher object into the Session.publish() method to publish a stream to a session.
Before calling Session.publish(), you can use this Publisher object to test the microphone and camera attached to the Publisher.
The insertMode property of the properties parameter of the OT.initPublisher() method specifies how the Publisher object will be inserted in the HTML DOM, in relation to the targetElement parameter. You can set this parameter to one of the following values:
"replace"— The Publisher object replaces contents of the targetElement. This is the default."after"— The Publisher object is a new element inserted after the targetElement in the HTML DOM. (Both the Publisher and targetElement have the same parent element.)"before"— The Publisher object is a new element inserted before the targetElement in the HTML DOM. (Both the Publisher and targetElement have the same parent element.)"append"— The Publisher object is a new element added as a child of the targetElement. If there are other child elements, the Publisher is appended as the last child element of the targetElement.
For example, the following code adds a new Publisher object as a child of a publisherContainer DOM element:
// Try setting insertMode to other values: "replace", "after", or "before":
var publisherProperties = {insertMode: "append"};
var publisher = OT.initPublisher('publisherContainer', publisherProperties, function (error) {
if (error) {
console.log(error);
} else {
console.log("Publisher initialized.");
}
});
For a basic publisher, use the Publisher.Builder class to instantiate a Publisher object. Pass in the Android application context for the publisher:
mPublisher = new Publisher.Builder(context)
.build();
Add a listener object for basic publisher-related events by calling the setPublisherListener(PublisherKit.PublisherListener listener) method of the Publisher object:
mPublisher.setPublisherListener(this);
Implement the methods of the PublisherKit.PublisherListener interface in the object you specify as the event listener object. These methods are called when publisher-related events occur.
The getView() method of the Publisher object returns the view of the video you publish. Add it as a subview of an android.view.ViewGroup object to display it in the app:
mPublisherViewContainer.addView(mPublisher.getView());
Pass the Publisher object into the Session.publish() method to publish a stream to a session.
You can call other methods of the Publisher.Builder object to define custom settings for the publisher:
mPublisher = new Publisher.Builder(context)
.name("Bob")
.audioTrack(true)
.frameRate(CameraCaptureFrameRate.FPS_7)
.resolution(CameraCaptureResolution.LOW)
.videoTrack(false)
.capturer(mCapturer)
.renderer(mRenderer)
.build();
Note that in sessions that use the Vonage Video Media Router (sessions with the media mode set to routed), lowering the frame rate proportionally reduces the bandwidth the stream uses. However, in sessions that have the media mode set to relayed, lowering the frame rate does not reduce the stream's bandwidth.
You can use a custom video capturer to publish a stream with a customized video source. You can also use the custom video capturer to publish a screen-sharing stream.
You can also use a customized audio source for the published stream — see Using a custom audio driver.
Use the OTPublisher object to create a publisher. The view property of the object contains the view of the video you publish:
let publisher = OTPublisher(delegate: self, settings: OTPublisherSettings())!
if let publisherView = publisher.view {
self.view.addSubview(publisherView)
publisherView.frame = CGRect(x: 0, y: 0, width: 200, height: 150)
}
Implement the methods of the OTPublisherDelegate protocol in the object you specify as the delegate object. These methods are called when publisher-related events occur.
Pass the Publisher object into the OTSession publish(_:error:) method to publish a stream to a session.
You can set other properties of the OTPublisherSettings object to define custom settings for the publisher:
let publisherSettings = OTPublisherSettings()
publisherSettings.name = "Bob's video"
publisherSettings.audioTrack = false
publisherSettings.videoTrack = true
publisherSettings.cameraResolution = .high
publisherSettings.cameraFrameRate = .rate30FPS
let publisher = OTPublisher(delegate: self, settings: publisherSettings)!
Note that in sessions that use the Vonage Video Media Router (sessions with the media mode set to routed), lowering the frame rate proportionally reduces the bandwidth the stream uses. However, in sessions that have the media mode set to relayed, lowering the frame rate does not reduce the stream's bandwidth.
You can use a custom video capturer to publish a stream with a customized video source — see Using a custom video capturer. You can also use the custom video capturer to publish a screen-sharing stream — see Screen-sharing.
You can also use a customized audio source for the published stream — see Using a custom audio driver.
Use the OTPublisher object to create a publisher. The view property of the object contains the view of the video you publish:
OTPublisher publisher = [[OTPublisher alloc]
initWithDelegate:self
settings:[[OTPublisherSettings alloc] init]];
[self.view addSubview:publisher.view];
[publisher.view setFrame:CGRectMake(0, 0, 200, 150)];
Implement the methods of the OTPublisherDelegate protocol in the object you specify as the delegate object. These methods are called when publisher-related events occur.
Pass the Publisher object into the [OTSession publish:error] method to publish a stream to a session.
You can set other properties of the OTPublisherSettings object to define custom settings for the publisher:
OTPublisherSettings *_publisherSettings = [[OTPublisherSettings alloc] init];
_publisherSettings.name = @"Bob's video";
_publisherSettings.audioTrack = NO;
_publisherSettings.videoTrack = YES;
_publisherSettings.cameraResolution = OTCameraCaptureResolutionHigh;
_publisherSettings.cameraFrameRate = OTCameraCaptureFrameRate30FPS;
_publisher = [[OTPublisher alloc]
initWithDelegate:self
settings:_publisherSettings];
Note that in sessions that use the Vonage Video Media Router (sessions with the media mode set to routed), lowering the frame rate proportionally reduces the bandwidth the stream uses. However, in sessions that have the media mode set to relayed, lowering the frame rate does not reduce the stream's bandwidth.
You can use a custom video capturer to publish a stream with a customized video source — see Using a custom video capturer.
You can also use the custom video capturer to publish a screen-sharing stream — see Screen-sharing.
You can also use a customized audio source for the published stream — see Using a custom audio driver.
You can create a basic Publisher by calling the Publisher() constructor, passing in the Windows application instance:
publisher = new Publisher(Context.Instance);
publisher.Error += Publisher_Error;
private void Publisher_Error(object sender, Publsher.ErrorEventArgs e)
{
Console.WriteLine("Publisher error:" + e.ErrorCode);
}
Pass the Publisher object into the Session.publish() method to publish a stream to a session.
By default, the Publisher uses the default video capturer, which uses the system's default microphone and camera as the audio an video source for the published stream. This is defined by the VideoCapturer in the Vonage Video SDK.
You can define a specific video capturer object for the publisher to use. This object captures video from a video source (such as a camera), and it has settings for the video (such as the frame rate and resolution).
You can use the VideoCapturer.EnumerateDevices() method to enumerate video capture devices (cameras) on the system. This returns a list of VideoDevice objects. The VideoDevice class includes a number of CreateVideoCapturer() methods to instantiate a VideoCapturer object that uses the video device:
VideoCapturer.CreateVideoCapturer(format)— Creates a VideoCapturer object using the settings defined by a VideoFormat object.VideoCapturer.CreateVideoCapturer(width, height, fps)— Creates a VideoCapturer object with the resolution defined by thewidthandheightparameters and with the frame rate defined by thefpsparameter.VideoCapturer.CreateVideoCapturer(resolution, fps)— Creates a VideoCapturer object with the resolution defined by theresolutionparameter and with the frame rate defined by thefpsparameter. TheOpenTok.VideoCapturer.Resolutionenum defines values used by theresolutionparameter:Low(320x240 pixels),Medium(640x480 pixels), andHigh(1280x720 pixels), andHigh 1080p(1920x1080 pixels).
The video capturer uses the resolution supported on the system closest to the resolution you specify.
By default, publishers use a default video renderer for Windows Presentation Foundation, which is defined by the VideoRenderer class in the Vonage Video Windows SDK. You can also specify a renderer for the video by creating a class that implements the IVideoRenderer interface. You can use a custom video renderer — see Using a custom video renderer.
The following code creates a Publisher that uses the default system video capturer with the resolution set to 720p (VideoCapturer.Resolution.High) and the frame rate set to 30 frames per second:
var devices = VideoCapturer.EnumerateDevices();
var selectedDevice = devices[0];
capturer = selectedDevice.CreateVideoCapturer(VideoCapturer.Resolution.High,
VideoCapturer.VideoCapturer.FrameRate.Fps30);
publisher = new Publisher(Context.Instance, renderer: publisherVideoRenderer, capturer: capturer);
publisher.Error += Publisher_Error;
private void Publisher_Error(object sender, Publsher.ErrorEventArgs e)
{
Console.WriteLine("Publisher error:" + e.ErrorCode);
}
Note: In sessions that use the Vonage Video Media Router (sessions with the media mode set to routed), lowering the frame rate proportionally reduces the bandwidth the stream uses. However, in sessions that have the media mode set to relayed, lowering the frame rate does not reduce the stream's bandwidth.
You pass in other optional parameters of the Publisher() constructor to define custom settings for the published stream:
name— The name of the publisher video. TheStream.Nameproperty for a stream published by this publisher will be set to this value (on all clients).hasAudioTrack— Whether to include an audio track in the published stream. The default istrue.hasVideoTrack— Whether to include an audio track in the published stream. The default istrue.stereo— Whether to enable stereo audio in the published stream. The default isfalse. Set this totrueto publish audio from a stereo microphone.
You can also change the audio source used by the publisher. Or you can create a custom audio driver to be used by all publishers and subscribers.
You can use a custom video capturer to publish a stream with a customized video source — see Using a custom video capturer.
You can also use the custom video capturer to publish a screen-sharing stream — see Screen-sharing.
Note: The OpenTok.Publisher class and the OpenTok.VideoCapturer implement the System.IDisposable interface. Be sure to call the Dispose() method of these objects to release their resources when you no longer need the object (for example, when the Publisher is removed or when the app or window is closing).
Start by creating a structure of type otc_publisher_callbacks:
char *publisher_user_data = strdup("Publisher user data");
static void on_publisher_stream_created(otc_publisher *publisher,
void *user_data,
const otc_stream *stream) {
// The stream has been created.
}
static void on_publisher_render_frame(otc_publisher *publisher,
void *user_data,
const otc_video_frame *frame) {
// You can render the frame to the UI.
}
static void on_publisher_stream_destroyed(otc_publisher *publisher,
void *user_data,
const otc_stream *stream) {
// The stream has been destroyed.
}
static void on_publisher_error(otc_publisher *publisher,
void *user_data,
const char* error_string,
enum otc_publisher_error_code error_code) {
// Handle the error.
}
struct otc_publisher_callbacks publisher_callbacks = {0};
publisher_callbacks.user_data = publisher_user_data;
publisher_callbacks.on_stream_created = on_publisher_stream_created;
publisher_callbacks.on_render_frame = on_publisher_render_frame;
publisher_callbacks.on_stream_destroyed = on_publisher_stream_destroyed;
publisher_callbacks.on_error = on_publisher_error;
Use the user_data member of the otc_publisher_callbacks structure to set data you may want to reference in the callback functions. In this example, we set it to a pointer to a char array. But it could be a pointer to an instance of some other type that contains meaningful information.
The other members of the otc_publisher_callbacks structure are each callback functions that are invoked when events related to the published stream occur:
on_stream_created— Called when the publisher starts streaming to the session.on_render_frame— Called each time the publisher is ready to render a new video frame to the stream.on_stream_destroyed— Called when the publisher's stream is destroyed.on_error— Called when an error occurs in publishing the stream.
All callbacks will not be made on the application or main thread but on an internal thread. The application should return the callback as quickly as possible to avoid blocking the internal thread.
See otc_publisher_callbacks in the OpenTok Linux SDK reference for details on each of the callback functions.
Call the otc_publisher_new() to create an otc_publisher structure, which represents the OpenTok publisher:
publisher = otc_publisher_new("Bob's video",
NULL, /* Use the system camera. */
&publisher_callbacks);
if (publisher == NULL) {
printf("Could not create OpenTok publisher successfully");
otc_session_delete(session);
otc_destroy();
return EXIT_FAILURE;
}
The otc_publisher_new() method takes three arguments:
name— A name (optional) identifying the publisher of the stream.capturer— Use this parameter if you want to provide a custom video capturer. If it is set toNULL, the publisher uses the default system camera as the video source.
See Using a custom video capturer to see how to implement a custom video capturer.
callbacks— Theotc_publisher_callbackspublisher callback structure, described above.
Create a custom audio driver to be used by all publishers and subscribers.
You can use a custom video capturer to publish a stream with a customized video source — see Using a custom video capturer.