Commit b175c76d authored by Linshizhi's avatar Linshizhi

update

parent b291d6df
......@@ -141,39 +141,46 @@ export class Channel {
return true;
}
enableBlockMode(size) {
enableBlockMode(blksize) {
let capacity = this.#size - 1;
if (capacity % size) {
if (capacity % blksize) {
return false;
}
this.#readPointerUpdate(this.#metaSize);
this.#writePointerUpdate(this.#metaSize);
this.#setRBlks(0);
this.#setWBlks(0);
this.#mode = CHANNEL_MODE.BLOCK;
this.#blockSize = size;
this.#blockSize = blksize;
this.#scheduler = new CopyStrategyInBlock(
this.#size-1, this.#metaSize, this.#blockSize);
}
enableByteMode() {
this.#readPointerUpdate(this.#metaSize);
this.#writePointerUpdate(this.#metaSize);
this.#mode = CHANNEL_MODE.BYTE;
thsi.#scheduler = new CopyStrategyInByte(
this.#size ,this.#metaSize);
}
getRBlks() {
#getRBlks() {
return this.#view.getUint8(this.#rBlkShift);
}
setRBlks(num) {
#setRBlks(num) {
this.#view.setUint8(this.#rBlkShift, num);
}
getWBlks() {
#getWBlks() {
return this.#view.getUint8(this.#wBlkShift);
}
setWBlks(num) {
#setWBlks(num) {
this.#view.setUint8(this.#wBlkShift, num);
}
......@@ -206,7 +213,13 @@ export class Channel {
dataSize() {
this.#readPointerCache = this.#getReadPointer();
this.#writePointerCache = this.#getWritePointer();
return this.#size - this.#remain() - 1;
switch (this.#mode) {
case CHANNEL_MODE.BYTE:
return this.#size - this.#remain() -1;
case CHANNEL_MODE.BLOCK:
return (this.#size - 1) - this.#remain();
}
}
#remain() {
......@@ -219,12 +232,28 @@ export class Channel {
}
#remainBlocks() {
console.log(this.#readPointerCache, this.#writePointerCache);
if (this.#readPointerCache == this.#writePointerCache) {
// Check RBLKS and WBLKS
let rBlks = this.#getRBlks(),
wBlks = this.#getWBlks();
if (rBlks == wBlks) {
/* Empty */
return this.#size-1;
} else {
/* Full */
return 0;
}
} else if (this.#readPointerCache > this.#writePointerCache) {
return this.#readPointerCache - this.#writePointerCache;
} else {
// this.#readPointerCache < this.#writePointerCache
return (this.#size - 1) - (this.#writePointerCache - this.#readPointerCache);
}
}
#remainBytes() {
this.#readPointerCache = this.#getReadPointer();
if (this.#writePointerCache == this.#readPointerCache) {
return this.#size - 1;
} else if (this.#writePointerCache > this.#readPointerCache) {
......@@ -234,6 +263,17 @@ export class Channel {
}
}
#scheduleArgs(size) {
let args = new CopyArgument();
args.rPos = this.#readPointerCache;
args.wPos = this.#writePointerCache;
args.rBlks = this.#getRBlks();
args.wBlks = this.#getWBlks();
args.size = size;
return args;
}
/* Channel use an array buffer as
* a circular buffer, so some datas
* may unable to be done in one copy.
......@@ -243,8 +283,13 @@ export class Channel {
* Caution: Before making a copy schedule you must
* make sure there has enough spaces.*/
#cpySchedule(size) {
return this.#scheduler.schedule(
this.#readPointerCache, this.#writePointerCache , size);
let args = this.#scheduleArgs(size);
let sch = this.#scheduler.schedule(args);
if (this.#mode == CHANNEL_MODE.BLOCK)
this.#setWBlks(args.wBlks);
return sch;
}
dataView() {
......@@ -252,11 +297,11 @@ export class Channel {
}
isEmpty() {
return this.#getWritePointer() == this.#getReadPointer();
return this.#getWritePointer() == this.#getReadPointer() &&
this.#getRBlks() == this.#getWBlks();
}
// This method is for testing purposes.
readData(size) {
#readDataBytes(size) {
let writePos = this.#getWritePointer();
let readTo = 0, readBuffer = null;
......@@ -294,6 +339,46 @@ export class Channel {
return readBuffer;
}
#readDataBlks(size) {
let endPos = this.#endPos - 1;
let buffer = new Uint8Array(0);
this.#writePointerCache = this.#getWritePointer();
if (this.dataSize() < size)
return buffer;
let blksRequired = Math.floor(size / this.#blockSize);
if (blksRequired == 0) {
return buffer;
}
if (this.#readPointerCache >= this.#writePointerCache) {
let spaceBetweenEnd = endPos - this.#readPointerCache;
let blks = Math.floor(spaceBetweenEnd / this.#blockSize);
if (blks >= blksRequired) {
buffer.set(this.#buffer.subarray(
this.#readPointerCache, this.#readPointerCache+(blksRequired*this.#blockSize)))
} else {
}
} else if (this.#readPointerCache < this.#writePointerCache) {
}
return buffer;
}
readData(size) {
switch (this.#mode) {
case CHANNEL_MODE.BYTE:
return this.#readDataBytes(size);
case CHANNEL_MODE.BLOCK:
return this.#readDataBlks(size);
}
}
/* Return array of buffers to caller
* so caller able to directly write to
* channel. */
......@@ -403,8 +488,18 @@ export class Channel {
// Copy Strategies //
///////////////////////////////////////////////////////////////////////////////
class CopyArgument {
rPos = 0;
wPos = 0;
rBlks = 0;
wBlks = 0;
size = 0;
};
class CopyStrategy {
schedule(rPos, wPos) {
/* This method assume that Channel has enough space
* to hold datas you want to writes. */
schedule(args) {
return NEED_TO_IMPLEMENT;
}
};
......@@ -423,10 +518,14 @@ class CopyStrategyInByte extends CopyStrategy {
this.#endPos = this.#shift + this.#size;
}
schedule(rPos, wPos, size) {
schedule(args) {
let firstCpySize = 0, secondCpySize = 0, spaceToTail = 0;
let schedule = new CpySchedule();
let rPos = args.rPos;
let wPos = args.wPos;
let size = args.size;
if (wPos >= rPos) {
spaceToTail = this.#endPos - wPos;
firstCpySize = Math.min(size, spaceToTail);
......@@ -462,10 +561,14 @@ class CopyStrategyInBlock extends CopyStrategy {
this.#blockSize = blockSize;
}
schedule(rPos, wPos, size) {
schedule(args) {
let retSchedule = new CpySchedule();
let wPos = args.wPos == this.#endPos ? this.#shift : args.wPos;
let rPos = args.rPos;
let size = args.size;
let blocksRequired = Math.floor(size / this.#blockSize);
let endPos = this.#endPos - 1;
if (blocksRequired == 0) {
// Schedules with all zero size
......@@ -474,36 +577,40 @@ class CopyStrategyInBlock extends CopyStrategy {
if (wPos > rPos) {
/* Make sure block has consecutive bytes */
let spaceBetweenEnd = endPos - wPos;
let numOfBlksFirstAblePlace = Math.floor(spaceBetweenEnd / this.#blockSize);
if (numOfBlksFirstAblePlace == 0 || (spaceBetweenEnd % this.#blockSize) > 0) {
/* Must not happen */
let spaceBetweenEnd = this.#endPos - wPos;
let blksAbleToWrite = Math.floor(spaceBetweenEnd / this.#blockSize);
if (blksAbleToWrite == 0) {
return retSchedule();
}
/* Do first copy schedule */
retSchedule.first.pos = wPos;
if (numOfBlksFirstAblePlace >= blocksRequired) {
if (blksAbleToWrite >= blocksRequired) {
retSchedule.first.size = blocksRequired * this.#blockSize;
console.log(retSchedule)
return retSchedule;
} else {
retSchedule.first.size = numOfBlksFirstAblePlace * this.#blockSize;
blocksRequired -= numOfBlksFirstAblePlace;
retSchedule.first.size = blksAbleToWrite * this.#blockSize;
blocksRequired -= blksAbleToWrite;
}
/* Second one is required */
retSchedule.second.pos = this.#shift;
let spaceBetweenRWPtr = rPos - wPos;
/* No enough space */
if (Math.floor(spaceBetweenRWPtr/this.#blockSize) < blocksRequired) {
retSchedule.reset();
return retSchedule;
}
retSchedule.second.size = blocksRequired * this.#blockSize;
} else if (wPos == rPos) {
// Under the assumption of schedule() 'wPos == rPos' means
// channel is empty
retSchedule.first.pos = wPos;
retSchedule.first.size = blocksRequired * this.#blockSize;
} else {
// wPos < rPos
retSchedule.first.pos = wPos;
retSchedule.first.size = blocksRequired * this.#blockSize;
}
args.wBlks += blocksRequired;
console.log(retSchedule);
return retSchedule;
}
}
......@@ -316,5 +316,17 @@ describe("Channel Spec", () => {
}, 10000);
fit("BLK-BASED Channel Case 1", async () => {
const blkSize = 10;
let chn = new Channel(blkSize * 2);
chn.enableBlockMode(blkSize);
let blk = new Uint8Array([...Array(blkSize).keys()]);
for (let i = 0; i < 10; ++i)
chn.push(blk);
expect(chn.dataSize()).toBe(blkSize * 2);
});
});
......@@ -4,21 +4,21 @@ import { sleep, waitCond } from "../src/utils.js";
let paraEnc;
const RGBAFrameSize = 1920*1080*4;
beforeEach(async () => {
let trNum = navigator.hardwareConcurrency;
paraEnc = new ParaEncoder(trNum, {
codec: "H264",
config: {
encchnlsize: RGBAFrameSize * 10,
bridgechnlsize: Math.pow(2, 25),
}
});
await paraEnc.init();
}, 20000);
// beforeEach(async () => {
// let trNum = navigator.hardwareConcurrency;
// paraEnc = new ParaEncoder(trNum, {
// codec: "H264",
// config: {
// encchnlsize: RGBAFrameSize * 10,
// bridgechnlsize: Math.pow(2, 25),
// }
// });
// await paraEnc.init();
// }, 20000);
describe("ParaEncoder", () => {
fit("Encode With ParaEncoder", async () => {
it("Encode With ParaEncoder", async () => {
const data = new Uint8Array([...Array(RGBAFrameSize).keys()]);
let st = new Date();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment