OSDN Git Service

ffmpeg-extractor: Fix deadlock when stopping the reader thread
[android-x86/external-stagefright-plugins.git] / extractor / FFmpegExtractor.h
1 /*
2  * Copyright 2012 Michael Chen <omxcodec@gmail.com>
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 SUPER_EXTRACTOR_H_
18
19 #define SUPER_EXTRACTOR_H_
20
21 #include <media/stagefright/foundation/ABase.h>
22 #include <media/stagefright/MediaExtractor.h>
23 #include <utils/threads.h>
24 #include <utils/KeyedVector.h>
25 #include <media/stagefright/MediaSource.h>
26
27 #include "utils/ffmpeg_utils.h"
28
29 namespace android {
30
31 struct ABuffer;
32 struct AMessage;
33 struct String8;
34 struct FFmpegSource;
35
36 struct FFmpegExtractor : public MediaExtractor {
37     FFmpegExtractor(const sp<DataSource> &source, const sp<AMessage> &meta);
38
39     virtual size_t countTracks();
40     virtual sp<MediaSource> getTrack(size_t index);
41     virtual sp<MetaData> getTrackMetaData(size_t index, uint32_t flags);
42
43     virtual sp<MetaData> getMetaData();
44
45     virtual uint32_t flags() const;
46
47 protected:
48     virtual ~FFmpegExtractor();
49
50 private:
51     friend struct FFmpegSource;
52
53     struct TrackInfo {
54         int mIndex; //stream index
55         sp<MetaData> mMeta;
56         AVStream *mStream;
57         PacketQueue *mQueue;
58     };
59
60     Vector<TrackInfo> mTracks;
61
62     mutable Mutex mLock;
63     mutable Mutex mExtractorMutex;
64     Condition mCondition;
65
66     sp<DataSource> mDataSource;
67     sp<MetaData> mMeta;
68     status_t mInitCheck;
69
70     char mFilename[PATH_MAX];
71     int mGenPTS;
72     int mVideoDisable;
73     int mAudioDisable;
74     int mShowStatus;
75     int mSeekByBytes;
76     int mAutoExit;
77     int64_t mStartTime;
78     int64_t mDuration;
79     int mLoop;
80     bool mEOF;
81     size_t mProbePkts;
82
83     int mAbortRequest;
84     int mPaused;
85     int mLastPaused;
86     int mSeekIdx;
87     MediaSource::ReadOptions::SeekMode mSeekMode;
88     int64_t mSeekPos;
89     int64_t mSeekMin;
90     int64_t mSeekMax;
91
92     int mReadPauseReturn;
93     PacketQueue mAudioQ;
94     PacketQueue mVideoQ;
95     bool mVideoEOSReceived;
96     bool mAudioEOSReceived;
97
98     bool mFFmpegInited;
99     AVFormatContext *mFormatCtx;
100     int mVideoStreamIdx;
101     int mAudioStreamIdx;
102     AVStream *mVideoStream;
103     AVStream *mAudioStream;
104     bool mDefersToCreateVideoTrack;
105     bool mDefersToCreateAudioTrack;
106     AVBitStreamFilterContext *mVideoBsfc;
107     AVBitStreamFilterContext *mAudioBsfc;
108
109     static int decode_interrupt_cb(void *ctx);
110     int initStreams();
111     void deInitStreams();
112     void fetchStuffsFromSniffedMeta(const sp<AMessage> &meta);
113     void setFFmpegDefaultOpts();
114     void printTime(int64_t time);
115     bool is_codec_supported(enum AVCodecID codec_id);
116     sp<MetaData> setVideoFormat(AVStream *stream);
117     sp<MetaData> setAudioFormat(AVStream *stream);
118     void setDurationMetaData(AVStream *stream, sp<MetaData> &meta);
119     int stream_component_open(int stream_index);
120     void stream_component_close(int stream_index);
121     void reachedEOS(enum AVMediaType media_type);
122     int stream_seek(int64_t pos, enum AVMediaType media_type,
123             MediaSource::ReadOptions::SeekMode mode);
124     int check_extradata(AVCodecContext *avctx);
125
126     bool mReaderThreadStarted;
127     pthread_t mReaderThread;
128     status_t startReaderThread();
129     void stopReaderThread();
130     static void *ReaderWrapper(void *me);
131     void readerEntry();
132
133     DISALLOW_EVIL_CONSTRUCTORS(FFmpegExtractor);
134 };
135
136 bool SniffFFMPEG(
137         const sp<DataSource> &source, String8 *mimeType, float *confidence,
138         sp<AMessage> *);
139
140 }  // namespace android
141
142 #endif  // SUPER_EXTRACTOR_H_
143