Fixed opus encoding completion

This commit is contained in:
Ilya Laktyushin 2019-08-12 05:22:13 +03:00
parent 41c468c139
commit b4541f7495
3 changed files with 201 additions and 212 deletions

View File

@ -178,7 +178,7 @@ static const int encoderPacketSizeInBytes = TGBridgeAudioEncoderSampleRate / 100
if (_assetReader.status == AVAssetReaderStatusCompleted)
{
NSLog(@"finished");
if (_oggWriter != nil)
if (_oggWriter != nil && [_oggWriter writeFrame:NULL frameByteCount:0])
{
dataItemResult = _tempFileItem;
durationResult = [_oggWriter encodedDuration];

View File

@ -592,6 +592,7 @@ final class ManagedAudioRecorderContext {
}
func takeData() -> RecordedAudioData? {
if self.oggWriter.writeFrame(nil, frameByteCount: 0) {
var scaledSamplesMemory = malloc(100 * 2)!
var scaledSamples: UnsafeMutablePointer<Int16> = scaledSamplesMemory.assumingMemoryBound(to: Int16.self)
defer {
@ -639,6 +640,9 @@ final class ManagedAudioRecorderContext {
}
return RecordedAudioData(compressedData: self.dataItem.data(), duration: self.oggWriter.encodedDuration(), waveform: waveform)
} else {
return nil
}
}
}

View File

@ -426,10 +426,12 @@ static inline int writeOggPage(ogg_page *page, TGDataItem *fileItem)
pages_out++;
}
if (framePcmBytes != NULL) {
op.packet = (unsigned char *)_packet;
op.bytes = nbBytes;
op.b_o_s = 0;
op.granulepos = enc_granulepos;
if (op.e_o_s)
{
/* We compute the final GP as ceil(len*48k/input_rate). When a resampling
@ -441,24 +443,7 @@ static inline int writeOggPage(ogg_page *page, TGDataItem *fileItem)
op.packetno = 2 + _packetId;
ogg_stream_packetin(&os, &op);
last_segments += size_segments;
/* The downside of early reading is if the input is an exact
multiple of the frame_size you'll get an extra frame that needs
to get cropped off. The downside of late reading is added delay.
If your ogg_delay is 120ms or less we'll assume you want the
low delay behavior.
*/
/*if ((!op.e_o_s) && max_ogg_delay > 5760)
{
nb_samples = inopt.read_samples(inopt.readdata, input, frame_size);
total_samples += nb_samples;
if (nb_samples < frame_size)
eos = 1;
if (nb_samples == 0)
op.e_o_s = 1;
}
else
nb_samples = -1;*/
// If the stream is over or we're sure that the delayed flush will fire, go ahead and flush now to avoid adding delay
while ((op.e_o_s || (enc_granulepos + (frame_size * 48000 / coding_rate) - last_granulepos > max_ogg_delay) ||