1 /* 2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "logging/rtc_event_log/encoder/bit_writer.h" 12 13 namespace webrtc { 14 15 namespace { BitsToBytes(size_t bits)16size_t BitsToBytes(size_t bits) { 17 return (bits / 8) + (bits % 8 > 0 ? 1 : 0); 18 } 19 } // namespace 20 WriteBits(uint64_t val,size_t bit_count)21void BitWriter::WriteBits(uint64_t val, size_t bit_count) { 22 RTC_DCHECK(valid_); 23 const bool success = bit_writer_.WriteBits(val, bit_count); 24 RTC_DCHECK(success); 25 written_bits_ += bit_count; 26 } 27 WriteBits(absl::string_view input)28void BitWriter::WriteBits(absl::string_view input) { 29 RTC_DCHECK(valid_); 30 for (char c : input) { 31 WriteBits(static_cast<unsigned char>(c), CHAR_BIT); 32 } 33 } 34 35 // Returns everything that was written so far. 36 // Nothing more may be written after this is called. GetString()37std::string BitWriter::GetString() { 38 RTC_DCHECK(valid_); 39 valid_ = false; 40 41 buffer_.resize(BitsToBytes(written_bits_)); 42 written_bits_ = 0; 43 44 std::string result; 45 std::swap(buffer_, result); 46 return result; 47 } 48 49 } // namespace webrtc 50