2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2017-03-25 13:29:29 +00:00
|
|
|
* Copyright (c) 2013-2017 OSSRS(winlin)
|
2017-03-25 09:21:39 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2015-01-18 14:51:07 +00:00
|
|
|
|
|
|
|
#include <srs_kernel_aac.hpp>
|
|
|
|
|
2015-10-01 05:04:28 +00:00
|
|
|
#if !defined(SRS_EXPORT_LIBRTMP)
|
|
|
|
|
2015-11-11 02:37:50 +00:00
|
|
|
// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213
|
2015-01-18 14:51:07 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <srs_kernel_log.hpp>
|
|
|
|
#include <srs_kernel_error.hpp>
|
2015-09-22 00:48:55 +00:00
|
|
|
#include <srs_kernel_buffer.hpp>
|
2015-01-18 14:51:07 +00:00
|
|
|
#include <srs_kernel_file.hpp>
|
2015-01-18 16:05:12 +00:00
|
|
|
#include <srs_kernel_codec.hpp>
|
2017-12-31 04:11:48 +00:00
|
|
|
#include <srs_core_autofree.hpp>
|
2015-01-18 14:51:07 +00:00
|
|
|
|
2017-02-12 13:50:02 +00:00
|
|
|
SrsAacTransmuxer::SrsAacTransmuxer()
|
2015-01-18 14:51:07 +00:00
|
|
|
{
|
|
|
|
_fs = NULL;
|
2015-01-18 16:05:12 +00:00
|
|
|
got_sequence_header = false;
|
2015-03-08 09:56:49 +00:00
|
|
|
aac_object = SrsAacObjectTypeReserved;
|
2015-01-18 14:51:07 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 13:50:02 +00:00
|
|
|
SrsAacTransmuxer::~SrsAacTransmuxer()
|
2015-01-18 14:51:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsAacTransmuxer::initialize(SrsFileWriter* fs)
|
2015-01-18 14:51:07 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-18 14:51:07 +00:00
|
|
|
|
|
|
|
srs_assert(fs);
|
|
|
|
|
|
|
|
if (!fs->is_open()) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_error_new(ERROR_KERNEL_AAC_STREAM_CLOSED, "stream is not open");
|
2015-01-18 14:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_fs = fs;
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-01-18 14:51:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsAacTransmuxer::write_audio(int64_t timestamp, char* data, int size)
|
2015-01-18 14:51:07 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-18 14:51:07 +00:00
|
|
|
|
|
|
|
srs_assert(data);
|
|
|
|
|
|
|
|
timestamp &= 0x7fffffff;
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
SrsBuffer* stream = new SrsBuffer(data, size);
|
|
|
|
SrsAutoFree(SrsBuffer, stream);
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-01-18 16:05:12 +00:00
|
|
|
// audio decode
|
|
|
|
if (!stream->require(1)) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac decode audio sound_format failed");
|
2015-01-18 16:05:12 +00:00
|
|
|
}
|
2015-01-18 14:51:07 +00:00
|
|
|
|
2015-01-18 16:05:12 +00:00
|
|
|
// @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
|
|
|
|
int8_t sound_format = stream->read_1bytes();
|
2015-01-18 14:51:07 +00:00
|
|
|
|
2015-01-18 16:05:12 +00:00
|
|
|
//int8_t sound_type = sound_format & 0x01;
|
|
|
|
//int8_t sound_size = (sound_format >> 1) & 0x01;
|
|
|
|
//int8_t sound_rate = (sound_format >> 2) & 0x03;
|
|
|
|
sound_format = (sound_format >> 4) & 0x0f;
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
if ((SrsAudioCodecId)sound_format != SrsAudioCodecIdAAC) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac required, format=%d", sound_format);
|
2015-01-18 16:05:12 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-01-18 16:05:12 +00:00
|
|
|
if (!stream->require(1)) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac decode aac_packet_type failed");
|
2015-01-18 16:05:12 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsAudioAacFrameTrait aac_packet_type = (SrsAudioAacFrameTrait)stream->read_1bytes();
|
|
|
|
if (aac_packet_type == SrsAudioAacFrameTraitSequenceHeader) {
|
2015-01-18 16:05:12 +00:00
|
|
|
// AudioSpecificConfig
|
2017-01-26 09:28:49 +00:00
|
|
|
// 1.6.2.1 AudioSpecificConfig, in ISO_IEC_14496-3-AAC-2001.pdf, page 33.
|
2015-01-18 16:05:12 +00:00
|
|
|
//
|
|
|
|
// only need to decode the first 2bytes:
|
2015-03-08 09:33:52 +00:00
|
|
|
// audioObjectType, 5bits.
|
2015-01-18 16:05:12 +00:00
|
|
|
// samplingFrequencyIndex, aac_sample_rate, 4bits.
|
|
|
|
// channelConfiguration, aac_channels, 4bits
|
|
|
|
if (!stream->require(2)) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac decode sequence header failed");
|
2015-01-18 16:05:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-08 09:33:52 +00:00
|
|
|
int8_t audioObjectType = stream->read_1bytes();
|
2015-01-18 16:05:12 +00:00
|
|
|
aac_sample_rate = stream->read_1bytes();
|
|
|
|
|
|
|
|
aac_channels = (aac_sample_rate >> 3) & 0x0f;
|
2015-03-08 09:33:52 +00:00
|
|
|
aac_sample_rate = ((audioObjectType << 1) & 0x0e) | ((aac_sample_rate >> 7) & 0x01);
|
|
|
|
|
|
|
|
audioObjectType = (audioObjectType >> 3) & 0x1f;
|
2015-03-08 09:56:49 +00:00
|
|
|
aac_object = (SrsAacObjectType)audioObjectType;
|
2015-01-18 16:05:12 +00:00
|
|
|
|
|
|
|
got_sequence_header = true;
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-01-18 16:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!got_sequence_header) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac no sequence header");
|
2015-01-18 16:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// the left is the aac raw frame data.
|
2015-01-18 16:14:14 +00:00
|
|
|
int16_t aac_raw_length = stream->size() - stream->pos();
|
2015-01-18 16:05:12 +00:00
|
|
|
|
|
|
|
// write the ADTS header.
|
2017-01-26 09:28:49 +00:00
|
|
|
// @see ISO_IEC_14496-3-AAC-2001.pdf, page 75,
|
2015-01-18 16:05:12 +00:00
|
|
|
// 1.A.2.2 Audio_Data_Transport_Stream frame, ADTS
|
2015-11-11 02:37:50 +00:00
|
|
|
// @see https://github.com/ossrs/srs/issues/212#issuecomment-64145885
|
2015-01-18 16:05:12 +00:00
|
|
|
// byte_alignment()
|
|
|
|
|
|
|
|
// adts_fixed_header:
|
|
|
|
// 12bits syncword,
|
|
|
|
// 16bits left.
|
|
|
|
// adts_variable_header:
|
|
|
|
// 28bits
|
|
|
|
// 12+16+28=56bits
|
|
|
|
// adts_error_check:
|
|
|
|
// 16bits if protection_absent
|
|
|
|
// 56+16=72bits
|
|
|
|
// if protection_absent:
|
|
|
|
// require(7bytes)=56bits
|
|
|
|
// else
|
|
|
|
// require(9bytes)=72bits
|
|
|
|
char aac_fixed_header[7];
|
|
|
|
if(true) {
|
|
|
|
char* pp = aac_fixed_header;
|
2015-01-18 16:14:14 +00:00
|
|
|
int16_t aac_frame_length = aac_raw_length + 7;
|
2015-01-18 16:05:12 +00:00
|
|
|
|
|
|
|
// Syncword 12 bslbf
|
|
|
|
*pp++ = 0xff;
|
|
|
|
// 4bits left.
|
|
|
|
// adts_fixed_header(), 1.A.2.2.1 Fixed Header of ADTS
|
|
|
|
// ID 1 bslbf
|
|
|
|
// Layer 2 uimsbf
|
|
|
|
// protection_absent 1 bslbf
|
|
|
|
*pp++ = 0xf1;
|
|
|
|
|
2015-03-08 09:33:52 +00:00
|
|
|
// profile 2 uimsbf
|
2015-01-18 16:05:12 +00:00
|
|
|
// sampling_frequency_index 4 uimsbf
|
|
|
|
// private_bit 1 bslbf
|
|
|
|
// channel_configuration 3 uimsbf
|
|
|
|
// original/copy 1 bslbf
|
|
|
|
// home 1 bslbf
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsAacProfile aac_profile = srs_aac_rtmp2ts(aac_object);
|
2015-03-08 09:33:52 +00:00
|
|
|
*pp++ = ((aac_profile << 6) & 0xc0) | ((aac_sample_rate << 2) & 0x3c) | ((aac_channels >> 2) & 0x01);
|
2015-01-18 16:05:12 +00:00
|
|
|
// 4bits left.
|
|
|
|
// adts_variable_header(), 1.A.2.2.2 Variable Header of ADTS
|
|
|
|
// copyright_identification_bit 1 bslbf
|
|
|
|
// copyright_identification_start 1 bslbf
|
|
|
|
*pp++ = ((aac_channels << 6) & 0xc0) | ((aac_frame_length >> 11) & 0x03);
|
|
|
|
|
|
|
|
// aac_frame_length 13 bslbf: Length of the frame including headers and error_check in bytes.
|
|
|
|
// use the left 2bits as the 13 and 12 bit,
|
|
|
|
// the aac_frame_length is 13bits, so we move 13-2=11.
|
|
|
|
*pp++ = aac_frame_length >> 3;
|
|
|
|
// adts_buffer_fullness 11 bslbf
|
|
|
|
*pp++ = (aac_frame_length << 5) & 0xe0;
|
|
|
|
|
|
|
|
// no_raw_data_blocks_in_frame 2 uimsbf
|
|
|
|
*pp++ = 0xfc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write 7bytes fixed header.
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = _fs->write(aac_fixed_header, 7, NULL)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "write aac header");
|
2015-01-18 16:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write aac frame body.
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = _fs->write(data + stream->pos(), aac_raw_length, NULL)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "write aac frame");
|
2015-01-18 16:05:12 +00:00
|
|
|
}
|
2015-01-18 14:51:07 +00:00
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-01-18 14:51:07 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 05:04:28 +00:00
|
|
|
#endif
|
|
|
|
|