Skip to content

Commit

Permalink
修改app中的player,添加日志工具
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonXiao001 committed Jan 21, 2019
1 parent f0069e2 commit 4790f04
Show file tree
Hide file tree
Showing 17 changed files with 348 additions and 196 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/build
/captures
.externalNativeBuild
/.idea
3 changes: 2 additions & 1 deletion app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ set_target_properties(swscale-4_lib PROPERTIES IMPORTED_LOCATION
# build application's shared lib
add_library(native-lib SHARED
${CMAKE_SOURCE_DIR}/src/main/cpp/native-lib.cpp
${CMAKE_SOURCE_DIR}/src/main/cpp/log_util.cpp
${CMAKE_SOURCE_DIR}/src/main/cpp/player.cpp
${CMAKE_SOURCE_DIR}/src/main/cpp/opensles.cpp
${CMAKE_SOURCE_DIR}/src/main/cpp/easyPlayer.cpp)
${CMAKE_SOURCE_DIR}/src/main/cpp/easy_player.cpp)

# Include libraries needed for VideoPlayer lib
target_link_libraries(native-lib
Expand Down
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile project(':easyplayerlib')
testCompile 'junit:junit:4.12'
}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.jx.easyplayer">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/cpp/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by jasonxiao{github.com/JasonXiao001} on 2019/1/20.
//

#ifndef EASYPLAYER_COMMON_H
#define EASYPLAYER_COMMON_H

#define SUCCESS 0
#define ERROR_ILLEGAL_STATE 1000
#define ERROR_IO 1001

