| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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_AUDIO_CODING_CODECS_AUDIO_ENCODER_MUTABLE_IMPL_H_ | 11 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_MUTABLE_IMPL_H_ |
| 12 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_MUTABLE_IMPL_H_ | 12 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_MUTABLE_IMPL_H_ |
| 13 | 13 |
| 14 #include "webrtc/base/scoped_ptr.h" | 14 #include "webrtc/base/scoped_ptr.h" |
| 15 #include "webrtc/base/thread_annotations.h" |
| 15 #include "webrtc/modules/audio_coding/codecs/audio_encoder.h" | 16 #include "webrtc/modules/audio_coding/codecs/audio_encoder.h" |
| 17 #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
| 18 #include "webrtc/system_wrappers/interface/thread_wrapper.h" |
| 16 | 19 |
| 17 namespace webrtc { | 20 namespace webrtc { |
| 18 | 21 |
| 19 // This is a convenient base class for implementations of AudioEncoderMutable. | 22 // This is a convenient base class for implementations of AudioEncoderMutable. |
| 20 // T is the type of the encoder state; it has to look like an AudioEncoder | 23 // T is the type of the encoder state; it has to look like an AudioEncoder |
| 21 // subclass whose constructor takes a single T::Config parameter. If P is | 24 // subclass whose constructor takes a single T::Config parameter. If P is |
| 22 // given, this class will inherit from it instead of directly from | 25 // given, this class will inherit from it instead of directly from |
| 23 // AudioEncoderMutable. | 26 // AudioEncoderMutable. |
| 24 template <typename T, typename P = AudioEncoderMutable> | 27 template <typename T, typename P = AudioEncoderMutable> |
| 25 class AudioEncoderMutableImpl : public P { | 28 class AudioEncoderMutableImpl : public P { |
| 26 public: | 29 public: |
| 27 void Reset() override { Reconstruct(config_); } | 30 void Reset() override { |
| 31 typename T::Config config; |
| 32 { |
| 33 CriticalSectionScoped cs(encoder_lock_.get()); |
| 34 config = config_; |
| 35 } |
| 36 Reconstruct(config); |
| 37 } |
| 28 | 38 |
| 29 bool SetFec(bool enable) override { return false; } | 39 bool SetFec(bool enable) override { return false; } |
| 30 | 40 |
| 31 bool SetDtx(bool enable) override { return false; } | 41 bool SetDtx(bool enable) override { return false; } |
| 32 | 42 |
| 33 bool SetApplication(AudioEncoderMutable::Application application) override { | 43 bool SetApplication(AudioEncoderMutable::Application application) override { |
| 34 return false; | 44 return false; |
| 35 } | 45 } |
| 36 | 46 |
| 37 void SetMaxPayloadSize(int max_payload_size_bytes) override {} | 47 void SetMaxPayloadSize(int max_payload_size_bytes) override {} |
| 38 | 48 |
| 39 void SetMaxRate(int max_rate_bps) override {} | 49 void SetMaxRate(int max_rate_bps) override {} |
| 40 | 50 |
| 41 bool SetMaxPlaybackRate(int frequency_hz) override { return false; } | 51 bool SetMaxPlaybackRate(int frequency_hz) override { return false; } |
| 42 | 52 |
| 43 AudioEncoderMutable::EncodedInfo EncodeInternal(uint32_t rtp_timestamp, | 53 AudioEncoderMutable::EncodedInfo EncodeInternal(uint32_t rtp_timestamp, |
| 44 const int16_t* audio, | 54 const int16_t* audio, |
| 45 size_t max_encoded_bytes, | 55 size_t max_encoded_bytes, |
| 46 uint8_t* encoded) override { | 56 uint8_t* encoded) override { |
| 57 CriticalSectionScoped cs(encoder_lock_.get()); |
| 47 return encoder_->EncodeInternal(rtp_timestamp, audio, max_encoded_bytes, | 58 return encoder_->EncodeInternal(rtp_timestamp, audio, max_encoded_bytes, |
| 48 encoded); | 59 encoded); |
| 49 } | 60 } |
| 50 int SampleRateHz() const override { return encoder_->SampleRateHz(); } | 61 int SampleRateHz() const override { |
| 51 int NumChannels() const override { return encoder_->NumChannels(); } | 62 CriticalSectionScoped cs(encoder_lock_.get()); |
| 63 return encoder_->SampleRateHz(); |
| 64 } |
| 65 int NumChannels() const override { |
| 66 CriticalSectionScoped cs(encoder_lock_.get()); |
| 67 return encoder_->NumChannels(); |
| 68 } |
| 52 size_t MaxEncodedBytes() const override { | 69 size_t MaxEncodedBytes() const override { |
| 70 CriticalSectionScoped cs(encoder_lock_.get()); |
| 53 return encoder_->MaxEncodedBytes(); | 71 return encoder_->MaxEncodedBytes(); |
| 54 } | 72 } |
| 55 int RtpTimestampRateHz() const override { | 73 int RtpTimestampRateHz() const override { |
| 74 CriticalSectionScoped cs(encoder_lock_.get()); |
| 56 return encoder_->RtpTimestampRateHz(); | 75 return encoder_->RtpTimestampRateHz(); |
| 57 } | 76 } |
| 58 int Num10MsFramesInNextPacket() const override { | 77 int Num10MsFramesInNextPacket() const override { |
| 78 CriticalSectionScoped cs(encoder_lock_.get()); |
| 59 return encoder_->Num10MsFramesInNextPacket(); | 79 return encoder_->Num10MsFramesInNextPacket(); |
| 60 } | 80 } |
| 61 int Max10MsFramesInAPacket() const override { | 81 int Max10MsFramesInAPacket() const override { |
| 82 CriticalSectionScoped cs(encoder_lock_.get()); |
| 62 return encoder_->Max10MsFramesInAPacket(); | 83 return encoder_->Max10MsFramesInAPacket(); |
| 63 } | 84 } |
| 64 void SetTargetBitrate(int bits_per_second) override { | 85 void SetTargetBitrate(int bits_per_second) override { |
| 86 CriticalSectionScoped cs(encoder_lock_.get()); |
| 65 encoder_->SetTargetBitrate(bits_per_second); | 87 encoder_->SetTargetBitrate(bits_per_second); |
| 66 } | 88 } |
| 67 void SetProjectedPacketLossRate(double fraction) override { | 89 void SetProjectedPacketLossRate(double fraction) override { |
| 90 CriticalSectionScoped cs(encoder_lock_.get()); |
| 68 encoder_->SetProjectedPacketLossRate(fraction); | 91 encoder_->SetProjectedPacketLossRate(fraction); |
| 69 } | 92 } |
| 70 | 93 |
| 71 protected: | 94 protected: |
| 72 explicit AudioEncoderMutableImpl(const typename T::Config& config) { | 95 explicit AudioEncoderMutableImpl(const typename T::Config& config) |
| 96 : encoder_lock_(CriticalSectionWrapper::CreateCriticalSection()) { |
| 73 Reconstruct(config); | 97 Reconstruct(config); |
| 74 } | 98 } |
| 75 | 99 |
| 76 bool Reconstruct(const typename T::Config& config) { | 100 bool Reconstruct(const typename T::Config& config) { |
| 77 if (!config.IsOk()) | 101 if (!config.IsOk()) |
| 78 return false; | 102 return false; |
| 103 CriticalSectionScoped cs(encoder_lock_.get()); |
| 79 config_ = config; | 104 config_ = config; |
| 80 encoder_.reset(new T(config_)); | 105 encoder_.reset(new T(config_)); |
| 81 return true; | 106 return true; |
| 82 } | 107 } |
| 83 | 108 |
| 84 const typename T::Config& config() const { return config_; } | 109 typename T::Config config() const { |
| 85 T* encoder() { return encoder_.get(); } | 110 CriticalSectionScoped cs(encoder_lock_.get()); |
| 86 const T* encoder() const { return encoder_.get(); } | 111 return config_; |
| 112 } |
| 113 T* encoder() EXCLUSIVE_LOCKS_REQUIRED(encoder_lock_) { |
| 114 return encoder_.get(); |
| 115 } |
| 116 const T* encoder() const EXCLUSIVE_LOCKS_REQUIRED(encoder_lock_) { |
| 117 return encoder_.get(); |
| 118 } |
| 119 |
| 120 const rtc::scoped_ptr<CriticalSectionWrapper> encoder_lock_; |
| 87 | 121 |
| 88 private: | 122 private: |
| 89 rtc::scoped_ptr<T> encoder_; | 123 rtc::scoped_ptr<T> encoder_ GUARDED_BY(encoder_lock_); |
| 90 typename T::Config config_; | 124 typename T::Config config_ GUARDED_BY(encoder_lock_); |
| 91 }; | 125 }; |
| 92 | 126 |
| 93 } // namespace webrtc | 127 } // namespace webrtc |
| 94 | 128 |
| 95 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_MUTABLE_IMPL_H_ | 129 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_MUTABLE_IMPL_H_ |
| OLD | NEW |