Skip to content

Commit

Permalink
coreaudio-encoder: Fix pts/dts not including encoder delay
Browse files Browse the repository at this point in the history
  • Loading branch information
derrod committed May 18, 2024
1 parent 842d249 commit 2758b91
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 9 additions & 2 deletions plugins/coreaudio-encoder/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ struct ca_encoder {

uint64_t total_samples = 0;
uint64_t samples_per_second = 0;
uint32_t leading_frames = 0;

vector<uint8_t> extra_data;

Expand Down Expand Up @@ -595,6 +596,12 @@ static void *aac_create(obs_data_t *settings, obs_encoder_t *encoder)
ca->converter, kAudioConverterCurrentOutputStreamDescription,
&size, &out));

AudioConverterPrimeInfo priming = {};
size = sizeof(priming);
STATUS_CHECK(AudioConverterGetProperty(
ca->converter, kAudioConverterPrimeInfo, &size, &priming));
ca->leading_frames = priming.leadingFrames;

/*
* Fix channel map differences between CoreAudio AAC, FFmpeg, Wav
* New channel mappings below assume 2.1, 4.0, 4.1, 5.1, 7.1 resp.
Expand Down Expand Up @@ -770,8 +777,8 @@ static bool aac_encode(void *data, struct encoder_frame *frame,
if (!(*received_packet = packets > 0))
return true;

packet->pts = ca->total_samples;
packet->dts = ca->total_samples;
packet->pts = ca->total_samples - ca->leading_frames;
packet->dts = ca->total_samples - ca->leading_frames;
packet->timebase_num = 1;
packet->timebase_den = (uint32_t)ca->samples_per_second;
packet->type = OBS_ENCODER_AUDIO;
Expand Down
6 changes: 6 additions & 0 deletions plugins/coreaudio-encoder/windows-imports.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ struct AudioChannelLayout {
};
typedef struct AudioChannelLayout AudioChannelLayout;

struct AudioConverterPrimeInfo {
UInt32 leadingFrames;
UInt32 trailingFrames;
};
typedef struct AudioConverterPrimeInfo AudioConverterPrimeInfo;

typedef OSStatus (*AudioConverterComplexInputDataProc)(
AudioConverterRef inAudioConverter, UInt32 *ioNumberDataPackets,
AudioBufferList *ioData,
Expand Down

0 comments on commit 2758b91

Please sign in to comment.