Solved

Stream video or Jpeg across internet

Posted on 2001-07-03
13
852 Views
Last Modified: 2008-03-17
I want to be able to stream video (PVIDEOHDR) or jpeg  across the internet.

I have some code working (using socket components), but the full jpeg is not getting delivered. across the internet maybe only 10% of the picture is "decoded" correctly, over a local lan (10 speed), if i make the compression of the jpeg 50% or less, it displays correctly, anything above that gives jpeg error #52 on the client end.

Dropping the frame rate seems to have no effect on the display, can anyone give me working code ?.

PS : I am using the TVideoCap Version 2.3 component to capture the video at the server end.
0
Comment
Question by:justcomputers
  • 6
  • 6
13 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6247951
I have 2 examples of sending a stream over TCP/IP. One is non-blocking the other is blocking (faster but more difficult). They both transfer files but it won't be difficult to have it loading a jpeg (or something else) from memory into a stream and transfer that.
0
 

Author Comment

by:justcomputers
ID: 6249065
Can you post the code here or email it as an attatchment so i can see if it works ?.

Thanks.

Alternativley i can post my code here, then you can see where my code is faulty.


0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6249766
Post to where..?

If it's not too big the show me your code.
0
 

Author Comment

by:justcomputers
ID: 6250046
Post to experts-exchange if possible.

OK here is my code.

******************** client socket read **************
procedure TForm1.ClientSocketRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
 Memorystream: TMemoryStream;
 JPEG : TJPEGImage;
 Buffer: Array[0..10000] of byte;
 Received: Integer;
begin

     try

             MemoryStream := TMemoryStream.Create;
             Received := socket.ReceiveBuf(Buffer, sizeof(Buffer));
             Memorystream.Write(Buffer,Received);
             Memorystream.Seek(0,0);
             cledit2.Text := inttostr(Received);
             JPEG := TJpegImage.Create;

              try
                        JPeg.LoadFromStream(MemoryStream);
              except;
                        exit;
                {Ive placed this exception here just to get rid of those annoying messgaes}
              end;

             Image1.Width:= JPEG.Width;
             CLEditImageHeight.Text := inttostr(JPEG.Height);
             CLEditImageWidth.Text := inttostr(JPEG.Width);
             Image1.Height:= JPEG.Height;
             image1.Picture.Assign(Jpeg);


     finally

             Memorystream.free;
             Jpeg.free;
     end;

     end;

*****************  end of client socket read ************




******  start of onvideostream event of videocap *********


procedure TForm1.VideoCap1VideoStream(sender: TObject; lpVhdr: PVIDEOHDR);
var Bitmap:TBitmap;
    info:TBitmapInfo;
    j : TJPegImage;
    MemoryStream:  TMemoryStream;
begin

try


       
        inc(Frames);
        Editframes.text := inttostr(Frames);


        j:=TJPegImage.create;
        info:= videocap1.BitMapInfo;

        Bitmap:=TBitmap.Create;

       
        frameToBitmap(Bitmap,lpvhdr^.lpData,Info);


        j.assign(Bitmap);
        J.CompressionQuality :=CompressionValue;// Anything above 50 causes corruption

        J.Compress;
        Image1.Picture.Assign(J);
        MemoryStream := TMemoryStream.Create;
        Memorystream.Seek(0,0);
        J.SaveToStream(MemoryStream);
        Memorystream.Seek(0,0);
        ServerSocket.Socket.Connections[0].sendstream(MemoryStream);
finally;

        J.free;
        Bitmap.free;
end;
end;

0
 

Expert Comment

by:bloodline
ID: 6250423
//Bloodline Listening
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6250707
Hi, the way you receive the packets is not right. It seems that everyone makes the same mistakes  ;o)

The best thing you should do is sending the number of bytes to send first. This is because the receiving part can get multiple OnRead events. This means that the first packet contains the size (let's say 4 or 8 bytes) and some data. The second packet has more data and so on until the number of received bytes equals the size received at the beginning of the first packet.
If you are sending a continuous stream of data (e.g. multiple image), you must take into account that the size of the n-th image can be in the middle of a packet. So if image n-1 has been received, don't throw away the date after the last byte of image n-1.
Use some FIFO queue or something like that during the life of the connection.

I hope this clears things up a little.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:justcomputers
ID: 6251612
Yes, it's more or less what i had guessed at, but do you have any example code, i cant find any examples to work from, and this is my first time working with sockets.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6251853
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 200 total points
ID: 6261782
Hello?
0
 

Author Comment

by:justcomputers
ID: 6262154
Thanks for the examples, i only got an email telling me of your post on Sunday morning @ 08:00, i am looking at the code now, and will get back within a day or 2, but it certainly looks like you will be getting the points.

Thanks very much for your help.
0
 

Author Comment

by:justcomputers
ID: 6262869
Thanks Epsylon, your code did the trick so here are the points, thanks very much once again, you are a star.

PS :- Do you know where i can find decent documentation explaining sockets, and how your code works.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6262916
So far I haven't found any good documentation for Delphi's components. Most of it I found out myself using the knowlegde I have since 1990.

delphi.about.com has a search engine which is the best option I can think of right now.
0
 

Author Comment

by:justcomputers
ID: 6263290
OK Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now