Commit fc83b1f8 authored by Philippe Gorley's avatar Philippe Gorley Committed by Adrien Béraud

media: make observers/obervables use MediaFrame

Makes it so observers don't need to implement Observer<T> twice to
receive audio and video frames.

Change-Id: Id73c384342bd786b4f83950937b4dbd13a31fc41
parent 3fcd9b9e
......@@ -28,6 +28,7 @@
#include <future>
#include <chrono>
#include <memory>
namespace ring {
......@@ -69,7 +70,7 @@ AudioInput::process()
frame->pointer()->pts = sent_samples;
sent_samples += frame->pointer()->nb_samples;
notify(frame);
notify(std::static_pointer_cast<MediaFrame>(frame));
}
bool
......
......@@ -36,7 +36,7 @@ namespace ring {
struct MediaStream;
class Resampler;
class AudioInput : public Observable<std::shared_ptr<AudioFrame>>
class AudioInput : public Observable<std::shared_ptr<MediaFrame>>
{
public:
AudioInput(const std::string& id);
......
......@@ -30,6 +30,8 @@
#include "ringbufferpool.h"
#include "smartools.h"
#include <memory>
namespace ring {
AudioReceiveThread::AudioReceiveThread(const std::string& id,
......@@ -96,7 +98,7 @@ AudioReceiveThread::process()
case MediaDecoder::Status::FrameFinished:
audioDecoder_->writeToRingBuffer(*decodedFrame, *ringbuffer_,
mainBuffFormat);
notify(decodedFrame);
notify(std::static_pointer_cast<MediaFrame>(decodedFrame));
return;
case MediaDecoder::Status::DecodeError:
RING_WARN("decoding failure, trying to reset decoder...");
......
......@@ -36,7 +36,7 @@ class MediaIOHandle;
struct MediaStream;
class RingBuffer;
class AudioReceiveThread : public Observable<std::shared_ptr<AudioFrame>>
class AudioReceiveThread : public Observable<std::shared_ptr<MediaFrame>>
{
public:
AudioReceiveThread(const std::string &id,
......
......@@ -30,6 +30,8 @@
#include "resampler.h"
#include "smartools.h"
#include <memory>
namespace ring {
AudioSender::AudioSender(const std::string& id,
......@@ -94,7 +96,7 @@ AudioSender::setup(SocketPair& socketPair)
}
void
AudioSender::update(Observable<std::shared_ptr<ring::AudioFrame>>* /*obs*/, const std::shared_ptr<ring::AudioFrame>& framePtr)
AudioSender::update(Observable<std::shared_ptr<ring::MediaFrame>>* /*obs*/, const std::shared_ptr<ring::MediaFrame>& framePtr)
{
auto frame = framePtr->pointer();
auto ms = MediaStream("a:local", frame->format, rational<int>(1, frame->sample_rate),
......@@ -103,7 +105,7 @@ AudioSender::update(Observable<std::shared_ptr<ring::AudioFrame>>* /*obs*/, cons
ms.firstTimestamp = frame->pts;
sent_samples += frame->nb_samples;
if (audioEncoder_->encodeAudio(*framePtr) < 0)
if (audioEncoder_->encodeAudio(*std::static_pointer_cast<AudioFrame>(framePtr)) < 0)
RING_ERR("encoding failed");
}
......
......@@ -33,7 +33,7 @@ class MediaEncoder;
class MediaIOHandle;
class Resampler;
class AudioSender : public Observer<std::shared_ptr<AudioFrame>> {
class AudioSender : public Observer<std::shared_ptr<MediaFrame>> {
public:
AudioSender(const std::string& id,
const std::string& dest,
......@@ -47,8 +47,8 @@ public:
void setMuted(bool isMuted);
uint16_t getLastSeqValue();
void update(Observable<std::shared_ptr<ring::AudioFrame>>*,
const std::shared_ptr<ring::AudioFrame>&) override;
void update(Observable<std::shared_ptr<ring::MediaFrame>>*,
const std::shared_ptr<ring::MediaFrame>&) override;
private:
NON_COPYABLE(AudioSender);
......
......@@ -141,36 +141,27 @@ MediaRecorder::addStream(const MediaStream& ms)
}
void
MediaRecorder::update(Observable<std::shared_ptr<AudioFrame>>* ob, const std::shared_ptr<AudioFrame>& a)
MediaRecorder::update(Observable<std::shared_ptr<MediaFrame>>* ob, const std::shared_ptr<MediaFrame>& m)
{
if (!isRecording_)
return;
std::string name;
if (dynamic_cast<AudioReceiveThread*>(ob))
name = "a:remote";
else // ob is of type AudioInput*
else if (dynamic_cast<AudioInput*>(ob))
name = "a:local";
// copy frame to not mess with the original frame's pts
AudioFrame clone;
clone.copyFrom(*a);
clone.pointer()->pts -= streams_[name].firstTimestamp;
audioFilter_->feedInput(clone.pointer(), name);
}
void MediaRecorder::update(Observable<std::shared_ptr<VideoFrame>>* ob, const std::shared_ptr<VideoFrame>& v)
{
if (!isRecording_)
return;
std::string name;
if (dynamic_cast<video::VideoReceiveThread*>(ob))
else if (dynamic_cast<video::VideoReceiveThread*>(ob))
name = "v:remote";
else // ob is of type VideoInput*
else if (dynamic_cast<video::VideoInput*>(ob))
name = "v:local";
// copy frame to not mess with the original frame's pts
VideoFrame clone;
clone.copyFrom(*v);
// copy frame to not mess with the original frame's pts (does not actually copy frame data)
MediaFrame clone;
clone.copyFrom(*m);
clone.pointer()->pts -= streams_[name].firstTimestamp;
videoFilter_->feedInput(clone.pointer(), name);
if (clone.pointer()->width > 0 && clone.pointer()->height > 0)
videoFilter_->feedInput(clone.pointer(), name);
else
audioFilter_->feedInput(clone.pointer(), name);
}
int
......
......@@ -41,10 +41,7 @@
namespace ring {
class MediaRecorder : public Observer<std::shared_ptr<AudioFrame>>
#ifdef RING_VIDEO
, public video::VideoFramePassiveReader
#endif
class MediaRecorder : public Observer<std::shared_ptr<MediaFrame>>
, public std::enable_shared_from_this<MediaRecorder>
{
public:
......@@ -104,8 +101,7 @@ public:
/**
* Updates the recorder with an audio or video frame.
*/
void update(Observable<std::shared_ptr<AudioFrame>>* ob, const std::shared_ptr<AudioFrame>& a) override;
void update(Observable<std::shared_ptr<VideoFrame>>* ob, const std::shared_ptr<VideoFrame>& v) override;
void update(Observable<std::shared_ptr<MediaFrame>>* ob, const std::shared_ptr<MediaFrame>& a) override;
private:
NON_COPYABLE(MediaRecorder);
......
......@@ -313,10 +313,10 @@ SinkClient::SinkClient(const std::string& id, bool mixer)
{}
void
SinkClient::update(Observable<std::shared_ptr<VideoFrame>>* /*obs*/,
const std::shared_ptr<VideoFrame>& frame_p)
SinkClient::update(Observable<std::shared_ptr<MediaFrame>>* /*obs*/,
const std::shared_ptr<MediaFrame>& frame_p)
{
auto& f = *frame_p;
auto& f = *std::static_pointer_cast<VideoFrame>(frame_p);
#ifdef DEBUG_FPS
auto currentTime = std::chrono::system_clock::now();
......
......@@ -63,8 +63,8 @@ class SinkClient : public VideoFramePassiveReader
}
// as VideoFramePassiveReader
void update(Observable<std::shared_ptr<ring::VideoFrame>>*,
const std::shared_ptr<ring::VideoFrame>&) override;
void update(Observable<std::shared_ptr<ring::MediaFrame>>*,
const std::shared_ptr<ring::MediaFrame>&) override;
bool start() noexcept;
bool stop() noexcept;
......
......@@ -44,7 +44,7 @@ VideoGenerator::publishFrame()
{
std::lock_guard<std::mutex> lk(mutex_);
lastFrame_ = std::move(writableFrame_);
notify(lastFrame_);
notify(std::static_pointer_cast<MediaFrame>(lastFrame_));
}
void
......
......@@ -49,17 +49,19 @@ struct AVIOContext;
#endif
namespace DRing {
class MediaFrame;
class VideoFrame;
}
namespace ring {
using MediaFrame = DRing::MediaFrame;
using VideoFrame = DRing::VideoFrame;
}
namespace ring { namespace video {
struct VideoFrameActiveWriter: Observable<std::shared_ptr<VideoFrame>> {};
struct VideoFramePassiveReader: Observer<std::shared_ptr<VideoFrame>> {};
struct VideoFrameActiveWriter: Observable<std::shared_ptr<MediaFrame>> {};
struct VideoFramePassiveReader: Observer<std::shared_ptr<MediaFrame>> {};
/*=== VideoGenerator =========================================================*/
......
......@@ -33,7 +33,7 @@
namespace ring { namespace video {
struct VideoMixer::VideoMixerSource {
Observable<std::shared_ptr<VideoFrame>>* source = nullptr;
Observable<std::shared_ptr<MediaFrame>>* source = nullptr;
std::unique_ptr<VideoFrame> update_frame;
std::unique_ptr<VideoFrame> render_frame;
void atomic_swap_render(std::unique_ptr<VideoFrame>& other) {
......@@ -77,7 +77,7 @@ VideoMixer::~VideoMixer()
}
void
VideoMixer::attached(Observable<std::shared_ptr<VideoFrame>>* ob)
VideoMixer::attached(Observable<std::shared_ptr<MediaFrame>>* ob)
{
auto lock(rwMutex_.write());
......@@ -87,7 +87,7 @@ VideoMixer::attached(Observable<std::shared_ptr<VideoFrame>>* ob)
}
void
VideoMixer::detached(Observable<std::shared_ptr<VideoFrame>>* ob)
VideoMixer::detached(Observable<std::shared_ptr<MediaFrame>>* ob)
{
auto lock(rwMutex_.write());
......@@ -100,8 +100,8 @@ VideoMixer::detached(Observable<std::shared_ptr<VideoFrame>>* ob)
}
void
VideoMixer::update(Observable<std::shared_ptr<VideoFrame>>* ob,
const std::shared_ptr<VideoFrame>& frame_p)
VideoMixer::update(Observable<std::shared_ptr<MediaFrame>>* ob,
const std::shared_ptr<MediaFrame>& frame_p)
{
auto lock(rwMutex_.read());
......@@ -111,7 +111,7 @@ VideoMixer::update(Observable<std::shared_ptr<VideoFrame>>* ob,
x->update_frame.reset(new VideoFrame);
else
x->update_frame->reset();
x->update_frame->copyFrom(*frame_p); // copy frame content, it will be destroyed after return
x->update_frame->copyFrom(*std::static_pointer_cast<VideoFrame>(frame_p)); // copy frame content, it will be destroyed after return
x->atomic_swap_render(x->update_frame);
return;
}
......
......@@ -49,9 +49,9 @@ public:
int getPixelFormat() const override;
// as VideoFramePassiveReader
void update(Observable<std::shared_ptr<VideoFrame>>* ob, const std::shared_ptr<VideoFrame>& v) override;
void attached(Observable<std::shared_ptr<VideoFrame>>* ob) override;
void detached(Observable<std::shared_ptr<VideoFrame>>* ob) override;
void update(Observable<std::shared_ptr<MediaFrame>>* ob, const std::shared_ptr<MediaFrame>& v) override;
void attached(Observable<std::shared_ptr<MediaFrame>>* ob) override;
void detached(Observable<std::shared_ptr<MediaFrame>>* ob) override;
private:
NON_COPYABLE(VideoMixer);
......
......@@ -78,10 +78,10 @@ VideoSender::encodeAndSendVideo(VideoFrame& input_frame)
}
void
VideoSender::update(Observable<std::shared_ptr<VideoFrame>>* /*obs*/,
const std::shared_ptr<VideoFrame>& frame_p)
VideoSender::update(Observable<std::shared_ptr<MediaFrame>>* /*obs*/,
const std::shared_ptr<MediaFrame>& frame_p)
{
encodeAndSendVideo(*frame_p);
encodeAndSendVideo(*std::static_pointer_cast<VideoFrame>(frame_p));
}
void
......
......@@ -54,8 +54,8 @@ public:
void forceKeyFrame();
// as VideoFramePassiveReader
void update(Observable<std::shared_ptr<VideoFrame>>* obs,
const std::shared_ptr<VideoFrame>& frame_p) override;
void update(Observable<std::shared_ptr<MediaFrame>>* obs,
const std::shared_ptr<MediaFrame>& frame_p) override;
void setMuted(bool isMuted);
uint16_t getLastSeqValue();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment