//Server
var
Server: TServer;
PlayStat:bool;
sound:TFileStream;
len:integer;
implementation
{$R *.dfm}
function GetBRate(fname: string): Integer;
const kbps : array[0..14,1..6] of word =
(( 0, 0 , 0 , 0, 0, 0),
( 32, 32, 32, 32, 8, 8),
( 64, 48, 40, 48, 16, 16),
( 96, 56, 48, 56, 24, 24),
(128, 64, 56, 64, 32, 32),
(160, 80, 64, 80, 40, 40),
(192, 96, 80, 96, 48, 48),
(224, 112, 96, 112, 56, 56),
(256, 128, 112, 128, 64, 64),
(288, 160, 128, 144, 80, 80),
(320, 192, 160, 160, 96, 96),
(352, 224, 192, 176, 112, 112),
(384, 256, 224, 192, 128, 128),
(416, 320, 256, 224, 144, 144),
(448, 384, 320, 256, 160, 160));
var f,l: Integer;
buf:array[1..4] of byte;
begin
f:=FileOpen(fname, fmOpenRead);
FileRead(f,buf,4);
if (buf[1]=$49) and (buf[2]=$44)and (buf[3]=$33) then
begin
FileSeek(f,2,1);
FileRead(f,buf,4);
l:=(buf[1] shl 21)+(buf[2] shl 14)+(buf[3] shl 7)+(buf[4]);
FileSeek(f,l,1);
FileRead(f,buf,4);
end;
l:=4-((buf[2] and 6) shr 1);
if (buf[2] and 24) shr 3 <> 3 then l:=l+3;
Result:=kbps[(buf[3] and 240) shr 4,l];
FileClose(f);
end;
procedure TServer.addClick(Sender: TObject);
begin
Open.Execute;
Playlist.Lines.Add(Open.FileName);
len:=GetBRate(open.FileName);
sound:=TFileStream.Create(open.FileName,fmOpenRead);
end;
procedure TServer.StartRadioClick(Sender: TObject);
begin
if Tag=0 then
begin
startRadio.Caption:='stopRadio';
Server.Active:=true;
PlayStat:=true;
Tag:=1;
end
else
begin
startRadio.Caption:='startRadio';
Server.Active:=false;
client.Active:=false;
Tag:=0;
end;
end;
procedure TServer.ServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
msg: TStringList;
tmp,ip,port:string;
i,j,buf:integer;
begin
msg:=TStringList.Create;
msg.LoadFromStream(AData);
tmp:=msg.GetText;
if tmp<>'' then
begin
for i:=0 to length(tmp) do
begin
while tmp<>':'do
begin
ip:=tmp;
buf:=i;
end;
for j:=buf+1 to length(tmp) do
port[j]:=tmp[j];
end;
client.Host:=ip;
client.Port:=StrToInt(port);
client.Active:=true;
timer.Enabled:=true;
end;
end;
procedure TServer.TimerTimer(Sender: TObject);
var
buf: PChar;
i:integer;
begin
if PlayStat=true then begin
sound.Position:=0;
buf:=AllocMem(len);
if sound.Position< sound.Size then
i:= sound.Read(buf^,len);
Client.SendBuffer(client.Host,client.Port,buf^,i);
end;
end;
end.
Трансляция аудио
Проблема состоит в том,что приложение(я) либо не передают(принимают) данные либо просто не воспроизводится.
Вот код который уже написал. Просьба помочь разобраться в чем проблема.
Код:
Код:
//Client
var
Form1: TForm1;
StrBuf: PChar;
RStrPos, LStrPos, BufLen: Integer ;
Playing:Boolean;
Stream:TFileStream;
implementation
{$R *.dfm}
function RdBuf(action: DWORD; param1: DWORD; param2: DWORD; user: DWORD): DWORD; stdcall;
var
buf: ^Char;
tmp: PChar;
i: Integer;
begin
case action of
BASS_FILE_LEN: Result:=0;
BASS_FILE_READ:
begin
buf := Pointer(param2);
i:=0;
repeat
tmp :=StrBuf+LStrPos;
buf^:=tmp^;
Inc(LStrPos);
Inc(buf);
Inc(i);
if LStrPos>BufLen then LStrPos:=0;
until (i=param1) or (LStrPos=RStrPos);
Result:=i;
end;
BASS_FILE_QUERY: if LStrPos=RStrPos then Result:=0
else Result:=param1;
BASS_FILE_CLOSE: Playing:=False;
end;
end;
procedure TForm1.PlayClick(Sender: TObject);
begin
if Tag=0 then
begin
play.Caption:='Stop';
client.Host:=ip.Text;
server.Active:=true;
client.Active:=true;
client.Send(ip.Text,client.Port,'127.0.0.1:5555');
Tag:=1;
end
else
begin
play.Caption:='Play';
server.Active:=false;
client.Active:=false;
Tag:=0;
end;
end;
procedure TForm1.ServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
tmp: PChar;
len:integer;
begin
len:=AData.Size;
tmp :=StrBuf+RStrPos;
if BufLen-RStrPos < len then
begin
AData.Read(tmp^,BufLen-RStrPos);
RStrPos:=Stream.Read(StrBuf^,len-BufLen+RStrPos)-1;
end
else
begin
RStrPos:=RStrPos+Stream.Read(tmp^,len);
end;
timer.Enabled:=true;
end;
procedure TForm1.TimerTimer(Sender: TObject);
var
OutStream: HSTREAM;
begin
BASS_Init(1,44100,0,Application.Handle,nil);
OutStream:=BASS_StreamCreateFileUser(true, BASS_STREAM_BLOCK, @RdBuf, 0);
BASS_ChannelPlay(OutStream, False);
timer.Enabled:=false;
end;
end.
var
Form1: TForm1;
StrBuf: PChar;
RStrPos, LStrPos, BufLen: Integer ;
Playing:Boolean;
Stream:TFileStream;
implementation
{$R *.dfm}
function RdBuf(action: DWORD; param1: DWORD; param2: DWORD; user: DWORD): DWORD; stdcall;
var
buf: ^Char;
tmp: PChar;
i: Integer;
begin
case action of
BASS_FILE_LEN: Result:=0;
BASS_FILE_READ:
begin
buf := Pointer(param2);
i:=0;
repeat
tmp :=StrBuf+LStrPos;
buf^:=tmp^;
Inc(LStrPos);
Inc(buf);
Inc(i);
if LStrPos>BufLen then LStrPos:=0;
until (i=param1) or (LStrPos=RStrPos);
Result:=i;
end;
BASS_FILE_QUERY: if LStrPos=RStrPos then Result:=0
else Result:=param1;
BASS_FILE_CLOSE: Playing:=False;
end;
end;
procedure TForm1.PlayClick(Sender: TObject);
begin
if Tag=0 then
begin
play.Caption:='Stop';
client.Host:=ip.Text;
server.Active:=true;
client.Active:=true;
client.Send(ip.Text,client.Port,'127.0.0.1:5555');
Tag:=1;
end
else
begin
play.Caption:='Play';
server.Active:=false;
client.Active:=false;
Tag:=0;
end;
end;
procedure TForm1.ServerUDPRead(Sender: TObject; AData: TStream;
ABinding: TIdSocketHandle);
var
tmp: PChar;
len:integer;
begin
len:=AData.Size;
tmp :=StrBuf+RStrPos;
if BufLen-RStrPos < len then
begin
AData.Read(tmp^,BufLen-RStrPos);
RStrPos:=Stream.Read(StrBuf^,len-BufLen+RStrPos)-1;
end
else
begin
RStrPos:=RStrPos+Stream.Read(tmp^,len);
end;
timer.Enabled:=true;
end;
procedure TForm1.TimerTimer(Sender: TObject);
var
OutStream: HSTREAM;
begin
BASS_Init(1,44100,0,Application.Handle,nil);
OutStream:=BASS_StreamCreateFileUser(true, BASS_STREAM_BLOCK, @RdBuf, 0);
BASS_ChannelPlay(OutStream, False);
timer.Enabled:=false;
end;
end.
Ну а суть-то тут в чем?
Не работает.) Не могу понять почему!