Feature: add MediaClock

pull/221/head
xufuji456 2 years ago
parent acc42e49d5
commit b6feca2e8c
  1. 10
      app/src/main/cpp/CMakeLists.txt
  2. 3
      app/src/main/cpp/ffplayer/FFMessageQueue.cpp
  3. 6
      app/src/main/cpp/ffplayer/FFMessageQueue.h
  4. 50
      app/src/main/cpp/ffplayer/clock/MediaClock.cpp
  5. 45
      app/src/main/cpp/ffplayer/clock/MediaClock.h
  6. 5
      app/src/main/cpp/ffplayer/ffplay_define.h

@ -34,7 +34,14 @@ set(SRC_METADATA
set(SRC_FFPLAYER
ffplayer/queue/FrameQueue.cpp
ffplayer/queue/PacketQueue.cpp)
ffplayer/queue/PacketQueue.cpp
ffplayer/decode/BaseDecoder.cpp
ffplayer/decode/FFAudioDecoder.cpp
ffplayer/decode/FFVideoDecoder.cpp
ffplayer/clock/MediaClock.cpp
ffplayer/render/android/AudioAndroidRender.cpp
ffplayer/FFMessageQueue.cpp
ffplayer/FFMediaPlayer.cpp)
add_library( # Sets the name of the library.
media-handle
@ -74,6 +81,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(.)
include_directories(include)
include_directories(metadata)
include_directories(ffplayer)
if(${CMAKE_ANDROID_ARCH_ABI} MATCHES "armeabi-v7a")
include_directories(include/armeabi-v7a)

@ -1,6 +1,3 @@
//
// Created by xu fulong on 2022/9/24.
//
#include "FFMessageQueue.h"

@ -1,6 +1,3 @@
//
// Created by xu fulong on 2022/9/24.
//
#ifndef FFMPEGANDROID_FFMESSAGEQUEUE_H
#define FFMPEGANDROID_FFMESSAGEQUEUE_H
@ -26,6 +23,8 @@
#define FFP_MSG_COMPONENT_OPEN 409
#define FFP_MSG_VIDEO_SEEK_RENDERING_START 410
#define FFP_MSG_AUDIO_SEEK_RENDERING_START 411
#define FFP_MSG_DECODER_OPEN_ERROR 412
#define FFP_MSG_DEMUX_ERROR 413
#define FFP_MSG_BUFFERING_START 500
#define FFP_MSG_BUFFERING_END 501
@ -36,6 +35,7 @@
#define FFP_MSG_PLAYBACK_STATE_CHANGED 700
#define FFP_MSG_TIMED_TEXT 800
#define FFP_MSG_VIDEO_DECODER_OPEN 900
#define FFP_MSG_OPEN_ERROR 901
#define FFP_REQ_START 1001
#define FFP_REQ_PAUSE 1002

@ -0,0 +1,50 @@
#include <ffplay_define.h>
#include "MediaClock.h"
MediaClock::MediaClock() {
m_speed = 1.0;
m_pause = false;
setClock(NAN);
}
void MediaClock::setClock(double pts) {
double time = (double) av_gettime_relative() / 1000000.0;
setClock(pts, time);
}
void MediaClock::setClock(double pts, double time) {
m_pts = pts;
m_last_update = time;
m_pts_drift = m_pts - time;
}
double MediaClock::getClock() const {
if (m_pause) {
return m_pts;
} else {
double time = (double) av_gettime_relative() / 1000000.0;
return m_pts_drift + time - (time - m_last_update) * (1.0 - m_speed);
}
}
void MediaClock::setSpeed(double speed) {
setClock(getClock());
m_speed = speed;
}
double MediaClock::getSpeed() const {
return m_speed;
}
void MediaClock::syncToSlave(MediaClock *slave) {
double clock = getClock();
double slave_clock = slave->getClock();
if (!isnan(slave_clock) && (isnan(clock) || fabs(clock - slave_clock) > AV_NOSYNC_THRESHOLD)) {
setClock(slave_clock);
}
}
MediaClock::~MediaClock() {
}

@ -0,0 +1,45 @@
#ifndef FFMPEGANDROID_MEDIACLOCK_H
#define FFMPEGANDROID_MEDIACLOCK_H
#include <math.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <libavutil/time.h>
#ifdef __cplusplus
}
#endif
class MediaClock {
private:
bool m_pause;
double m_pts;
double m_speed;
double m_pts_drift;
double m_last_update;
public:
MediaClock();
void setClock(double pts);
void setClock(double pts, double time);
double getClock() const;
void setSpeed(double speed);
double getSpeed() const;
void syncToSlave(MediaClock *slave);
virtual ~MediaClock();
};
#endif //FFMPEGANDROID_MEDIACLOCK_H

@ -1,6 +1,3 @@
//
// Created by xu fulong on 2022/9/22.
//
#ifndef FFMPEGANDROID_FFPLAY_DEFINE_H
#define FFMPEGANDROID_FFPLAY_DEFINE_H
@ -57,7 +54,7 @@ class PlayerParams {
public:
std::mutex mutex;
MessageQueue *messageQueue;
FFMessageQueue *messageQueue;
int64_t videoDuration;
AVInputFormat *iformat;

Loading…
Cancel
Save