Skip to content

Commit

Permalink
Remove audioWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Nov 25, 2024
1 parent 6ad6b09 commit 60f01f6
Showing 1 changed file with 5 additions and 33 deletions.
38 changes: 5 additions & 33 deletions packages/mp4-media-stream/src/mp4_media_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,38 +293,22 @@ class Mp4MediaStream {
const config = this.wasmJsonToValue(configWasmJson) as AudioDecoderConfig
const init = {
output: async (data: AudioData) => {
if (player.audioWriter === undefined) {
// writer の出力先がすでに閉じられている場合などにここに来る可能性がある
return
}
if (player.audioContext === undefined || player.audioInputNode === undefined) {
return
}
// TODO
// if (data.format !== 'f32-planar') {
// }
// console.log(data.numberOfChannels);
// TODO: error
// if (data.format !== 'f32-planar') {
// }
try {
// TODO: support stereo
// TODO: add timestamp
const buffer = new Float32Array(data.allocationSize({ planeIndex: 0 })/ 4)
const buffer = new Float32Array(data.allocationSize({ planeIndex: 0 }) / 4)
data.copyTo(buffer, { planeIndex: 0 })
player.audioInputNode.port.postMessage(buffer, [buffer.buffer])

// await player.audioWriter.write(data)
} catch (e) {
// 書き込みエラーが発生した場合には再生を停止する

if (e instanceof DOMException && e.name === 'InvalidStateError') {
// 出力先の MediaStreamTrack が停止済み、などの理由で write() が失敗した場合にここに来る。
// このケースは普通に発生し得るので正常系の一部。
// writer はすでに閉じているので、重複 close() による警告ログ出力を避けるために undefined に設定する。
player.audioWriter = undefined
await this.stopPlayer(playerId)
return
}

// 想定外のエラーの場合は再送する
await this.stopPlayer(playerId)
throw e
}
Expand Down Expand Up @@ -444,7 +428,6 @@ class Player {
private video: boolean
audioDecoder?: AudioDecoder
videoDecoder?: VideoDecoder
audioWriter?: WritableStreamDefaultWriter
canvas?: HTMLCanvasElement
canvasCtx?: CanvasRenderingContext2D
audioContext?: AudioContext
Expand All @@ -464,10 +447,7 @@ class Player {
this.audioInputNode = new AudioWorkletNode(this.audioContext, AUDIO_WORKLET_PROCESSOR_NAME)
const destination = this.audioContext.createMediaStreamDestination()
this.audioInputNode.connect(destination)

const generator = new MediaStreamTrackGenerator({ kind: 'audio' })
tracks.push(destination.stream.getAudioTracks()[0])
this.audioWriter = generator.writable.getWriter()
}
if (this.video) {
this.canvas = document.createElement('canvas')
Expand Down Expand Up @@ -535,15 +515,7 @@ class Player {
await this.closeAudioDecoder()
await this.closeVideoDecoder()

if (this.audioWriter !== undefined) {
try {
await this.audioWriter.close()
} catch (e) {
// writer がエラー状態になっている場合などには close() に失敗する模様
// 特に対処法も実害もなさそうなので、ログだけ出して無視しておく
console.log(`[WARNING] ${e}`)
}
this.audioWriter = undefined
if (this.audioContext !== undefined) {
this.audioContext = undefined
this.audioInputNode = undefined
}
Expand Down

0 comments on commit 60f01f6

Please sign in to comment.