@@ -66,9 +66,10 @@ status_t MediaPlayer::prepareAudio()
6666 if (mAudioStreamIndex == -1 ) {
6767 return INVALID_OPERATION;
6868 }
69-
69+
70+ AVStream* stream = mMovieFile ->streams [mAudioStreamIndex ];
7071 // Get a pointer to the codec context for the video stream
71- AVCodecContext* codec_ctx = mMovieFile -> streams [ mAudioStreamIndex ] ->codec ;
72+ AVCodecContext* codec_ctx = stream ->codec ;
7273 AVCodec* codec = avcodec_find_decoder (codec_ctx->codec_id );
7374 if (codec == NULL ) {
7475 return INVALID_OPERATION;
@@ -78,6 +79,20 @@ status_t MediaPlayer::prepareAudio()
7879 if (avcodec_open (codec_ctx, codec) < 0 ) {
7980 return INVALID_OPERATION;
8081 }
82+
83+ // prepare os output
84+ if (Output::AudioDriver_set (MUSIC,
85+ stream->codec ->sample_rate ,
86+ PCM_16_BIT,
87+ (stream->codec ->channels == 2 ) ? CHANNEL_OUT_STEREO
88+ : CHANNEL_OUT_MONO) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
89+ return INVALID_OPERATION;
90+ }
91+
92+ if (Output::AudioDriver_start () != ANDROID_AUDIOTRACK_RESULT_SUCCESS) {
93+ return INVALID_OPERATION;
94+ }
95+
8196 return NO_ERROR;
8297}
8398
@@ -179,6 +194,9 @@ status_t MediaPlayer::suspend() {
179194
180195 // Close the video file
181196 av_close_input_file (mMovieFile );
197+
198+ Output::AudioDriver_unregister ();
199+
182200 return NO_ERROR;
183201}
184202
@@ -223,18 +241,49 @@ bool MediaPlayer::shouldCancel(PacketQueue* queue)
223241 frames = 0;
224242 }
225243 frames++;
226- */
244+
245+ */
246+
247+ void MediaPlayer::decode (AVFrame* frame, double pts)
248+ {
249+ /*
250+ AVFrame* buffFrame = avcodec_alloc_frame();
251+ if (frame == NULL) {
252+ return false;
253+ }
254+
255+ // Convert the image from its native format to RGB
256+ sws_scale(mConvertCtx,
257+ frame->data,
258+ frame->linesize,
259+ 0,
260+ mStream->codec->height,
261+ buffFrame->data,
262+ buffFrame->linesize);
263+
264+ Output::VideoDriver_updateSurface();
265+ */
266+ }
267+
268+ void MediaPlayer::decode (int16_t * buffer, int buffer_size)
269+ {
270+ if (Output::AudioDriver_write (buffer, buffer_size) <= 0 ) {
271+ __android_log_print (ANDROID_LOG_ERROR, TAG, " Couldn't write samples to audio track" );
272+ }
273+ }
227274
228275void MediaPlayer::decodeMovie (void * ptr)
229276{
230277 AVPacket pPacket;
231278
232279 AVStream* stream_audio = mMovieFile ->streams [mAudioStreamIndex ];
233280 mDecoderAudio = new DecoderAudio (stream_audio);
281+ mDecoderAudio ->onDecode = decode;
234282 mDecoderAudio ->startAsync ();
235283
236284 AVStream* stream_video = mMovieFile ->streams [mVideoStreamIndex ];
237285 mDecoderVideo = new DecoderVideo (stream_video);
286+ mDecoderVideo ->onDecode = decode;
238287 mDecoderVideo ->startAsync ();
239288
240289 mCurrentState = MEDIA_PLAYER_STARTED;
0 commit comments