Getting information about a stream
In this guide you will learn to collect diagnostic stats concerning active streams.
To get audio and video statistics for subscribers, add event handlers for the audioNetworkStats and videoNetworkStats event dispatched by the OTSubscriber. These provide information about the subscriber's stream, including the following:
- The total number of audio and video packets lost.
- The total number of audio and video packets received.
- The total number of audio and video bytes received.
- The current average video frame rate.
<OTSubscriber
eventHandlers={{
audioNetworkStats: event => {
console.log('subscriber audioNetworkStats event', event);
},
videoNetworkStats: event => {
console.log('subscriber videoNetworkStats event', event);
},
}}
/>
To get more detailed stream statistics, use the Subscriber.getRtcStatsReport() method of the OTSubscriber object. It returns a promise that, on success, resolves with a JSON representation of an RtcStatsReport object for the subscribed stream:
<OTSubscriber
eventHandlers={{
connected: event => {
setTimeout(() => {
this.subscriber.getRtcStatsReport();
}, 4000);
},
rtcStatsReport: event => {
console.log('subscriber rtcStatsReport event', event);
},
}}
/>
The Stream object has the following properties that define the stream:
connection— The Connection object corresponding to the connection that is publishing the stream. You can compare this to the connection property of the Session object to see if the stream is being published by the local web page.creationTime— The timestamp (a number) for the creation of the stream. This value is calculated in milliseconds. You can convert this value to a Date object by callingnew Date(stream.creationTime).hasAudio— (Boolean) Whether the stream has audio. This property can change if the publisher turns on or off audio (by calling Publisher.publishAudio()). When this occurs, the Session object dispatches astreamPropertyChangedevent.hasVideo— (Boolean) Whether the stream has video. initials—(Boolean) The initials for the stream (if initials were set when the stream's publisher was initialized).name— (String) The name of the stream. This is, by default, displayed when the user mouses over the Subscriber in the HTML DOM. You can, however, customize the UI to hide the name or display it without mousing over.videoDimensions— This object has two properties:widthandheight. Both are numbers. Thewidthproperty is the width of the encoded stream; theheightproperty is the height of the encoded stream. (These are independent of the actual width of Publisher and Subscriber objects corresponding to the stream.) This property can change if a stream published from an iOS device resizes, based on a change in the device orientation.videoType— The type of video: either "camera", "screen", "custom", or undefined. A "screen" video uses screen sharing on the publisher as the video source; a "custom" video uses a VideoTrack element as the video source on the publisher. ThevideoTypeisundefinedwhen a stream is voice-only (see the Voice-only guide). This property can change if a stream published from a mobile device changes from a camera to a screen-sharing video type. For more information, see Screen sharing — Web.
The hasAudio, hasVideo, videoDimensions, and videoType properties can change (for example, when the publisher turns on or off video). When this occurs, the Session object dispatches a streamPropertyChanged event (see StreamPropertyChangedEvent).
The getStats() method of a Subscriber object provides you with information about the subscriber's stream, including the following:
- The total number of audio and video packets lost
- The total number of audio and video packets received
- The total number of audio and video bytes received
- The current average video frame rate
The following code logs the audio packet loss ratio, the audio bit rate, and the video packet loss ratio, and the video bit rate for a subscriber every second:
var prevStats;
window.setInterval(function() {
subscriber.getStats(function(error, stats) {
if (error) {
console.error('Error getting subscriber stats. ', error.message);
return;
}
if (prevStats) {
var videoPacketLossRatio = stats.video.packetsLost /
(stats.video.packetsLost + stats.video.packetsReceived);
console.log('video packet loss ratio: ', videoPacketLossRatio);
var videoBitRate = 8 * (stats.video.bytesReceived - prevStats.video.bytesReceived);
console.log('video bit rate: ', videoBitRate, 'bps');
var audioPacketLossRatio = stats.audio.packetsLost /
(stats.audio.packetsLost + stats.audio.packetsReceived);
console.log('audio packet loss ratio: ', audioPacketLossRatio);
var audioBitRate = 8 * (stats.audio.bytesReceived - prevStats.audio.bytesReceived);
console.log('audio bit rate: ', audioBitRate, 'bps');
}
prevStats = stats;
});
}, 1000);
To get statistics for a stream published by the local client, you must use a session that uses the OpenTok Media Router (sessions with the media mode set to routed), and you must set the testNetwork property to true in the options object you pass into the Session.subscribe() method:
var publisher = OT.initPublisher();
publisher.on('streamCreated', function(event)) {
var subscriberOptions = {testNetwork: true};
var subscriber = session.subscribe(event.stream, 'publisher-element', subscriberOptions);
}
To get more detailed stream statistics, use the Subscriber.getRtcStatsReport() method. It returns a promise that, on success, resolves with an RtcStatsReport object for the subscribed stream:
subscriber.getRtcStatsReport()
.then((stats) => stats.forEach(console.log))
.catch(console.log);
The Stream object has the following methods that return values that define the stream:
getConnection()— (Connection) Returns the Connection object corresponding to the connection that is publishing the stream. You can compare this to the value returned by thegetConnection()method of the Session object to see if the stream is being published by your client.getCreationTime()— (Date) The Date timestamp for the creation time of the stream.hasAudio()— (boolean) Whether the stream has audio.hasVideo()— (boolean) Whether the stream has video.getName()— (String) Returns the name of the stream. This is set when you initialize the Publisher of the stream (see Initializing a Publisher object).getStreamId()— (String) The unique ID for the stream.getVideoHeight()— (int) The height of the stream, in pixels.getVideoType()— (StreamVideoType) Whether the stream uses a camera video source (StreamVideoTypeCamera.StreamVideoTypeCamera), a screen-sharing video source (StreamVideoTypeScreen.StreamVideoTypeScreen), or a custom video source (StreamVideoTypeScreen.StreamVideoTypeScreen).
See Screen sharing.
getVideoWidth()— (int) The width of the stream, in pixels.
You can set listeners to monitor the following statistics for a subscriber's stream:
- The total number of audio and video packets lost
- The total number of audio and video packets received
- The total number of audio and video bytes received
See the SubscriberKit.setAudioStatsListener(AudioStatsListener listener) and SubscriberKit.setVideoStatsListener(VideoStatsListener listener) methods.
To get statistics for a stream published by the local client, you must use a session that uses the Vonage Video Media Router (sessions with the media mode set to routed).
To get more detailed stream statistics, use the SubscriberKit.getRtcStatsReport() method. This provides an RTC stats report for the media stream.
This is an asynchronous operation. Call the SubscriberKit.setRtcStatsReportListener(SubscriberKit.SubscriberRtcStatsReportListener listener) method, and then implement the SubscriberKit.SubscriberRtcStatsReportListener.onRtcStatsReport(SubscriberKit subscriber, java.lang.String jsonArrayOfReports) method prior to calling SubscriberKit.getRtcStatsReport().
When the stats are available, the implementation of the SubscriberKit.SubscriberRtcStatsReportListener.onRtcStatsReport(SubscriberKit subscriber, java.lang.String jsonArrayOfReports) method is called. The jsonArrayOfReports parameter is a JSON array of RTC stats reports, which are similar to the format of the RtcStatsReport object implemented in web browsers (see these Mozilla docs).
Also see this W3C documentation.
The OTStream object has the following properties that define the stream:
connection—The OTConnection object corresponding to the connection that is publishing the stream. You can compare this to theconnectionproperty of the OTSession object to see if the stream is being published by your client.creationTime—The Date timestamp for the creation time of the stream.hasAudio—(Bool) Whether the stream has audio.hasVideo—(Bool) Whether the stream has video.name—(String?) The name of the stream. This is set when you initialize the Publisher of the stream (see Initializing an OTPublisher object).session—(OTSession) The Vonage Video session to which the stream is bound.streamId—(String) The unique ID for the stream.videoDimensions—A CGSize object defining the current dimensions of the video media track on this stream.videoType—(OTStreamVideoType) Whether the stream uses a camera video source (OTStreamVideoTypeCamera), a screen-sharing video source (OTStreamVideoTypeScreen), or a custom video source (OTStreamVideoTypeCustom).
See Screen sharing.
You can set a OTSubscriberKitNetworkStatsDelegate object for the OTSubscriberKit object to monitor the following statistics for a subscriber's stream:
- The total number of audio and video packets lost
- The total number of audio and video packets received
- The total number of audio and video bytes received
See the SubscriberKit.networkStatsDelegate property.
To get statistics for a stream published by the local client, you must use a session that uses the Vonage Video Media Router (sessions with the media mode set to routed).
The OTStream object has the following properties that define the stream:
connection—The OTConnection object corresponding to the connection that is publishing the stream. You can compare this to theconnectionproperty of the OTSession object to see if the stream is being published by your client.creationTime—The NSDate timestamp for the creation time of the stream.hasAudio—(Boolean) Whether the stream has audio.hasVideo—(Boolean) Whether the stream has video.name—(NSString) The name of the stream. This is set when you initialize the Publisher of the stream (see Initializing an OTPublisher object).session—(OTSession) The Vonage Video session to which the stream is bound.streamId—(NSString) The unique ID for the stream.videoDimensions—A CGSize object defining the current dimensions of the video media track on this stream.videoType—(OTStreamVideoType) Whether the stream uses a camera video source (OTStreamVideoTypeCamera), a screen-sharing video source (OTStreamVideoTypeScreen), or a custom video source (OTStreamVideoTypeCustom).
See Screen sharing.
You can set a OTSubscriberKitNetworkStatsDelegate object for the OTSubscriberKit object to monitor the following statistics for a subscriber's stream:
- The total number of audio and video packets lost
- The total number of audio and video packets received
- The total number of audio and video bytes received
See the SubscriberKit.networkStatsDelegate property.
To get statistics for a stream published by the local client, you must use a session that uses the Vonage Video Media Router (sessions with the media mode set to routed).
To get more detailed stream statistics, use the [OTSubscriberKit getRtcStatsReport:] method. This provides an RTC stats report for the media stream.
This is an asynchronous operation. Set the [OTSubscriberKit rtcStatsReportDelegate]> property and implement the >[OTSubscriberKitRtcStatsReportDelegate subscriber:rtcStatsReport:]> method prior to calling [OTSubscriberKit getRtcStatsReport:].
When the stats are available, the implementation of the >[OTSubscriberKitRtcStatsReportDelegate subscriber:rtcStatsReport:]> message is sent. The message includes an a jsonArrayOfReports parameter.
This is a JSON array of RTC stats reports, which are similar to the format the RtcStatsReport object implemented in web browsers (see these Mozilla docs).
Also see this W3C documentation.
The Stream object has the following properties that define the stream:
Connection— (Connection) The Connection object corresponding to the connection that is publishing the stream. You can compare this to theConnectionproperty of the Session object to see if the stream is being published by your client.CreationTime— (DateTime) The DateTime timestamp for the creation time of the stream.HasAudio— (bool) Whether the stream has audio.HasVideo— (bool) Whether the stream has video.Name— (string) The name of the stream. This is set when you initialize the Publisher of the stream (see Initializing a Publisher object).Id— (string) The unique ID for the stream.Height— (int) The height of the stream, in pixels.VideoSourceType— (VideoSourceType) Whether the stream uses a camera video source (VideoSourceType.StreamVideoTypeCamera), a screen-sharing video source (VideoSourceType.StreamVideoTypeScreen), or a custom video source (VideoSourceType.StreamVideoTypeCustom).
See Screen sharing.
Width— (int) The width of the stream, in pixels.
You use the Subscriber.AudioStatsUpdated and Subscriber.VideoStatsUpdated events to monitor the following statistics for a subscriber's stream:
- The total number of audio and video packets lost
- The total number of audio and video packets received
- The total number of audio and video bytes received
To get more detailed stream statistics, use the Subscriber.GetRtcStatsReport() method. This provides an RTC stats report for the media stream.
This is an asynchronous operation. When the stats are available, the RtcStatsReport event is sent. The RtcStatsReportArgs object includes a JsonArrayOfReports property. This is a JSON array of RTC stats reports, which are similar to the format the RtcStatsReport object implemented in web browsers (see these Mozilla docs).
Also see this W3C documentation.
Call the following functions to get information about a stream:
otc_stream_get_connection()— Returns theotc_connectioninstance corresponding to the connection that is publishing the stream. You can compare the connection ID for this to the connection ID for theotc_connectioninstance returned by theotc_session_get_connection()function to see if the stream is being published by your client.otc_stream_get_creation_time()— Returns the timestamp for the creation time of the stream.otc_stream_has_audio()— Whether the stream is currently publishing audio.otc_stream_has_video()— Whether the stream is currently publishing video.otc_stream_has_audio_track()— Whether the stream has an audio track.otc_stream_has_video_track()— Whether the stream has a video track.otc_stream_get_name()— Returns the name of the stream. This is set when you initialize the Publisher of the stream (see Initializing an otc_publisher struct and setting publisher callbacks).otc_stream_get_id()— Returns the unique ID for the stream.otc_stream_get_video_height()— The height of the stream, in pixels.otc_stream_get_video_type()— Whether the stream uses a camera video source (OTC_STREAM_VIDEO_TYPE_CAMERA) or a screen-sharing video source (OTC_STREAM_VIDEO_TYPE_SCREEN).otc_stream_get_video_width()— The width of the stream, in pixels.
Use the on_audio_stats() and on_video_stats() callback functions of the otc_subscriber_callbacks to monitor the following statistics for a subscriber's stream:
- The total number of audio and video packets lost
- The total number of audio and video packets received
- The total number of audio and video bytes received
To get more detailed stream statistics, use the otc_subscriber_get_rtc_stats_report() function. This provides an RTC stats report for the media stream.
This is an asynchronous operation. Create an otc_subscriber_rtc_stats_report_cb struct and pass it into the otc_subscriber_set_rtc_stats_report_cb function prior to calling otc_subscriber_get_rtc_stats_report(). When the stats are available, the otc_subscriber_rtc_stats_report_cb.on_rtc_stats_report callback function is called.
This callback function includes a json_array_of_reports parameter. This is a JSON array of RTC stats reports, which are similar to the format the RtcStatsReport object implemented in web browsers (see these Mozilla docs).
Also see this W3C documentation.