liveMedia/H264VideoFileSink.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2012 Live Networks, Inc.  All rights reserved.
00018 // H.264 Video File sinks
00019 // Implementation
00020 
00021 #include "H264VideoFileSink.hh"
00022 #include "OutputFile.hh"
00023 #include "H264VideoRTPSource.hh"
00024 
00026 
00027 H264VideoFileSink
00028 ::H264VideoFileSink(UsageEnvironment& env, FILE* fid,
00029                     char const* sPropParameterSetsStr,
00030                     unsigned bufferSize, char const* perFrameFileNamePrefix)
00031   : FileSink(env, fid, bufferSize, perFrameFileNamePrefix),
00032     fSPropParameterSetsStr(sPropParameterSetsStr), fHaveWrittenFirstFrame(False) {
00033 }
00034 
00035 H264VideoFileSink::~H264VideoFileSink() {
00036 }
00037 
00038 H264VideoFileSink*
00039 H264VideoFileSink::createNew(UsageEnvironment& env, char const* fileName,
00040                              char const* sPropParameterSetsStr,
00041                              unsigned bufferSize, Boolean oneFilePerFrame) {
00042   do {
00043     FILE* fid;
00044     char const* perFrameFileNamePrefix;
00045     if (oneFilePerFrame) {
00046       // Create the fid for each frame
00047       fid = NULL;
00048       perFrameFileNamePrefix = fileName;
00049     } else {
00050       // Normal case: create the fid once
00051       fid = OpenOutputFile(env, fileName);
00052       if (fid == NULL) break;
00053       perFrameFileNamePrefix = NULL;
00054     }
00055 
00056     return new H264VideoFileSink(env, fid, sPropParameterSetsStr, bufferSize, perFrameFileNamePrefix);
00057   } while (0);
00058 
00059   return NULL;
00060 }
00061 
00062 void H264VideoFileSink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime) {
00063   unsigned char const start_code[4] = {0x00, 0x00, 0x00, 0x01};
00064 
00065   if (!fHaveWrittenFirstFrame) {
00066     // If we have PPS/SPS NAL units encoded in a "sprop parameter string", prepend these to the file:
00067     unsigned numSPropRecords;
00068     SPropRecord* sPropRecords = parseSPropParameterSets(fSPropParameterSetsStr, numSPropRecords);
00069     for (unsigned i = 0; i < numSPropRecords; ++i) {
00070       addData(start_code, 4, presentationTime);
00071       addData(sPropRecords[i].sPropBytes, sPropRecords[i].sPropLength, presentationTime);
00072     }
00073     delete[] sPropRecords;
00074     fHaveWrittenFirstFrame = True; // for next time
00075   }
00076 
00077   // Write the input data to the file, with the start code in front:
00078   addData(start_code, 4, presentationTime);
00079 
00080   // Call the parent class to complete the normal file write with the input data:
00081   FileSink::afterGettingFrame(frameSize, numTruncatedBytes, presentationTime);
00082 }

Generated on Thu May 10 20:25:11 2012 for live by  doxygen 1.5.2