1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MPEG2TS_WRITER_H_
18 
19 #define MPEG2TS_WRITER_H_
20 
21 #include <media/stagefright/foundation/ABase.h>
22 #include <media/stagefright/foundation/AHandlerReflector.h>
23 #include <media/stagefright/foundation/ALooper.h>
24 #include <media/stagefright/MediaWriter.h>
25 
26 namespace android {
27 
28 struct ABuffer;
29 
30 struct MPEG2TSWriter : public MediaWriter {
31     MPEG2TSWriter(int fd);
32 
33     MPEG2TSWriter(
34             void *cookie,
35             ssize_t (*write)(void *cookie, const void *data, size_t size));
36 
37     virtual status_t addSource(const sp<MediaSource> &source);
38     virtual status_t start(MetaData *param = NULL);
stopMPEG2TSWriter39     virtual status_t stop() { return reset(); }
40     virtual status_t pause();
41     virtual bool reachedEOS();
42     virtual status_t dump(int fd, const Vector<String16>& args);
43 
44     void onMessageReceived(const sp<AMessage> &msg);
45 
46 protected:
47     virtual ~MPEG2TSWriter();
48 
49 private:
50     enum {
51         kWhatSourceNotify = 'noti'
52     };
53 
54     struct SourceInfo;
55 
56     FILE *mFile;
57 
58     void *mWriteCookie;
59     ssize_t (*mWriteFunc)(void *cookie, const void *data, size_t size);
60 
61     sp<ALooper> mLooper;
62     sp<AHandlerReflector<MPEG2TSWriter> > mReflector;
63 
64     bool mStarted;
65 
66     Vector<sp<SourceInfo> > mSources;
67     size_t mNumSourcesDone;
68 
69     int64_t mNumTSPacketsWritten;
70     int64_t mNumTSPacketsBeforeMeta;
71     int mPATContinuityCounter;
72     int mPMTContinuityCounter;
73     uint32_t mCrcTable[256];
74 
75     void init();
76 
77     void writeTS();
78     void writeProgramAssociationTable();
79     void writeProgramMap();
80     void writeAccessUnit(int32_t sourceIndex, const sp<ABuffer> &buffer);
81     void initCrcTable();
82     uint32_t crc32(const uint8_t *start, size_t length);
83 
84     ssize_t internalWrite(const void *data, size_t size);
85     status_t reset();
86 
87     DISALLOW_EVIL_CONSTRUCTORS(MPEG2TSWriter);
88 };
89 
90 }  // namespace android
91 
92 #endif  // MPEG2TS_WRITER_H_
93