diff --git a/src/media/audio/audio_rtp_session.cpp b/src/media/audio/audio_rtp_session.cpp
index dd5b8bcc76f3b3b6175d87897b7b195ebd128d05..edb7d7b4b74dd30649999c8a6eb28c71342b71bf 100644
--- a/src/media/audio/audio_rtp_session.cpp
+++ b/src/media/audio/audio_rtp_session.cpp
@@ -120,7 +120,7 @@ AudioSender::setup(SocketPair& socketPair)
         /* Encoder setup */
         RING_DBG("audioEncoder_->openOutput %s", dest_.c_str());
         audioEncoder_->setMuted(muteState_);
-        audioEncoder_->openOutput(dest_.c_str(), args_);
+        audioEncoder_->openOutput(dest_, args_);
         audioEncoder_->setInitSeqVal(seqVal_);
         audioEncoder_->setIOContext(muxContext_);
         audioEncoder_->startIO();
diff --git a/src/media/libav_utils.cpp b/src/media/libav_utils.cpp
index 57955678209f8a47b313e9bb063a20951d250b95..05ea52cf43a3e0b6750013d143202870493bc2ac 100644
--- a/src/media/libav_utils.cpp
+++ b/src/media/libav_utils.cpp
@@ -37,6 +37,7 @@
 
 namespace ring { namespace libav_utils {
 
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
 // protect libav/ffmpeg access
 static int
 avcodecManageMutex(void **data, enum AVLockOp op)
@@ -70,6 +71,7 @@ avcodecManageMutex(void **data, enum AVLockOp op)
     }
     return AVERROR(ret);
 }
+#endif
 
 static constexpr const char* AVLOGLEVEL = "AVLOGLEVEL";
 
@@ -134,11 +136,15 @@ androidAvLogCb(void* ptr, int level, const char* fmt, va_list vl)
 static void
 init_once()
 {
+#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58, 9, 100)
     av_register_all();
+#endif
     avdevice_register_all();
     avformat_network_init();
 
+#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100)
     av_lockmgr_register(avcodecManageMutex);
+#endif
 
     if (getDebugMode())
         setAvLogLevel();
diff --git a/src/media/media_encoder.cpp b/src/media/media_encoder.cpp
index 38de69e4ffd80dc49c4b825060a9d0603d5ca585..622dedeebb0eba8c3006d7f8c81bed0bf883364a 100644
--- a/src/media/media_encoder.cpp
+++ b/src/media/media_encoder.cpp
@@ -123,21 +123,26 @@ MediaEncoder::getEncoderName() const
 }
 
 void
-MediaEncoder::openOutput(const char *filename,
+MediaEncoder::openOutput(const std::string& filename,
                          const ring::MediaDescription& args)
 {
     setOptions(args);
-    AVOutputFormat *oformat = av_guess_format("rtp", filename, nullptr);
+    AVOutputFormat *oformat = av_guess_format("rtp", filename.c_str(), nullptr);
 
     if (!oformat) {
-        RING_ERR("Unable to find a suitable output format for %s", filename);
+        RING_ERR("Unable to find a suitable output format for %s", filename.c_str());
         throw MediaEncoderException("No output format");
     }
 
     outputCtx_->oformat = oformat;
-    strncpy(outputCtx_->filename, filename, sizeof(outputCtx_->filename));
-    // guarantee that buffer is NULL terminated
+#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 7, 100)
+    // c_str guarantees NULL termination
+    outputCtx_->url = av_strdup(filename.c_str()); // must be compatible with av_free
+#else
+    strncpy(outputCtx_->filename, filename.c_str(), sizeof(outputCtx_->filename));
+    // in case our filename is longer than the space reserved for AVFormatContext.filename
     outputCtx_->filename[sizeof(outputCtx_->filename) - 1] = '\0';
+#endif
 
     /* find the video encoder */
     if (args.codec->systemCodecInfo.avcodecId == AV_CODEC_ID_H263)
@@ -265,7 +270,11 @@ MediaEncoder::startIO()
         throw MediaEncoderException("Failed to write output file header");
     }
 
+#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(58, 7, 100)
+    av_dump_format(outputCtx_, 0, outputCtx_->url, 1);
+#else
     av_dump_format(outputCtx_, 0, outputCtx_->filename, 1);
+#endif
 }
 
 #ifdef RING_VIDEO
diff --git a/src/media/media_encoder.h b/src/media/media_encoder.h
index 6fa8941a42b12e364460a166cbebddcb525eabea..68285280a71a80241949b9e9f8d2458a03d240ca 100644
--- a/src/media/media_encoder.h
+++ b/src/media/media_encoder.h
@@ -63,7 +63,7 @@ public:
     void setInterruptCallback(int (*cb)(void*), void *opaque);
 
     void setDeviceOptions(const DeviceParams& args);
-    void openOutput(const char *filename, const MediaDescription& args);
+    void openOutput(const std::string& filename, const MediaDescription& args);
     void startIO();
     void setIOContext(const std::unique_ptr<MediaIOHandle> &ioctx);
 
diff --git a/src/media/video/video_sender.cpp b/src/media/video/video_sender.cpp
index 5d188878bca95a8b4c804ab2b8eea99361f75627..a0c6ba2c29d7037396c1d992da296c13d62ad3de 100644
--- a/src/media/video/video_sender.cpp
+++ b/src/media/video/video_sender.cpp
@@ -44,7 +44,7 @@ VideoSender::VideoSender(const std::string& dest, const DeviceParams& dev,
 {
     videoEncoder_->setDeviceOptions(dev);
     keyFrameFreq_ = dev.framerate.numerator() * KEY_FRAME_PERIOD;
-    videoEncoder_->openOutput(dest.c_str(), args);
+    videoEncoder_->openOutput(dest, args);
     videoEncoder_->setInitSeqVal(seqVal);
     videoEncoder_->setIOContext(muxContext_);
     videoEncoder_->startIO();