@@ -57,67 +57,63 @@ MediaPlayer::~MediaPlayer()
5757status_t MediaPlayer::prepareAudio ()
5858{
5959 __android_log_print (ANDROID_LOG_INFO, TAG, " prepareAudio" );
60- mFFmpegStorage .audio .stream = -1 ;
61- for (int i = 0 ; i < mFFmpegStorage .pFormatCtx ->nb_streams ; i++) {
62- if (mFFmpegStorage .pFormatCtx ->streams [i]->codec ->codec_type == CODEC_TYPE_AUDIO) {
63- mFFmpegStorage .audio .stream = i;
64- }
65- if (mFFmpegStorage .audio .stream != -1 ) {
60+ mAudioStreamIndex = -1 ;
61+ for (int i = 0 ; i < mMovieFile ->nb_streams ; i++) {
62+ if (mMovieFile ->streams [i]->codec ->codec_type == CODEC_TYPE_AUDIO) {
63+ mAudioStreamIndex = i;
6664 break ;
6765 }
6866 }
6967
70- if (mFFmpegStorage . audio . stream == -1 ) {
68+ if (mAudioStreamIndex == -1 ) {
7169 return INVALID_OPERATION;
7270 }
7371
7472 // Get a pointer to the codec context for the video stream
75- mFFmpegStorage . audio . codec_ctx = mFFmpegStorage . pFormatCtx ->streams [mFFmpegStorage . audio . stream ]->codec ;
76- mFFmpegStorage . audio . codec = avcodec_find_decoder (mFFmpegStorage . audio . codec_ctx ->codec_id );
77- if (mFFmpegStorage . audio . codec == NULL ) {
73+ AVCodecContext* codec_ctx = mMovieFile ->streams [mAudioStreamIndex ]->codec ;
74+ AVCodec* codec = avcodec_find_decoder (codec_ctx->codec_id );
75+ if (codec == NULL ) {
7876 return INVALID_OPERATION;
7977 }
8078
8179 // Open codec
82- if (avcodec_open (mFFmpegStorage . audio . codec_ctx ,
83- mFFmpegStorage . audio . codec ) < 0 ) {
84- return INVALID_OPERATION; }
80+ if (avcodec_open (codec_ctx, codec) < 0 ) {
81+ return INVALID_OPERATION;
82+ }
8583 return NO_ERROR;
8684}
8785
8886status_t MediaPlayer::prepareVideo ()
8987{
9088 __android_log_print (ANDROID_LOG_INFO, TAG, " prepareVideo" );
9189 // Find the first video stream
92- mFFmpegStorage .video .stream = -1 ;
93- for (int i = 0 ; i < mFFmpegStorage .pFormatCtx ->nb_streams ; i++) {
94- if (mFFmpegStorage .pFormatCtx ->streams [i]->codec ->codec_type == CODEC_TYPE_VIDEO) {
95- mFFmpegStorage .video .stream = i;
96- }
97- if (mFFmpegStorage .video .stream != -1 ) {
90+ mVideoStreamIndex = -1 ;
91+ for (int i = 0 ; i < mMovieFile ->nb_streams ; i++) {
92+ if (mMovieFile ->streams [i]->codec ->codec_type == CODEC_TYPE_VIDEO) {
93+ mVideoStreamIndex = i;
9894 break ;
9995 }
10096 }
10197
102- if (mFFmpegStorage . video . stream == -1 ) {
98+ if (mVideoStreamIndex == -1 ) {
10399 return INVALID_OPERATION;
104100 }
105101
106102 // Get a pointer to the codec context for the video stream
107- mFFmpegStorage . video . codec_ctx = mFFmpegStorage . pFormatCtx ->streams [mFFmpegStorage . video . stream ]->codec ;
108- mFFmpegStorage . video . codec = avcodec_find_decoder (mFFmpegStorage . video . codec_ctx ->codec_id );
109- if (mFFmpegStorage . video . codec == NULL ) {
103+ AVCodecContext* codec_ctx = mMovieFile ->streams [mVideoStreamIndex ]->codec ;
104+ AVCodec* codec = avcodec_find_decoder (codec_ctx->codec_id );
105+ if (codec == NULL ) {
110106 return INVALID_OPERATION;
111107 }
112108
113109 // Open codec
114- if (avcodec_open (mFFmpegStorage . video . codec_ctx , mFFmpegStorage . video . codec ) < 0 ) {
110+ if (avcodec_open (codec_ctx, codec) < 0 ) {
115111 return INVALID_OPERATION;
116112 }
117113
118- mVideoWidth = mFFmpegStorage . video . codec_ctx ->width ;
119- mVideoHeight = mFFmpegStorage . video . codec_ctx ->height ;
120- mDuration = mFFmpegStorage . pFormatCtx ->duration ;
114+ mVideoWidth = codec_ctx->width ;
115+ mVideoHeight = codec_ctx->height ;
116+ mDuration = mMovieFile ->duration ;
121117
122118 return NO_ERROR;
123119}
@@ -151,11 +147,11 @@ status_t MediaPlayer::setDataSource(const char *url)
151147 __android_log_print (ANDROID_LOG_INFO, TAG, " setDataSource(%s)" , url);
152148 status_t err = BAD_VALUE;
153149 // Open video file
154- if (av_open_input_file (&mFFmpegStorage . pFormatCtx , url, NULL , 0 , NULL ) != 0 ) {
150+ if (av_open_input_file (&mMovieFile , url, NULL , 0 , NULL ) != 0 ) {
155151 return INVALID_OPERATION;
156152 }
157153 // Retrieve stream information
158- if (av_find_stream_info (mFFmpegStorage . pFormatCtx ) < 0 ) {
154+ if (av_find_stream_info (mMovieFile ) < 0 ) {
159155 return INVALID_OPERATION;
160156 }
161157 mCurrentState = MEDIA_PLAYER_INITIALIZED;
@@ -180,11 +176,13 @@ status_t MediaPlayer::suspend() {
180176 __android_log_print (ANDROID_LOG_ERROR, TAG, " suspended" );
181177
182178 // Close the codec
183- avcodec_close (mFFmpegStorage .video .codec_ctx );
184- avcodec_close (mFFmpegStorage .audio .codec_ctx );
179+ free (mDecoderAudio );
180+ free (mDecoderVideo );
181+ // avcodec_close(mFFmpegStorage.video.codec_ctx);
182+ // avcodec_close(mFFmpegStorage.audio.codec_ctx);
185183
186184 // Close the video file
187- av_close_input_file (mFFmpegStorage . pFormatCtx );
185+ av_close_input_file (mMovieFile );
188186 return NO_ERROR;
189187}
190188
@@ -236,15 +234,15 @@ void MediaPlayer::decodeMovie(void* ptr)
236234 AVPacket pPacket;
237235 char err[256 ];
238236
239- AVStream* stream_audio = mFFmpegStorage . pFormatCtx ->streams [mFFmpegStorage . audio . stream ];
237+ AVStream* stream_audio = mMovieFile ->streams [mAudioStreamIndex ];
240238 mDecoderAudio = new DecoderAudio (stream_audio);
241239 if (!mDecoderAudio ->startAsync (err))
242240 {
243241 __android_log_print (ANDROID_LOG_INFO, TAG, " Couldn't start audio decoder: %s" , err);
244242 return ;
245243 }
246244
247- AVStream* stream_video = mFFmpegStorage . pFormatCtx ->streams [mFFmpegStorage . video . stream ];
245+ AVStream* stream_video = mMovieFile ->streams [mVideoStreamIndex ];
248246 mDecoderVideo = new DecoderVideo (stream_video);
249247 if (!mDecoderVideo ->startAsync (err))
250248 {
@@ -257,22 +255,22 @@ void MediaPlayer::decodeMovie(void* ptr)
257255 while (mCurrentState != MEDIA_PLAYER_DECODED && mCurrentState != MEDIA_PLAYER_STOPPED &&
258256 mCurrentState != MEDIA_PLAYER_STATE_ERROR)
259257 {
260- if (mDecoderVideo ->packets ()/* mVideoQueue->size() */ > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
258+ if (mDecoderVideo ->packets () > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
261259 mDecoderAudio ->packets () > FFMPEG_PLAYER_MAX_QUEUE_SIZE) {
262260 usleep (200 );
263261 continue ;
264262 }
265263
266- if (av_read_frame (mFFmpegStorage . pFormatCtx , &pPacket) < 0 ) {
264+ if (av_read_frame (mMovieFile , &pPacket) < 0 ) {
267265 mCurrentState = MEDIA_PLAYER_DECODED;
268266 continue ;
269267 }
270268
271269 // Is this a packet from the video stream?
272- if (pPacket.stream_index == mFFmpegStorage . video . stream ) {
270+ if (pPacket.stream_index == mVideoStreamIndex ) {
273271 mDecoderVideo ->enqueue (&pPacket);
274272 }
275- else if (pPacket.stream_index == mFFmpegStorage . audio . stream ) {
273+ else if (pPacket.stream_index == mAudioStreamIndex ) {
276274 mDecoderAudio ->enqueue (&pPacket);
277275 }
278276 else {
@@ -360,7 +358,8 @@ status_t MediaPlayer::getCurrentPosition(int *msec)
360358 if (mCurrentState < MEDIA_PLAYER_PREPARED) {
361359 return INVALID_OPERATION;
362360 }
363- *msec = mTime * 1000 ;
361+ *msec = 0 /* av_gettime()*/ ;
362+ // __android_log_print(ANDROID_LOG_INFO, TAG, "position %i", *msec);
364363 return NO_ERROR;
365364}
366365
0 commit comments