Skip to content

Commit

Permalink
docs: 修复多线程环境下无法播放声音的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaohappy committed Dec 17, 2024
1 parent 5bfc318 commit 7874980
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 12 deletions.
1 change: 0 additions & 1 deletion site/docs/demo/audio-encode-wasm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ async function encode(set: (v: string) => void) {
let newFrame = frame
if (resampler) {
resampler.resample(frame.extendedData, addressof(pcmBuffer), frame.nbSamples)
pcmBuffer.format = AVSampleFormat.AV_SAMPLE_FMT_FLTP
newFrame = avPCMBuffer2AVFrame(addressof(pcmBuffer), false, frame)
newFrame.pts = frame.pts
newFrame.duration = frame.duration
Expand Down
1 change: 0 additions & 1 deletion site/docs/demo/audio-encode-webcodecs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ async function encode(set: (v: string) => void) {
let newFrame = frame
if (resampler) {
resampler.resample(frame.extendedData, addressof(pcmBuffer), frame.nbSamples)
pcmBuffer.format = AVSampleFormat.AV_SAMPLE_FMT_FLTP
newFrame = avPCMBuffer2AVFrame(addressof(pcmBuffer), false, frame)
newFrame.pts = frame.pts
newFrame.duration = frame.duration
Expand Down
19 changes: 15 additions & 4 deletions site/docs/demo/audio-render-audiodata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import { mapFloat32Array } from '@libmedia/cheap/std/memory'
import { destroyAVFrame } from '@libmedia/avutil/util/avframe'
import WebAudioDecoder from '@libmedia/avcodec/webcodec/AudioDecoder'
import { audioData2AVFrame } from '@libmedia/avutil/function/audioData2AVFrame'
import * as cheapConfig from '@libmedia/cheap/config'
import { avRescaleQ } from '@libmedia/avutil/util/rational'
import { AV_TIME_BASE_Q } from '@libmedia/avutil/constant'

import { formatUrl, getIOReader, getAVFormat, getAccept, getWasm } from './utils'
import { useEffect, useRef, useState } from 'react'
import { useEffect } from 'react'
import React from 'react'
import { avRescaleQ } from '@libmedia/avutil/util/rational'
import { AV_TIME_BASE_Q } from '@libmedia/avutil/constant'


let file: File
Expand Down Expand Up @@ -74,7 +75,17 @@ async function render() {
function play(data: pointer<pointer<float>>, nbSamples: int32, sampleRate: int32) {
const audioBuffer = audioContext.createBuffer(channels, nbSamples, sampleRate)
for (let i = 0; i < channels; i++) {
audioBuffer.copyToChannel(mapFloat32Array(data[i], nbSamples), i)
if (audioBuffer.copyToChannel && !cheapConfig.USE_THREADS) {
audioBuffer.copyToChannel(
mapFloat32Array(data[i], nbSamples),
i,
0
)
}
else {
const audioData = audioBuffer.getChannelData(i)
audioData.set(mapFloat32Array(data[i], nbSamples), 0)
}
}
const bufferSource = audioContext.createBufferSource()
bufferSource.buffer = audioBuffer
Expand Down
15 changes: 13 additions & 2 deletions site/docs/demo/audio-render-avframe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import AVPCMBuffer from '@libmedia/avutil/struct/avpcmbuffer'
import { avFreep } from '@libmedia/avutil/util/mem'
import { mapFloat32Array } from '@libmedia/cheap/std/memory'
import { destroyAVFrame } from '@libmedia/avutil/util/avframe'
import * as cheapConfig from '@libmedia/cheap/config'

import { formatUrl, getIOReader, getAVFormat, getAccept, getWasm } from './utils'
import { useEffect, useRef, useState } from 'react'
import { useEffect } from 'react'
import React from 'react'

let file: File
Expand Down Expand Up @@ -70,7 +71,17 @@ async function render() {
function play(data: pointer<pointer<float>>, nbSamples: int32, sampleRate: int32) {
const audioBuffer = audioContext.createBuffer(channels, nbSamples, sampleRate)
for (let i = 0; i < channels; i++) {
audioBuffer.copyToChannel(mapFloat32Array(data[i], nbSamples), i)
if (audioBuffer.copyToChannel && !cheapConfig.USE_THREADS) {
audioBuffer.copyToChannel(
mapFloat32Array(data[i], nbSamples),
i,
0
)
}
else {
const audioData = audioBuffer.getChannelData(i)
audioData.set(mapFloat32Array(data[i], nbSamples), 0)
}
}
const bufferSource = audioContext.createBufferSource()
bufferSource.buffer = audioBuffer
Expand Down
2 changes: 1 addition & 1 deletion site/docs/demo/video-decode-wasm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async function decode(set: (v: string) => void) {
const stream = iformatContext.getStreamByMediaType(AVMediaType.AVMEDIA_TYPE_VIDEO)

const decoder = new WasmVideoDecoder({
resource: await compileResource(getWasm('decoder', stream.codecpar.codecId)),
resource: await compileResource(getWasm('decoder', stream.codecpar.codecId), true),
onError: (error) => {
set(`decode error: ${error}\n`)
},
Expand Down
4 changes: 2 additions & 2 deletions site/docs/demo/video-encode-wasm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function encode(set: (v: string) => void) {
let counter = 0

const encoder = new WasmVideoEncoder({
resource: await compileResource(getWasm('encoder', codecpar.codecId)),
resource: await compileResource(getWasm('encoder', codecpar.codecId), true),
onError: (error) => {
set(`encode error: ${error}\n`)
},
Expand All @@ -61,7 +61,7 @@ async function encode(set: (v: string) => void) {
})

const decoder = new WasmVideoDecoder({
resource: await compileResource(getWasm('decoder', stream.codecpar.codecId)),
resource: await compileResource(getWasm('decoder', stream.codecpar.codecId), true),
onError: (error) => {
set(`decode error: ${error}\n`)
},
Expand Down
2 changes: 1 addition & 1 deletion site/docs/demo/video-encode-webcodecs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function encode(set: (v: string) => void) {
})

const decoder = new WasmVideoDecoder({
resource: await compileResource(getWasm('decoder', stream.codecpar.codecId)),
resource: await compileResource(getWasm('decoder', stream.codecpar.codecId), true),
onError: (error) => {
set(`decode error: ${error}\n`)
},
Expand Down

0 comments on commit 7874980

Please sign in to comment.