DIRECTORY IO USING [noWhereStream, STREAM], Jukebox USING [bytesPerChirp, CloseTune, Handle, pagesPerChirp], PrincOps USING [ByteBltBlock], PrincOpsUtils USING [ByteBlt], Process USING [MsecToTicks, SetTimeout], Rope USING [ROPE], VM USING [AddressForPageNumber, Allocate, Free], VoiceStream; VoiceStreamBasic: MONITOR LOCKS Lock IMPORTS Jukebox, IO, PrincOpsUtils, Process, VM EXPORTS VoiceStream SHARES VoiceStream = BEGIN OPEN VoiceStream; Lock: PUBLIC MONITORLOCK; wholeTune: PUBLIC INT _ 100000000; serverCondition: PUBLIC CONDITION; client: PUBLIC CONDITION; waitCondition: PUBLIC CONDITION; closeCondition: PUBLIC CONDITION; ioStream: PUBLIC IO.STREAM _ IO.noWhereStream; VSList: PUBLIC Handle _ NIL; Error: PUBLIC ERROR[reason: ErrorCode, rope: Rope.ROPE] = CODE; DemonError: PUBLIC ERROR = CODE; nOpens: INT _ 0; nWaits: INT _ 0; Open: PUBLIC PROC [jukebox: Jukebox.Handle, proc: NotifyProc _ NIL, clientData: REF ANY _ NIL] RETURNS [handle: Handle] = { ENABLE UNWIND => NULL; buffer: REF Buffer _ NIL; handle _ NEW[VSRecord _ [jukebox, NIL, NIL, NIL, NIL, NIL, NIL, Bug, proc, FALSE, clientData, NIL, ,FALSE]]; Process.SetTimeout[condition: @handle.newPiece, ticks: Process.MsecToTicks[400]]; FOR i:INTEGER IN [0..buffersPerStream) DO buffer _ NEW[Buffer]; buffer.valid _ FALSE; buffer.chirpSpace _ VM.Allocate[count: Jukebox.pagesPerChirp]; buffer.block.blockPointer _ LOOPHOLE[VM.AddressForPageNumber[buffer.chirpSpace.page]]; buffer.runData _ buffer.block.blockPointer + (Jukebox.bytesPerChirp/2); buffer.next _ handle.firstIdleBuffer; handle.firstIdleBuffer _ buffer; ENDLOOP; OpenLocked[handle]; RETURN [handle]; }; OpenLocked: ENTRY PROC [handle: Handle] = { ENABLE UNWIND => NULL; handle.next _ VSList; VSList _ handle; nOpens _ nOpens + 1; NOTIFY serverCondition; }; Close: PUBLIC ENTRY PROC [handle: Handle] = { ENABLE UNWIND => NULL; buffer: REF Buffer; record: Handle; IF (handle.errorRope # NIL) AND (handle.errorCode = StreamClosed) THEN ERROR Error[StreamClosed, handle.errorRope]; flushBuffer[handle]; flushProc[handle]; WHILE (handle.firstClientBuffer # NIL) DO handle.firstClientBuffer.valid _ FALSE; giveServerBuffer[handle:handle]; ENDLOOP; WHILE (handle.firstServerBuffer # NIL) AND (handle.errorRope = NIL) DO NOTIFY serverCondition; WAIT closeCondition; ENDLOOP; IF handle.piece # NIL THEN { IF handle.piece.tune # NIL THEN Jukebox.CloseTune[handle.jukebox, handle.piece.tune]; }; IF handle.connection # NIL THEN handle.connection.socket _ NIL; IF VSList = handle THEN VSList _ handle.next ELSE { record _ VSList; WHILE record.next # handle DO IF record.next = NIL THEN ERROR Error[Bug, "Unexpected VSList end."]; record _ record.next; ENDLOOP; record.next _ handle.next; }; WHILE handle.firstClientBuffer # NIL DO buffer _ handle.firstClientBuffer; handle.firstClientBuffer _ buffer.next; VM.Free[buffer.chirpSpace]; ENDLOOP; WHILE handle.firstServerBuffer # NIL DO buffer _ handle.firstServerBuffer; handle.firstServerBuffer _ buffer.next; VM.Free[buffer.chirpSpace]; ENDLOOP; WHILE handle.firstIdleBuffer # NIL DO buffer _ handle.firstIdleBuffer; handle.firstIdleBuffer _ buffer.next; VM.Free[buffer.chirpSpace]; ENDLOOP; handle.errorCode _ StreamClosed; handle.errorRope _ "Can't use voice stream after it's closed."; handle.jukebox _ NIL; BROADCAST client; BROADCAST waitCondition; }; Get: PUBLIC ENTRY PROC [handle: Handle, maxSilentBytes: NAT, block: PrincOps.ByteBltBlock, wait: BOOL _ FALSE] RETURNS [silence: NAT _ 0, bytesTransferred: NAT _ 0, keyIndex: NAT _ 0] = { ENABLE UNWIND => NULL; buffer: REF Buffer; silencePass: BOOL _ TRUE; -- state variable silenceRunType: BOOL; runSize: NAT; DO WHILE (handle.firstClientBuffer = NIL) DO IF handle.errorRope # NIL THEN ERROR Error[handle.errorCode, handle.errorRope]; IF handle.piece = NIL THEN { IF handle.proc # NIL AND NOT handle.notified THEN { handle.notified _ TRUE; handle.proc[handle, handle.clientData]; }; BROADCAST waitCondition; RETURN; }; nWaits _ nWaits + 1; IF wait THEN WAIT client ELSE RETURN; ENDLOOP; buffer _ handle.firstClientBuffer; handle.notified _ FALSE; keyIndex _ buffer.keyIndex; IF buffer.toJukebox THEN RETURN; IF handle.piece.flush THEN { handle.firstClientBuffer.valid _ FALSE; giveServerBuffer[handle: handle]; LOOP; }; IF buffer.toJukebox THEN RETURN; IF buffer.bytesAccountedFor >= Jukebox.bytesPerChirp THEN { giveServerBuffer[handle: handle]; LOOP; }; runSize _ buffer.runData.runArray[buffer.runIndex]; IF runSize = 0 THEN { buffer.runIndex _ buffer.runIndex + 1; LOOP; }; silenceRunType _ (buffer.runIndex MOD 2) = 0; IF silencePass AND NOT silenceRunType THEN silencePass _ FALSE; SELECT TRUE FROM silencePass AND NOT silenceRunType => ERROR; silencePass AND silenceRunType => { silenceAmount: CARDINAL _ MIN[runSize, maxSilentBytes]; silence _ silence + silenceAmount; buffer.runData.runArray[buffer.runIndex] _ runSize - silenceAmount; buffer.bytesAccountedFor _ buffer.bytesAccountedFor + silenceAmount; maxSilentBytes _ maxSilentBytes - silenceAmount; IF maxSilentBytes = 0 THEN silencePass _ FALSE; }; NOT silencePass AND NOT silenceRunType => { voiceAmount: CARDINAL _ MIN[runSize, block.stopIndexPlusOne - block.startIndex]; origStopIndex: CARDINAL _ block.stopIndexPlusOne; count: CARDINAL; block.stopIndexPlusOne _ block.startIndex + voiceAmount; count _ PrincOpsUtils.ByteBlt[from: buffer.block, to: block]; IF count # voiceAmount THEN ERROR; block.stopIndexPlusOne _ origStopIndex; block.startIndex _ block.startIndex + voiceAmount; buffer.block.startIndex _ buffer.block.startIndex + voiceAmount; buffer.bytesAccountedFor _ buffer.bytesAccountedFor + voiceAmount; buffer.runData.runArray[buffer.runIndex] _ runSize - voiceAmount; bytesTransferred _ bytesTransferred + voiceAmount; IF block.startIndex >= block.stopIndexPlusOne THEN { IF buffer.bytesAccountedFor >= Jukebox.bytesPerChirp THEN { giveServerBuffer[handle: handle]; }; handle.action _ TRUE; RETURN; }; }; NOT silencePass AND silenceRunType => { RETURN; }; ENDCASE => ERROR; handle.action _ TRUE; ENDLOOP; }; Put: PUBLIC ENTRY PROC [handle: Handle, silentBytes: NAT, block: PrincOps.ByteBltBlock] RETURNS [bytesTransferred: NAT _ 0] = { ENABLE UNWIND => NULL; buffer: REF Buffer; silencePass: BOOL _ silentBytes > 0; silenceRunType: BOOL; runSize: NAT; DO WHILE handle.firstClientBuffer = NIL DO IF handle.errorRope # NIL THEN ERROR Error[handle.errorCode, handle.errorRope]; IF handle.piece = NIL THEN { IF handle.proc # NIL AND NOT handle.notified THEN { handle.notified _ TRUE; handle.proc[handle, handle.clientData]; }; BROADCAST waitCondition; RETURN; }; WAIT client; ENDLOOP; buffer _ handle.firstClientBuffer; handle.notified _ FALSE; IF NOT buffer.toJukebox THEN RETURN; IF handle.piece.flush THEN { IF handle.firstClientBuffer.valid THEN flushBuffer[handle] ELSE giveServerBuffer[handle: handle]; LOOP; }; IF NOT buffer.toJukebox THEN RETURN; IF buffer.bytesAccountedFor >= Jukebox.bytesPerChirp THEN { giveServerBuffer[handle: handle]; LOOP; }; silenceRunType _ (buffer.runIndex MOD 2) = 0; runSize _ buffer.runData.runArray[buffer.runIndex]; SELECT TRUE FROM silencePass AND NOT silenceRunType => { buffer.runIndex _ buffer.runIndex + 1; buffer.runData.runArray[buffer.runIndex] _ 0; }; silencePass AND silenceRunType => { silenceAmount: CARDINAL _ MIN[silentBytes, Jukebox.bytesPerChirp - buffer.bytesAccountedFor]; silentBytes _ silentBytes - silenceAmount; buffer.runData.runArray[buffer.runIndex] _ runSize + silenceAmount; buffer.bytesAccountedFor _ buffer.bytesAccountedFor + silenceAmount; bytesTransferred _ bytesTransferred + silenceAmount; IF silentBytes = 0 THEN silencePass _ FALSE; }; NOT silencePass AND silenceRunType => { buffer.runIndex _ buffer.runIndex + 1; buffer.runData.runArray[buffer.runIndex] _ 0; }; NOT silencePass AND NOT silenceRunType => { voiceAmount: CARDINAL _ MIN[block.stopIndexPlusOne - block.startIndex, Jukebox.bytesPerChirp - buffer.bytesAccountedFor]; origStopIndex: CARDINAL _ block.stopIndexPlusOne; count: CARDINAL; block.stopIndexPlusOne _ block.startIndex + voiceAmount; count _ PrincOpsUtils.ByteBlt[from: block, to: buffer.block]; IF count # voiceAmount THEN ERROR; block.stopIndexPlusOne _ origStopIndex; IF voiceAmount > 0 THEN buffer.valid _ TRUE; block.startIndex _ block.startIndex + voiceAmount; buffer.block.startIndex _ buffer.block.startIndex + voiceAmount; buffer.bytesAccountedFor _ buffer.bytesAccountedFor + voiceAmount; buffer.runData.runArray[buffer.runIndex] _ runSize + voiceAmount; bytesTransferred _ bytesTransferred + voiceAmount; IF block.startIndex >= block.stopIndexPlusOne THEN { IF buffer.bytesAccountedFor >= Jukebox.bytesPerChirp THEN { giveServerBuffer[handle: handle]; }; handle.action _ TRUE; RETURN; }; }; ENDCASE => ERROR; handle.action _ TRUE; ENDLOOP; }; AddPiece: PUBLIC ENTRY PROC [handle: Handle, tuneId: INT, firstByte: INT, nBytes: INT, create: BOOLEAN, playback: BOOLEAN _ TRUE, keyIndex: NAT _ 0, flush: BOOLEAN _ FALSE] = { ENABLE UNWIND => NULL; piece, p2: REF Piece; IF handle.errorRope # NIL THEN ERROR Error[handle.errorCode, handle.errorRope]; IF flush THEN flushProc[handle]; piece _ NEW[Piece _ [tuneId, create, firstByte, nBytes, NIL, playback, keyIndex, NIL, FALSE]]; IF handle.piece = NIL THEN handle.piece _ piece ELSE { p2 _ handle.piece; WHILE p2.next # NIL DO p2 _ p2.next ENDLOOP; p2.next _ piece; }; NOTIFY serverCondition; }; FlushPieces: PUBLIC ENTRY PROC [handle: Handle] = { ENABLE UNWIND => NULL; flushProc[handle]; }; flushProc: INTERNAL PROC [handle: Handle] = { p: REF Piece _ handle.piece; WHILE p # NIL DO p.flush _ TRUE; p _ p.next; ENDLOOP; BROADCAST client; BROADCAST waitCondition; }; IsEmpty: PUBLIC PROC [handle: Handle] RETURNS [BOOLEAN] = { IF handle.firstClientBuffer # NIL THEN RETURN[FALSE]; IF handle.piece # NIL THEN RETURN[FALSE]; RETURN[TRUE]; }; WaitEmpty: PUBLIC ENTRY PROC [handle: Handle] = { ENABLE UNWIND => NULL; WHILE TRUE DO IF handle.errorRope # NIL THEN RETURN; IF (handle.firstClientBuffer = NIL) AND (handle.piece = NIL) THEN RETURN; WAIT waitCondition; ENDLOOP; }; Check: PUBLIC ENTRY PROC [handle: Handle] RETURNS[BOOLEAN] = { ENABLE UNWIND => NULL; IF handle.errorRope # NIL THEN ERROR Error[handle.errorCode, handle.errorRope]; IF handle.firstClientBuffer = NIL THEN RETURN[FALSE]; IF handle.firstClientBuffer.toJukebox THEN RETURN[FALSE]; RETURN[TRUE]; }; giveServerBuffer: INTERNAL PROC [handle: Handle] = { buffer, b2: REF Buffer; buffer _ handle.firstClientBuffer; handle.firstClientBuffer _ buffer.next; buffer.next _ NIL; IF handle.firstServerBuffer = NIL THEN handle.firstServerBuffer _ buffer ELSE { b2 _ handle.firstServerBuffer; WHILE b2.next # NIL DO b2 _ b2.next ENDLOOP; b2.next _ buffer; }; NOTIFY serverCondition; }; flushBuffer: PUBLIC INTERNAL PROC [handle: Handle] = { buffer: REF Buffer; buffer _ handle.firstClientBuffer; IF buffer = NIL THEN RETURN; IF ~buffer.toJukebox THEN RETURN; IF buffer.bytesAccountedFor IN (0..Jukebox.bytesPerChirp) THEN { IF (buffer.runIndex MOD 2) # 0 THEN { -- voice run IF buffer.runData.runArray[buffer.runIndex] = 0 THEN buffer.runIndex _ buffer.runIndex - 1 ELSE { buffer.runIndex _ buffer.runIndex + 1; buffer.runData.runArray[buffer.runIndex] _ 0; }; }; buffer.runData.runArray[buffer.runIndex] _ buffer.runData.runArray[buffer.runIndex] + (Jukebox.bytesPerChirp - buffer.bytesAccountedFor); giveServerBuffer[handle: handle]; }; }; END. Last Edited by: L. Stewart, March 25, 1983 3:54 pm, VoiceStream change L. Stewart, April 5, 1983 2:38 pm, Tioga formatting, rundata L. Stewart, April 11, 1983 1:06 pm, bug fixing in rundata Last Edited by: L. Stewart, April 19, 1983 3:17 pm, Silence encoding L. Stewart, June 4, 1983 5:42 pm, call notifyProc only once L. Stewart, December 27, 1983 1:44 pm, Cedar 5 File: VoiceStreamBasic.mesa This file contains part of the VoiceStream implementation. Routines in this file provide basic client facilities such as opening closing, reading, and writing voice streams. Last Edited by: Swinehart, April 14, 1983 10:48 am Last Edited by: Ousterhout, March 8, 1983 11:37 amcompile Last Edited by: L. Stewart, December 30, 1983 11:19 am See VoiceStream.mesa for documentation on the following things. The following variables keep track of how many streams have been used and how often waiting occurred. Invoke the server to prepare the buffers for actual use. This procedure closes out a voice stream. It waits for pending I/O to complete, then deallocates the stream. IO.PutF[ioStream, "Closing tune.\n"]; Make sure that the stream isn't already closed. Output any partially full buffers to the jukebox. Flush any pending pieces. Flush any pending client buffers. Wait for the server to get completely caught up. This code also synchronizes with the server so we're sure the server isn't touching the voice stream info anymore. Close any tune that might be open. If there is a socket process, then signal it to die. Remove the VSRecord from our list, then de-allocate the spaces for the buffers (everything else is taken care of by the garbage collector. Mark the VSRecord invalid. 1) Aquire a non-empty chirp If the server isn't doing its job, then we may have to wait here to get a chirp. This shouldn't happen and should probably be reported. Rather than wait, we should play silence and try again later in order to keep the protocol inviolate. If the stream is currently going the wrong way, then just return immediately whith whatever has been accumulated. Typically this is a RETURN[0, 0]. If the current piece is being flushed, then just give the buffers back to the server immediately (ignore the data). If presently recording, then quietly return. Typically this is a RETURN[0, 0]. If we have used up all of this chirp, then give it back and get the next chirp. This should be done at the end of the code, not the beginning. IO.PutF[ioStream, "Got chirp %d.\n", IO.int[buffer.chirp]]; 2) Aquire a non-empty run 3) build state variables IO.PutF[ioStream, "Got chirp %d.\n", IO.int[buffer.chirp]]; This procedure adds the bytes from block to the end of the voice stream. WHILE silentBytes > 0 OR block.startIndex < block.stopIndexPlusOne If the stream is currently going the wrong way, then just return immediately. Typically this is a RETURN[0]. If the current piece is being flushed, then get rid of the current buffer (throw it away if it's invalid, otherwise fill it with zeroes). If not presently recording, then quietly return. Typically this is a RETURN[0]. If we have used up all of this chirp, then give it back and get the next chirp. This should be done at the end of the code, not the beginning. IO.PutF[ioStream, "Got chirp %d.\n", IO.int[buffer.chirp]]; build state variable IO.PutF[ioStream, "Put chirp %d.\n", IO.int[buffer.chirp]]; This routine allocates another piece descriptor and adds it to the list for the voice stream. ioStream.PutF["%s: size %d\n", IO.rope[IF playback THEN "Playback" ELSE "Record"], IO.int[nBytes]]; This routine flushes any unused voice info in a stream. This is done by marking all the pieces as "flushed". Then, various routines see the flush flag and ignore information. Buffers currently waiting to be written to disk will not be flushed, but everything else will be. It isn't safe synchronization-wise for us to just remove all the buffers and pieces, hence the use of the flag. Figure out whether the socket process has used up all the available pieces. Just wait until the stream empties completely. If there are any errors pending for the given voice stream, they are signalled immediately. If there are no errors and there is data ready for playback, then TRUE is returned. Otherwise, FALSE is returned. This routine passes the leading buffer to the server, and activates the server. If there is a partially-filled leading client buffer, this routine pads it with zeroes and sends it to the server. ʸ˜Jšœ™Jšœ:™:Jšœ=™=Jšœ4™4J˜Jšœ2™2Jšœ9™9Jšœ6™6J˜šÏk ˜ Jšœœœ˜!Jšœœ3˜@Jšœ œ˜Jšœœ ˜Jšœœ˜(Jšœœœ˜Jšœœ(˜0J˜ J˜—Jšœœœ˜$Jšœ œ˜/Jšœ ˜šœ˜J˜—Jšœœ ˜J˜Jšœ?™?J˜Jšœœ œ˜Jšœ œœ ˜"Jšœœ œ˜"Jšœœ œ˜Jšœœ œ˜ Jšœœ œ˜!Jš œ œœœœ˜.Jšœœ œ˜Jš œœœœœ˜?Jšœ œœœ˜ J˜Jšœ@™@Jšœ$™$J˜Jšœœ˜Jšœœ˜J˜šÏnœœœ/œ˜DJšœ œœœ˜Jšœ˜J˜Jšœœœ˜J˜Jšœœ œ˜J˜šœ œœœœœœœ˜?Jšœ œœœ˜,—JšœQ˜Qšœœœ˜)Jšœ œ ˜Jšœœ˜Jšœœ(˜>Jšœœœ/˜VJšœG˜GJ˜%J˜ Jšœ˜—šœ˜J˜—Jšœ ˜Jšœ˜—J˜šž œœœ˜+Jšœœœ˜J˜J˜J˜J˜J˜Jšœ8™8J˜Jšœ˜Jšœ˜—J˜Jšžœœœœ˜-˜Jšœ7™7Jšœ5™5J˜Jšœœœ˜J˜Jšœœ˜J˜J˜Jšœ%™%J˜Jšœ/™/J˜šœœœ"˜AJšœœ(˜2J˜—Jšœ1™1J˜J˜J˜Jšœ™J˜J˜J˜Jšœ!™!J˜šœœ˜)Jšœ!œ˜'J˜ Jšœ˜J˜—Jšœ;™;Jšœ:™:Jšœ-™-J˜š œœœœ˜FJšœ˜Jšœ˜Jšœ˜J˜—Jšœ"™"J˜šœœœ˜Jšœœœ6˜U˜J˜——Jšœ4™4J˜Jšœœœœ˜?J˜Jšœ>™>Jšœ@™@Jšœ ™ J˜Jšœœ˜,šœ˜J˜šœ˜Jšœœœœ&˜EJ˜—Jšœ˜J˜Jšœ˜J˜—šœœ˜'J˜"J˜'Jšœ˜Jšœ˜J˜—šœœ˜'J˜"J˜'Jšœ˜Jšœ˜J˜—šœœ˜%J˜ J˜%Jšœ˜Jšœ˜J˜Jšœ™J˜—J˜ J˜?Jšœœ˜Jš œ˜Jš œ˜Jšœ˜J˜—šžœœœœ"œ&œœ˜nJšœ œœœ ˜LJšœœœ˜J˜Jšœœ˜Jšœ œœÏc˜+Jšœœ˜Jšœ œ˜ J˜š˜J™J™J™ïJ™šœœ˜)Jšœœœœ+˜OJ˜šœœœ˜š œœœœœ˜3Jšœœ˜Jšœ'˜'J˜—Jš œ˜Jšœ˜Jšœ˜—J˜J˜Jšœœœ˜Jšœœ˜ Jšœ˜—J˜J˜"Jšœœ˜J˜J˜Jšœ9™9JšœZ™ZJ˜Jšœœœ˜ J˜JšœA™AJšœ1™1J˜šœœ˜Jšœ!œ˜'J˜!Jšœ˜Jšœ˜—J™JšœO™OJšœœœ˜ J˜J™Žšœ3œ˜;Jšœ<™