Commit e059062a authored by Philippe Gorley's avatar Philippe Gorley

encoder: cleanup and simplify

Cleans up the encoder a bit and removes methods that weren't used or did
nothing. In any case, it should not be the encoder's responsibility to
mute audio/video.

Removes direct accesses to private data. All these options are accessible
through the av_opt_* API with the AV_OPT_SEARCH_CHILDREN flag.

Adding streams to the encoder is now done separately than opening an RTP
output in an effort to streamline the encoder setup (file vs RTP).

Change-Id: I7a868d098fa942697cfbe3246f368fb9fc7bfb0f
parent 9de822ce
......@@ -69,11 +69,12 @@ AudioSender::setup(SocketPair& socketPair)
try {
/* Encoder setup */
RING_DBG("audioEncoder_->openLiveOutput %s", dest_.c_str());
audioEncoder_->setMuted(muteState_);
audioEncoder_->openLiveOutput(dest_, args_);
RING_DBG("audioEncoder_->openOutput %s", dest_.c_str());
audioEncoder_->openOutput(dest_, "rtp");
audioEncoder_->setOptions(args_);
audioEncoder_->addStream(args_.codec->systemCodecInfo);
audioEncoder_->setInitSeqVal(seqVal_);
audioEncoder_->setIOContext(muxContext_);
audioEncoder_->setIOContext(muxContext_->getContext());
audioEncoder_->startIO();
} catch (const MediaEncoderException &e) {
RING_ERR("%s", e.what());
......@@ -114,7 +115,6 @@ AudioSender::setMuted(bool isMuted)
{
muteState_ = isMuted;
audioInput_->setMuted(isMuted);
audioEncoder_->setMuted(isMuted);
}
uint16_t
......
This diff is collapsed.
......@@ -41,18 +41,14 @@
#include <vector>
struct AVCodecContext;
struct AVStream;
struct AVFormatContext;
struct AVDictionary;
struct AVCodec;
namespace ring {
class AudioBuffer;
class MediaIOHandle;
struct MediaDescription;
struct AccountCodecInfo;
class MediaRecorder;
class MediaEncoderException : public std::runtime_error {
public:
......@@ -64,14 +60,13 @@ public:
MediaEncoder();
~MediaEncoder();
void setInterruptCallback(int (*cb)(void*), void *opaque);
void openOutput(const std::string& filename, const std::string& format="");
void setDeviceOptions(const DeviceParams& args);
void openLiveOutput(const std::string& filename, const MediaDescription& args);
void openFileOutput(const std::string& filename, std::map<std::string, std::string> options);
int addStream(const SystemCodecInfo& codec, std::string parameters = "");
void setOptions(const MediaDescription& args);
void setOptions(std::map<std::string, std::string> options);
int addStream(const SystemCodecInfo& codec);
void setIOContext(AVIOContext* ioctx);
void startIO();
void setIOContext(const std::unique_ptr<MediaIOHandle> &ioctx);
bool send(AVPacket& packet, int streamIdx = -1);
......@@ -93,7 +88,6 @@ public:
int getWidth() const { return device_.width; }
int getHeight() const { return device_.height; }
void setMuted(bool isMuted);
void setInitSeqVal(uint16_t seqVal);
uint16_t getLastSeqValue();
std::string getEncoderName() const;
......@@ -105,8 +99,6 @@ public:
private:
NON_COPYABLE(MediaEncoder);
void setOptions(const MediaDescription& args);
void setScaleDest(void *data, int width, int height, int pix_fmt);
AVCodecContext* prepareEncoderContext(AVCodec* outputCodec, bool is_video);
void forcePresetX264(AVCodecContext* encoderCtx);
void extractProfileLevelID(const std::string &parameters, AVCodecContext *ctx);
......@@ -123,7 +115,6 @@ private:
std::vector<uint8_t> scaledFrameBuffer_;
int scaledFrameBufferSize_ = 0;
bool is_muted = false;
protected:
void readConfig(AVDictionary** dict, AVCodecContext* encoderCtx);
......
......@@ -225,7 +225,8 @@ MediaRecorder::initRecord()
encoderOptions["channels"] = std::to_string(audioStream.nbChannels);
}
encoder_->openFileOutput(getPath(), encoderOptions);
encoder_->openOutput(getPath());
encoder_->setOptions(encoderOptions);
if (hasVideo_) {
auto videoCodec = std::static_pointer_cast<ring::SystemVideoCodecInfo>(
......@@ -248,8 +249,7 @@ MediaRecorder::initRecord()
}
try {
std::unique_ptr<MediaIOHandle> ioHandle;
encoder_->setIOContext(ioHandle);
encoder_->setIOContext(nullptr);
encoder_->startIO();
} catch (const MediaEncoderException& e) {
RING_ERR() << "Could not start recorder: " << e.what();
......
......@@ -42,11 +42,13 @@ VideoSender::VideoSender(const std::string& dest, const DeviceParams& dev,
: muxContext_(socketPair.createIOContext(mtu))
, videoEncoder_(new MediaEncoder)
{
videoEncoder_->setDeviceOptions(dev);
keyFrameFreq_ = dev.framerate.numerator() * KEY_FRAME_PERIOD;
videoEncoder_->openLiveOutput(dest, args);
videoEncoder_->openOutput(dest, "rtp");
videoEncoder_->setDeviceOptions(dev);
videoEncoder_->setOptions(args);
videoEncoder_->addStream(args.codec->systemCodecInfo);
videoEncoder_->setInitSeqVal(seqVal);
videoEncoder_->setIOContext(muxContext_);
videoEncoder_->setIOContext(muxContext_->getContext());
videoEncoder_->startIO();
videoEncoder_->print_sdp();
......@@ -101,12 +103,6 @@ VideoSender::forceKeyFrame()
++forceKeyFrame_;
}
void
VideoSender::setMuted(bool isMuted)
{
videoEncoder_->setMuted(isMuted);
}
uint16_t
VideoSender::getLastSeqValue()
{
......
......@@ -57,7 +57,6 @@ public:
void update(Observable<std::shared_ptr<MediaFrame>>* obs,
const std::shared_ptr<MediaFrame>& frame_p) override;
void setMuted(bool isMuted);
uint16_t getLastSeqValue();
bool useCodec(const AccountVideoCodecInfo* codec) const;
......
......@@ -48,7 +48,6 @@ private:
CPPUNIT_TEST_SUITE_END();
std::unique_ptr<MediaEncoder> encoder_;
std::unique_ptr<MediaIOHandle> ioHandle_;
std::vector<std::string> files_;
};
......@@ -162,7 +161,8 @@ MediaEncoderTest::testMultiStream()
);
try {
encoder_->openFileOutput("test.mkv", options);
encoder_->openOutput("test.mkv");
encoder_->setOptions(options);
int videoIdx = encoder_->addStream(*vp8Codec.get());
CPPUNIT_ASSERT(videoIdx >= 0);
CPPUNIT_ASSERT(encoder_->getStreamCount() == 1);
......@@ -170,7 +170,7 @@ MediaEncoderTest::testMultiStream()
CPPUNIT_ASSERT(audioIdx >= 0);
CPPUNIT_ASSERT(videoIdx != audioIdx);
CPPUNIT_ASSERT(encoder_->getStreamCount() == 2);
encoder_->setIOContext(ioHandle_);
encoder_->setIOContext(nullptr);
encoder_->startIO();
int sentSamples = 0;
AVFrame* audio = nullptr;
......
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