Browse Source

Send initial state to newly connecting peers.

pull/83/head
Joachim Bauch 12 years ago
parent
commit
98d60e4b5b
  1. 72
      static/js/directives/youtubevideo.js

72
static/js/directives/youtubevideo.js

@ -36,8 +36,10 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
var isPublisher = null; var isPublisher = null;
var isPaused = null; var isPaused = null;
var seekDetector = null; var seekDetector = null;
var playReceivedNow = null;
var prevTime = null; var prevTime = null;
var prevNow = null; var prevNow = null;
var initialState = null;
var stateEvents = { var stateEvents = {
"-1": "youtube.unstarted", "-1": "youtube.unstarted",
@ -58,6 +60,7 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
$scope.playbackActive = false; $scope.playbackActive = false;
$scope.hideControlsBar = true; $scope.hideControlsBar = true;
$scope.currentVideoUrl = null; $scope.currentVideoUrl = null;
$scope.currentVideoId = null;
$scope.youtubeurl = "http://www.youtube.com/watch?v=_C92v6uKCIU"; $scope.youtubeurl = "http://www.youtube.com/watch?v=_C92v6uKCIU";
var onPlayerReady = function(event) { var onPlayerReady = function(event) {
@ -151,6 +154,12 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
}; };
$scope.$on("youtube.playing", function() { $scope.$on("youtube.playing", function() {
if (initialState === 2) {
initialState = null;
player.pauseVideo();
return;
}
prevTime = null; prevTime = null;
startDetectSeek(); startDetectSeek();
if (isPaused) { if (isPaused) {
@ -158,13 +167,20 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
mediaStream.webrtc.callForEachCall(function(peercall) { mediaStream.webrtc.callForEachCall(function(peercall) {
mediaStreamSendYouTubeVideo(peercall, currentToken, { mediaStreamSendYouTubeVideo(peercall, currentToken, {
Type: "Resume", Type: "Resume",
Resume: true Resume: {
position: player.getCurrentTime()
}
}); });
}); });
} }
}); });
$scope.$on("youtube.buffering", function() { $scope.$on("youtube.buffering", function() {
if (initialState === 2) {
initialState = null;
player.pauseVideo();
}
startDetectSeek(); startDetectSeek();
}); });
@ -179,7 +195,9 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
mediaStream.webrtc.callForEachCall(function(peercall) { mediaStream.webrtc.callForEachCall(function(peercall) {
mediaStreamSendYouTubeVideo(peercall, currentToken, { mediaStreamSendYouTubeVideo(peercall, currentToken, {
Type: "Pause", Type: "Pause",
Pause: true Pause: {
position: player.getCurrentTime()
}
}); });
}); });
} }
@ -198,20 +216,34 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
mediaStreamSendYouTubeVideo(peercall, currentToken, { mediaStreamSendYouTubeVideo(peercall, currentToken, {
Type: "Seek", Type: "Seek",
Seek: { Seek: {
"position": position position: position
} }
}); });
}); });
}); });
var playVideo = function(id) { var playVideo = function(id, position, state) {
playerReady.done(function() { playerReady.done(function() {
$("#youtubeplayer").show(); $("#youtubeplayer").show();
$scope.playbackActive = true; $scope.playbackActive = true;
prevTime = null; prevTime = null;
prevNow = null; prevNow = null;
isPaused = null; isPaused = null;
if (playReceivedNow) {
var delta = ((new Date()) - playReceivedNow) * 0.001;
playReceivedNow = null;
if (position) {
position += delta;
} else {
position = delta;
}
}
initialState = state;
if (position) {
player.loadVideoById(id, position);
} else {
player.loadVideoById(id); player.loadVideoById(id);
}
}); });
}; };
@ -273,6 +305,7 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
createVideoPlayer(true); createVideoPlayer(true);
$scope.youtubeurl = ""; $scope.youtubeurl = "";
$scope.currentVideoUrl = url; $scope.currentVideoUrl = url;
$scope.currentVideoId = id;
playVideo(id); playVideo(id);
}; };
@ -300,10 +333,16 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
case "Play": case "Play":
console.log("Received YouTubeVideo play request", data); console.log("Received YouTubeVideo play request", data);
playReceivedNow = new Date();
$scope.$apply(function(scope) { $scope.$apply(function(scope) {
createVideoPlayer(false); createVideoPlayer(false);
playerReady.done(function() {
safeApply(scope, function(scope) {
scope.currentVideoUrl = data.Play.url; scope.currentVideoUrl = data.Play.url;
playVideo(data.Play.id); scope.currentVideoId = data.Play.id;
playVideo(data.Play.id, data.Play.position, data.Play.state);
});
});
}); });
break; break;
@ -312,6 +351,9 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
$scope.$apply(function(scope) { $scope.$apply(function(scope) {
if (player) { if (player) {
player.pauseVideo(); player.pauseVideo();
if (data.Pause.position) {
player.seekTo(data.Pause.position, true);
}
} }
}); });
break; break;
@ -320,6 +362,9 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
console.log("Received YouTubeVideo resume request", data); console.log("Received YouTubeVideo resume request", data);
$scope.$apply(function(scope) { $scope.$apply(function(scope) {
if (player) { if (player) {
if (data.Resume.position) {
player.seekTo(data.Resume.position, true);
}
player.playVideo(); player.playVideo();
} }
}); });
@ -370,6 +415,20 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
Type: "Show", Type: "Show",
Show: true Show: true
}); });
if (isPublisher && $scope.currentVideoUrl) {
var playInfo = {
url: $scope.currentVideoUrl,
id: $scope.currentVideoId
};
if (player) {
playInfo.position = player.getCurrentTime();
playInfo.state = player.getPlayerState();
}
mediaStreamSendYouTubeVideo(peercall, token, {
Type: "Play",
Play: playInfo
});
}
}; };
// Updater function to bring in new calls. // Updater function to bring in new calls.
@ -439,9 +498,12 @@ define(['jquery', 'underscore', 'text!partials/youtubevideo.html', 'bigscreen'],
} }
isPublisher = null; isPublisher = null;
$scope.playbackActive = false; $scope.playbackActive = false;
$scope.currentVideoUrl = null;
$scope.currentVideoId = null;
peers = {}; peers = {};
stopDetectSeek(); stopDetectSeek();
playerReady = null; playerReady = null;
initialState = null;
mediaStream.webrtc.e.off("statechange", updater); mediaStream.webrtc.e.off("statechange", updater);
}; };

Loading…
Cancel
Save