xref: /aosp_15_r20/external/openscreen/cast/streaming/offer_messages.h (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CAST_STREAMING_OFFER_MESSAGES_H_
6 #define CAST_STREAMING_OFFER_MESSAGES_H_
7 
8 #include <chrono>
9 #include <string>
10 #include <vector>
11 
12 #include "absl/strings/string_view.h"
13 #include "absl/types/optional.h"
14 #include "cast/streaming/message_fields.h"
15 #include "cast/streaming/resolution.h"
16 #include "cast/streaming/rtp_defines.h"
17 #include "cast/streaming/session_config.h"
18 #include "json/value.h"
19 #include "platform/base/error.h"
20 #include "util/simple_fraction.h"
21 
22 // This file contains the implementation of the Cast V2 Mirroring Control
23 // Protocol offer object definition.
24 namespace openscreen {
25 namespace cast {
26 
27 // If the target delay provided by the sender is not bounded by
28 // [kMinTargetDelay, kMaxTargetDelay], it will be set to
29 // kDefaultTargetPlayoutDelay.
30 constexpr auto kMinTargetPlayoutDelay = std::chrono::milliseconds(0);
31 constexpr auto kMaxTargetPlayoutDelay = std::chrono::milliseconds(5000);
32 
33 // If the sender provides an invalid maximum frame rate, it ill
34 // be set to kDefaultMaxFrameRate.
35 constexpr int kDefaultMaxFrameRate = 30;
36 
37 constexpr int kDefaultNumVideoChannels = 1;
38 constexpr int kDefaultNumAudioChannels = 2;
39 
40 // A stream, as detailed by the CastV2 protocol spec, is a segment of an
41 // offer message specifically representing a configuration object for
42 // a codec and its related fields, such as maximum bit rate, time base,
43 // and other fields.
44 // Composed classes include AudioStream and VideoStream, which contain
45 // fields specific to audio and video respectively.
46 struct Stream {
47   enum class Type : uint8_t { kAudioSource, kVideoSource };
48 
49   static Error TryParse(const Json::Value& root,
50                         Stream::Type type,
51                         Stream* out);
52   Json::Value ToJson() const;
53   bool IsValid() const;
54 
55   int index = 0;
56   Type type = {};
57 
58   // Default channel count is 1, e.g. for video.
59   int channels = 0;
60   RtpPayloadType rtp_payload_type = {};
61   Ssrc ssrc = {};
62   std::chrono::milliseconds target_delay = {};
63 
64   // AES Key and IV mask format is very strict: a 32 digit hex string that
65   // must be converted to a 16 digit byte array.
66   std::array<uint8_t, 16> aes_key = {};
67   std::array<uint8_t, 16> aes_iv_mask = {};
68   bool receiver_rtcp_event_log = false;
69   std::string receiver_rtcp_dscp;
70   int rtp_timebase = 0;
71 
72   // The codec parameter field honors the format laid out in RFC 6381:
73   // https://datatracker.ietf.org/doc/html/rfc6381.
74   std::string codec_parameter;
75 };
76 
77 struct AudioStream {
78   static Error TryParse(const Json::Value& root, AudioStream* out);
79   Json::Value ToJson() const;
80   bool IsValid() const;
81 
82   Stream stream;
83   AudioCodec codec = AudioCodec::kNotSpecified;
84   int bit_rate = 0;
85 };
86 
87 
88 struct VideoStream {
89   static Error TryParse(const Json::Value& root, VideoStream* out);
90   Json::Value ToJson() const;
91   bool IsValid() const;
92 
93   Stream stream;
94   VideoCodec codec = VideoCodec::kNotSpecified;
95   SimpleFraction max_frame_rate;
96   int max_bit_rate = 0;
97   std::string protection;
98   std::string profile;
99   std::string level;
100   std::vector<Resolution> resolutions;
101   std::string error_recovery_mode;
102 };
103 
104 struct Offer {
105   // TODO(jophba): remove deprecated declaration in a separate patch.
106   static ErrorOr<Offer> Parse(const Json::Value& root);
107   static Error TryParse(const Json::Value& root, Offer* out);
108   Json::Value ToJson() const;
109   bool IsValid() const;
110 
111   CastMode cast_mode = CastMode::kMirroring;
112   std::vector<AudioStream> audio_streams;
113   std::vector<VideoStream> video_streams;
114 };
115 
116 }  // namespace cast
117 }  // namespace openscreen
118 
119 #endif  // CAST_STREAMING_OFFER_MESSAGES_H_
120