[Live-devel] can not mix a 264 file and a aac file together to a ts file

jun li lijun at ndtv.com.cn
Wed Feb 13 17:51:57 PST 2019


I modified testH264VideoToTransportStream.cpp for mix two stream files, but when I play ts file only have video.
If  only have a video convert to TS is ok.
If only have a audio convert to TS only have very few seconds, but extract aac from this ts file and play it use VLC alone is ok!

#include "MyADTSAudioFileSource.h"
…
std::string  inputFileNameAAC = "d:\\rango.track_68.aac";
std::string  inputFileName = "d:\\rango.track_69.264";
std::string outputFileName = "d:\\rango.track_.ts";
.. ..
//open the file is ok
FramedSource* inputSourceAAC = MyADTSAudioFileSource::createNew(*env, inputFileNameAAC.c_str());

FramedSource* inputSource = ByteStreamFileSource::createNew(*env, inputFileName.c_str());
H264VideoStreamFramer* framer = H264VideoStreamFramer::createNew(*env, inputSource, True/*includeStartCodeInOutput*/);

MPEG2TransportStreamFromESSource* tsFrames = MPEG2TransportStreamFromESSource::createNew(*env);
tsFrames->addNewVideoSource(framer, 5/*mpegVersion: H.264*/);
tsFrames->addNewAudioSource(inputSourceAAC, 4/*mpegVersion: AAC*/);

MediaSink* outputSink = FileSink::createNew(*env, outputFileName.c_str());
outputSink->startPlaying(*tsFrames, cbAfterPlaying, NULL);

env->taskScheduler().doEventLoop(); 
…


I referenced ADTSAudioFileSource.cpp to create MyADTSAudioFileSouce class for not ignore ADTS Header
void MyADTSAudioFileSource::doGetNextFrame() {
      // Begin by reading the 7-byte fixed_variable headers:
      unsigned char headers[7];
      if (fread(headers, 1, sizeof headers, fFid) < sizeof headers
            || feof(fFid) || ferror(fFid)) {
            // The input source has ended:
            handleClosure();
            return;
      }

      // Extract important fields from the headers:
      Boolean protection_absent = headers[1] & 0x01;
      u_int16_t frame_length
            = ((headers[3] & 0x03) << 11) | (headers[4] << 3) | ((headers[5] & 0xE0) >> 5);
#ifdef DEBUG
      u_int16_t syncword = (headers[0] << 4) | (headers[1] >> 4);
      fprintf(stderr, "Read frame: syncword 0x%x, protection_absent %d, frame_length %d\n", syncword, protection_absent, frame_length);
      if (syncword != 0xFFF) fprintf(stderr, "WARNING: Bad syncword!\n");
#endif
      /*
      unsigned numBytesToRead
            = frame_length > sizeof headers ? frame_length - sizeof headers : 0;
      */
      unsigned numBytesToRead
            = frame_length > sizeof headers ? frame_length : 0;

      // If there's a 'crc_check' field, skip it:
      //if (!protection_absent) {
      //    SeekFile64(fFid, 2, SEEK_CUR);
      //    numBytesToRead = numBytesToRead > 2 ? numBytesToRead - 2 : 0;
      //}

      // Next, read the raw frame data into the buffer provided:
      if (numBytesToRead > fMaxSize) {
            fNumTruncatedBytes = numBytesToRead - fMaxSize;
            numBytesToRead = fMaxSize;
      }
      {
            int offset = 0 - sizeof(headers);
            SeekFile64(fFid, offset, SEEK_CUR);
      }
      int numBytesRead = fread(fTo, 1, numBytesToRead, fFid);
      if (numBytesRead < 0) numBytesRead = 0;
      fFrameSize = numBytesRead;
      fNumTruncatedBytes += numBytesToRead - numBytesRead;

      // Set the 'presentation time':
      if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec == 0) {
            // This is the first frame, so use the current time:
            gettimeofday(&fPresentationTime, NULL);
      }
      else {
            // Increment by the play time of the previous frame:
            unsigned uSeconds = fPresentationTime.tv_usec + fuSecsPerFrame;
            fPresentationTime.tv_sec += uSeconds / 1000000;
            fPresentationTime.tv_usec = uSeconds % 1000000;
      }

      fDurationInMicroseconds = fuSecsPerFrame;

      // Switch to another task, and inform the reader that he has data:
      nextTask() = envir().taskScheduler().scheduleDelayedTask(fDurationInMicroseconds,
            (TaskFunc*)FramedSource::afterGetting, this);
}

I think maybe fPresentationTime is not synchronized between audio and video,  but how to modify code? 
The entire MyADTSAudioFileSource.cpp and other relation files in attachment.

Sent from Mail for Windows 10

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20190214/a519e929/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MyADTSAudioFileSource.cpp
Type: application/octet-stream
Size: 4797 bytes
Desc: not available
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20190214/a519e929/attachment-0004.obj>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: MyADTSAudioFileSource.h
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20190214/a519e929/attachment-0001.h>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testH264VideoToTransportStream.cpp
Type: application/octet-stream
Size: 3218 bytes
Desc: not available
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20190214/a519e929/attachment-0005.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rango.track_68.aac
Type: application/octet-stream
Size: 295797 bytes
Desc: not available
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20190214/a519e929/attachment-0006.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rango.track_69.264
Type: application/octet-stream
Size: 3017616 bytes
Desc: not available
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20190214/a519e929/attachment-0007.obj>


More information about the live-devel mailing list