我需要播放来自实时流的 AAC LC 音频。为了实现这一点,我已经实现了 MediaStreamSource。
当我收到第一个流数据包时,我将 MediaElement 的源设置为我的 MediaStreamSource。似乎一切正常:调用 OpenMediaAsync -> 使用 ReportOpenMediaCompleted 报告,然后调用 GetSampleAsync -> 使用 ReportGetSampleCompleted 报告,但是,在第 10 次调用 GetSampleAsync 时,ReportGetSampleCompleted 抛出 NullReferenceException。
这是我的 CodecPrivateData:
var waveFormat = new AACWaveFormat();
waveFormat.FormatTag = 0xFF;
waveFormat.Channels = 2; // For my stream is always stereo
waveFormat.Frequency = 44100; //For my stream is always 44Khz
waveFormat.BitsPerSample = 16; //For my stream is always 16bit
waveFormat.BlockAlign = waveFormat.Channels * waveFormat.BitsPerSample / 8; //is this true formula?
waveFormat.AverageBytesPerSecond = waveFormat.Frequency * waveFormat.BlockAlign; //is this true formula? because usually this value is 176400 or 1411Kbps is this real value for sound?
waveFormat.ExtraDataSize = 2; //usually, but i read these values from first packet of stream
waveFormat.ExtraData = AudioSpecificConfig; //AudioSpecificConfig usually 2 bytes length, readed from stream.
流的第一个数据包始终是 AACSequenceHeader - 我在其中读取我的 CodecPrivateData 和 AudioSpecificConfig。其余的都是 AACraw。
我的 CodecPrivateData 看起来像
FF00020044AC000010B102000400100002001210
.
我的 GetSampleAsync
protected override void GetSampleAsync(MediaStreamType mediaStreamType)
{
var audioStreamDescription = new MediaStreamDescription(MediaStreamType.Audio, AudioStreamAttibutes); //AudioStreamAttibutes is field that contains data filled on OpenMediaAsync step.
//using (var memoryStream = new MemoryStream(AudioPackets[0].Data))
var memoryStream = new MemoryStream(AudioPackets[0].Data);
ReportGetSampleCompleted(new MediaStreamSample(audioStreamDescription, memoryStream, 0, AudioPackets[0].Data.Length, TimeSpan.FromMilliseconds(GetAudioSampleCalls++ * 32).Ticks, new Dictionary<MediaSampleAttributeKeys, String>())); //throws NullReferenceException, when debugger stops i can be see that all passed params is not null!
}
这里的问题是我不知道任何时间戳,也不知道这是否是个问题。
最后,什么是Data
场?Data
字段包含Byte[]
我从 AudioTag 中提取的所有接收到的 RawAAC 音频。E.4.2.2 AACAUDIODATA
(参见http://download.macromedia.com/f4v/video_file_format_spec_v10_1.pdf)