Commit 9920b38d authored by Hugo Lefeuvre's avatar Hugo Lefeuvre Committed by Philippe Gorley

move recording code from encoder to audio_rtp

It is bad design to have recording related code in the encoder. The
encoder should handle exclusively encoding related tasks.

Move recording related code to audio_rtp.

Change-Id: I4e2c59b09d40d1ae07b88cfb61865312a69c0942
Reviewed-by: Philippe Gorley's avatarPhilippe Gorley <philippe.gorley@savoirfairelinux.com>
parent 496fbbd6
......@@ -31,6 +31,7 @@
#endif //RING_VIDEO
#include "socket_pair.h"
#include "media_recorder.h"
#include "media_encoder.h"
#include "media_decoder.h"
#include "media_io_handle.h"
......@@ -73,6 +74,10 @@ class AudioSender {
std::unique_ptr<MediaIOHandle> muxContext_;
std::unique_ptr<Resampler> resampler_;
std::weak_ptr<MediaRecorder> recorder_;
bool recordingStarted_ = false;
unsigned sent_samples = 0;
AudioBuffer micData_;
AudioBuffer resampledData_;
const uint16_t seqVal_;
......@@ -109,6 +114,8 @@ AudioSender::AudioSender(const std::string& id,
AudioSender::~AudioSender()
{
if (auto rec = recorder_.lock())
rec->stopRecording();
loop_.join();
}
......@@ -147,6 +154,16 @@ AudioSender::cleanup()
resampledData_.clear();
}
// seq: frame number for video, sent samples audio
// sampleFreq: fps for video, sample rate for audio
// clock: stream time base (packetization interval times)
// FIXME duplicate code from media_encoder
static int64_t
getNextTimestamp(int64_t seq, rational<int64_t> sampleFreq, rational<int64_t> clock)
{
return (seq / (sampleFreq * clock)).real<int64_t>();
}
void
AudioSender::process()
{
......@@ -192,7 +209,26 @@ AudioSender::process()
if (muteState_) // audio is muted, set samples to 0
buffer.reset();
if (audioEncoder_->encodeAudio(buffer.toAVFrame()) < 0)
AVFrame* frame = buffer.toAVFrame();
auto ms = MediaStream("audio", buffer.getFormat());
frame->pts = getNextTimestamp(sent_samples, ms.sampleRate, static_cast<rational<int64_t>>(ms.timeBase));
sent_samples += frame->nb_samples;
{
auto rec = recorder_.lock();
if (rec && !recordingStarted_ && rec->addStream(false, false, ms) >= 0) {
recordingStarted_ = true;
}
if (rec && recordingStarted_) {
rec->recordData(frame, false, false);
} else {
recordingStarted_ = false;
recorder_ = std::weak_ptr<MediaRecorder>();
}
}
if (audioEncoder_->encodeAudio(frame) < 0)
RING_ERR("encoding failed");
}
void
......@@ -211,8 +247,11 @@ AudioSender::getLastSeqValue()
void
AudioSender::initRecorder(std::shared_ptr<MediaRecorder>& rec)
{
if (audioEncoder_)
audioEncoder_->initRecorder(rec);
recordingStarted_ = false;
recorder_ = rec;
if (auto r = recorder_.lock()) {
r->incrementStreams(1);
}
}
class AudioReceiveThread
......
......@@ -24,7 +24,6 @@
#include "media_encoder.h"
#include "media_buffer.h"
#include "media_io_handle.h"
#include "media_recorder.h"
#include "audio/audiobuffer.h"
#include "string_utils.h"
......@@ -50,8 +49,6 @@ MediaEncoder::MediaEncoder()
MediaEncoder::~MediaEncoder()
{
if (auto rec = recorder_.lock())
rec->stopRecording();
if (outputCtx_) {
if (outputCtx_->priv_data)
av_write_trailer(outputCtx_);
......@@ -344,7 +341,7 @@ MediaEncoder::startIO()
// seq: frame number for video, sent samples audio
// sampleFreq: fps for video, sample rate for audio
// clock: stream time base (packetization interval times)
int64_t
static int64_t
getNextTimestamp(int64_t seq, rational<int64_t> sampleFreq, rational<int64_t> clock)
{
return (seq / (sampleFreq * clock)).real<int64_t>();
......@@ -383,25 +380,8 @@ int MediaEncoder::encodeAudio(AVFrame* frame)
auto enc = encoders_[currentStreamIdx_];
frame->pts = getNextTimestamp(sent_samples, enc->sample_rate, enc->time_base);
sent_samples += frame->nb_samples;
if (auto rec = recorder_.lock()) {
if (!recordingStarted_) {
auto ms = MediaStream("", encoders_[currentStreamIdx_], frame->pts);
if (rec->addStream(false, false, ms) >= 0) {
recordingStarted_ = true;
} else {
recorder_ = std::weak_ptr<MediaRecorder>();
}
}
if (recordingStarted_)
rec->recordData(frame, false, false);
} else {
recordingStarted_ = false;
recorder_ = std::weak_ptr<MediaRecorder>();
}
encode(frame, currentStreamIdx_);
av_frame_free(&frame);
return 0;
}
......@@ -658,15 +638,4 @@ MediaEncoder::getStreamCount() const
return 0;
}
void
MediaEncoder::initRecorder(std::shared_ptr<MediaRecorder>& rec)
{
// recording will start once we can send an AVPacket to the recorder
recordingStarted_ = false;
recorder_ = rec;
if (auto r = recorder_.lock()) {
r->incrementStreams(1);
}
}
} // namespace ring
......@@ -98,8 +98,6 @@ public:
unsigned getStreamCount() const;
void initRecorder(std::shared_ptr<MediaRecorder>& rec);
private:
NON_COPYABLE(MediaEncoder);
void setOptions(const MediaDescription& args);
......@@ -122,9 +120,6 @@ private:
int scaledFrameBufferSize_ = 0;
bool is_muted = false;
std::weak_ptr<MediaRecorder> recorder_;
bool recordingStarted_ = false;
protected:
AVDictionary *options_ = nullptr;
DeviceParams device_;
......
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