OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_MODULES_INCLUDE_MODULE_COMMON_TYPES_H_ | 11 #ifndef WEBRTC_MODULES_INCLUDE_MODULE_COMMON_TYPES_H_ |
12 #define WEBRTC_MODULES_INCLUDE_MODULE_COMMON_TYPES_H_ | 12 #define WEBRTC_MODULES_INCLUDE_MODULE_COMMON_TYPES_H_ |
13 | 13 |
14 #include <assert.h> | 14 #include <assert.h> |
15 #include <string.h> // memcpy | 15 #include <string.h> // memcpy |
16 | 16 |
17 #include <algorithm> | 17 #include <algorithm> |
18 #include <limits> | 18 #include <limits> |
19 | 19 |
20 #include "webrtc/base/constructormagic.h" | 20 #include "webrtc/base/constructormagic.h" |
| 21 #include "webrtc/base/deprecation.h" |
| 22 #include "webrtc/base/safe_conversions.h" |
21 #include "webrtc/common_types.h" | 23 #include "webrtc/common_types.h" |
22 #include "webrtc/common_video/rotation.h" | 24 #include "webrtc/common_video/rotation.h" |
23 #include "webrtc/typedefs.h" | 25 #include "webrtc/typedefs.h" |
24 | 26 |
25 namespace webrtc { | 27 namespace webrtc { |
26 | 28 |
27 struct RTPAudioHeader { | 29 struct RTPAudioHeader { |
28 uint8_t numEnergy; // number of valid entries in arrOfEnergy | 30 uint8_t numEnergy; // number of valid entries in arrOfEnergy |
29 uint8_t arrOfEnergy[kRtpCsrcSize]; // one energy byte (0-9) per channel | 31 uint8_t arrOfEnergy[kRtpCsrcSize]; // one energy byte (0-9) per channel |
30 bool isCNG; // is this CNG | 32 bool isCNG; // is this CNG |
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 /* This class holds up to 60 ms of super-wideband (32 kHz) stereo audio. It | 515 /* This class holds up to 60 ms of super-wideband (32 kHz) stereo audio. It |
514 * allows for adding and subtracting frames while keeping track of the resulting | 516 * allows for adding and subtracting frames while keeping track of the resulting |
515 * states. | 517 * states. |
516 * | 518 * |
517 * Notes | 519 * Notes |
518 * - The total number of samples in |data_| is | 520 * - The total number of samples in |data_| is |
519 * samples_per_channel_ * num_channels_ | 521 * samples_per_channel_ * num_channels_ |
520 * | 522 * |
521 * - Stereo data is interleaved starting with the left channel. | 523 * - Stereo data is interleaved starting with the left channel. |
522 * | 524 * |
523 * - The +operator assume that you would never add exactly opposite frames when | |
524 * deciding the resulting state. To do this use the -operator. | |
525 */ | 525 */ |
526 class AudioFrame { | 526 class AudioFrame { |
527 public: | 527 public: |
528 // Stereo, 32 kHz, 60 ms (2 * 32 * 60) | 528 // Stereo, 32 kHz, 60 ms (2 * 32 * 60) |
529 enum : size_t { | 529 enum : size_t { |
530 kMaxDataSizeSamples = 3840 | 530 kMaxDataSizeSamples = 3840 |
531 }; | 531 }; |
532 | 532 |
533 enum VADActivity { | 533 enum VADActivity { |
534 kVadActive = 0, | 534 kVadActive = 0, |
(...skipping 14 matching lines...) Expand all Loading... |
549 // contents of |data_|). | 549 // contents of |data_|). |
550 void Reset(); | 550 void Reset(); |
551 | 551 |
552 void UpdateFrame(int id, uint32_t timestamp, const int16_t* data, | 552 void UpdateFrame(int id, uint32_t timestamp, const int16_t* data, |
553 size_t samples_per_channel, int sample_rate_hz, | 553 size_t samples_per_channel, int sample_rate_hz, |
554 SpeechType speech_type, VADActivity vad_activity, | 554 SpeechType speech_type, VADActivity vad_activity, |
555 size_t num_channels = 1); | 555 size_t num_channels = 1); |
556 | 556 |
557 void CopyFrom(const AudioFrame& src); | 557 void CopyFrom(const AudioFrame& src); |
558 | 558 |
559 void Mute(); | 559 // These methods are deprecated. Use the functions in |
560 | 560 // webrtc/audio/utility instead. These methods will exists for a |
561 AudioFrame& operator>>=(const int rhs); | 561 // short period of time until webrtc clients have updated. See |
562 AudioFrame& operator+=(const AudioFrame& rhs); | 562 // webrtc:6548 for details. |
| 563 RTC_DEPRECATED void Mute(); |
| 564 RTC_DEPRECATED AudioFrame& operator>>=(const int rhs); |
| 565 RTC_DEPRECATED AudioFrame& operator+=(const AudioFrame& rhs); |
563 | 566 |
564 int id_; | 567 int id_; |
565 // RTP timestamp of the first sample in the AudioFrame. | 568 // RTP timestamp of the first sample in the AudioFrame. |
566 uint32_t timestamp_; | 569 uint32_t timestamp_ = 0; |
567 // Time since the first frame in milliseconds. | 570 // Time since the first frame in milliseconds. |
568 // -1 represents an uninitialized value. | 571 // -1 represents an uninitialized value. |
569 int64_t elapsed_time_ms_; | 572 int64_t elapsed_time_ms_ = -1; |
570 // NTP time of the estimated capture time in local timebase in milliseconds. | 573 // NTP time of the estimated capture time in local timebase in milliseconds. |
571 // -1 represents an uninitialized value. | 574 // -1 represents an uninitialized value. |
572 int64_t ntp_time_ms_; | 575 int64_t ntp_time_ms_ = -1; |
573 int16_t data_[kMaxDataSizeSamples]; | 576 int16_t data_[kMaxDataSizeSamples]; |
574 size_t samples_per_channel_; | 577 size_t samples_per_channel_ = 0; |
575 int sample_rate_hz_; | 578 int sample_rate_hz_ = 0; |
576 size_t num_channels_; | 579 size_t num_channels_ = 0; |
577 SpeechType speech_type_; | 580 SpeechType speech_type_ = kUndefined; |
578 VADActivity vad_activity_; | 581 VADActivity vad_activity_ = kVadUnknown; |
579 | 582 |
580 private: | 583 private: |
581 RTC_DISALLOW_COPY_AND_ASSIGN(AudioFrame); | 584 RTC_DISALLOW_COPY_AND_ASSIGN(AudioFrame); |
582 }; | 585 }; |
583 | 586 |
584 // TODO(henrik.lundin) Can we remove the call to data_()? | 587 // TODO(henrik.lundin) Can we remove the call to data_()? |
585 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=5647. | 588 // See https://bugs.chromium.org/p/webrtc/issues/detail?id=5647. |
586 inline AudioFrame::AudioFrame() | 589 inline AudioFrame::AudioFrame() |
587 : data_() { | 590 : data_() { |
588 Reset(); | |
589 } | 591 } |
590 | 592 |
591 inline void AudioFrame::Reset() { | 593 inline void AudioFrame::Reset() { |
592 id_ = -1; | 594 id_ = -1; |
593 // TODO(wu): Zero is a valid value for |timestamp_|. We should initialize | 595 // TODO(wu): Zero is a valid value for |timestamp_|. We should initialize |
594 // to an invalid value, or add a new member to indicate invalidity. | 596 // to an invalid value, or add a new member to indicate invalidity. |
595 timestamp_ = 0; | 597 timestamp_ = 0; |
596 elapsed_time_ms_ = -1; | 598 elapsed_time_ms_ = -1; |
597 ntp_time_ms_ = -1; | 599 ntp_time_ms_ = -1; |
598 samples_per_channel_ = 0; | 600 samples_per_channel_ = 0; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 inline AudioFrame& AudioFrame::operator>>=(const int rhs) { | 654 inline AudioFrame& AudioFrame::operator>>=(const int rhs) { |
653 assert((num_channels_ > 0) && (num_channels_ < 3)); | 655 assert((num_channels_ > 0) && (num_channels_ < 3)); |
654 if ((num_channels_ > 2) || (num_channels_ < 1)) return *this; | 656 if ((num_channels_ > 2) || (num_channels_ < 1)) return *this; |
655 | 657 |
656 for (size_t i = 0; i < samples_per_channel_ * num_channels_; i++) { | 658 for (size_t i = 0; i < samples_per_channel_ * num_channels_; i++) { |
657 data_[i] = static_cast<int16_t>(data_[i] >> rhs); | 659 data_[i] = static_cast<int16_t>(data_[i] >> rhs); |
658 } | 660 } |
659 return *this; | 661 return *this; |
660 } | 662 } |
661 | 663 |
662 namespace { | |
663 inline int16_t ClampToInt16(int32_t input) { | |
664 if (input < -0x00008000) { | |
665 return -0x8000; | |
666 } else if (input > 0x00007FFF) { | |
667 return 0x7FFF; | |
668 } else { | |
669 return static_cast<int16_t>(input); | |
670 } | |
671 } | |
672 } | |
673 | |
674 inline AudioFrame& AudioFrame::operator+=(const AudioFrame& rhs) { | 664 inline AudioFrame& AudioFrame::operator+=(const AudioFrame& rhs) { |
675 // Sanity check | 665 // Sanity check |
676 assert((num_channels_ > 0) && (num_channels_ < 3)); | 666 assert((num_channels_ > 0) && (num_channels_ < 3)); |
677 if ((num_channels_ > 2) || (num_channels_ < 1)) return *this; | 667 if ((num_channels_ > 2) || (num_channels_ < 1)) return *this; |
678 if (num_channels_ != rhs.num_channels_) return *this; | 668 if (num_channels_ != rhs.num_channels_) return *this; |
679 | 669 |
680 bool noPrevData = false; | 670 bool noPrevData = false; |
681 if (samples_per_channel_ != rhs.samples_per_channel_) { | 671 if (samples_per_channel_ != rhs.samples_per_channel_) { |
682 if (samples_per_channel_ == 0) { | 672 if (samples_per_channel_ == 0) { |
683 // special case we have no data to start with | 673 // special case we have no data to start with |
(...skipping 13 matching lines...) Expand all Loading... |
697 if (speech_type_ != rhs.speech_type_) speech_type_ = kUndefined; | 687 if (speech_type_ != rhs.speech_type_) speech_type_ = kUndefined; |
698 | 688 |
699 if (noPrevData) { | 689 if (noPrevData) { |
700 memcpy(data_, rhs.data_, | 690 memcpy(data_, rhs.data_, |
701 sizeof(int16_t) * rhs.samples_per_channel_ * num_channels_); | 691 sizeof(int16_t) * rhs.samples_per_channel_ * num_channels_); |
702 } else { | 692 } else { |
703 // IMPROVEMENT this can be done very fast in assembly | 693 // IMPROVEMENT this can be done very fast in assembly |
704 for (size_t i = 0; i < samples_per_channel_ * num_channels_; i++) { | 694 for (size_t i = 0; i < samples_per_channel_ * num_channels_; i++) { |
705 int32_t wrap_guard = | 695 int32_t wrap_guard = |
706 static_cast<int32_t>(data_[i]) + static_cast<int32_t>(rhs.data_[i]); | 696 static_cast<int32_t>(data_[i]) + static_cast<int32_t>(rhs.data_[i]); |
707 data_[i] = ClampToInt16(wrap_guard); | 697 data_[i] = rtc::saturated_cast<int16_t>(wrap_guard); |
708 } | 698 } |
709 } | 699 } |
710 return *this; | 700 return *this; |
711 } | 701 } |
712 | 702 |
713 inline bool IsNewerSequenceNumber(uint16_t sequence_number, | 703 inline bool IsNewerSequenceNumber(uint16_t sequence_number, |
714 uint16_t prev_sequence_number) { | 704 uint16_t prev_sequence_number) { |
715 // Distinguish between elements that are exactly 0x8000 apart. | 705 // Distinguish between elements that are exactly 0x8000 apart. |
716 // If s1>s2 and |s1-s2| = 0x8000: IsNewer(s1,s2)=true, IsNewer(s2,s1)=false | 706 // If s1>s2 and |s1-s2| = 0x8000: IsNewer(s1,s2)=true, IsNewer(s2,s1)=false |
717 // rather than having IsNewer(s1,s2) = IsNewer(s2,s1) = false. | 707 // rather than having IsNewer(s1,s2) = IsNewer(s2,s1) = false. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
781 return unwrapped; | 771 return unwrapped; |
782 } | 772 } |
783 | 773 |
784 private: | 774 private: |
785 int64_t last_seq_; | 775 int64_t last_seq_; |
786 }; | 776 }; |
787 | 777 |
788 } // namespace webrtc | 778 } // namespace webrtc |
789 | 779 |
790 #endif // WEBRTC_MODULES_INCLUDE_MODULE_COMMON_TYPES_H_ | 780 #endif // WEBRTC_MODULES_INCLUDE_MODULE_COMMON_TYPES_H_ |
OLD | NEW |