-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: downloadStill from media pool (#167)
- Loading branch information
Showing
8 changed files
with
373 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
DataTransferAckCommand, | ||
DataTransferCompleteCommand, | ||
DataTransferDataCommand, | ||
DataTransferDownloadRequestCommand, | ||
DataTransferErrorCommand, | ||
ErrorCode, | ||
} from '../commands/DataTransfer' | ||
import { IDeserializedCommand } from '../commands/CommandBase' | ||
import { DataTransfer, ProgressTransferResult, DataTransferState } from './dataTransfer' | ||
|
||
// TODO - this should be reimplemented on top of a generic DataTransferDownloadBuffer class | ||
export class DataTransferDownloadStill extends DataTransfer<Buffer> { | ||
#data: Buffer[] = [] | ||
|
||
constructor(public readonly stillIndex: number) { | ||
super() | ||
} | ||
|
||
public async startTransfer(transferId: number): Promise<ProgressTransferResult> { | ||
const command = new DataTransferDownloadRequestCommand({ | ||
transferId: transferId, | ||
transferStoreId: 0x00, | ||
transferIndex: this.stillIndex, | ||
transferType: 0x00f9, | ||
}) | ||
|
||
return { | ||
newState: DataTransferState.Ready, | ||
commands: [command], | ||
} | ||
} | ||
|
||
public async handleCommand( | ||
command: IDeserializedCommand, | ||
oldState: DataTransferState | ||
): Promise<ProgressTransferResult> { | ||
if (command instanceof DataTransferErrorCommand) { | ||
switch (command.properties.errorCode) { | ||
case ErrorCode.Retry: | ||
return this.restartTransfer(command.properties.transferId) | ||
|
||
case ErrorCode.NotFound: | ||
this.abort(new Error('Invalid download')) | ||
|
||
return { | ||
newState: DataTransferState.Finished, | ||
commands: [], | ||
} | ||
default: | ||
// Abort the transfer. | ||
this.abort(new Error(`Unknown error ${command.properties.errorCode}`)) | ||
|
||
return { | ||
newState: DataTransferState.Finished, | ||
commands: [], | ||
} | ||
} | ||
} else if (command instanceof DataTransferDataCommand) { | ||
this.#data.push(command.properties.body) | ||
|
||
// todo - have we received all data? maybe check if the command.body < max_len | ||
|
||
return { | ||
newState: oldState, | ||
commands: [ | ||
new DataTransferAckCommand({ | ||
transferId: command.properties.transferId, | ||
transferIndex: this.stillIndex, | ||
}), | ||
], | ||
} | ||
} else if (command instanceof DataTransferCompleteCommand) { | ||
this.resolvePromise(Buffer.concat(this.#data)) | ||
|
||
return { | ||
newState: DataTransferState.Finished, | ||
commands: [], | ||
} | ||
} | ||
|
||
return { newState: oldState, commands: [] } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.