#endif //EASYPLAYER_COMMON_H
2 changes: 1 addition & 1 deletion app/src/main/cpp/easyPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void EasyPlayer::init(const std::string input_filename) {

void EasyPlayer::read() {
int err, i, ret;
av_log(NULL, AV_LOG_INFO, "start read thread.\n");
av_log(NULL, AV_LOG_INFO, "start read thread 11.\n");
AVPacket *pkt = (AVPacket *)av_malloc(sizeof(AVPacket));
if (pkt == NULL) {
av_log(NULL, AV_LOG_FATAL, "Could not allocate avPacket.\n");
Expand Down
47 changes: 47 additions & 0 deletions app/src/main/cpp/easy_player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Created by jasonxiao{github.com/JasonXiao001} on 2019/1/19.
//

#include "easy_player.h"
#include "log_util.h"
#include "common.h"


EasyPlayer::EasyPlayer() : state_(State::Idle), ic_(nullptr) {
av_log_set_callback(ff_log_callback);
av_log_set_level(AV_LOG_DEBUG);
av_register_all();
avformat_network_init();
}

EasyPlayer::~EasyPlayer() {
if (ic_) {
avformat_free_context(ic_);
ic_ = nullptr;
}
}

int EasyPlayer::SetDataSource(const std::string &path) {
if (state_ != State::Idle) {
ELOG("illegal state|current:%d", state_);
return ERROR_ILLEGAL_STATE;
}
ILOG("source path:%s", path.c_str());
int err;
err = avformat_open_input(&ic_, path.c_str(), NULL, NULL);
if (err) {
ELOG("avformat_open_input failed|ret:%d", err);
return ERROR_IO;
}
state_ = State ::Initialized;
return SUCCESS;
}

int EasyPlayer::PrepareAsync() {
if (state_ != State::Initialized || state_ != State::Stoped) {
ELOG("illegal state|current:%d", state_);
return ERROR_ILLEGAL_STATE;
}

return SUCCESS;
}
49 changes: 49 additions & 0 deletions app/src/main/cpp/easy_player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Created by jasonxiao{github.com/JasonXiao001} on 2019/1/19.
//

#ifndef EASYPLAYER_EASY_PLAYER_H
#define EASYPLAYER_EASY_PLAYER_H

#include <thread>
#include <string>
extern "C"{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavutil/opt.h"
#include "libavutil/imgutils.h"
#include "libavutil/time.h"
#include "libavcodec/avfft.h"
};

enum class State {
Idle,
Initialized,
Preparing,
Prepared,
Started,
Paused,
Stoped,
Completed,
End,
Error,
};


class EasyPlayer {
public:
EasyPlayer();
EasyPlayer(const EasyPlayer&) = delete;
~EasyPlayer();
int SetDataSource(const std::string &path);
int PrepareAsync();

private:
State state_;
AVFormatContext *ic_;
};


#endif //EASYPLAYER_EASY_PLAYER_H
4 changes: 2 additions & 2 deletions app/src/main/cpp/include/easyPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ enum class PlayerState {
};


class EasyPlayer {
class EasyPlayer1 {
public:
EasyPlayer() = default;
EasyPlayer1() = default;
void init(const std::string input_filename);
bool has_video();
bool get_img_frame(AVFrame *frame);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/include/opensles.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void createBufferQueueAudioPlayer(int sampleRate, int channel);
void audioStart();
void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context);
void releaseResampleBuf(void);
void init(EasyPlayer *player);




Expand Down
64 changes: 64 additions & 0 deletions app/src/main/cpp/log_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Created by jasonxiao{github.com/JasonXiao001} on 2019/1/20.
//

#include "log_util.h"
#include <string>
#include <sstream>
extern "C" {
#include "libavutil/log.h"
};

void ff_log_callback(void* ptr, int level, const char* fmt, va_list vl) {
switch (level) {
case AV_LOG_VERBOSE:
__android_log_vprint(ANDROID_LOG_VERBOSE, FF_LOG_TAG, fmt, vl);
break;
case AV_LOG_DEBUG:
__android_log_vprint(ANDROID_LOG_DEBUG, FF_LOG_TAG, fmt, vl);
break;
case AV_LOG_INFO:
__android_log_vprint(ANDROID_LOG_INFO, FF_LOG_TAG, fmt, vl);
break;
case AV_LOG_WARNING:
__android_log_vprint(ANDROID_LOG_WARN, FF_LOG_TAG, fmt, vl);
break;
case AV_LOG_ERROR:
case AV_LOG_FATAL:
case AV_LOG_PANIC:
__android_log_vprint(ANDROID_LOG_FATAL, FF_LOG_TAG, fmt, vl);
break;
case AV_LOG_QUIET:
__android_log_vprint(ANDROID_LOG_SILENT, FF_LOG_TAG, fmt, vl);
break;
default:
__android_log_vprint(ANDROID_LOG_VERBOSE, FF_LOG_TAG, fmt, vl);
break;
}
}

void WriteLog(LogLevel level, const char *tag, const char *file_name, const char *func_name, int line, const char *fmt, ...) {
if (level >= MY_LOG_LEVEL) {
std::string file_name_no_path(file_name);
if (file_name_no_path.rfind("\\") != std::string::npos) {
file_name_no_path = file_name_no_path.substr(file_name_no_path.rfind("\\") + 1);
}else {
file_name_no_path = file_name_no_path.substr(file_name_no_path.rfind("/") + 1);
}
char buf[1024];
std::string log;
va_list ap;
va_start(ap, fmt);
int write_n = vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap);
if (write_n >= 0 && write_n < static_cast<int>(sizeof(buf))) {
log = std::string(buf);
}else {
log = "log too long";
}
std::ostringstream ss;
ss << file_name_no_path << ":" << line << "|" << func_name << ": " << log;
__android_log_write(level, tag, ss.str().c_str());
}
}

46 changes: 46 additions & 0 deletions app/src/main/cpp/log_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Created by jasonxiao{github.com/JasonXiao001} on 2019/1/19.
//

#ifndef EASYPLAYER_LOG_H
#define EASYPLAYER_LOG_H

#include <android/log.h>

enum LogLevel {
kVerbose = 2,
kDebug = 3,
kInfo = 4,
kWarn = 5,
kError = 6,
kOff = 8,
};

#ifndef MY_LOG_LEVEL
#define MY_LOG_LEVEL kVerbose
#endif
#ifndef MY_LOG_TAG
#define MY_LOG_TAG "easy_player"
#endif
#ifndef FF_LOG_TAG
#define FF_LOG_TAG "ffmpeg"
#endif


void ff_log_callback(void* ptr, int level, const char* fmt, va_list vl);

void WriteLog(LogLevel level, const char *tag, const char *file_name, const char *func_name, int line, const char *fmt, ...);


#define MLOG(level, tag, ...) WriteLog(level, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__);

#define ELOG(...) MLOG(kError, MY_LOG_TAG, __VA_ARGS__)
#define WLOG(...) MLOG(kWarn, MY_LOG_TAG, __VA_ARGS__)
#define ILOG(...) MLOG(kInfo, MY_LOG_TAG, __VA_ARGS__)
#define DLOG(...) MLOG(kDebug, MY_LOG_TAG, __VA_ARGS__)
#define VLOG(...) MLOG(kVerbose, MY_LOG_TAG, __VA_ARGS__)




#endif //EASYPLAYER_LOG_H
Loading

0 comments on commit 4790f04

Please sign in to comment.