Skip to content

Commit

Permalink
feat: support Cheater render
Browse files Browse the repository at this point in the history
Signed-off-by: pingkai <[email protected]>
  • Loading branch information
pingkai committed Jan 10, 2020
1 parent 0e755ec commit 58c19b4
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 16 deletions.
23 changes: 18 additions & 5 deletions cmdline/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ add_subdirectory(../mediaPlayer mediaPlayer.out)

include(../framework/${TARGET_PLATFORM}.cmake)

add_executable(cicadaPlayer
list(APPEND SRC_FILE
cicadaPlayer.cpp
SDLEventReceiver.cpp
SDLEventReceiver.h
NetWorkEventReceiver.cpp
cicadaEventListener.cpp)
cicadaEventListener.cpp
)

if (ENABLE_SDL)
list(APPEND SRC_FILE
SDLEventReceiver.cpp
SDLEventReceiver.h
)
endif ()

add_executable(cicadaPlayer
${SRC_FILE})
target_include_directories(cicadaPlayer PUBLIC ../mediaPlayer)
target_link_directories(cicadaPlayer PRIVATE
../external/install/ffmpeg/${CMAKE_SYSTEM_NAME}/x86_64/lib
Expand All @@ -63,8 +72,12 @@ target_link_libraries(cicadaPlayer PRIVATE
ssl
crypto
pthread
SDL2
${FRAMEWORK_LIBS})
if (ENABLE_SDL)
target_link_libraries(cicadaPlayer PUBLIC
SDL2
)
endif ()

if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
target_link_libraries(cicadaPlayer PUBLIC
Expand Down
6 changes: 4 additions & 2 deletions cmdline/SDLEventReceiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#ifndef CICADAMEDIA_SDLEVENTRECEIVER_H
#define CICADAMEDIA_SDLEVENTRECEIVER_H
#ifdef ENABLE_SDL
#include <SDL2/SDL.h>
#endif
#include "IEventReceiver.h"

class SDLEventReceiver : public IEventReceiver {
Expand All @@ -16,11 +18,11 @@ class SDLEventReceiver : public IEventReceiver {

void poll(bool &exit) override;
private:
#ifdef ENABLE_SDL
SDL_Event event{};

private:
SDL_Window *window = nullptr;
SDL_Renderer *mVideoRender = nullptr;
#endif
};


Expand Down
5 changes: 4 additions & 1 deletion cmdline/cicadaEventListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

#include <MediaPlayer.h>
#include <memory>
#ifdef ENABLE_SDL
#include <SDL2/SDL.h>
#include <utils/timer.h>
#include "SDLEventReceiver.h"
#endif
#include <utils/timer.h>
#include "IEventReceiver.h"


class cicadaEventListener : public IEventReceiver::Listener {
Expand Down
23 changes: 20 additions & 3 deletions cmdline/cicadaPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

using namespace Cicada;
using namespace std;
#define SDL_MAIN_HANDLED

#include "SDLEventReceiver.h"
#include "cicadaEventListener.h"
#include "NetWorkEventReceiver.h"

#ifdef ENABLE_SDL
#include <SDL2/SDL_main.h>
#define SDL_MAIN_HANDLED
#endif

#include <media_player_error_def.h>

using IEvent = IEventReceiver::IEvent;
struct cicadaCont {
MediaPlayer *player;
Expand All @@ -22,7 +28,8 @@ static void onVideoSize(int64_t width, int64_t height, void *userData)
using IEvent = IEventReceiver::IEvent;
auto *cont = static_cast<cicadaCont *>(userData);
auto *event = new IEvent(IEvent::TYPE_SET_VIEW);
cont->receiver->push(std::unique_ptr<IEvent>(event));
if (cont->receiver)
cont->receiver->push(std::unique_ptr<IEvent>(event));
}

static void onEOS(void *userData)
Expand Down Expand Up @@ -82,9 +89,15 @@ int main(int argc, const char **argv)
pListener.EventCallback = onEvent;
pListener.ErrorCallback = onError;
cicadaEventListener eListener(player.get());
#ifdef ENABLE_SDL
SDLEventReceiver receiver(eListener);
NetWorkEventReceiver netWorkEventReceiver(eListener);
cicada.receiver = &receiver;
#else
int view = 0;
player->SetView(&view);
#endif
NetWorkEventReceiver netWorkEventReceiver(eListener);

player->SetListener(pListener);
player->SetDefaultBandWidth(100000000);
player->SetDataSource(url.c_str());
Expand All @@ -94,14 +107,18 @@ int main(int argc, const char **argv)
bool quite = false;

while (!quite) {
#ifdef ENABLE_SDL
receiver.poll(quite);
#endif

if (!quite) {
netWorkEventReceiver.poll(quite);

if (quite) {
auto *event = new IEvent(IEvent::TYPE_EXIT);
#ifdef ENABLE_SDL
receiver.push(std::unique_ptr<IEvent>(event));
#endif
}
}

Expand Down
8 changes: 7 additions & 1 deletion framework/Linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,11 @@ endif (USEMSAN)
set(TARGET_LIBRARY_TYPE STATIC)

set(ENABLE_GLRENDER OFF)
set(ENABLE_SDL ON)

set(BUILD_TEST ON)
if (TRAVIS)
set(ENABLE_CHEAT_RENDER ON)
else ()
set(ENABLE_SDL ON)
endif ()

13 changes: 12 additions & 1 deletion framework/render/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ if (ENABLE_SDL)
)
endif ()

if (ENABLE_CHEAT_RENDER)
list(APPEND SRC_FILES
video/CheaterVideoRender.cpp
audio/CheaterAudioRender.cpp
)
endif ()

if (ANDROID)
list(APPEND SRC_FILES
audio/filterAudioRender.cpp
Expand Down Expand Up @@ -131,13 +138,17 @@ add_library(render STATIC ${SRC_FILES})

target_include_directories(render PUBLIC ${COMMON_INC_DIR})
if (ENABLE_SDL)
add_definitions(-DENABLE_SDL)
target_compile_definitions(render PUBLIC ENABLE_SDL)
target_include_directories(render PUBLIC /usr/local/include/)
target_link_directories(render PRIVATE ${COMMON_LIB_DIR})
target_link_directories(render PUBLIC /usr/local/lib)
target_link_libraries(render PUBLIC SDL2)
endif ()

if (ENABLE_CHEAT_RENDER)
target_compile_definitions(render PUBLIC ENABLE_CHEAT_RENDER)
endif ()

if (ENABLE_GLRENDER)

target_include_directories(render PUBLIC
Expand Down
69 changes: 69 additions & 0 deletions framework/render/audio/CheaterAudioRender.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Created by moqi on 2020/1/10.
//

#include <utils/ffmpeg_utils.h>
#include <base/media/AVAFPacket.h>
#include "CheaterAudioRender.h"

int Cicada::CheaterAudioRender::init_device()
{
mOutputInfo.format = AF_SAMPLE_FMT_S16;
mOutputInfo.sample_rate = 44100;
mOutputInfo.channels = std::min(mInputInfo.channels, 2);
mClock.set(0);
mClock.start();
return 0;
}

int Cicada::CheaterAudioRender::pause_device()
{
mClock.pause();
return 0;
}

int Cicada::CheaterAudioRender::start_device()
{
mClock.start();
return 0;
}

void Cicada::CheaterAudioRender::flush_device()
{
mClock.reset();
mPCMDuration = 0;
}

int64_t Cicada::CheaterAudioRender::device_get_position()
{
int64_t clock = mClock.get();
if (clock > mPCMDuration) {
mClock.set(mPCMDuration);
return mPCMDuration;
}
return clock;
}

int Cicada::CheaterAudioRender::device_write(unique_ptr<IAFFrame> &frame)
{
if (!frame)
return 0;
if (mPCMDuration - mClock.get() > 1000000) {
return -EAGAIN;
}
auto *avafFrame = dynamic_cast<AVAFFrame *> (frame.get());
if (avafFrame) {
int duration = getPCMFrameDuration(avafFrame->ToAVFrame());
if (duration > 0)
mPCMDuration += duration;
}
frame = nullptr;
return 0;
}

uint64_t Cicada::CheaterAudioRender::device_get_que_duration()
{
int64_t clock = mClock.get();
return std::max((int64_t) 0, mPCMDuration - clock);

}
53 changes: 53 additions & 0 deletions framework/render/audio/CheaterAudioRender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Created by moqi on 2020/1/10.
//

#ifndef CICADAMEDIA_CHEATERAUDIORENDER_H
#define CICADAMEDIA_CHEATERAUDIORENDER_H

#include "filterAudioRender.h"
#include "utils/af_clock.h"

namespace Cicada {
class CheaterAudioRender : public filterAudioRender {
public:
CheaterAudioRender()
{

}

~CheaterAudioRender() override
{

}

private:
int init_device() override;

int pause_device() override;

int start_device() override;

void flush_device() override;

void device_setVolume(float gain) override
{

}

int64_t device_get_position() override;

int device_write(unique_ptr<IAFFrame> &frame) override;

uint64_t device_get_que_duration() override;

private:
af_clock mClock{};
int64_t mPCMDuration{0};


};
}


#endif //CICADAMEDIA_CHEATERAUDIORENDER_H
24 changes: 21 additions & 3 deletions framework/render/renderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,31 @@
#endif
#ifdef ENABLE_SDL

#include "audio/SdlAFAudioRender.h"
#include "video/SdlAFVideoRender.h"
#include "audio/SdlAFAudioRender.h"
#include "video/SdlAFVideoRender.h"

#endif

#ifdef GLRENDER

#include "video/glRender/GLRender.h"
#include "video/glRender/GLRender.h"

#endif

#ifdef ENABLE_CHEAT_RENDER

#include "video/CheaterVideoRender.h"

#endif

#include "audio/audioRenderPrototype.h"

#ifdef ENABLE_CHEAT_RENDER

#include "audio/CheaterAudioRender.h"

#endif

using namespace Cicada;

std::unique_ptr<IAudioRender> AudioRenderFactory::create()
Expand All @@ -38,6 +51,9 @@ std::unique_ptr<IAudioRender> AudioRenderFactory::create()
if (render) {
return render;
}
#ifdef ENABLE_CHEAT_RENDER
return std::unique_ptr<IAudioRender>(new CheaterAudioRender());
#endif

#ifdef ANDROID
return std::unique_ptr<IAudioRender>(new AudioTrackRender());
Expand All @@ -57,6 +73,8 @@ std::unique_ptr<IVideoRender> videoRenderFactory::create()
return std::unique_ptr<IVideoRender>(new GLRender());
#elif defined(ENABLE_SDL)
return std::unique_ptr<IVideoRender>(new SdlAFVideoRender());
#elif defined(ENABLE_CHEAT_RENDER)
return std::unique_ptr<IVideoRender>(new CheaterVideoRender());
#endif
return nullptr;
}
Loading

0 comments on commit 58c19b4

Please sign in to comment.