Getting statistics about a publisher's stream
The OTPublisher audioNetworkStats and audioNetworkStats events provide an array of objects defining the current audio or video statistics for the publisher. For a publisher in a routed session (one that uses the Media Router), these event arrays each include one object, defining the statistics for the audio or video that is sent to the Media Router. In a relayed session, the array includes an object for each subscriber to the published stream. Each object in the array has the following properties:
videoBytesSent — The total number of audio and video bytes sent videoPacketsSent — The total number of audio and video packets sent videoPacketsSent — The total number of audio and video packets lost Additionally, for a publisher in a relayed session, each object in the array contains the following two properties:
connectionId — The unique ID of the subscribing client's connection, which matches the connectionId property of the connectionCreated event for that client's connection.
subscriberId — The unique ID of the subscriber.
The following code logs these stats for the publisher's stream every second:
To get more detailed stream statics, use the OTPublisher.getRtcStatsReport() method. Calling this method results in the OTPublisher instance dispatching an rtcStatsReport event:
For a publisher in a routed session, event array includes one object, defining the stats of the stream sent to the OpenTok Media Router. In a relayed session, the array includes an object defining the RTC Stats Reports for each subscriber to the published stream. Each object in the array has a jsonArrayOfReports property that includes the data. The structure of the JSON array is similar to the format of the RtcStatsReport object implemented in web browsers (see the Mozilla docs). Also see this W3C documentation.
To register callbacks methods for periodic reports of audio and video statistics for a publisher, call the PublisherKit.setAudioStatsListener(listener) and PublisherKit.setVideoStatsListener(listener) methods.
Pass in objects that implement the PublisherKit.AudioStatsListener and PublisherKit.VideoStatsListener interfaces.
The implementations of the PublisherKit.AudioStatsListener.onAudioStats(publisher, stats) and PublisherKit.VideoStatsListener.onVideoStats(publisher, stats) methods are called periodically to report audio and video statistics for the publisher. Each method is passed in two objects: the publisher and an array of stats objects.
For a publisher in a routed session (one that uses the Vonage Video Media Router), the stats array includes one object, defining the statistics for the single audio or video media stream that is sent to the Vonage Video Media Router. In a relayed session, the stats array includes an object for each subscriber to the published stream.
The stats object includes the following properties:
- The total number of audio or video packets sent
- The total number of audio or video packets lost
- The total number of audio or video bytes sent
- The timestamp for when the stats were gathered
Additionally, for a publisher in a relayed session, each object in the array contains the following two properties:
connectionId— The connection ID of the client subscribing to the streamsubscriberId— The subscriber ID of the client subscribing to the stream
These two properties are undefined for a publisher in a routed session.
To get more detailed stream statics, use the PublisherKit.getRtcStatsReport() method. This provides RTC stats reports for the media stream. This is an asynchronous operation. Call the PublisherKit.setRtcStatsReportListener(PublisherKit.PublisherRtcStatsReportListener listener) method, and then implement the PublisherKit.PublisherRtcStatsReportListener.onRtcStatsReport(PublisherKit publisher, PublisherKit.PublisherRtcStats[] stats) method prior to calling PublisherKit.getRtcStatsReport().
When the stats are available, the implementation of the PublisherKit.PublisherRtcStatsReportListener.onRtcStatsReport(PublisherKit publisher, PublisherKit.PublisherRtcStats[] stats) method is called.
An array of PublisherRtcStats objects is passed into that method. The PublisherRtcStats object includes a jsonArrayOfReports property. This 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.
Set the networkStatsDelegate property of an OTPublisherKit to an object implements the OTPublisherKitNetworkStatsDelegate protocol.
This protocol includes an [OTPublisherKitNetworkStatsDelegate publisher: audioNetworkStatsUpdated:] message and an [OTPublisherKitNetworkStatsDelegate publisher: videoNetworkStatsUpdated:] message, which are periodically sent to report publisher audio and video quality statistics.
The second parameter of each of these — audioNetworkStatsUpdated and videoNetworkStatsUpdated — is an array of stats objects that include properties defining the network statistics (audio and video).
For a publisher in a routed session (one that uses the Vonage Video Media Router), the stats array includes one object, defining the statistics for the single audio or video media stream that is sent to the Vonage Video Media Router. In a relayed session, the stats array includes an object for each subscriber to the published stream. The object includes the following properties:
- The total number of audio or video packets sent
- The total number of audio or video packets lost
- The total number of audio or video bytes sent
- The timestamp for when the stats were gathered
- The timestamp from which the cumulative totals started accumulating
Additionally, for a publisher in a relayed session, each stats object in the array contains the following two properties:
connectionId— The connection ID of the client subscribing to the streamsubscriberId— The subscriber ID of the client subscribing to the stream
These two properties are undefined for a publisher in a routed session.
To get more detailed stream statics, use the [OTPublisherKit getRtcStatsReport:] method. This provides RTC stats reports for the media stream.
This is an asynchronous operation. Set the >[OTPublisherKit rtcStatsReportDelegate]> property and implement the >[OTPublisherKitRtcStatsReportDelegate publisher:rtcStatsReport:]> method prior to calling [OTPublisherKit getRtcStatsReport:].
When the stats are available, the implementation of the >[OTPublisherKitRtcStatsReportDelegate publisher:rtcStatsReport:]> message is sent. The message includes an array of OTPublisherRtcStats objects, which 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.
To register callbacks methods for periodic reports of audio and video statistics for a publisher, set an event handler for the Publisher AudioStatsUpdated and VideoUpdated events.
These events are sent periodically to report audio and video statistics for the publisher. The event handlers for these are passed in an array of AudioNetworkStats and VideoNetworkStats Each method is passed in two objects: the publisher and an array of stats objects. For a publisher in a routed session (one that uses the Vonage Video Media Router), the array includes one object, defining the statistics for the single audio or video media stream that is sent to the Vonage Video Media Router. In a relayed session, the array includes an object for each subscriber to the published stream. The stats object includes the following properties:
- The total number of audio or video packets sent
- The total number of audio or video packets lost
- The total number of audio or video bytes sent
- The timestamp for when the stats were gathered
Additionally, for a publisher in a relayed session, each object in the array contains the following two properties:
ConnectionId— The connection ID of the client subscribing to the streamSubscriberId— The subscriber ID of the client subscribing to the stream
These two properties are undefined for a publisher in a routed session.
To get more detailed stream statics, use the Publisher.GetRtcStatsReport() method. This provides RTC stats reports for the media stream. This is an asynchronous operation. When the stats are available, the RtcStatsReport event is sent. The RtcStatsReportArgs object includes an array of PublisherRtcStats objects, which 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.
To register callbacks methods for periodic reports of audio and video statistics for a publisher, set the on_audio_stats() and on_video_stats() callback functions when you initialize the otc_publisher_callbacks struct to be used by the publisher.
These callback functions are called periodically to report audio and video statistics for the publisher. Each function is passed in the following: A pointer to the publisher struct, A pointer to the user_data you set for the publisher, an array of stats, and the number of stats in the array. The stats parameter is defined by the otc_publisher_audio_stats and otc_publisher_video_stats structs. For a publisher in a routed session (one that uses the Media Router), the array includes one object, defining the statistics for the single audio or video media stream that is sent to the Media Router. In a relayed session, the array includes an object for each subscriber to the published stream. The struct passed in as the stats parameter includes the following properties:
- The total number of audio or video packets sent
- The total number of audio or video packets lost
- The total number of audio or video bytes sent
- The timestamp for when the stats were gathered
- The audio level (for audio stats)
Additionally, for a publisher in a relayed session, each object in the array contains the following two properties:
ConnectionId— The connection ID of the client subscribing to the streamSubscriberId— The subscriber ID of the client subscribing to the stream
These two properties are undefined for a publisher in a routed session.
To get more detailed stream statics, use the otc_publisher_get_rtc_stats_report() function. This provides RTC stats reports for the media stream. This is an asynchronous operation. Create an otc_publisher_rtc_stats_report_cb struct and pass it into the otc_publisher_set_rtc_stats_report_cb function prior to calling otc_publisher_get_rtc_stats_report(). When the stats are available, the otc_publisher_rtc_stats_report_cb.on_rtc_stats_report() callback function is called. This function includes a stats parameter, which is a pointer to an array of otc_publisher_rtc_stats structs. This struct includes a json_array_of_reports 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.