Integrate WebRTC in chat - refs #7558
parent
7fb57d4245
commit
1c9bfc4eb9
@ -0,0 +1,101 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
/** |
||||||
|
* VideoChat class |
||||||
|
* |
||||||
|
* This class provides methods for video chat management. |
||||||
|
* |
||||||
|
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com> |
||||||
|
*/ |
||||||
|
class VideoChat |
||||||
|
{ |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the video chat info by its users |
||||||
|
* @param int $user1 User id |
||||||
|
* @param int $user2 Other user id |
||||||
|
* @return array The video chat info. Otherwise return false |
||||||
|
*/ |
||||||
|
public static function getChatRoomByUsers($user1, $user2) |
||||||
|
{ |
||||||
|
$user1 = intval($user1); |
||||||
|
$user2 = intval($user2); |
||||||
|
|
||||||
|
if (empty($user1) || empty($user2)) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
return Database::select( |
||||||
|
'*', |
||||||
|
Database::get_main_table(TABLE_MAIN_VIDEO_CHAT), |
||||||
|
[ |
||||||
|
'where' => [ |
||||||
|
'(from_user = ? AND to_user = ?)' => [$user1, $user2], |
||||||
|
'OR (from_user = ? AND to_user = ?)' => [$user2, $user1] |
||||||
|
] |
||||||
|
], |
||||||
|
'first' |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Create a video chat |
||||||
|
* @param string $name The video chat name |
||||||
|
* @param int $fromUser The sender user |
||||||
|
* @param int $toUser The receiver user |
||||||
|
* @return int The created video chat id. Otherwise return false |
||||||
|
*/ |
||||||
|
public static function createRoom($name, $fromUser, $toUser) |
||||||
|
{ |
||||||
|
return Database::insert( |
||||||
|
Database::get_main_table(TABLE_MAIN_VIDEO_CHAT), |
||||||
|
[ |
||||||
|
'from_user' => intval($fromUser), |
||||||
|
'to_user' => intval($toUser), |
||||||
|
'room_name' => $name, |
||||||
|
'datetime' => api_get_utc_datetime() |
||||||
|
] |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Check if the video chat exists by its room name |
||||||
|
* @param string $name The video chat name |
||||||
|
* @return boolean |
||||||
|
*/ |
||||||
|
public static function nameExists($name) |
||||||
|
{ |
||||||
|
$resultData = Database::select( |
||||||
|
'COUNT(1) AS count', |
||||||
|
Database::get_main_table(TABLE_MAIN_VIDEO_CHAT), |
||||||
|
[ |
||||||
|
'where' => ['room_name = ?' => $name] |
||||||
|
], |
||||||
|
'first' |
||||||
|
); |
||||||
|
|
||||||
|
if ($resultData !== false) { |
||||||
|
return $resultData['count'] > 0; |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Get the video chat info by its room name |
||||||
|
* @param string $name The video chat name |
||||||
|
* @return array The video chat info. Otherwise return false |
||||||
|
*/ |
||||||
|
public static function getChatRoomByName($name) |
||||||
|
{ |
||||||
|
return Database::select( |
||||||
|
'*', |
||||||
|
Database::get_main_table(TABLE_MAIN_VIDEO_CHAT), |
||||||
|
[ |
||||||
|
'where' => ['room_name = ?' => $name] |
||||||
|
], |
||||||
|
'first' |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
require_once '../../../global.inc.php'; |
||||||
|
|
||||||
|
if (api_is_anonymous()) { |
||||||
|
api_not_allowed(true); |
||||||
|
} |
||||||
|
|
||||||
|
$roomName = isset($_GET['room']) ? $_GET['room'] : null; |
||||||
|
|
||||||
|
$room = VideoChat::getChatRoomByName($roomName); |
||||||
|
|
||||||
|
if ($room === false) { |
||||||
|
Header::location(api_get_path(WEB_PATH)); |
||||||
|
} |
||||||
|
|
||||||
|
$isSender = $room['from_user'] == api_get_user_id(); |
||||||
|
$isReceiver = $room['to_user'] == api_get_user_id(); |
||||||
|
|
||||||
|
if (!$isSender && !$isReceiver) { |
||||||
|
Header::location(api_get_path(WEB_PATH)); |
||||||
|
} |
||||||
|
|
||||||
|
if ($isSender) { |
||||||
|
$chatUser = api_get_user_info($room['to_user']); |
||||||
|
} elseif ($isReceiver) { |
||||||
|
$chatUser = api_get_user_info($room['from_user']); |
||||||
|
} |
||||||
|
|
||||||
|
$htmlHeadXtra[] = '<script type="text/javascript" src="' |
||||||
|
. api_get_path(WEB_PATH) . 'web/assets/simplewebrtc/latest.js' |
||||||
|
. '"></script>' . "\n"; |
||||||
|
|
||||||
|
$template = new Template(); |
||||||
|
$template->assign('room_name', $room['room_name']); |
||||||
|
$template->assign('chat_user', $chatUser); |
||||||
|
|
||||||
|
$content = $template->fetch('default/chat/video.tpl'); |
||||||
|
|
||||||
|
$template->assign('header', $room['room_name']); |
||||||
|
$template->assign('content', $content); |
||||||
|
$template->display_one_col_template(); |
||||||
@ -0,0 +1,28 @@ |
|||||||
|
<p class="lead">{{ "Chat with %s"|get_lang|format(chat_user.complete_name) }}</p> |
||||||
|
<div class="row"> |
||||||
|
<div class="col-sm-3"> |
||||||
|
<div class="thumbnail"> |
||||||
|
<video id="chat-local-video" class="skip"></video> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
<div class="col-sm-9"> |
||||||
|
<div class="thumbnail" id="chat-remote-video"></div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
|
||||||
|
<script> |
||||||
|
(function() { |
||||||
|
var webRTC = new SimpleWebRTC({ |
||||||
|
localVideoEl: 'chat-local-video', |
||||||
|
remoteVideosEl: 'chat-remote-video', |
||||||
|
autoRequestMedia: true |
||||||
|
}); |
||||||
|
|
||||||
|
webRTC.on('readyToCall', function() { |
||||||
|
webRTC.joinRoom('{{ room_name }}'); |
||||||
|
}); |
||||||
|
webRTC.on('videoAdded', function (video, peer) { |
||||||
|
$(video).addClass('skip'); |
||||||
|
}); |
||||||
|
})(); |
||||||
|
</script> |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
{{ form }} |
||||||
|
|
||||||
|
<script> |
||||||
|
$('form[name="start_video_chat"]').on('submit', function(e) { |
||||||
|
e.preventDefault(); |
||||||
|
|
||||||
|
var createChatRoom = $.post( |
||||||
|
'{{ _p.web_ajax }}chat.ajax.php', |
||||||
|
{ |
||||||
|
room_name: $(this).find('input[name="chat_room_name"]').val(), |
||||||
|
to: $(this).find('input[name="to"]').val(), |
||||||
|
action: 'create_room' |
||||||
|
} |
||||||
|
); |
||||||
|
|
||||||
|
$.when(createChatRoom).done(function(response) { |
||||||
|
$('#global-modal').find('.modal-body').html(response); |
||||||
|
}); |
||||||
|
}); |
||||||
|
</script> |
||||||
Loading…
Reference in new issue