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"
9 #include "base/logging.h"
10 #include "base/utf_string_conversions.h"
14 RtcDataChannelHandler::RtcDataChannelHandler(
15 webrtc::DataChannelInterface
* channel
)
17 webkit_client_(NULL
) {
19 channel_
->RegisterObserver(this);
22 RtcDataChannelHandler::~RtcDataChannelHandler() {
24 channel_
->UnregisterObserver();
27 void RtcDataChannelHandler::setClient(
28 WebKit::WebRTCDataChannelHandlerClient
* client
) {
29 webkit_client_
= client
;
32 WebKit::WebString
RtcDataChannelHandler::label() {
33 return UTF8ToUTF16(channel_
->label());
36 bool RtcDataChannelHandler::isReliable() {
37 return channel_
->reliable();
40 unsigned long RtcDataChannelHandler::bufferedAmount() {
41 return channel_
->buffered_amount();
44 bool RtcDataChannelHandler::sendStringData(const WebKit::WebString
& data
) {
45 std::string utf8_buffer
= UTF16ToUTF8(data
);
46 webrtc::DataBuffer buffer
;
47 buffer
.binary
= false;
48 buffer
.data
.SetData(utf8_buffer
.c_str(), utf8_buffer
.length());
49 return channel_
->Send(buffer
);
52 bool RtcDataChannelHandler::sendRawData(const char* data
, size_t length
) {
53 webrtc::DataBuffer buffer
;
54 buffer
.data
.SetData(data
, length
);
56 return channel_
->Send(buffer
);
59 void RtcDataChannelHandler::close() {
63 void RtcDataChannelHandler::OnStateChange() {
64 if (!webkit_client_
) {
65 LOG(ERROR
) << "WebRTCDataChannelHandlerClient not set.";
68 DVLOG(1) << "OnStateChange " << channel_
->state();
69 switch (channel_
->state()) {
70 case webrtc::DataChannelInterface::kConnecting
:
71 webkit_client_
->didChangeReadyState(
72 WebKit::WebRTCDataChannelHandlerClient::ReadyStateConnecting
);
74 case webrtc::DataChannelInterface::kOpen
:
75 webkit_client_
->didChangeReadyState(
76 WebKit::WebRTCDataChannelHandlerClient::ReadyStateOpen
);
78 case webrtc::DataChannelInterface::kClosing
:
79 webkit_client_
->didChangeReadyState(
80 WebKit::WebRTCDataChannelHandlerClient::ReadyStateClosing
);
82 case webrtc::DataChannelInterface::kClosed
:
83 webkit_client_
->didChangeReadyState(
84 WebKit::WebRTCDataChannelHandlerClient::ReadyStateClosed
);
92 void RtcDataChannelHandler::OnMessage(const webrtc::DataBuffer
& buffer
) {
93 if (!webkit_client_
) {
94 LOG(ERROR
) << "WebRTCDataChannelHandlerClient not set.";
99 webkit_client_
->didReceiveRawData(buffer
.data
.data(), buffer
.data
.length());
102 if (!UTF8ToUTF16(buffer
.data
.data(), buffer
.data
.length(), &utf16
)) {
103 LOG(ERROR
) << "Failed convert received data to UTF16";
106 webkit_client_
->didReceiveStringData(utf16
);
110 } // namespace content