forked from havlenapetr/FFMpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3910e7f
commit 53ff5b7
Showing
5 changed files
with
200 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#include <android/log.h> | ||
#include "decoder_audio.h" | ||
|
||
// map system drivers methods | ||
#include <drivers_map.h> | ||
|
||
extern "C" { | ||
|
||
#include "libavcodec/avcodec.h" | ||
#include "libavformat/avformat.h" | ||
|
||
} // end of extern C | ||
|
||
#define TAG "FFMpegAudioDecoder" | ||
|
||
static DecoderAudio* sInstance; | ||
|
||
DecoderAudio::DecoderAudio(PacketQueue* queue, | ||
AVCodecContext* codec_ctx, | ||
struct DecoderAudioConfig* config) | ||
{ | ||
mQueue = queue; | ||
mCodecCtx = codec_ctx; | ||
mConfig = config; | ||
sInstance = this; | ||
} | ||
|
||
DecoderAudio::~DecoderAudio() | ||
{ | ||
if(mDecoding) | ||
{ | ||
stop(); | ||
} | ||
} | ||
|
||
bool DecoderAudio::start(const char* err) | ||
{ | ||
if(!prepare(err)) | ||
{ | ||
return false; | ||
} | ||
|
||
pthread_create(&mThread, NULL, startDecoding, NULL); | ||
return true; | ||
} | ||
|
||
void DecoderAudio::stop() | ||
{ | ||
mQueue->abort(); | ||
__android_log_print(ANDROID_LOG_INFO, TAG, "waiting on end of audio decoder"); | ||
int ret = -1; | ||
if((ret = pthread_join(mThread, NULL)) != 0) { | ||
__android_log_print(ANDROID_LOG_ERROR, TAG, "Couldn't cancel audio decoder: %i", ret); | ||
return; | ||
} | ||
__android_log_print(ANDROID_LOG_INFO, TAG, "audio decoder stopped"); | ||
} | ||
|
||
void* DecoderAudio::startDecoding(void* ptr) | ||
{ | ||
__android_log_print(ANDROID_LOG_INFO, TAG, "starting audio thread"); | ||
sInstance->decode(ptr); | ||
} | ||
|
||
bool DecoderAudio::prepare(const char *err) | ||
{ | ||
mSamplesSize = AVCODEC_MAX_AUDIO_FRAME_SIZE; | ||
mSamples = (int16_t *) av_malloc(mSamplesSize); | ||
|
||
if(AudioDriver_set(mConfig->streamType, | ||
mConfig->sampleRate, | ||
mConfig->format, | ||
mConfig->channels) != ANDROID_AUDIOTRACK_RESULT_SUCCESS) { | ||
err = "Couldnt' set audio track"; | ||
return false; | ||
} | ||
if(AudioDriver_start() != ANDROID_AUDIOTRACK_RESULT_SUCCESS) { | ||
err = "Couldnt' start audio track"; | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
bool DecoderAudio::process(AVPacket *packet) | ||
{ | ||
int size = mSamplesSize; | ||
int len = avcodec_decode_audio3(mCodecCtx, mSamples, &size, packet); | ||
if(AudioDriver_write(mSamples, size) <= 0) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
void DecoderAudio::decode(void* ptr) | ||
{ | ||
AVPacket pPacket; | ||
|
||
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding audio"); | ||
|
||
mDecoding = true; | ||
while(mDecoding) | ||
{ | ||
if(mQueue->get(&pPacket, true) < 0) | ||
{ | ||
mDecoding = false; | ||
continue; | ||
} | ||
if(!process(&pPacket)) | ||
{ | ||
mDecoding = false; | ||
continue; | ||
} | ||
// Free the packet that was allocated by av_read_frame | ||
av_free_packet(&pPacket); | ||
} | ||
|
||
__android_log_print(ANDROID_LOG_INFO, TAG, "decoding audio ended"); | ||
|
||
AudioDriver_unregister(); | ||
|
||
// Free audio samples buffer | ||
av_free(mSamples); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef FFMPEG_DECODER_AUDIO_H | ||
#define FFMPEG_DECODER_AUDIO_H | ||
|
||
#include <pthread.h> | ||
|
||
#include "packetqueue.h" | ||
|
||
struct DecoderAudioConfig | ||
{ | ||
int streamType; | ||
int sampleRate; | ||
int format; | ||
int channels; | ||
}; | ||
|
||
class DecoderAudio | ||
{ | ||
public: | ||
DecoderAudio(PacketQueue* queue, | ||
AVCodecContext* codec_ctx, | ||
struct DecoderAudioConfig* config); | ||
|
||
~DecoderAudio(); | ||
|
||
bool start(const char* err); | ||
void stop(); | ||
|
||
private: | ||
PacketQueue* mQueue; | ||
AVCodecContext* mCodecCtx; | ||
struct DecoderAudioConfig* mConfig; | ||
bool mDecoding; | ||
pthread_t mThread; | ||
int16_t* mSamples; | ||
int mSamplesSize; | ||
|
||
bool prepare(const char *err); | ||
void decode(void* ptr); | ||
bool process(AVPacket *packet); | ||
static void* startDecoding(void* ptr); | ||
}; | ||
|
||
#endif //FFMPEG_DECODER_AUDIO_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters