Skip to content
Snippets Groups Projects
tty-player.js 55.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
    		// We could render the first frame if we wanted to. Should we?
    		//self.render();
    
    		// > 6. Set the readyState attribute to HAVE_METADATA.
    		// >
    		// >    Note: A loadedmetadata DOM event will be fired as part of
    		// >    setting the readyState attribute to a new value.
    		self.readyState = HAVE_METADATA;
    		self.fireSimpleEvent("loadedmetadata");
    
    		// > 7. Let jumped be false.
    		var jumped = false;
    
    		// > 8. If the media element's default playback start position is
    		// >    greater than zero, then seek to that time, and let jumped
    		// >    be true.
    		if (self.defaultPlaybackStartPosition > 0) {
    			self.currentTime = self.defaultPlaybackStartPosition;
    			jumped = true;
    		}
    
    		// > 9. Let the media element's default playback start position be
    		// >    zero.
    		self.defaultPlaybackStartPosition = 0;
    
    		// > 10. If either the media resource or the address of the current
    		// >     media resource indicate a particular start time, then set
    		// >     the initial playback position to that time and, if jumped
    		// >     is still false, seek to that time and let jumped be true.
    		// >
    		// >     For example, with media formats that support the Media
    		// >     Fragments URI fragment identifier syntax, the fragment
    		// >     identifier can be used to indicate a start position.
    		// >     [MEDIAFRAG]
    		// TODO: support Media Fragments, e.g. ?t=a,b#t=c will trim the
    		// range to [a, b) seconds, starting at c seconds into that range,
    		// ?t=,b is [0, b); ?t=a is [a, end), it uses NPT.
    
    		// > 11. If either the media resource or the address of the current
    		// >     media resource indicate a particular set of audio or video
    		// >     tracks to enable, or if the user agent has information
    		// >     that would enable it to select specific tracks to improve
    		// >     the user's experience, then the relevant audio tracks must
    		// >     be enabled in the element's audioTracks object, and, of
    		// >     the relevant video tracks, the one that is listed first in
    		// >     the element's videoTracks object must be selected. All
    		// >     other tracks must be disabled.
    		// >
    		// >     This could again be triggered by Media Fragments URI
    		// >     fragment identifier syntax, but it could also be triggered
    		// >     e.g. by the user agent selecting a 5.1 surround sound
    		// >     audio track over a stereo audio track. [MEDIAFRAG]
    		// [Not applicable.]
    
    		// > 12. If the media element has a current media controller, then:
    		// >     if jumped is true and the initial playback position,
    		// >     relative to the current media controller's timeline, is
    		// >     greater than the current media controller's media
    		// >     controller position, then seek the media controller to the
    		// >     media element's initial playback position, relative to the
    		// >     current media controller's timeline; otherwise, seek the
    		// >     media element to the media controller position, relative
    		// >     to the media element's timeline.
    		// [Not applicable.]
    
    		// >    Once the readyState attribute reaches HAVE_CURRENT_DATA, after the loadeddata event has been fired, set the element's delaying-the-load-event flag to false. This stops delaying the load event.
    		// >
    		// >    A user agent that is attempting to reduce network usage while still fetching the metadata for each media resource would also stop buffering at this point, following the rules described previously, which involve the networkState attribute switching to the NETWORK_IDLE value and a suspend event firing.
    		// >
    		// >    The user agent is required to determine the duration of the media resource and go through this step before playing.
    
    		// > Once the entire media resource has been fetched (but
    		// > potentially before any of it has been decoded)
    		// >
    		// >    Fire a simple event named progress at the media element.
    		self.fireSimpleEvent("progress");
    
    		// >    Set the networkState to NETWORK_IDLE and fire a simple event named suspend at the media element.
    		self.networkState = NETWORK_IDLE;
    		self.fireSimpleEvent("suspend");
    
    		// >    If the user agent ever discards any media data and then needs to resume the network activity to obtain it again, then it must queue a task to set the networkState to NETWORK_LOADING.
    		// [This won’t happen.]
    
    		// >    If the user agent can keep the media resource loaded, then the algorithm will continue to its final step below, which aborts the algorithm.
    
    		// The description of when this is supposed to happen is
    		// surprisingly unclear. Hopefully this will do.
    		self.readyState = HAVE_ENOUGH_DATA;
    		self.fireSimpleEvent("loadeddata");
    		self.fireSimpleEvent("canplay");
    		self.fireSimpleEvent("canplaythrough");
    
    		finishResourceFetchAlgorithm();
    	}
    
    	function handleResponse(xhr, useResponseText) {
    		if (xhr.status === 200) {
    			var data;
    			try {
    				/** @type {Array<number>|ArrayBuffer} */
    				var response;
    				if (useResponseText) {
    					var string = xhr.responseText;
    					response = [];
    					for (var i = 0; i < string.length; i++) {
    						response.push(string.charCodeAt(i) & 0xff);
    					}
    				} else {
    					response = xhr.response;
    				}
    
    				data = parseTTYRec(response);
    				// TODO: add a bit of validation/sanity checking?
    			} catch (e) {
    				// window.console && console.warn && console.warn("parseTTYRec failed: ", e);
    				// > If the media data can be fetched but is found by
    				// > inspection to be in an unsupported format, or can
    				// > otherwise not be rendered at all
    				// > [Give up and go back to resource selection, which
    				// > we don’t need to return to due to our design.]
    				finishResourceFetchAlgorithm();
    				self.resourceSelectionAlgorithmFailedWithAttribute();
    				return;
    			}
    
    			// TODO: implement something like this:
    			//
    			// > If the media resource is found to have a video track
    			// >
    			// > 1. Create a VideoTrack object to represent the
    			// >    video track.
    			// >
    			// > 2. Update the media element's videoTracks
    			// >    attribute's VideoTrackList object with the new
    			// >    VideoTrack object.
    			// >
    			// > 3. Fire a trusted event with the name addtrack,
    			// >    that does not bubble and is not cancelable, and
    			// >    that uses the TrackEvent interface, with the
    			// >    track attribute initialized to the new
    			// >    VideoTrack object, at this VideoTrackList
    			// >    object.
    			continueResourceFetchAlgorithm(data);
    		} else {
    			// > If the media data cannot be fetched at all, due to
    			// > network errors, causing the user agent to give up
    			// > trying to fetch the resource
    			// > [Give up and go back to resource selection, which
    			// > we don’t need to return to due to our design.]
    			finishResourceFetchAlgorithm();
    			self.resourceSelectionAlgorithmFailedWithAttribute();
    			return;
    		}
    	}
    
    	// > 1. Let the current media resource be the resource given by the
    	// >    absolute URL passed to this algorithm. This is now the
    	// >    element's media resource.
    	// current media resource = self.currentSrc
    
    	// > 2. Remove all media-resource-specific text tracks from the
    	// >    media element's list of pending text tracks, if any.
    	// [Nothing to do.]
    
    	// > 3. Optionally, run the following substeps. This is the expected
    	// >    behavior if the user agent intends to not attempt to fetch
    	// >    the resource until the user requests it explicitly (e.g. as
    	// >    a way to implement the preload attribute's none keyword).
    	// [Substeps omitted as I don’t wish to implement no-preload.]
    
    	// > 4. Perform a potentially CORS-enabled fetch of the current
    	// >    media resource's absolute URL, with the mode being the
    	// >    state of the media element's crossorigin content attribute,
    	// >    the origin being the origin of the media element's
    	// >    Document, and the default origin behaviour set to taint.
    	// >
    	//
    	// [Vast swathes of text follow, mostly irrelevant as we load the
    	// entire resource at once; we don’t need to bother about the
    	// "stalled" and "suspend" events, and won’t bother for now about
    	// "progress" every 350±200ms/every byte (whichever is least
    	// frequent)]
    	//
    	// INCORRECTNESS: the window’s origin is used instead of the media
    	// element’s document’s. Security prevents doing this right.
    	// Dunno about the taint bit.
    
    	// Past here we go laissez-faire, mostly ignoring the specs.
    
    	var useResponseText;
    
    	// Some browsers (Firefox, Chrome) allow you to use data URIs with
    	// an XMLHttpRequest, others (IE) don’t.
    	// To be sure, let’s parse it ourself. (TODO: assess perf.)
    	var data = parseDataURI(self.currentSrc);
    	if (data !== null) {
    		handleResponse({status: 200, responseText: data[1]}, true);
    	} else {
    		var xhr = new XMLHttpRequest();
    		if (self.ttyPlayer["crossOrigin"] === "use-credentials") {
    			xhr.withCredentials = true;
    		} else if (self.ttyPlayer["crossOrigin"] === "anonymous" && "mozAnon" in xhr) {
    			// INCORRECTNESS: no anonymous support outside Firefox.
    			// (No one has implemented AnonXMLHttpRequest ☹.)
    			xhr.mozAnon = true;
    		}
    		xhr.onabort = finishResourceFetchAlgorithm;
    		xhr.open("GET", self.currentSrc);
    		useResponseText = !("responseType" in xhr);
    		xhr.responseType = "arraybuffer";
    		if (xhr.responseType !== "arraybuffer") {
    			useResponseText = true;
    		}
    		if (useResponseText) {
    			xhr.overrideMimeType("text/plain; charset=x-user-defined");
    			xhr.onreadystatechange = function() {
    				if (xhr.readyState === 4) {
    					handleResponse(xhr, true);
    				}
    			};
    		} else {
    			xhr.onload = xhr.onerror = function() { handleResponse(xhr, false); };
    		}
    		self.resourceFetchXHR = xhr;
    		try {
    			xhr.send();
    		} catch (e) {
    			// e.g. relative URL on file: in some browsers.
    			handleResponse(xhr, undefined);
    		}
    	}
    };
    
    var TTYPlayerPrototype = Object.create(HTMLElement.prototype);
    
    TTYPlayerPrototype["createdCallback"] = function() {
    	this["_"] = new TTYPlayerInternalState(this);
    	this["_"].setUp();
    
    	// TODO: put no-preload in load(), as defined, rather than here.
    	// As it stands, changing src will preload even though it need not.
    	if (this["preload"] !== "none") {
    		this["load"]();
    	}
    
    	if (this["autoplay"]) {
    		this["play"]();
    	}
    };
    
    TTYPlayerPrototype["attributeChangedCallback"] = function(name) {
    	if (name === "src" && this.hasAttribute("src")) {
    		this["pause"]();
    		// > If a src attribute of a media element is set or changed, the user
    		// > agent must invoke the media element's media element load
    		// > algorithm. (Removing the src attribute does not do this, even if
    		// > there are source elements present.)
    		this["load"]();
    	} else if (name === "controls" && this.hasAttribute("controls")) {
    		// While the controls are display: none, the position of
    		// this element is garbage, so we need to fix it now.
    		this["_"].controlsShownOrHidden();
    	} else if (name === "poster") {
    		// Update the poster if necessary.
    		//
    		// > If the specified resource is to be used, then, when the element is
    		// > created or when the poster attribute is set, changed, or removed,
    		// > the user agent must run the following steps to determine the
    		// > element's poster frame (regardless of the value of the element's
    		// > show poster flag):
    		//
    		// Due to the poster=npt:… possibility and how we could otherwise palm
    		// it off to the browser, we *do* actually regard the show poster flag
    		// in deciding whether to “run these steps”.
    		this["_"].showPoster = this["_"].showPoster;
    	}
    };
    
    TTYPlayerPrototype["attachedCallback"] = function() {
    	this["_"].controlsShownOrHidden();
    };
    
    Object.defineProperties(TTYPlayerPrototype, {
    	/** @lends {TTYPlayerPrototype} */
    
    	/// @idl readonly attribute MediaError? error;
    	"error": {
    		get: function() {
    			return this["_"].error;
    		}
    	},
    
    	/// @idl attribute DOMString src;
    	"src": {
    		get: function() {
    			// It needs to be an absolute URL, and we’re not doing <source> tags,
    			// so src and currentSrc will actually always be the same.
    			return this["_"].currentSrc;
    		},
    		set: function(value) {
    			this.setAttribute("src", value);
    		}
    	},
    
    	/// @idl readonly attribute DOMString currentSrc;
    	"currentSrc": {
    		get: function() {
    			return this["_"].currentSrc;
    		}
    	},
    
    	/// @idl attribute DOMString crossOrigin;
    	"crossOrigin": {
    		get: function() {
    			var value = this.getAttribute("crossorigin");
    			if (value === null || value === "anonymous" || value === "use-credentials") {
    				return value;
    			} else {
    				return "anonymous";
    			}
    		},
    		set: function(value) {
    			if (value === null) {
    				this.removeAttribute("crossorigin");
    			} else if (value === "use-credentials") {
    				this.setAttribute("crossorigin", value);
    			} else {
    				// "" == invalid value == "anonymous"
    				this.setAttribute("crossorigin", "anonymous");
    			}
    		}
    	},
    
    	/// @idl const unsigned short NETWORK_EMPTY = 0;
    	"NETWORK_EMPTY": { value: 0 },
    
    	/// @idl const unsigned short NETWORK_IDLE = 1;
    	"NETWORK_IDLE": { value: 1 },
    
    	/// @idl const unsigned short NETWORK_LOADING = 2;
    	"NETWORK_LOADING": { value: 2 },
    
    	/// @idl const unsigned short NETWORK_NO_SOURCE = 3;
    	"NETWORK_NO_SOURCE": { value: 3 },
    
    	/// @idl readonly attribute unsigned short networkState;
    	"networkState": {
    		get: function() {
    			return this["_"].networkState;
    		}
    	},
    
    	/// @idl attribute DOMString preload;
    	"preload": {
    		get: function() {
    			var value = this.getAttribute("preload");
    			if (value === "none" || value === "metadata" || value === "auto") {
    				return value;
    			} else {
    				// "" == auto, nothing is said about malformed values, and the missing value default is user-agent defined and we have no use for Metadata.
    				return "auto";
    			}
    		},
    		set: function(value) {
    			if (value === "none" || value === "metadata" || value === "auto") {
    				this.setAttribute("metadata", value);
    			} else {
    				this.removeAttribute("metadata");
    			}
    		}
    	},
    
    	/// @idl readonly attribute TimeRanges buffered;
    	"buffered": {
    		get: function() {
    			return this["seekable"];
    		}
    	},
    
    	// Although they sit here in the IDL, load() and canPlayType() are defined
    	// later because they’re methods, not properties.
    
    	/// @idl const unsigned short HAVE_NOTHING = 0;
    	"HAVE_NOTHING": { value: 0 },
    
    	/// @idl const unsigned short HAVE_METADATA = 1;
    	"HAVE_METADATA": { value: 1 },
    
    	/// @idl const unsigned short HAVE_CURRENT_DATA = 2;
    	"HAVE_CURRENT_DATA": { value: 2 },
    
    	/// @idl const unsigned short HAVE_FUTURE_DATA = 3;
    	"HAVE_FUTURE_DATA": { value: 3 },
    
    	/// @idl const unsigned short HAVE_ENOUGH_DATA = 4;
    	"HAVE_ENOUGH_DATA": { value: 4 },
    
    	/// @idl readonly attribute unsigned short readyState;
    	"readyState": {
    		get: function() {
    			return this["_"].readyState;
    		}
    	},
    
    	/// @idl readonly attribute boolean seeking;
    	// I’m sloppily ignoring the whole seeking thing. Meh, it’s fast, hopefully no one cares about the seeking and seeked events? (TODO evaluate further.)
    	"seeking": {
    		value: false
    	},
    
    	/// @idl attribute double currentTime;
    	"currentTime": {
    		get: function() {
    			return this["_"].currentTime;
    		},
    		set: function(newTime) {
    			if (!this["_"].data) {
    				throw invalidStateError();
    			}
    			this["_"].currentTime = Math.max(0, Math.min(newTime, this["duration"]));
    			this["_"].render();
    		}
    	},
    
    	/// @idl readonly attribute unrestricted double duration;
    	"duration": {
    		get: function() {
    			return this["_"].duration;
    		}
    	},
    
    	// @idl Date getStartDate();
    	// Firefox and Chrome don’t implement this on HTMLMediaElement, so I’m skipping it for now.
    
    	/// @idl readonly attribute boolean paused
    	"paused": {
    		get: function() {
    			return this["_"].paused;
    		}
    	},
    
    	/// @idl attribute double defaultPlaybackRate;
    	"defaultPlaybackRate": {
    		get: function() {
    			return this["_"].defaultPlaybackRate;
    		},
    		set: function(rate) {
    			rate = +rate;
    			var notify = this["_"].defaultPlaybackRate !== rate;
    			this["_"].defaultPlaybackRate = rate;
    			if (notify) {
    				this["_"].fireSimpleEvent("ratechange");
    			}
    		}
    	},
    
    	/// @idl attribute double playbackRate;
    	"playbackRate": {
    		get: function() {
    			return this["_"].playbackRate;
    		},
    		set: function(rate) {
    			rate = +rate;
    			var notify = this["_"].playbackRate !== rate;
    			this["_"].playbackRate = rate;
    			if (notify) {
    				this["_"].fireSimpleEvent("ratechange");
    			}
    		}
    	},
    
    	/// @idl readonly attribute TimeRanges played;
    	/// @stub Tracking which ranges have been played would take effort!
    	"played": {
    		value: EMPTY_TIME_RANGES
    	},
    
    	/// @idl readonly attribute TimeRanges seekable;
    	"seekable": {
    		get: function() {
    			if (this["readyState"] === HAVE_ENOUGH_DATA) {
    				return new My.TimeRanges([0, this["duration"]]);
    			} else {
    				return EMPTY_TIME_RANGES;
    			}
    		}
    	},
    
    	/// @idl readonly attribute boolean ended;
    	"ended": {
    		get: function() {
    			// XXX: I’m guessing these semantics, haven’t checked them.
    			return this["paused"] && this["currentTime"] === (this["playbackRate"] < 0 ? 0 : this["duration"]);
    		}
    	},
    
    	/// @idl attribute boolean autoplay;
    	"autoplay": attributeBooleanProperty("autoplay"),
    
    	/// @idl attribute boolean loop;
    	"loop": attributeBooleanProperty("loop"),
    
    	// play() and pause() are simple properties and so appear later
    
    	// @idl attribute DOMString mediaGroup;
    	// Firefox and Chrome don’t implement this on HTMLMediaElement, so I’m skipping it for now.
    
    	// @idl attribute MediaController? controller;
    	// Firefox and Chrome don’t implement this on HTMLMediaElement, so I’m skipping it for now.
    
    	/// @idl attribute boolean controls;
    	"controls": attributeBooleanProperty("controls"),
    
    	// volume and muted are simple properties and so appear later
    
    	/// @idl attribute boolean defaultMuted;
    	/// @stub volume is irrelevant
    	"defaultMuted": attributeBooleanProperty("muted"),
    
    	// @idl readonly attribute AudioTrackList audioTracks;
    	// Firefox and Chrome don’t implement this on HTMLMediaElement, so I’m skipping it for now.
    
    	// @idl readonly attribute VideoTrackList videoTracks;
    	// Firefox and Chrome don’t implement this on HTMLMediaElement, so I’m skipping it for now.
    
    	/// @idl readonly attribute TextTrackList textTracks;
    	/// @stub text tracks aren’t implemented yet—will they be?
    	"textTracks": {
    		get: function() {
    			return document.createElement("video").textTracks;
    		}
    	},
    
    	// addTextTrack() is a simple property and so is added later.
    
    	// The remainder is things that are not part of HTMLMediaElement, as noted earlier.
    
    	/// The window title.
    	///
    	/// @idl attribute DOMString title;
    	"title": {
    		get: function() {
    			return this["_"].titleElement.textContent;
    		},
    		set: function(value) {
    			this["_"].titleElement.textContent = value;
    			this["_"].fireSimpleEvent("titlechange");
    		}
    	},
    
    	/// defaultTitle is to title as defaultRateChange is to rateChange.
    	///
    	/// @idl attribute DOMString defaultTitle;
    	"defaultTitle": {
    		get: function() {
    			return this["_"].defaultTitle;
    		},
    		set: function(value) {
    			this["_"].defaultTitle = value;
    			this["_"].fireSimpleEvent("titlechange");
    		}
    	},
    
    	/// The number of columns in the terminal.
    	/// This is like HTMLVideoElement.videoWidth but not in pixels.
    	///
    	/// Read-only as it’s rarely altered alone; use this.resize(cols, this.rows) instead.
    	"cols": {
    		get: function() {
    			return this["_"].terminal.cols;
    		}
    	},
    
    	/// The number of rows in the terminal.
    	/// This is like HTMLVideoElement.videoHeight but not in pixels.
    	///
    	/// Read-only as it’s rarely altered alone; use this.resize(this.cols, rows) instead.
    	"rows": {
    		get: function() {
    			return this["_"].terminal.rows;
    		}
    	},
    
    	// Borrowed from HTMLVideoElement.
    	/// @idl attribute DOMString poster;
    	"poster": {
    		get: function() {
    			return (this.getAttribute("poster") || "").trim();
    		},
    		set: function(value) {
    			this.setAttribute("poster", value);
    		}
    	}
    });
    
    // Here are the simple properties that don’t go in the defineProperties block above.
    
    /// @idl void load();
    TTYPlayerPrototype["load"] = function() {
    	this["_"].mediaLoadAlgorithm();
    };
    
    /// @idl CanPlayTypeEnum canPlayType(DOMString type);
    /// @stub we only support one format at present, anyway.
    TTYPlayerPrototype["canPlayType"] = function() {
    	return "maybe";
    };
    
    /// @idl void play();
    TTYPlayerPrototype["play"] = function() {
    	var self = this;
    
    	function realPlay() {
    		if (self["ended"]) {
    			self["currentTime"] = self["playbackRate"] < 0 ? self["duration"] : 0;
    		}
    		self["_"].showPoster = false;
    		self["_"].paused = false;
    		var lastTime = new Date();
    		self["_"].ticker = setInterval(function() {
    			var newTime = new Date();
    			if (!self["_"].semipaused) {
    				self["currentTime"] += (newTime - lastTime) / 1000 * self["playbackRate"];
    			}
    			lastTime = newTime;
    		}, TICK);
    		self["_"].fireSimpleEvent("play");
    	}
    
    	function loaded() {
    		self.removeEventListener("loadeddata", loaded);
    		realPlay();
    	}
    
    	if (this["_"].paused) {
    		if (this["_"].data) {
    			realPlay();
    		} else {
    			this.addEventListener("loadeddata", loaded);
    			this["_"].loadIfNotLoading();
    		}
    	}
    };
    
    /// @idl void pause();
    TTYPlayerPrototype["pause"] = function() {
    	if (!this["_"].paused) {
    		this["_"].paused = true;
    		clearInterval(this["_"].ticker);
    		this["_"].fireSimpleEvent("pause");
    	}
    };
    
    /// @idl attribute double volume;
    /// @stub volume is irrelevant
    TTYPlayerPrototype["volume"] = 1;
    
    /// @idl attribute boolean muted;
    /// @stub volume is irrelevant
    TTYPlayerPrototype["muted"] = false;
    
    /// @idl TextTrack addTextTrack(TextTrackKind kind, optional DOMString label = "", optional DOMString language = "");
    /// @stub text tracks aren’t implemented yet—will they be?
    TTYPlayerPrototype["addTextTrack"] = function() {
    	return null;
    };
    // This should theoretically go on HTMLElement.prototype. Too bad.
    /// @idl attribute EventHandler ontitlechange;
    TTYPlayerPrototype["ontitlechange"] = null;
    
    TTYPlayerPrototype["resize"] = function(x, y) {
    	this["_"].terminal.resize(x, y);
    };
    
    TTYPlayerPrototype["pretendToBeAVideo"] = function() {
    	Object.defineProperties(this, {
    		/** @lends {TTYPlayerPrototype} */
    
    		// Let’s pretend (badly) that we’re an HTMLVideoElement!
    		"tagName": {value: "VIDEO"},
    		"width": {
    			get: function() {
    				return this.offsetWidth;
    			},
    			set: function(value) {
    				// TODO this is a little poor as a technique, refine it.
    				this.style.fontSize = "100%";
    				this.style.fontSize = (value / this.offsetWidth * 100) + "%";
    			}
    		},
    		"height": {
    			get: function() {
    				return this.offsetHeight;
    			},
    			set: function(value) {
    				// TODO this is a little poor as a technique, refine it.
    				this.style.fontSize = "100%";
    				this.style.fontSize = (value / this.offsetHeight * 100) + "%";
    			}
    		},
    		"videoWidth": {
    			get: function() {
    				return this["width"];
    			}
    		},
    		"videoHeight": {
    			get: function() {
    				return this["height"];
    			}
    		}
    	});
    };
    
    document.registerElement("tty-player", {prototype: TTYPlayerPrototype});
    })();