@@ -57,67 +57,63 @@ MediaPlayer::~MediaPlayer()
57
57
status_t MediaPlayer::prepareAudio ()
58
58
{
59
59
__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;
66
64
break ;
67
65
}
68
66
}
69
67
70
- if (mFFmpegStorage . audio . stream == -1 ) {
68
+ if (mAudioStreamIndex == -1 ) {
71
69
return INVALID_OPERATION;
72
70
}
73
71
74
72
// 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 ) {
78
76
return INVALID_OPERATION;
79
77
}
80
78
81
79
// 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
+ }
85
83
return NO_ERROR;
86
84
}
87
85
88
86
status_t MediaPlayer::prepareVideo ()
89
87
{
90
88
__android_log_print (ANDROID_LOG_INFO, TAG, " prepareVideo" );
91
89
// 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;
98
94
break ;
99
95
}
100
96
}
101
97
102
- if (mFFmpegStorage . video . stream == -1 ) {
98
+ if (mVideoStreamIndex == -1 ) {
103
99
return INVALID_OPERATION;
104
100
}
105
101
106
102
// 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 ) {
110
106
return INVALID_OPERATION;
111
107
}
112
108
113
109
// Open codec
114
- if (avcodec_open (mFFmpegStorage . video . codec_ctx , mFFmpegStorage . video . codec ) < 0 ) {
110
+ if (avcodec_open (codec_ctx, codec) < 0 ) {
115
111
return INVALID_OPERATION;
116
112
}
117
113
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 ;
121
117
122
118
return NO_ERROR;
123
119
}
@@ -151,11 +147,11 @@ status_t MediaPlayer::setDataSource(const char *url)
151
147
__android_log_print (ANDROID_LOG_INFO, TAG, " setDataSource(%s)" , url);
152
148
status_t err = BAD_VALUE;
153
149
// 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 ) {
155
151
return INVALID_OPERATION;
156
152
}
157
153
// Retrieve stream information
158
- if (av_find_stream_info (mFFmpegStorage . pFormatCtx ) < 0 ) {
154
+ if (av_find_stream_info (mMovieFile ) < 0 ) {
159
155
return INVALID_OPERATION;
160
156
}
161
157
mCurrentState = MEDIA_PLAYER_INITIALIZED;
@@ -180,11 +176,13 @@ status_t MediaPlayer::suspend() {
180
176
__android_log_print (ANDROID_LOG_ERROR, TAG, " suspended" );
181
177
182
178
// 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);
185
183
186
184
// Close the video file
187
- av_close_input_file (mFFmpegStorage . pFormatCtx );
185
+ av_close_input_file (mMovieFile );
188
186
return NO_ERROR;
189
187
}
190
188
@@ -236,15 +234,15 @@ void MediaPlayer::decodeMovie(void* ptr)
236
234
AVPacket pPacket;
237
235
char err[256 ];
238
236
239
- AVStream* stream_audio = mFFmpegStorage . pFormatCtx ->streams [mFFmpegStorage . audio . stream ];
237
+ AVStream* stream_audio = mMovieFile ->streams [mAudioStreamIndex ];
240
238
mDecoderAudio = new DecoderAudio (stream_audio);
241
239
if (!mDecoderAudio ->startAsync (err))
242
240
{
243
241
__android_log_print (ANDROID_LOG_INFO, TAG, " Couldn't start audio decoder: %s" , err);
244
242
return ;
245
243
}
246
244
247
- AVStream* stream_video = mFFmpegStorage . pFormatCtx ->streams [mFFmpegStorage . video . stream ];
245
+ AVStream* stream_video = mMovieFile ->streams [mVideoStreamIndex ];
248
246
mDecoderVideo = new DecoderVideo (stream_video);
249
247
if (!mDecoderVideo ->startAsync (err))
250
248
{
@@ -257,22 +255,22 @@ void MediaPlayer::decodeMovie(void* ptr)
257
255
while (mCurrentState != MEDIA_PLAYER_DECODED && mCurrentState != MEDIA_PLAYER_STOPPED &&
258
256
mCurrentState != MEDIA_PLAYER_STATE_ERROR)
259
257
{
260
- if (mDecoderVideo ->packets ()/* mVideoQueue->size() */ > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
258
+ if (mDecoderVideo ->packets () > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
261
259
mDecoderAudio ->packets () > FFMPEG_PLAYER_MAX_QUEUE_SIZE) {
262
260
usleep (200 );
263
261
continue ;
264
262
}
265
263
266
- if (av_read_frame (mFFmpegStorage . pFormatCtx , &pPacket) < 0 ) {
264
+ if (av_read_frame (mMovieFile , &pPacket) < 0 ) {
267
265
mCurrentState = MEDIA_PLAYER_DECODED;
268
266
continue ;
269
267
}
270
268
271
269
// Is this a packet from the video stream?
272
- if (pPacket.stream_index == mFFmpegStorage . video . stream ) {
270
+ if (pPacket.stream_index == mVideoStreamIndex ) {
273
271
mDecoderVideo ->enqueue (&pPacket);
274
272
}
275
- else if (pPacket.stream_index == mFFmpegStorage . audio . stream ) {
273
+ else if (pPacket.stream_index == mAudioStreamIndex ) {
276
274
mDecoderAudio ->enqueue (&pPacket);
277
275
}
278
276
else {
@@ -360,7 +358,8 @@ status_t MediaPlayer::getCurrentPosition(int *msec)
360
358
if (mCurrentState < MEDIA_PLAYER_PREPARED) {
361
359
return INVALID_OPERATION;
362
360
}
363
- *msec = mTime * 1000 ;
361
+ *msec = 0 /* av_gettime()*/ ;
362
+ // __android_log_print(ANDROID_LOG_INFO, TAG, "position %i", *msec);
364
363
return NO_ERROR;
365
364
}
366
365
0 commit comments