Skip to content

Commit

Permalink
feat(avformat): mp4 封装支持设置 matrix 配置,mse 播放支持 matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaohappy committed Feb 12, 2025
1 parent 0e167c0 commit 9a39ae4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
9 changes: 7 additions & 2 deletions src/avformat/formats/mov/writing/tkhd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ export default function write(ioWriter: IOWriter, stream: Stream, movContext: MO

// reserved
ioWriter.writeInt16(0)

writeMatrix(ioWriter, 1, 0, 0, 1, 0, 0)
const matrix = stream.metadata[AVStreamMetadataKey.MATRIX]
if (matrix) {
writeMatrix(ioWriter, matrix[0], matrix[1], matrix[3], matrix[4], matrix[6], matrix[7])
}
else {
writeMatrix(ioWriter, 1, 0, 0, 1, 0, 0)
}

ioWriter.writeUint32(width)
ioWriter.writeUint32(height)
Expand Down
14 changes: 11 additions & 3 deletions src/avplayer/AVPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,8 @@ export default class AVPlayer extends Emitter implements ControllerObserver {
serializeAVCodecParameters(videoStream.codecpar),
videoStream.timeBase,
videoStream.startTime,
this.demuxer2VideoDecoderChannel.port2
this.demuxer2VideoDecoderChannel.port2,
videoStream.metadata[AVStreamMetadataKey.MATRIX]
)
}
if (audioStream && options.audio) {
Expand Down Expand Up @@ -1766,7 +1767,7 @@ export default class AVPlayer extends Emitter implements ControllerObserver {
: this.canvas

// 处理旋转
if (videoStream.metadata[AVStreamMetadataKey.MATRIX]) {
if (videoStream.metadata[AVStreamMetadataKey.MATRIX] && !this.useMSE) {
this.renderRotate = -(Math.atan2(videoStream.metadata[AVStreamMetadataKey.MATRIX][3], videoStream.metadata[AVStreamMetadataKey.MATRIX][0]) * (180 / Math.PI))
}

Expand Down Expand Up @@ -2971,7 +2972,14 @@ export default class AVPlayer extends Emitter implements ControllerObserver {
await this.doSeek(this.currentTime, stream.index, {
onBeforeSeek: async () => {
await AVPlayer.DemuxerThread.changeConnectStream(this.taskId, stream.index, this.selectedVideoStream.index)
await AVPlayer.MSEThread.reAddStream(this.taskId, stream.index, serializeAVCodecParameters(stream.codecpar), stream.timeBase, stream.startTime)
await AVPlayer.MSEThread.reAddStream(
this.taskId,
stream.index,
serializeAVCodecParameters(stream.codecpar),
stream.timeBase,
stream.startTime,
stream.metadata[AVStreamMetadataKey.MATRIX]
)
}
})
}
Expand Down
13 changes: 11 additions & 2 deletions src/avplayer/mse/MSEPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import isPointer from 'cheap/std/function/isPointer'
import * as is from 'common/util/is'
import os from 'common/util/os'
import { MPEG4AudioObjectTypes } from 'avutil/codecs/aac'
import { AVStreamMetadataKey } from 'avutil/AVStream'

export interface MSETaskOptions extends TaskOptions {
isLive: boolean
Expand Down Expand Up @@ -912,7 +913,8 @@ export default class MSEPipeline extends Pipeline {
parameters: pointer<AVCodecParameters> | AVCodecParametersSerialize,
timeBase: Rational,
startPTS: int64,
pullIPCPort: MessagePort
pullIPCPort: MessagePort,
matrix?: number[]
) {
const task = this.tasks.get(taskId)
if (task) {
Expand Down Expand Up @@ -951,6 +953,9 @@ export default class MSEPipeline extends Pipeline {
if (codecpar.codecId === AVCodecID.AV_CODEC_ID_MP3) {
stream.codecpar.codecTag = mktag('.mp3')
}
if (matrix) {
stream.metadata[AVStreamMetadataKey.MATRIX] = matrix
}

const useSampleRateTimeBase = codecpar.codecType === AVMediaType.AVMEDIA_TYPE_AUDIO
&& codecpar.frameSize
Expand Down Expand Up @@ -1046,7 +1051,8 @@ export default class MSEPipeline extends Pipeline {
streamIndex: int32,
parameters: pointer<AVCodecParameters> | AVCodecParametersSerialize,
timeBase: Rational,
startPTS: int64
startPTS: int64,
matrix?: number[]
) {
const task = this.tasks.get(taskId)
if (task) {
Expand Down Expand Up @@ -1095,6 +1101,9 @@ export default class MSEPipeline extends Pipeline {
if (codecpar.codecId === AVCodecID.AV_CODEC_ID_MP3) {
stream.codecpar.codecTag = mktag('.mp3')
}
if (matrix) {
stream.metadata[AVStreamMetadataKey.MATRIX] = matrix
}

const useSampleRateTimeBase = codecpar.codecType === AVMediaType.AVMEDIA_TYPE_AUDIO
&& codecpar.frameSize
Expand Down

0 comments on commit 9a39ae4

Please sign in to comment.