diff --git a/webrtc/modules/BUILD.gn b/webrtc/modules/BUILD.gn index 46a2aa2ac0..4abd193343 100644 --- a/webrtc/modules/BUILD.gn +++ b/webrtc/modules/BUILD.gn @@ -495,6 +495,7 @@ if (rtc_include_tests) { "video_coding/codecs/vp8/simulcast_unittest.h", "video_coding/decoding_state_unittest.cc", "video_coding/frame_buffer2_unittest.cc", + "video_coding/h264_sprop_parameter_sets_unittest.cc", "video_coding/h264_sps_pps_tracker_unittest.cc", "video_coding/histogram_unittest.cc", "video_coding/include/mock/mock_vcm_callbacks.h", diff --git a/webrtc/modules/video_coding/BUILD.gn b/webrtc/modules/video_coding/BUILD.gn index cd43b83217..4e933e5c58 100644 --- a/webrtc/modules/video_coding/BUILD.gn +++ b/webrtc/modules/video_coding/BUILD.gn @@ -29,6 +29,8 @@ rtc_static_library("video_coding") { "generic_decoder.h", "generic_encoder.cc", "generic_encoder.h", + "h264_sprop_parameter_sets.cc", + "h264_sprop_parameter_sets.h", "h264_sps_pps_tracker.cc", "h264_sps_pps_tracker.h", "histogram.cc", diff --git a/webrtc/modules/video_coding/h264_sprop_parameter_sets.cc b/webrtc/modules/video_coding/h264_sprop_parameter_sets.cc new file mode 100644 index 0000000000..b9f32d7f01 --- /dev/null +++ b/webrtc/modules/video_coding/h264_sprop_parameter_sets.cc @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/modules/video_coding/h264_sprop_parameter_sets.h" + +#include +#include + +#include "webrtc/base/base64.h" +#include "webrtc/base/basictypes.h" +#include "webrtc/base/logging.h" + +namespace { + +bool DecodeAndConvert(const std::string& base64, std::vector* binary) { + // TODO(johan): Directly decode to std::vector when available. + std::vector tmp; + if (!rtc::Base64::DecodeFromArray(base64.data(), base64.size(), + rtc::Base64::DO_STRICT, &tmp, nullptr)) { + return false; + } + const uint8_t* data = reinterpret_cast(tmp.data()); + binary->assign(data, data + tmp.size()); + return true; +} +} // namespace + +namespace webrtc { + +bool H264SpropParameterSets::DecodeSprop(const std::string& sprop) { + size_t separator_pos = sprop.find(','); + if ((separator_pos <= 0) || (separator_pos >= sprop.length() - 1)) { + LOG(LS_WARNING) << "Invalid seperator position " << separator_pos << " *" + << sprop << "*"; + return false; + } + std::string sps_str = sprop.substr(0, separator_pos); + std::string pps_str = sprop.substr(separator_pos + 1, std::string::npos); + if (!DecodeAndConvert(sps_str, &sps_)) { + LOG(LS_WARNING) << "Failed to decode sprop/sps *" << sprop << "*"; + return false; + } + if (!DecodeAndConvert(pps_str, &pps_)) { + LOG(LS_WARNING) << "Failed to decode sprop/pps *" << sprop << "*"; + return false; + } + return true; +} + +} // namespace webrtc diff --git a/webrtc/modules/video_coding/h264_sprop_parameter_sets.h b/webrtc/modules/video_coding/h264_sprop_parameter_sets.h new file mode 100644 index 0000000000..c7bc6da77c --- /dev/null +++ b/webrtc/modules/video_coding/h264_sprop_parameter_sets.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_MODULES_VIDEO_CODING_H264_SPROP_PARAMETER_SETS_H_ +#define WEBRTC_MODULES_VIDEO_CODING_H264_SPROP_PARAMETER_SETS_H_ + +#include +#include + +#include "webrtc/base/common.h" + +namespace webrtc { + +class H264SpropParameterSets { + public: + H264SpropParameterSets() {} + bool DecodeSprop(const std::string& sprop); + const std::vector& sps_nalu() { return sps_; } + const std::vector& pps_nalu() { return pps_; } + + private: + std::vector sps_; + std::vector pps_; + RTC_DISALLOW_COPY_AND_ASSIGN(H264SpropParameterSets); +}; + +} // namespace webrtc + +#endif // WEBRTC_MODULES_VIDEO_CODING_H264_SPROP_PARAMETER_SETS_H_ diff --git a/webrtc/modules/video_coding/h264_sprop_parameter_sets_unittest.cc b/webrtc/modules/video_coding/h264_sprop_parameter_sets_unittest.cc new file mode 100644 index 0000000000..29b6617481 --- /dev/null +++ b/webrtc/modules/video_coding/h264_sprop_parameter_sets_unittest.cc @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/modules/video_coding/h264_sprop_parameter_sets.h" + +#include +#include "webrtc/base/basictypes.h" +#include "webrtc/test/gtest.h" + +namespace webrtc { + +class H264SpropParameterSetsTest : public testing::Test { + public: + H264SpropParameterSets h264_sprop; +}; + +TEST_F(H264SpropParameterSetsTest, Base64DecodeSprop) { + // Example sprop string from https://tools.ietf.org/html/rfc3984 . + EXPECT_TRUE(h264_sprop.DecodeSprop("Z0IACpZTBYmI,aMljiA==")); + static const std::vector raw_sps{0x67, 0x42, 0x00, 0x0A, 0x96, + 0x53, 0x05, 0x89, 0x88}; + static const std::vector raw_pps{0x68, 0xC9, 0x63, 0x88}; + EXPECT_EQ(raw_sps, h264_sprop.sps_nalu()); + EXPECT_EQ(raw_pps, h264_sprop.pps_nalu()); +} + +TEST_F(H264SpropParameterSetsTest, InvalidData) { + EXPECT_FALSE(h264_sprop.DecodeSprop(",")); + EXPECT_FALSE(h264_sprop.DecodeSprop("")); + EXPECT_FALSE(h264_sprop.DecodeSprop(",iA==")); + EXPECT_FALSE(h264_sprop.DecodeSprop("iA==,")); + EXPECT_TRUE(h264_sprop.DecodeSprop("iA==,iA==")); + EXPECT_FALSE(h264_sprop.DecodeSprop("--,--")); + EXPECT_FALSE(h264_sprop.DecodeSprop(",,")); + EXPECT_FALSE(h264_sprop.DecodeSprop("iA==")); +} + +} // namespace webrtc