Skip to content

Commit

Permalink
with -o check format compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
mpromonet committed Jan 4, 2025
1 parent ba52b14 commit 213f221
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
1 change: 0 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

"Bedford Hills": {"video": "rtsp://73.114.177.111/axis-media/media.amp"},
"Pocapaglia": {"video": "rtsp://176.65.94.105/axis-media/media.amp"},
"Norwich": {"video": "rtsp://37.157.51.30/axis-media/media.amp"},

"Great Falls": {"video": "rtsp://76.75.8.116/axis-media/media.amp", "position":"47.551039,-111.539400"},
"Western Cape": {"video":"rtsp://196.21.92.82/axis-media/media.amp", "position":"-33.925840,18.423220"},
Expand Down
7 changes: 5 additions & 2 deletions inc/EncodedVideoFrameBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@
class EncodedVideoFrameBuffer : public webrtc::VideoFrameBuffer
{
public:
EncodedVideoFrameBuffer(int width, int height, const rtc::scoped_refptr<webrtc::EncodedImageBufferInterface> &encoded_data, webrtc::VideoFrameType frameType)
: m_width(width), m_height(height), m_encoded_data(encoded_data), m_frameType(frameType) {}
EncodedVideoFrameBuffer(int width, int height, const rtc::scoped_refptr<webrtc::EncodedImageBufferInterface> &encoded_data, webrtc::VideoFrameType frameType, const webrtc::SdpVideoFormat& format)
: m_width(width), m_height(height), m_encoded_data(encoded_data), m_frameType(frameType), m_format(format) {}
virtual Type type() const { return webrtc::VideoFrameBuffer::Type::kNative; }
virtual rtc::scoped_refptr<webrtc::I420BufferInterface> ToI420() { return nullptr; }
virtual int width() const { return m_width; }
virtual int height() const { return m_height; }

webrtc::SdpVideoFormat getFormat() const { return m_format; }

webrtc::EncodedImage getEncodedImage(uint32_t rtptime, int ntptime ) const {
webrtc::EncodedImage encoded_image;
encoded_image.SetEncodedData(webrtc::EncodedImageBuffer::Create(m_encoded_data->data(), m_encoded_data->size()));
Expand All @@ -38,4 +40,5 @@ class EncodedVideoFrameBuffer : public webrtc::VideoFrameBuffer
const int m_height;
rtc::scoped_refptr<webrtc::EncodedImageBufferInterface> m_encoded_data;
webrtc::VideoFrameType m_frameType;
webrtc::SdpVideoFormat m_format;
};
2 changes: 1 addition & 1 deletion inc/NullDecoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class NullDecoder : public webrtc::VideoDecoder {
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}
rtc::scoped_refptr<webrtc::EncodedImageBufferInterface> encodedData = input_image.GetEncodedData();
rtc::scoped_refptr<webrtc::VideoFrameBuffer> frameBuffer = rtc::make_ref_counted<EncodedVideoFrameBuffer>(m_settings.max_render_resolution().Width(), m_settings.max_render_resolution().Height(), encodedData, input_image.FrameType());
rtc::scoped_refptr<webrtc::VideoFrameBuffer> frameBuffer = rtc::make_ref_counted<EncodedVideoFrameBuffer>(m_settings.max_render_resolution().Width(), m_settings.max_render_resolution().Height(), encodedData, input_image.FrameType(), m_format);

webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
.set_video_frame_buffer(frameBuffer)
Expand Down
14 changes: 11 additions & 3 deletions inc/NullEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,26 @@ class NullEncoder : public webrtc::VideoEncoder {

int32_t Encode(const webrtc::VideoFrame& frame, const std::vector<webrtc::VideoFrameType>* frame_types) override {
if (!m_encoded_image_callback) {
RTC_LOG(LS_WARNING) << "RegisterEncodeCompleteCallback() not called";
RTC_LOG(LS_ERROR) << "RegisterEncodeCompleteCallback() not called";
return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
}

rtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer = frame.video_frame_buffer();
if (buffer->type() != webrtc::VideoFrameBuffer::Type::kNative) {
RTC_LOG(LS_WARNING) << "buffer type must be kNative";
RTC_LOG(LS_ERROR) << "buffer type must be kNative";
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}

// get webrtc::EncodedImage
EncodedVideoFrameBuffer* encodedBuffer = (EncodedVideoFrameBuffer*)buffer.get();

// check format is consistent
webrtc::SdpVideoFormat format = encodedBuffer->getFormat();
if (format.name != m_format.name) {
RTC_LOG(LS_ERROR) << "format name must be " << m_format.name << " not " << format.name;
return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
}

// get webrtc::EncodedImage
webrtc::EncodedImage encoded_image = encodedBuffer->getEncodedImage(frame.rtp_timestamp(), frame.ntp_time_ms());

RTC_LOG(LS_VERBOSE) << "EncodedImage " << frame.id() << " " << encoded_image.FrameType() << " " << buffer->width() << "x" << buffer->height();
Expand Down
18 changes: 14 additions & 4 deletions inc/V4l2Capturer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class V4l2Capturer : public VideoSource
size_t width = 0;
size_t height = 0;
size_t fps = 0;
std::string format = "H264";
if (opts.find("width") != opts.end())
{
width = std::stoi(opts.at("width"));
Expand All @@ -40,9 +41,13 @@ class V4l2Capturer : public VideoSource
{
fps = std::stoi(opts.at("fps"));
}
if (opts.find("format") != opts.end())
{
format = opts.at("format");
}

std::unique_ptr<V4l2Capturer> capturer(new V4l2Capturer());
if (!capturer->Init(width, height, fps, videourl))
if (!capturer->Init(format, width, height, fps, videourl))
{
RTC_LOG(LS_WARNING) << "Failed to create V4l2Capturer(w = " << width
<< ", h = " << height << ", fps = " << fps
Expand All @@ -62,19 +67,23 @@ class V4l2Capturer : public VideoSource
private:
V4l2Capturer() : m_stop(false) {}

bool Init(size_t width,
bool Init(const std::string &format,
size_t width,
size_t height,
size_t fps,
const std::string &videourl)
{
m_format = format;
m_width = width;
m_height = height;


std::string device = "/dev/video0";
if (videourl.find("v4l2://") == 0) {
device = videourl.substr(strlen("v4l2://"));
}
V4L2DeviceParameters param(device.c_str(), V4L2_PIX_FMT_H264, width, height, fps);

V4L2DeviceParameters param(device.c_str(), V4l2Device::fourcc(format.c_str()), width, height, fps);
m_capture.reset(V4l2Capture::create(param));

bool ret = false;
Expand Down Expand Up @@ -136,7 +145,7 @@ class V4l2Capturer : public VideoSource

int64_t ts = std::chrono::high_resolution_clock::now().time_since_epoch().count()/1000/1000;
webrtc::VideoFrameType frameType = idr ? webrtc::VideoFrameType::kVideoFrameKey : webrtc::VideoFrameType::kVideoFrameDelta;
rtc::scoped_refptr<webrtc::VideoFrameBuffer> frameBuffer = rtc::make_ref_counted<EncodedVideoFrameBuffer>(m_capture->getWidth(), m_capture->getHeight(), encodedData, frameType);
rtc::scoped_refptr<webrtc::VideoFrameBuffer> frameBuffer = rtc::make_ref_counted<EncodedVideoFrameBuffer>(m_capture->getWidth(), m_capture->getHeight(), encodedData, frameType, webrtc::SdpVideoFormat(m_format));
webrtc::VideoFrame frame = webrtc::VideoFrame::Builder()
.set_video_frame_buffer(frameBuffer)
.set_rotation(webrtc::kVideoRotation_0)
Expand Down Expand Up @@ -164,6 +173,7 @@ class V4l2Capturer : public VideoSource
std::unique_ptr<V4l2Capture> m_capture;
rtc::scoped_refptr<webrtc::EncodedImageBufferInterface> m_sps;
rtc::scoped_refptr<webrtc::EncodedImageBufferInterface> m_pps;
std::string m_format;
int m_width;
int m_height;
};

0 comments on commit 213f221

Please sign in to comment.