internal void SendMetadata(string app, string path, IStreamConnect self, bool flvHeader = false) { ushort client_id; StreamConnectState state; IStreamConnect connect; var uri = new Uri("http://127.0.0.1/" + path); var key = new Tuple <string, string>(app, uri.AbsolutePath); if (!clientRoute.TryGetValue(key, out client_id)) { throw new KeyNotFoundException("Request Path Not Found"); } if (!connects.TryGetValue(client_id, out state)) { clientRoute.Remove(key); throw new KeyNotFoundException("Request Client Not Exists"); } connect = state.Connect; if (connect.IsPublishing) { var flv_metadata = (Dictionary <string, object>)connect.FlvMetaData.MethodCall.Parameters[0]; var has_audio = flv_metadata.ContainsKey("audiocodecid"); var has_video = flv_metadata.ContainsKey("videocodecid"); if (flvHeader) { var header_buffer = Enumerable.Repeat <byte>(0x00, 13).ToArray <byte>(); header_buffer[0] = 0x46; header_buffer[1] = 0x4C; header_buffer[2] = 0x56; header_buffer[3] = 0x01; byte has_audio_flag = 0x01 << 2; byte has_video_flag = 0x01; byte type_flag = 0x00; if (has_audio) { type_flag |= has_audio_flag; } if (has_video) { type_flag |= has_video_flag; } header_buffer[4] = type_flag; var data_offset = BitConverter.GetBytes((uint)9); header_buffer[5] = data_offset[3]; header_buffer[6] = data_offset[2]; header_buffer[7] = data_offset[1]; header_buffer[8] = data_offset[0]; self.SendRawData(header_buffer); } self.SendAmf0Data(connect.FlvMetaData); if (has_audio) { self.SendAmf0Data(connect.AACConfigureRecord); } if (has_video) { self.SendAmf0Data(connect.AvCConfigureRecord); } } }
/// <summary> /// 将发布者的媒体的meta发给播放者 /// </summary> /// <param name="liveChannel"></param> /// <param name="path"></param> /// <param name="self"></param> /// <param name="flvHeader"></param> internal void SendMetadataToPlayer(string liveChannel, string path, IStreamConnect self, bool flvHeader = false) { ushort publisherID; IStreamConnect publisher; ClientSession publisherState; var uri = new Uri("http://127.0.0.1/" + path); var key = new Tuple <string, string>(liveChannel, uri.AbsolutePath); if (!_clientRouteTable.TryGetValue(key, out publisherID)) { throw new KeyNotFoundException("请求地址不存在~"); } if (!ClientSessions.TryGetValue(publisherID, out publisherState)) { _clientRouteTable.Remove(key); throw new KeyNotFoundException("请求客户端不存在~"); } publisher = publisherState.Connect; if (publisher.IsPublishing) { var flv_metadata = (Dictionary <string, object>)publisher.FlvMetaData.MethodCall.Parameters[0]; var has_audio = flv_metadata.ContainsKey("audiocodecid"); var has_video = flv_metadata.ContainsKey("videocodecid"); if (flvHeader) { var header_buffer = Enumerable.Repeat <byte>(0x00, 13).ToArray <byte>(); header_buffer[0] = 0x46; header_buffer[1] = 0x4C; header_buffer[2] = 0x56; header_buffer[3] = 0x01; byte has_audio_flag = 0x01 << 2; byte has_video_flag = 0x01; byte type_flag = 0x00; if (has_audio) { type_flag |= has_audio_flag; } if (has_video) { type_flag |= has_video_flag; } header_buffer[4] = type_flag; var data_offset = BitConverter.GetBytes((uint)9); header_buffer[5] = data_offset[3]; header_buffer[6] = data_offset[2]; header_buffer[7] = data_offset[1]; header_buffer[8] = data_offset[0]; self.SendRawData(header_buffer); } self.SendAmf0Data(publisher.FlvMetaData); if (has_audio) { self.SendAmf0Data(publisher.AACConfigureRecord); } if (has_video) { self.SendAmf0Data(publisher.AvCConfigureRecord); } } }