@@ -174,42 +174,16 @@ status_t MediaPlayer::setDataSource(const char *url)
174
174
return NO_ERROR;
175
175
}
176
176
177
- AVFrame* MediaPlayer::createAndroidFrame ()
178
- {
179
- void * pixels;
180
- AVFrame* frame;
181
-
182
- frame = avcodec_alloc_frame ();
183
- if (frame == NULL ) {
184
- return NULL ;
185
- }
186
-
187
- if (Output::VideoDriver_getPixels (mVideoWidth ,
188
- mVideoHeight ,
189
- &pixels) != ANDROID_SURFACE_RESULT_SUCCESS) {
190
- return NULL ;
191
- }
192
-
193
- // Assign appropriate parts of buffer to image planes in pFrameRGB
194
- // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
195
- // of AVPicture
196
- avpicture_fill ((AVPicture *) frame,
197
- (uint8_t *)pixels,
198
- PIX_FMT_RGB565,
199
- mVideoWidth ,
200
- mVideoHeight );
201
-
202
- return frame;
203
- }
204
-
205
177
status_t MediaPlayer::suspend () {
206
178
__android_log_print (ANDROID_LOG_INFO, TAG, " suspend" );
207
179
208
180
mCurrentState = MEDIA_PLAYER_STOPPED;
209
- if (mDecoderAudio != NULL )
210
- {
181
+ if (mDecoderAudio != NULL ) {
211
182
mDecoderAudio ->stop ();
212
183
}
184
+ if (mDecoderVideo != NULL ) {
185
+ mDecoderVideo ->stop ();
186
+ }
213
187
214
188
if (pthread_join (mPlayerThread , NULL ) != 0 ) {
215
189
__android_log_print (ANDROID_LOG_ERROR, TAG, " Couldn't cancel player thread" );
@@ -247,33 +221,6 @@ status_t MediaPlayer::setVideoSurface(JNIEnv* env, jobject jsurface)
247
221
return NO_ERROR;
248
222
}
249
223
250
- status_t MediaPlayer::processVideo (AVPacket *packet, AVFrame *pFrame)
251
- {
252
- int completed;
253
-
254
- // Decode video frame
255
- avcodec_decode_video (mFFmpegStorage .video .codec_ctx ,
256
- mFFmpegStorage .pFrame ,
257
- &completed,
258
- packet->data ,
259
- packet->size );
260
-
261
- if (completed) {
262
- // Convert the image from its native format to RGB
263
- sws_scale (mFFmpegStorage .img_convert_ctx ,
264
- mFFmpegStorage .pFrame ->data ,
265
- mFFmpegStorage .pFrame ->linesize ,
266
- 0 ,
267
- mVideoHeight ,
268
- pFrame->data ,
269
- pFrame->linesize );
270
-
271
- Output::VideoDriver_updateSurface ();
272
- return NO_ERROR;
273
- }
274
- return INVALID_OPERATION;
275
- }
276
-
277
224
bool MediaPlayer::shouldCancel (PacketQueue* queue)
278
225
{
279
226
return (mCurrentState == MEDIA_PLAYER_STATE_ERROR || mCurrentState == MEDIA_PLAYER_STOPPED ||
@@ -298,83 +245,34 @@ bool MediaPlayer::shouldCancel(PacketQueue* queue)
298
245
}
299
246
frames++;
300
247
*/
301
- void MediaPlayer::decodeVideo (void * ptr)
302
- {
303
- AVPacket pPacket;
304
- AVFrame* pFrameRGB;
305
- bool run = true ;
306
-
307
- if ((pFrameRGB = createAndroidFrame ()) == NULL ) {
308
- mCurrentState = MEDIA_PLAYER_STATE_ERROR;
309
- }
310
-
311
- __android_log_print (ANDROID_LOG_INFO, TAG, " decoding video" );
312
-
313
- while (run)
314
- {
315
- if (mCurrentState == MEDIA_PLAYER_PAUSED) {
316
- usleep (50 );
317
- continue ;
318
- }
319
- if (shouldCancel (mVideoQueue )) {
320
- run = false ;
321
- continue ;
322
- }
323
- if (mVideoQueue ->get (&pPacket, true ) < 0 )
324
- {
325
- mCurrentState = MEDIA_PLAYER_STATE_ERROR;
326
- }
327
- if (processVideo (&pPacket, pFrameRGB) != NO_ERROR)
328
- {
329
- mCurrentState = MEDIA_PLAYER_STATE_ERROR;
330
- }
331
-
332
- AVStream* vs = mFFmpegStorage .pFormatCtx ->streams [mFFmpegStorage .video .stream ];
333
- if (pPacket.dts != AV_NOPTS_VALUE) {
334
- mTime = pPacket.dts ;
335
- } else {
336
- mTime = 0 ;
337
- }
338
- mTime *= av_q2d (vs->time_base );
339
- // __android_log_print(ANDROID_LOG_INFO, TAG, "time: %i", pts);
340
- // Free the packet that was allocated by av_read_frame
341
- av_free_packet (&pPacket);
342
- }
343
-
344
- __android_log_print (ANDROID_LOG_INFO, TAG, " decoding video ended" );
345
-
346
- Output::VideoDriver_unregister ();
347
-
348
- // Free the RGB image
349
- av_free (pFrameRGB);
350
- }
351
248
352
249
void MediaPlayer::decodeMovie (void * ptr)
353
250
{
354
251
AVPacket pPacket;
355
- char err[256 ];
252
+ char err[256 ];
356
253
357
- pthread_create (&mVideoThread , NULL , startVideoDecoding, NULL );
358
-
359
- DecoderAudioConfig cfg;
360
- cfg.streamType = MUSIC;
361
- cfg.sampleRate = mFFmpegStorage .audio .codec_ctx ->sample_rate ;
362
- cfg.format = PCM_16_BIT;
363
- cfg.channels = (mFFmpegStorage .audio .codec_ctx ->channels == 2 ) ? CHANNEL_OUT_STEREO : CHANNEL_OUT_MONO;
364
- AVStream* stream = mFFmpegStorage .pFormatCtx ->streams [mFFmpegStorage .audio .stream ];
365
- mDecoderAudio = new DecoderAudio (stream, &cfg);
254
+ AVStream* stream_audio = mFFmpegStorage .pFormatCtx ->streams [mFFmpegStorage .audio .stream ];
255
+ mDecoderAudio = new DecoderAudio (stream_audio);
366
256
if (!mDecoderAudio ->startAsync (err))
367
257
{
368
258
__android_log_print (ANDROID_LOG_INFO, TAG, " Couldn't start audio decoder: %s" , err);
369
259
return ;
370
260
}
371
261
262
+ AVStream* stream_video = mFFmpegStorage .pFormatCtx ->streams [mFFmpegStorage .video .stream ];
263
+ mDecoderVideo = new DecoderVideo (stream_video);
264
+ if (!mDecoderVideo ->startAsync (err))
265
+ {
266
+ __android_log_print (ANDROID_LOG_INFO, TAG, " Couldn't start video decoder: %s" , err);
267
+ return ;
268
+ }
269
+
372
270
mCurrentState = MEDIA_PLAYER_STARTED;
373
271
__android_log_print (ANDROID_LOG_INFO, TAG, " playing %ix%i" , mVideoWidth , mVideoHeight );
374
272
while (mCurrentState != MEDIA_PLAYER_DECODED && mCurrentState != MEDIA_PLAYER_STOPPED &&
375
273
mCurrentState != MEDIA_PLAYER_STATE_ERROR)
376
274
{
377
- if (mVideoQueue ->size () > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
275
+ if (mDecoderVideo -> packets () /* mVideoQueue->size()*/ > FFMPEG_PLAYER_MAX_QUEUE_SIZE &&
378
276
mDecoderAudio ->packets () > FFMPEG_PLAYER_MAX_QUEUE_SIZE) {
379
277
usleep (200 );
380
278
continue ;
@@ -387,7 +285,7 @@ void MediaPlayer::decodeMovie(void* ptr)
387
285
388
286
// Is this a packet from the video stream?
389
287
if (pPacket.stream_index == mFFmpegStorage .video .stream ) {
390
- mVideoQueue -> put (&pPacket);
288
+ mDecoderVideo -> enqueue (&pPacket);
391
289
}
392
290
else if (pPacket.stream_index == mFFmpegStorage .audio .stream ) {
393
291
mDecoderAudio ->enqueue (&pPacket);
@@ -401,7 +299,7 @@ void MediaPlayer::decodeMovie(void* ptr)
401
299
// waits on end of video thread
402
300
__android_log_print (ANDROID_LOG_ERROR, TAG, " waiting on video thread" );
403
301
int ret = -1 ;
404
- if ((ret = pthread_join ( mVideoThread , NULL )) != 0 ) {
302
+ if ((ret = mDecoderVideo -> wait ( )) != 0 ) {
405
303
__android_log_print (ANDROID_LOG_ERROR, TAG, " Couldn't cancel video thread: %i" , ret);
406
304
}
407
305
@@ -417,12 +315,6 @@ void MediaPlayer::decodeMovie(void* ptr)
417
315
__android_log_print (ANDROID_LOG_INFO, TAG, " end of playing" );
418
316
}
419
317
420
- void * MediaPlayer::startVideoDecoding (void * ptr)
421
- {
422
- __android_log_print (ANDROID_LOG_INFO, TAG, " starting video thread" );
423
- sPlayer ->decodeVideo (ptr);
424
- }
425
-
426
318
void * MediaPlayer::startPlayer (void * ptr)
427
319
{
428
320
__android_log_print (ANDROID_LOG_INFO, TAG, " starting main player thread" );
0 commit comments