| 1 | /********** |
| 2 | This library is free software; you can redistribute it and/or modify it under |
| 3 | the terms of the GNU Lesser General Public License as published by the |
| 4 | Free Software Foundation; either version 3 of the License, or (at your |
| 5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
| 6 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | **********/ |
| 16 | // "liveMedia" |
| 17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
| 18 | // H.264 or H.265 Video File sinks |
| 19 | // Implementation |
| 20 | |
| 21 | #include "H264or5VideoFileSink.hh" |
| 22 | #include "H264VideoRTPSource.hh" // for "parseSPropParameterSets()" |
| 23 | |
| 24 | ////////// H264or5VideoFileSink ////////// |
| 25 | |
| 26 | H264or5VideoFileSink |
| 27 | ::H264or5VideoFileSink(UsageEnvironment& env, FILE* fid, |
| 28 | unsigned bufferSize, char const* perFrameFileNamePrefix, |
| 29 | char const* sPropParameterSetsStr1, |
| 30 | char const* sPropParameterSetsStr2, |
| 31 | char const* sPropParameterSetsStr3) |
| 32 | : FileSink(env, fid, bufferSize, perFrameFileNamePrefix), |
| 33 | fHaveWrittenFirstFrame(False) { |
| 34 | fSPropParameterSetsStr[0] = strDup(sPropParameterSetsStr1); |
| 35 | fSPropParameterSetsStr[1] = strDup(sPropParameterSetsStr2); |
| 36 | fSPropParameterSetsStr[2] = strDup(sPropParameterSetsStr3); |
| 37 | } |
| 38 | |
| 39 | H264or5VideoFileSink::~H264or5VideoFileSink() { |
| 40 | for (unsigned j = 0; j < 3; ++j) delete[] (char*)fSPropParameterSetsStr[j]; |
| 41 | } |
| 42 | |
| 43 | void H264or5VideoFileSink::afterGettingFrame(unsigned frameSize, unsigned numTruncatedBytes, struct timeval presentationTime) { |
| 44 | unsigned char const start_code[4] = {0x00, 0x00, 0x00, 0x01}; |
| 45 | |
| 46 | if (!fHaveWrittenFirstFrame) { |
| 47 | // If we have NAL units encoded in "sprop parameter strings", prepend these to the file: |
| 48 | for (unsigned j = 0; j < 3; ++j) { |
| 49 | unsigned numSPropRecords; |
| 50 | SPropRecord* sPropRecords |
| 51 | = parseSPropParameterSets(fSPropParameterSetsStr[j], numSPropRecords); |
| 52 | for (unsigned i = 0; i < numSPropRecords; ++i) { |
| 53 | if (sPropRecords[i].sPropLength > 0) addData(start_code, 4, presentationTime); |
| 54 | addData(sPropRecords[i].sPropBytes, sPropRecords[i].sPropLength, presentationTime); |
| 55 | } |
| 56 | delete[] sPropRecords; |
| 57 | } |
| 58 | fHaveWrittenFirstFrame = True; // for next time |
| 59 | } |
| 60 | |
| 61 | // Write the input data to the file, with the start code in front: |
| 62 | addData(start_code, 4, presentationTime); |
| 63 | |
| 64 | // Call the parent class to complete the normal file write with the input data: |
| 65 | FileSink::afterGettingFrame(frameSize, numTruncatedBytes, presentationTime); |
| 66 | } |
| 67 | |