Commit b175c76d authored by Linshizhi's avatar Linshizhi

update

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