sheave_core/handlers/rtmp_context.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
mod last_chunk;
use std::{
collections::HashMap,
sync::Arc,
time::Duration
};
use crate::{
handshake::{
EncryptionAlgorithm,
Handshake
},
messages::{
ChunkSize,
WindowAcknowledgementSize,
PeerBandwidth,
amf::v0::{
Number,
AmfString,
Object
}
},
flv::Flv
};
use super::PublisherStatus;
pub use self::last_chunk::*;
/// RTMP's common contexts.
///
/// Many fields are optional by default.
/// Because these data need for both of client and server but are sent/received later.
/// Therefore the `default` constructor has been prepared instead of such as `new`.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// // When you create this struct.
/// RtmpContext::default();
/// ```
#[derive(Debug)]
pub struct RtmpContext {
signed: bool,
receiving_chunk_size: ChunkSize,
sending_chunk_size: ChunkSize,
window_acknowledgement_size: WindowAcknowledgementSize,
peer_bandwidth: PeerBandwidth,
last_transaction_id: Number,
app: Option<AmfString>,
playpath: Option<AmfString>,
tc_url: Option<AmfString>,
publisher_status: Option<PublisherStatus>,
encryption_algorithm: Option<EncryptionAlgorithm>,
client_handshake: Option<Handshake>,
server_handshake: Option<Handshake>,
command_object: Option<Object>,
properties: Option<Object>,
information: Option<Object>,
message_id: Option<u32>,
publishing_name: Option<AmfString>,
publishing_type: Option<AmfString>,
await_duration: Option<Duration>,
input: Option<Flv>,
last_received_chunks: HashMap<u16, LastChunk>,
last_sent_chunks: HashMap<u16, LastChunk>
}
impl Default for RtmpContext {
fn default() -> Self {
Self {
signed: bool::default(),
receiving_chunk_size: ChunkSize::default(),
sending_chunk_size: ChunkSize::default(),
window_acknowledgement_size: WindowAcknowledgementSize::default(),
peer_bandwidth: PeerBandwidth::default(),
last_transaction_id: Number::default(),
app: Option::default(),
playpath: Option::default(),
tc_url: Option::default(),
publisher_status: Option::default(),
encryption_algorithm: Option::default(),
client_handshake: Option::default(),
server_handshake: Option::default(),
command_object: Option::default(),
properties: Option::default(),
information: Option::default(),
message_id: Option::default(),
publishing_name: Option::default(),
publishing_type: Option::default(),
await_duration: Option::default(),
input: Option::default(),
last_received_chunks: HashMap::default(),
last_sent_chunks: HashMap::default()
}
}
}
impl RtmpContext {
/// Gets a mutable reference via this wrapped by `Arc`.
/// Sheave uses this after wrapping into `Arc`.
/// Because of making this shareable between every handling steps.
pub fn make_weak_mut<'a>(self: &'a Arc<Self>) -> &'a mut Self {
unsafe { &mut *(Arc::downgrade(self).as_ptr() as *mut Self) }
}
/// Stores a flag to mean this handshake is signed.
pub fn set_signed(&mut self, signed: bool) {
self.signed = signed;
}
/// Indicates whether the handshake is signed.
pub fn is_signed(&mut self) -> bool {
self.signed
}
/// Sets a chunking size which reads from streams.
pub fn set_receiving_chunk_size(&mut self, chunk_size: ChunkSize) {
self.receiving_chunk_size = chunk_size;
}
/// Gets a chunking size which reads from streams.
pub fn get_receiving_chunk_size(&mut self) -> ChunkSize {
self.receiving_chunk_size
}
/// Sets a chunking size which writes into streams.
pub fn set_sending_chunk_size(&mut self, chunk_size: ChunkSize) {
self.sending_chunk_size = chunk_size;
}
/// Gets a chunkign size which writes into stream.
pub fn get_sending_chunk_size(&mut self) -> ChunkSize {
self.sending_chunk_size
}
/// Sets the window acknowledgement size.
pub fn set_window_acknowledgement_size(&mut self, window_acknowledgement_size: WindowAcknowledgementSize) {
self.window_acknowledgement_size = window_acknowledgement_size;
}
/// Gets the window acknowledgement size.
pub fn get_window_acknowledgement_size(&mut self) -> WindowAcknowledgementSize {
self.window_acknowledgement_size
}
/// Sets the peer bandwidth.
pub fn set_peer_bandwidth(&mut self, peer_bandwidth: PeerBandwidth) {
self.peer_bandwidth = peer_bandwidth;
}
/// Gets the peer bandwidth.
pub fn get_peer_bandwidth(&mut self) -> PeerBandwidth {
self.peer_bandwidth
}
/// Sets a transaction ID.
/// Mainly, this is used by server side contexts.
/// Because of servers should echo same transaction ID in its response.
pub fn set_transaction_id(&mut self, transaction_id: Number) {
self.last_transaction_id = transaction_id;
}
/// Gets a transaction ID sent.
pub fn get_transaction_id(&mut self) -> Number {
self.last_transaction_id
}
/// Increases current transaction ID.
/// Mainly, this is used by client side contexts.
/// Because of clients should count which transaction is it now on.
pub fn increase_transaction_id(&mut self) {
self.last_transaction_id += 1f64;
}
/// Sets the `app` name.
/// This can be contained in a request URI of RTMP.
pub fn set_app(&mut self, app: AmfString) {
self.app = Some(app);
}
/// Gets the `app` name.
/// Note this can return `None`. e.g. When this field is default as it is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_app().is_none())
/// ```
pub fn get_app(&mut self) -> Option<&AmfString> {
self.app.as_ref()
}
/// Sets the `tcUrl`. This is a full URL in the RTMP request like following form.
///
/// `rtmp://hostname/[app]/[playpath]`
///
/// Where `app` and `playpath` can be unspecified.
/// Clients specify above URL at the start of RTMP requests.
/// Then the server checks `app` and `playpath` in client-side `Connect` commands (if they are specified).
pub fn set_tc_url(&mut self, tc_url: AmfString) {
self.tc_url = Some(tc_url);
}
/// Gets the `tcUrl`.
/// Note this can return `None`. e.g. this field is default as it is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_tc_url().is_none())
/// ```
pub fn get_tc_url(&mut self) -> Option<&AmfString> {
self.tc_url.as_ref()
}
/// Sets one of status to mean which a publication client is in.
pub fn set_publisher_status(&mut self, status: PublisherStatus) {
self.publisher_status = Some(status);
}
/// Gets one of status to mean which a publication client is in.
/// Note this can return `None`. e.g. When this field is default as it is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_publisher_status().is_none());
/// ```
pub fn get_publisher_status(&mut self) -> Option<PublisherStatus> {
self.publisher_status
}
/// Stores the algorithm to encrypt this handshake.
pub fn set_encryption_algorithm(&mut self, encryption_algorithm: EncryptionAlgorithm) {
self.encryption_algorithm = Some(encryption_algorithm);
}
/// Gets specieifed algorithm to encrypt this handshake.
/// Note this can return `None`. e.g. When is as the default is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_encryption_algorithm().is_none())
/// ```
pub fn get_encryption_algorithm(&mut self) -> Option<EncryptionAlgorithm> {
self.encryption_algorithm
}
/// Stores a cleint-side handshake bytes.
pub fn set_client_handshake(&mut self, handshake: Handshake) {
self.client_handshake = Some(handshake);
}
/// Gets a client-side handshake bytes.
/// Note this can return `None`. e.g. When is as the default is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_client_handshake().is_none())
/// ```
pub fn get_client_handshake(&mut self) -> Option<&Handshake> {
self.client_handshake.as_ref()
}
/// Gets a client-side handshake bytes as mutable.
/// Note:
///
/// * This is currently used for only testing (also intagration tests contained).
/// * This can return `None`. e.g. When is as the default is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_client_handshake_mut().is_none())
/// ```
pub fn get_client_handshake_mut(&mut self) -> Option<&mut Handshake> {
self.client_handshake.as_mut()
}
/// Stores a server-side handshake bytes.
pub fn set_server_handshake(&mut self, handshake: Handshake) {
self.server_handshake = Some(handshake);
}
/// Gets a server-side handshake bytes.
/// Note this can return `None`. e.g. When it is the default as is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_server_handshake().is_none())
/// ```
pub fn get_server_handshake(&mut self) -> Option<&Handshake> {
self.server_handshake.as_ref()
}
/// Sets a command object sent from a client.
pub fn set_command_object(&mut self, command_object: Object) {
self.command_object = Some(command_object);
}
/// Gets a command object sent from a client.
/// Note this can return `None`. e.g. When it is default as is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_command_object().is_none())
/// ```
pub fn get_command_object(&mut self) -> Option<&Object> {
self.command_object.as_ref()
}
/// Sets a properties object of a server.
pub fn set_properties(&mut self, properties: Object) {
self.properties = Some(properties);
}
/// Gets a properties object of a server.
/// Note this can return `None`. e.g. When it is the dafault as is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_properties().is_none())
/// ```
pub fn get_properties(&mut self) -> Option<&Object> {
self.properties.as_ref()
}
/// Sets a information object of a server.
pub fn set_information(&mut self, information: Object) {
self.information = Some(information);
}
/// Gets a information object of a server.
/// Note this can return `None`. e.g. When it is the default as is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_information().is_none())
/// ```
pub fn get_information(&mut self) -> Option<&Object> {
self.information.as_ref()
}
/// Sets a `playpath` (e.g. filename) sent from a client.
pub fn set_playpath(&mut self, playpath: AmfString) {
self.playpath = Some(playpath);
}
/// Resets a `playpath` from this context.
/// This is prepared for deleting the `playpath` when receives the `FCUnpublish` command.
pub fn reset_playpath(&mut self) {
self.playpath = None;
}
/// Gets a `playpath` (e.g. filename) sent from a client.
/// Note this can return `None`. e.g. When it is the default as is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_playpath().is_none())
/// ```
pub fn get_playpath(&mut self) -> Option<&AmfString> {
self.playpath.as_ref()
}
/// Sets a message ID of this stream.
pub fn set_message_id(&mut self, message_id: u32) {
self.message_id = Some(message_id);
}
/// Resets a message ID from this context.
/// This is prepared for deleting tne `message_id` when receives the `deleteStream` command.
pub fn reset_message_id(&mut self) {
self.message_id = None;
}
/// Gets a message ID of this stream.
/// Note this can return `None`. e.g. When it is the default as is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_message_id().is_none());
/// ```
pub fn get_message_id(&mut self) -> Option<u32> {
self.message_id
}
/// Sets a publishing name of this stream.
pub fn set_publishing_name(&mut self, publishing_name: AmfString) {
self.publishing_name = Some(publishing_name);
}
/// Gets a publishing name of this stream.
/// Note this can return `None`. e.g. When it is the default as is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_publishing_name().is_none())
/// ```
pub fn get_publishing_name(&mut self) -> Option<&AmfString> {
self.publishing_name.as_ref()
}
/// Sets a publishing type of this stream.
pub fn set_publishing_type(&mut self, publishing_type: AmfString) {
self.publishing_type = Some(publishing_type);
}
/// Gets a publishing type of this stream.
/// Note this can return `None`. e.g. When it is the default as is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_publishing_type().is_none())
/// ```
pub fn get_publishing_type(&mut self) -> Option<&AmfString> {
self.publishing_type.as_ref()
}
/// Sets a duration for awaiting of receiving some message.
///
/// Currently, this is used only clients during publishing audio/video data.
pub fn set_await_duration(&mut self, await_duration: Duration) {
self.await_duration = Some(await_duration);
}
/// Gets a duration for awaiting of receiving some message.
///
/// Currently, this is used only clients during publishing audio/video data.
pub fn get_await_duration(&mut self) -> Option<Duration> {
self.await_duration
}
/// Sets input file/device.
pub fn set_input(&mut self, input: Flv) {
self.input = Some(input);
}
/// Gets input file/device.
/// Note this can return `None`. e.g. When it is the default as is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_input().is_none())
/// ```
pub fn get_input(&mut self) -> Option<&Flv> {
self.input.as_ref()
}
/// Gets input file/device as mutable.
/// Note this can return `None`. e.g. When it is the default as is.
///
/// # Examples
///
/// ```rust
/// use sheave_core::handlers::RtmpContext;
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_input_mut().is_none())
/// ```
pub fn get_input_mut(&mut self) -> Option<&mut Flv> {
self.input.as_mut()
}
/// Stores a last received chunk.
pub fn insert_received_chunk(&mut self, chunk_id: u16, last_chunk: LastChunk) {
self.last_received_chunks.insert(chunk_id, last_chunk);
}
/// Loads a last received chunk.
/// If no last chunk is stored associated with specified ID, this returns `None`.
///
/// # Examples
///
/// ```rust
/// use sheave_core::{
/// handlers::RtmpContext,
/// messages::Channel
/// };
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_last_received_chunk(&Channel::System.into()).is_none())
/// ```
pub fn get_last_received_chunk(&mut self, chunk_id: &u16) -> Option<&LastChunk> {
self.last_received_chunks.get(chunk_id)
}
/// Loads a last received chunk as mutable.
/// If no last chunk is stored associated with specified ID, this returns `None`.
///
/// # Examples
///
/// ```rust
/// use sheave_core::{
/// handlers::RtmpContext,
/// messages::Channel
/// };
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_last_received_chunk_mut(&Channel::System.into()).is_none())
/// ```
pub fn get_last_received_chunk_mut(&mut self, chunk_id: &u16) -> Option<&mut LastChunk> {
self.last_received_chunks.get_mut(chunk_id)
}
/// Stores a last sent chunk.
pub fn insert_sent_chunk(&mut self, chunk_id: u16, last_chunk: LastChunk) {
self.last_sent_chunks.insert(chunk_id, last_chunk);
}
/// Loads a last sent chunk.
/// If no last chunk is stored associated with specified ID, this returns `None`.
///
/// # Examples
///
/// ```rust
/// use sheave_core::{
/// handlers::RtmpContext,
/// messages::Channel
/// };
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_last_sent_chunk(&Channel::System.into()).is_none())
/// ```
pub fn get_last_sent_chunk(&mut self, chunk_id: &u16) -> Option<&LastChunk> {
self.last_sent_chunks.get(chunk_id)
}
/// Loads a last sent chunk as mutable.
/// If no last chunk is stored associated with specified ID, this returns `None`.
///
/// # Examples
///
/// ```rust
/// use sheave_core::{
/// handlers::RtmpContext,
/// messages::Channel
/// };
///
/// let mut rtmp_context = RtmpContext::default();
/// assert!(rtmp_context.get_last_sent_chunk_mut(&Channel::System.into()).is_none())
/// ```
pub fn get_last_sent_chunk_mut(&mut self, chunk_id: &u16) -> Option<&mut LastChunk> {
self.last_sent_chunks.get_mut(chunk_id)
}
}