1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/media/rtc_data_channel_handler.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/strings/utf_string_conversions.h"
18 enum DataChannelCounters
{
27 void IncrementCounter(DataChannelCounters counter
) {
28 UMA_HISTOGRAM_ENUMERATION("WebRTC.DataChannelCounters",
35 RtcDataChannelHandler::RtcDataChannelHandler(
36 webrtc::DataChannelInterface
* channel
)
38 webkit_client_(NULL
) {
40 channel_
->RegisterObserver(this);
42 IncrementCounter(CHANNEL_CREATED
);
44 IncrementCounter(CHANNEL_RELIABLE
);
46 IncrementCounter(CHANNEL_ORDERED
);
48 IncrementCounter(CHANNEL_NEGOTIATED
);
50 UMA_HISTOGRAM_CUSTOM_COUNTS("WebRTC.DataChannelMaxRetransmits",
52 std::numeric_limits
<unsigned short>::max(), 50);
53 UMA_HISTOGRAM_CUSTOM_COUNTS("WebRTC.DataChannelMaxRetransmitTime",
54 maxRetransmitTime(), 0,
55 std::numeric_limits
<unsigned short>::max(), 50);
58 RtcDataChannelHandler::~RtcDataChannelHandler() {
60 channel_
->UnregisterObserver();
63 void RtcDataChannelHandler::setClient(
64 blink::WebRTCDataChannelHandlerClient
* client
) {
65 webkit_client_
= client
;
68 blink::WebString
RtcDataChannelHandler::label() {
69 return base::UTF8ToUTF16(channel_
->label());
72 bool RtcDataChannelHandler::isReliable() {
73 return channel_
->reliable();
76 bool RtcDataChannelHandler::ordered() const {
77 return channel_
->ordered();
80 unsigned short RtcDataChannelHandler::maxRetransmitTime() const {
81 return channel_
->maxRetransmitTime();
84 unsigned short RtcDataChannelHandler::maxRetransmits() const {
85 return channel_
->maxRetransmits();
88 blink::WebString
RtcDataChannelHandler::protocol() const {
89 return base::UTF8ToUTF16(channel_
->protocol());
92 bool RtcDataChannelHandler::negotiated() const {
93 return channel_
->negotiated();
96 unsigned short RtcDataChannelHandler::id() const {
97 return channel_
->id();
100 unsigned long RtcDataChannelHandler::bufferedAmount() {
101 return channel_
->buffered_amount();
104 bool RtcDataChannelHandler::sendStringData(const blink::WebString
& data
) {
105 std::string utf8_buffer
= base::UTF16ToUTF8(data
);
106 talk_base::Buffer
buffer(utf8_buffer
.c_str(), utf8_buffer
.length());
107 webrtc::DataBuffer
data_buffer(buffer
, false);
108 RecordMessageSent(data_buffer
.size());
109 return channel_
->Send(data_buffer
);
112 bool RtcDataChannelHandler::sendRawData(const char* data
, size_t length
) {
113 talk_base::Buffer
buffer(data
, length
);
114 webrtc::DataBuffer
data_buffer(buffer
, true);
115 RecordMessageSent(data_buffer
.size());
116 return channel_
->Send(data_buffer
);
119 void RtcDataChannelHandler::close() {
123 void RtcDataChannelHandler::OnStateChange() {
124 if (!webkit_client_
) {
125 LOG(ERROR
) << "WebRTCDataChannelHandlerClient not set.";
128 DVLOG(1) << "OnStateChange " << channel_
->state();
129 switch (channel_
->state()) {
130 case webrtc::DataChannelInterface::kConnecting
:
131 webkit_client_
->didChangeReadyState(
132 blink::WebRTCDataChannelHandlerClient::ReadyStateConnecting
);
134 case webrtc::DataChannelInterface::kOpen
:
135 IncrementCounter(CHANNEL_OPENED
);
136 webkit_client_
->didChangeReadyState(
137 blink::WebRTCDataChannelHandlerClient::ReadyStateOpen
);
139 case webrtc::DataChannelInterface::kClosing
:
140 webkit_client_
->didChangeReadyState(
141 blink::WebRTCDataChannelHandlerClient::ReadyStateClosing
);
143 case webrtc::DataChannelInterface::kClosed
:
144 webkit_client_
->didChangeReadyState(
145 blink::WebRTCDataChannelHandlerClient::ReadyStateClosed
);
153 void RtcDataChannelHandler::OnMessage(const webrtc::DataBuffer
& buffer
) {
154 if (!webkit_client_
) {
155 LOG(ERROR
) << "WebRTCDataChannelHandlerClient not set.";
160 webkit_client_
->didReceiveRawData(buffer
.data
.data(), buffer
.data
.length());
162 base::string16 utf16
;
163 if (!base::UTF8ToUTF16(buffer
.data
.data(), buffer
.data
.length(), &utf16
)) {
164 LOG(ERROR
) << "Failed convert received data to UTF16";
167 webkit_client_
->didReceiveStringData(utf16
);
171 void RtcDataChannelHandler::RecordMessageSent(size_t num_bytes
) {
172 // Currently, messages are capped at some fairly low limit (16 Kb?)
173 // but we may allow unlimited-size messages at some point, so making
174 // the histogram maximum quite large (100 Mb) to have some
175 // granularity at the higher end in that eventuality. The histogram
176 // buckets are exponentially growing in size, so we'll still have
177 // good granularity at the low end.
179 // This makes the last bucket in the histogram count messages from
180 // 100 Mb to infinity.
181 const int kMaxBucketSize
= 100 * 1024 * 1024;
182 const int kNumBuckets
= 50;
185 UMA_HISTOGRAM_CUSTOM_COUNTS("WebRTC.ReliableDataChannelMessageSize",
187 1, kMaxBucketSize
, kNumBuckets
);
189 UMA_HISTOGRAM_CUSTOM_COUNTS("WebRTC.UnreliableDataChannelMessageSize",
191 1, kMaxBucketSize
, kNumBuckets
);
195 } // namespace content