Using the RTMP_Write Function

I am trying to use the librtmp library and it works great to pull a stream. But now I am trying to publish a stream, and for this I believe that I need to use the RTMP_Write function.

What I'm trying to do here is a simple C ++ program that will read from a file and try to push the stream to the crtmp server. Connecting and creating the stream is fine, but I'm pretty puzzled using RTMP_Write.

Here is what I did:

int Upload(RTMP * rtmp, FILE * file){ int nRead = 0; unsigned int nWrite = 0; int diff = 0; int bufferSize = 64 * 1024; int byteSum = 0; int count = 0; char * buffer; buffer = (char *) malloc(bufferSize); do{ nRead = fread(buffer+diff,1,bufferSize-diff,file); if(nRead != bufferSize){ if(feof(file)){ RTMP_LogPrintf("End of file reached!\n"); break; }else if(ferror(file)){ RTMP_LogPrintf("Error reading from file stream detected\n"); break; } } count += 1; byteSum += nRead; RTMP_LogPrintf("Read %d from file, Sum: %d, Count: %d\n",nRead,byteSum,count); nWrite = RTMP_Write(rtmp,buffer,nRead); if(nWrite != nRead){ diff = nRead - nWrite; memcpy(buffer,(const void*)(buffer+bufferSize-diff),diff); } }while(!RTMP_ctrlC && RTMP_IsConnected(rtmp) && !RTMP_IsTimedout(rtmp)); free(buffer); return RD_SUCCESS; } 

In this download function, I get an already initialized RTMP structure and a pointer to an open file.

It really works, and I see some video being displayed, but it is soon lost and stops sending packets. I was able to understand that this happens whenever there is no separate reason for the buffer that I configure (and which I accidentally had to be 64 KB, there is no special reason for this) ( http://osflash.org/flv# flv_format ) of the new package.

To do this, I modified the RTMP_Write function and told her to check if he could decode the entire flv tag (packet type, body size, timestamp, etc.), and if not, then it should just return the number of useful bytes remaining in the buffer.

  if(s2 - 11 <= 0){ rest = size - s2; return rest; } 

The above code notices this, and if the value returned by RTMP_Write is not the number of bytes it should have sent, then it knows that the value is the number of usable bytes left in the buffer. Then I copy these bytes to the beginning of the buffer and read more from the file.

But I always have problems with this, so I was wondering: what is the proper use of this function? Is there a specific buffer value that I should use? (right) or is he wrong?

+6
source share

Source: https://habr.com/ru/post/912340/


All Articles