1 /* 2 * Copyright (c) 2017 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 "api/video/video_content_type.h" 12 13 // VideoContentType stored as a single byte, which is sent over the network. 14 // Structure: 15 // 16 // 0 1 2 3 4 5 6 7 17 // +---------------+ 18 // |r r e e e s s c| 19 // 20 // where: 21 // r - reserved bits. 22 // e - 3-bit number of an experiment group counted from 1. 0 means there's no 23 // experiment ongoing. 24 // s - 2-bit simulcast stream id or spatial layer, counted from 1. 0 means that 25 // no simulcast information is set. 26 // c - content type. 0 means real-time video, 1 means screenshare. 27 // 28 29 namespace webrtc { 30 namespace videocontenttypehelpers { 31 32 namespace { 33 static constexpr uint8_t kScreenshareBitsSize = 1; 34 static constexpr uint8_t kScreenshareBitsMask = 35 (1u << kScreenshareBitsSize) - 1; 36 37 static constexpr uint8_t kSimulcastShift = 1; 38 static constexpr uint8_t kSimulcastBitsSize = 2; 39 static constexpr uint8_t kSimulcastBitsMask = ((1u << kSimulcastBitsSize) - 1) 40 << kSimulcastShift; // 0b00000110 41 42 static constexpr uint8_t kExperimentShift = 3; 43 static constexpr uint8_t kExperimentBitsSize = 3; 44 static constexpr uint8_t kExperimentBitsMask = 45 ((1u << kExperimentBitsSize) - 1) << kExperimentShift; // 0b00111000 46 47 static constexpr uint8_t kTotalBitsSize = 48 kScreenshareBitsSize + kSimulcastBitsSize + kExperimentBitsSize; 49 } // namespace 50 SetExperimentId(VideoContentType * content_type,uint8_t experiment_id)51bool SetExperimentId(VideoContentType* content_type, uint8_t experiment_id) { 52 // Store in bits 2-4. 53 if (experiment_id >= (1 << kExperimentBitsSize)) 54 return false; 55 *content_type = static_cast<VideoContentType>( 56 (static_cast<uint8_t>(*content_type) & ~kExperimentBitsMask) | 57 ((experiment_id << kExperimentShift) & kExperimentBitsMask)); 58 return true; 59 } 60 SetSimulcastId(VideoContentType * content_type,uint8_t simulcast_id)61bool SetSimulcastId(VideoContentType* content_type, uint8_t simulcast_id) { 62 // Store in bits 5-6. 63 if (simulcast_id >= (1 << kSimulcastBitsSize)) 64 return false; 65 *content_type = static_cast<VideoContentType>( 66 (static_cast<uint8_t>(*content_type) & ~kSimulcastBitsMask) | 67 ((simulcast_id << kSimulcastShift) & kSimulcastBitsMask)); 68 return true; 69 } 70 GetExperimentId(const VideoContentType & content_type)71uint8_t GetExperimentId(const VideoContentType& content_type) { 72 return (static_cast<uint8_t>(content_type) & kExperimentBitsMask) >> 73 kExperimentShift; 74 } GetSimulcastId(const VideoContentType & content_type)75uint8_t GetSimulcastId(const VideoContentType& content_type) { 76 return (static_cast<uint8_t>(content_type) & kSimulcastBitsMask) >> 77 kSimulcastShift; 78 } 79 IsScreenshare(const VideoContentType & content_type)80bool IsScreenshare(const VideoContentType& content_type) { 81 return (static_cast<uint8_t>(content_type) & kScreenshareBitsMask) > 0; 82 } 83 IsValidContentType(uint8_t value)84bool IsValidContentType(uint8_t value) { 85 // Any 6-bit value is allowed. 86 return value < (1 << kTotalBitsSize); 87 } 88 ToString(const VideoContentType & content_type)89const char* ToString(const VideoContentType& content_type) { 90 return IsScreenshare(content_type) ? "screen" : "realtime"; 91 } 92 } // namespace videocontenttypehelpers 93 } // namespace webrtc 94