Chromium Code Reviews| 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 { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 37 void SetMaxPayloadSize(int max_payload_size_bytes) override {} | 40 void SetMaxPayloadSize(int max_payload_size_bytes) override {} |
| 38 | 41 |
| 39 void SetMaxRate(int max_rate_bps) override {} | 42 void SetMaxRate(int max_rate_bps) override {} |
| 40 | 43 |
| 41 bool SetMaxPlaybackRate(int frequency_hz) override { return false; } | 44 bool SetMaxPlaybackRate(int frequency_hz) override { return false; } |
| 42 | 45 |
| 43 AudioEncoderMutable::EncodedInfo EncodeInternal(uint32_t rtp_timestamp, | 46 AudioEncoderMutable::EncodedInfo EncodeInternal(uint32_t rtp_timestamp, |
| 44 const int16_t* audio, | 47 const int16_t* audio, |
| 45 size_t max_encoded_bytes, | 48 size_t max_encoded_bytes, |
| 46 uint8_t* encoded) override { | 49 uint8_t* encoded) override { |
| 50 CriticalSectionScoped cs(encoder_lock_.get()); | |
| 47 return encoder_->EncodeInternal(rtp_timestamp, audio, max_encoded_bytes, | 51 return encoder_->EncodeInternal(rtp_timestamp, audio, max_encoded_bytes, |
| 48 encoded); | 52 encoded); |
| 49 } | 53 } |
| 50 int SampleRateHz() const override { return encoder_->SampleRateHz(); } | 54 int SampleRateHz() const override { |
| 51 int NumChannels() const override { return encoder_->NumChannels(); } | 55 CriticalSectionScoped cs(encoder_lock_.get()); |
| 56 return encoder_->SampleRateHz(); | |
| 57 } | |
| 58 int NumChannels() const override { | |
| 59 CriticalSectionScoped cs(encoder_lock_.get()); | |
| 60 return encoder_->NumChannels(); | |
| 61 } | |
| 52 size_t MaxEncodedBytes() const override { | 62 size_t MaxEncodedBytes() const override { |
| 63 CriticalSectionScoped cs(encoder_lock_.get()); | |
| 53 return encoder_->MaxEncodedBytes(); | 64 return encoder_->MaxEncodedBytes(); |
| 54 } | 65 } |
| 55 int RtpTimestampRateHz() const override { | 66 int RtpTimestampRateHz() const override { |
| 67 CriticalSectionScoped cs(encoder_lock_.get()); | |
| 56 return encoder_->RtpTimestampRateHz(); | 68 return encoder_->RtpTimestampRateHz(); |
| 57 } | 69 } |
| 58 int Num10MsFramesInNextPacket() const override { | 70 int Num10MsFramesInNextPacket() const override { |
| 71 CriticalSectionScoped cs(encoder_lock_.get()); | |
| 59 return encoder_->Num10MsFramesInNextPacket(); | 72 return encoder_->Num10MsFramesInNextPacket(); |
| 60 } | 73 } |
| 61 int Max10MsFramesInAPacket() const override { | 74 int Max10MsFramesInAPacket() const override { |
| 75 CriticalSectionScoped cs(encoder_lock_.get()); | |
| 62 return encoder_->Max10MsFramesInAPacket(); | 76 return encoder_->Max10MsFramesInAPacket(); |
| 63 } | 77 } |
| 64 void SetTargetBitrate(int bits_per_second) override { | 78 void SetTargetBitrate(int bits_per_second) override { |
| 79 CriticalSectionScoped cs(encoder_lock_.get()); | |
| 65 encoder_->SetTargetBitrate(bits_per_second); | 80 encoder_->SetTargetBitrate(bits_per_second); |
| 66 } | 81 } |
| 67 void SetProjectedPacketLossRate(double fraction) override { | 82 void SetProjectedPacketLossRate(double fraction) override { |
| 83 CriticalSectionScoped cs(encoder_lock_.get()); | |
| 68 encoder_->SetProjectedPacketLossRate(fraction); | 84 encoder_->SetProjectedPacketLossRate(fraction); |
| 69 } | 85 } |
| 70 | 86 |
| 71 protected: | 87 protected: |
| 72 explicit AudioEncoderMutableImpl(const typename T::Config& config) { | 88 explicit AudioEncoderMutableImpl(const typename T::Config& config) |
| 89 : encoder_lock_(CriticalSectionWrapper::CreateCriticalSection()) { | |
| 73 Reconstruct(config); | 90 Reconstruct(config); |
| 74 } | 91 } |
| 75 | 92 |
| 76 bool Reconstruct(const typename T::Config& config) { | 93 bool Reconstruct(const typename T::Config& config) { |
| 77 if (!config.IsOk()) | 94 if (!config.IsOk()) |
| 78 return false; | 95 return false; |
| 79 config_ = config; | 96 config_ = config; |
| 97 CriticalSectionScoped cs(encoder_lock_.get()); | |
|
Jelena
2015/06/13 09:47:50
Actually, lock needs to protect the config_ as wel
kwiberg-webrtc
2015/06/13 20:27:49
I don't think it does, actually. If I'm right, all
hlundin-webrtc
2015/06/15 09:38:10
Moved the critsect as suggested, and added GUARDED
| |
| 80 encoder_.reset(new T(config_)); | 98 encoder_.reset(new T(config_)); |
| 81 return true; | 99 return true; |
| 82 } | 100 } |
| 83 | 101 |
| 84 const typename T::Config& config() const { return config_; } | 102 const typename T::Config& config() const { return config_; } |
|
Jelena
2015/06/13 09:47:50
Since config_ can be modified on another thread, y
kwiberg-webrtc
2015/06/13 20:27:49
Same comment as on line 97.
hlundin-webrtc
2015/06/15 09:38:10
Done.
| |
| 85 T* encoder() { return encoder_.get(); } | 103 T* encoder() EXCLUSIVE_LOCKS_REQUIRED(encoder_lock_) { |
| 86 const T* encoder() const { return encoder_.get(); } | 104 return encoder_.get(); |
| 105 } | |
| 106 const T* encoder() const EXCLUSIVE_LOCKS_REQUIRED(encoder_lock_) { | |
| 107 return encoder_.get(); | |
| 108 } | |
| 109 | |
| 110 const rtc::scoped_ptr<CriticalSectionWrapper> encoder_lock_; | |
| 87 | 111 |
| 88 private: | 112 private: |
| 89 rtc::scoped_ptr<T> encoder_; | 113 rtc::scoped_ptr<T> encoder_ GUARDED_BY(encoder_lock_); |
| 90 typename T::Config config_; | 114 typename T::Config config_; |
| 91 }; | 115 }; |
| 92 | 116 |
| 93 } // namespace webrtc | 117 } // namespace webrtc |
| 94 | 118 |
| 95 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_MUTABLE_IMPL_H_ | 119 #endif // WEBRTC_MODULES_AUDIO_CODING_CODECS_AUDIO_ENCODER_MUTABLE_IMPL_H_ |
| OLD | NEW |