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 | // A sink that generates an AVI file from a composite media session |
19 | // C++ header |
20 | |
21 | #ifndef _AVI_FILE_SINK_HH |
22 | #define _AVI_FILE_SINK_HH |
23 | |
24 | #ifndef _MEDIA_SESSION_HH |
25 | #include "MediaSession.hh" |
26 | #endif |
27 | |
28 | class AVIFileSink: public Medium { |
29 | public: |
30 | static AVIFileSink* createNew(UsageEnvironment& env, |
31 | MediaSession& inputSession, |
32 | char const* outputFileName, |
33 | unsigned bufferSize = 20000, |
34 | unsigned short movieWidth = 240, |
35 | unsigned short movieHeight = 180, |
36 | unsigned movieFPS = 15, |
37 | Boolean packetLossCompensate = False); |
38 | |
39 | typedef void (afterPlayingFunc)(void* clientData); |
40 | Boolean startPlaying(afterPlayingFunc* afterFunc, |
41 | void* afterClientData); |
42 | |
43 | unsigned numActiveSubsessions() const { return fNumSubsessions; } |
44 | |
45 | private: |
46 | AVIFileSink(UsageEnvironment& env, MediaSession& inputSession, |
47 | char const* outputFileName, unsigned bufferSize, |
48 | unsigned short movieWidth, unsigned short movieHeight, |
49 | unsigned movieFPS, Boolean packetLossCompensate); |
50 | // called only by createNew() |
51 | virtual ~AVIFileSink(); |
52 | |
53 | Boolean continuePlaying(); |
54 | static void afterGettingFrame(void* clientData, unsigned frameSize, |
55 | unsigned numTruncatedBytes, |
56 | struct timeval presentationTime, |
57 | unsigned durationInMicroseconds); |
58 | static void onSourceClosure(void* clientData); |
59 | void onSourceClosure1(); |
60 | static void onRTCPBye(void* clientData); |
61 | void addIndexRecord(class AVIIndexRecord* newIndexRecord); |
62 | void completeOutputFile(); |
63 | |
64 | private: |
65 | friend class AVISubsessionIOState; |
66 | MediaSession& fInputSession; |
67 | FILE* fOutFid; |
68 | class AVIIndexRecord *fIndexRecordsHead, *fIndexRecordsTail; |
69 | unsigned fNumIndexRecords; |
70 | unsigned fBufferSize; |
71 | Boolean fPacketLossCompensate; |
72 | Boolean fAreCurrentlyBeingPlayed; |
73 | afterPlayingFunc* fAfterFunc; |
74 | void* fAfterClientData; |
75 | unsigned fNumSubsessions; |
76 | unsigned fNumBytesWritten; |
77 | struct timeval fStartTime; |
78 | Boolean fHaveCompletedOutputFile; |
79 | |
80 | private: |
81 | ///// Definitions specific to the AVI file format: |
82 | |
83 | unsigned addWord(unsigned word); // outputs "word" in little-endian order |
84 | unsigned addHalfWord(unsigned short halfWord); |
85 | unsigned addByte(unsigned char byte) { |
86 | putc(byte, fOutFid); |
87 | return 1; |
88 | } |
89 | unsigned addZeroWords(unsigned numWords); |
90 | unsigned add4ByteString(char const* str); |
91 | void setWord(unsigned filePosn, unsigned size); |
92 | |
93 | // Define member functions for outputting various types of file header: |
94 | #define (name) unsigned addFileHeader_##name() |
95 | _header(AVI); |
96 | _header(hdrl); |
97 | _header(avih); |
98 | _header(strl); |
99 | _header(strh); |
100 | _header(strf); |
101 | _header(JUNK); |
102 | // _header(JUNK); |
103 | _header(movi); |
104 | private: |
105 | unsigned short fMovieWidth, fMovieHeight; |
106 | unsigned fMovieFPS; |
107 | unsigned fRIFFSizePosition, fRIFFSizeValue; |
108 | unsigned fAVIHMaxBytesPerSecondPosition; |
109 | unsigned fAVIHFrameCountPosition; |
110 | unsigned fMoviSizePosition, fMoviSizeValue; |
111 | class AVISubsessionIOState* fCurrentIOState; |
112 | unsigned fJunkNumber; |
113 | }; |
114 | |
115 | #endif |
116 | |