added some methods in queue class

This commit is contained in:
selim mustafaev 2017-01-05 23:12:03 +03:00
parent f81444d383
commit 23fd1db633
4 changed files with 74 additions and 6 deletions

View File

@ -3,6 +3,7 @@
#include "ffcpp/MediaFile.h"
#include <memory>
#include <thread>
namespace ffcpp {
@ -15,12 +16,20 @@ namespace ffcpp {
int uPitch, int vPitch) = 0;
};
enum class PlayerState {
Stopped,
Playing,
Paused
};
class Player {
private:
std::shared_ptr<IVideoSink> _vSink;
std::unique_ptr<MediaFile> _curMedia;
StreamPtr _aStream;
StreamPtr _vStream;
std::thread _decodeThread;
PlayerState _state;
public:
Player(std::shared_ptr<IVideoSink> vSink);
@ -28,6 +37,9 @@ namespace ffcpp {
void setMedia(std::string path);
void play();
private:
void decode();
};
}

View File

@ -13,10 +13,16 @@ namespace ffcpp {
private:
mutable std::mutex _mutex;
std::condition_variable _cond;
std::condition_variable _readCond;
std::condition_variable _writeCond;
std::queue<std::shared_ptr<T>> _queue;
size_t _maxSize;
public:
TSQueue(size_t maxSize): _maxSize(maxSize) {
}
bool empty() const {
LockType lock(_mutex);
return _queue.empty();
@ -26,7 +32,44 @@ namespace ffcpp {
auto data = std::make_shared<T>(std::move(value));
LockType lock(_mutex);
_queue.push(data);
_cond.notify_one();
_readCond.notify_one();
}
void pushOrWait(T value) {
auto data = std::make_shared<T>(std::move(value));
std::unique_lock<std::mutex> lock(_mutex);
if(_queue.size() == _maxSize) {
_writeCond.wait(lock, [this]{ return _queue.size() < _maxSize; });
}
_queue.push(data);
_readCond.notify_one();
}
std::shared_ptr<T> waitAndPop() {
std::unique_lock<std::mutex> lock(_mutex);
_readCond.wait(lock, [this]{ return !_queue.empty(); });
auto res = _queue.front();
_queue.pop();
return res;
}
std::shared_ptr<T> tryPop() {
LockType lock(_mutex);
if(_queue.empty())
return std::shared_ptr<T>();
auto res = _queue.front();
_queue.pop();
return res;
}
std::shared_ptr<T> popOrWait() {
std::unique_lock<std::mutex> lock(_mutex);
if(_queue.empty()) {
_readCond.wait(lock, [this]{ return !_queue.empty(); });
}
auto res = _queue.front();
_queue.pop();
return res;
}
};

View File

@ -1,7 +1,9 @@
project(ffcpp)
find_package(FFMPEG REQUIRED)
include_directories(${FFMPEG_INCLUDE_DIR})
# FIXME: FFMPEG_INCLUDE_DIR is incorrect and causes errors
# http://stackoverflow.com/questions/35982639/ctime-std-namespace-conflict
#include_directories(${FFMPEG_INCLUDE_DIR})
link_directories(${FFMPEG_LIBRARY_DIRS})
if(NOT FFMPEG_FOUND)

View File

@ -1,11 +1,17 @@
#include "ffcpp/Player.h"
#include "ffcpp/Stream.h"
#include "ffcpp/Scaler.h"
#include <iostream>
namespace ffcpp {
Player::Player(std::shared_ptr<IVideoSink> vSink): _vSink(vSink), _curMedia(nullptr), _aStream(nullptr), _vStream(
nullptr) {
Player::Player(std::shared_ptr<IVideoSink> vSink): _vSink(vSink),
_curMedia(nullptr),
_aStream(nullptr),
_vStream(nullptr),
_decodeThread(&Player::decode, this),
_state(PlayerState::Stopped)
{
init();
}
@ -36,8 +42,13 @@ namespace ffcpp {
frame = scaler.scale(frame);
AVFrame* f = frame;
//_vSink->drawFrame(f->data, f->linesize[0]);
_vSink->drawPlanarYUVFrame(f->data[0], f->data[1], f->data[2], f->linesize[0], f->linesize[1], f->linesize[2]);
_vSink->drawPlanarYUVFrame(f->data[0], f->data[1], f->data[2],
f->linesize[0], f->linesize[1], f->linesize[2]);
}
}
}
void Player::decode() {
std::cout << "decode function started" << std::endl;
}
}