import { AACMedia } from '../../../utils/protocols/sdp' ; import { Box , Container } from './isom' ; const AUDIO_OBJECT_TYPE_NAMES = { 1 : 'AAC Main' , 2 : 'AAC LC' , } ; const FREQUENCY_VALUES = { 0 : '96 kHz' , 1 : '88.2 kHz' , 2 : '64 kHz' , 3 : '48 kHz' , 4 : '44.1 kHz' , 5 : '32 kHz' , 6 : '24 kHz' , 7 : '22.05 kHz' , 8 : '16 kHz' , 9 : '12 kHz' , 10 : '11.025 kHz' , 11 : '8 kHz' , 12 : '7.35 kHz' , } ; const CHANNEL_CONFIG_NAMES = { 1 : 'Mono' , 2 : 'Stereo' , } ; export interface AACEncoding { coding : string ; samplingRate : string ; channels : string ; } const aacEncodingName = ( audioConfigBytes ) : AACEncoding => { const audioObjectType = ( audioConfigBytes >>> 11 ) & 0x001f ; const frequencyIndex = ( audioConfigBytes >>> 7 ) & 0x000f ; const channelConfig = ( audioConfigBytes >>> 3 ) & 0x000f ; const audioType = AUDIO_OBJECT_TYPE_NAMES [ audioObjectType ] || `AAC (${ audioObjectType })` ; const samplingRate = FREQUENCY_VALUES [ frequencyIndex ] || 'unknown' ; const channels = CHANNEL_CONFIG_NAMES [ channelConfig ] || channelConfig . toString ( ) ; return { coding : audioType , samplingRate , channels , } ; } ; export const aacSettings = ( media , date , trackId ) => { const bitrate = Number ( media . fmtp . parameters . bitrate ) || 320000 ; const audioConfigBytes = parseInt ( media . fmtp . parameters . config , 16 ) ; const audioObjectType = ( audioConfigBytes >>> 11 ) & 0x001f ; return { tkhd : { track_ID : trackId , creation_time : date , modification_time : date , width : 0 , height : 0 , volume : 1 , } , mdhd : { timescale : Number ( media . rtpmap . clockrate ) , creation_time : date , modification_time : date , duration : 0 , } , hdlr : { handler_type : 'soun' , name : 'SoundHandler\0' , } , mediaHeaderBox : new Box ( 'smhd' ) , sampleEntryBox : new Container ( 'mp4a' , { samplerate : ( media . rtpmap . clockrate << 16 ) >>> 0 , } , new Box ( 'esds' , { audioConfigBytes : audioConfigBytes , maxBitRate : bitrate , avgBitRate : bitrate , } ) ) , defaultFrameDuration : 1024 , mime : `mp4a.40.${ audioObjectType }` , codec : aacEncodingName ( audioConfigBytes ) , } ; } ;