OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2013 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 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 private static final String TAG = "MediaCodecVideoEncoder"; | 42 private static final String TAG = "MediaCodecVideoEncoder"; |
43 | 43 |
44 // Tracks webrtc::VideoCodecType. | 44 // Tracks webrtc::VideoCodecType. |
45 public enum VideoCodecType { VIDEO_CODEC_VP8, VIDEO_CODEC_VP9, VIDEO_CODEC_H26
4 } | 45 public enum VideoCodecType { VIDEO_CODEC_VP8, VIDEO_CODEC_VP9, VIDEO_CODEC_H26
4 } |
46 | 46 |
47 private static final int MEDIA_CODEC_RELEASE_TIMEOUT_MS = 5000; // Timeout for
codec releasing. | 47 private static final int MEDIA_CODEC_RELEASE_TIMEOUT_MS = 5000; // Timeout for
codec releasing. |
48 private static final int DEQUEUE_TIMEOUT = 0; // Non-blocking, no wait. | 48 private static final int DEQUEUE_TIMEOUT = 0; // Non-blocking, no wait. |
49 private static final int BITRATE_ADJUSTMENT_FPS = 30; | 49 private static final int BITRATE_ADJUSTMENT_FPS = 30; |
50 private static final int MAXIMUM_INITIAL_FPS = 30; | 50 private static final int MAXIMUM_INITIAL_FPS = 30; |
51 private static final double BITRATE_CORRECTION_SEC = 3.0; | 51 private static final double BITRATE_CORRECTION_SEC = 3.0; |
52 // Maximum bitrate correction scale - no more than 2 times. | 52 // Maximum bitrate correction scale - no more than 4 times. |
53 private static final double BITRATE_CORRECTION_MAX_SCALE = 2; | 53 private static final double BITRATE_CORRECTION_MAX_SCALE = 4; |
54 // Amount of correction steps to reach correction maximum scale. | 54 // Amount of correction steps to reach correction maximum scale. |
55 private static final int BITRATE_CORRECTION_STEPS = 10; | 55 private static final int BITRATE_CORRECTION_STEPS = 20; |
56 // Forced key frame interval - used to reduce color distortions on Qualcomm pl
atform. | 56 // Forced key frame interval - used to reduce color distortions on Qualcomm pl
atform. |
57 private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_M_MS = 25000; | 57 private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_M_MS = 25000; |
58 private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_N_MS = 15000; | 58 private static final long QCOM_VP8_KEY_FRAME_INTERVAL_ANDROID_N_MS = 15000; |
59 | 59 |
60 // Active running encoder instance. Set in initEncode() (called from native co
de) | 60 // Active running encoder instance. Set in initEncode() (called from native co
de) |
61 // and reset to null in release() call. | 61 // and reset to null in release() call. |
62 private static MediaCodecVideoEncoder runningInstance = null; | 62 private static MediaCodecVideoEncoder runningInstance = null; |
63 private static MediaCodecVideoEncoderErrorCallback errorCallback = null; | 63 private static MediaCodecVideoEncoderErrorCallback errorCallback = null; |
64 private static int codecErrors = 0; | 64 private static int codecErrors = 0; |
65 // List of disabled codec types - can be set from application. | 65 // List of disabled codec types - can be set from application. |
(...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
746 bitrateAccumulator = Math.max(bitrateAccumulator, -bitrateAccumulatorCap); | 746 bitrateAccumulator = Math.max(bitrateAccumulator, -bitrateAccumulatorCap); |
747 | 747 |
748 // Do bitrate adjustment every 3 seconds if actual encoder bitrate deviates
too much | 748 // Do bitrate adjustment every 3 seconds if actual encoder bitrate deviates
too much |
749 // form the target value. | 749 // form the target value. |
750 if (bitrateObservationTimeMs > 1000 * BITRATE_CORRECTION_SEC) { | 750 if (bitrateObservationTimeMs > 1000 * BITRATE_CORRECTION_SEC) { |
751 Logging.d(TAG, "Acc: " + (int) bitrateAccumulator + ". Max: " + (int) bitr
ateAccumulatorMax | 751 Logging.d(TAG, "Acc: " + (int) bitrateAccumulator + ". Max: " + (int) bitr
ateAccumulatorMax |
752 + ". ExpScale: " + bitrateAdjustmentScaleExp); | 752 + ". ExpScale: " + bitrateAdjustmentScaleExp); |
753 boolean bitrateAdjustmentScaleChanged = false; | 753 boolean bitrateAdjustmentScaleChanged = false; |
754 if (bitrateAccumulator > bitrateAccumulatorMax) { | 754 if (bitrateAccumulator > bitrateAccumulatorMax) { |
755 // Encoder generates too high bitrate - need to reduce the scale. | 755 // Encoder generates too high bitrate - need to reduce the scale. |
| 756 int bitrateAdjustmentInc = (int) (bitrateAccumulator / bitrateAccumulato
rMax + 0.5); |
| 757 bitrateAdjustmentScaleExp -= bitrateAdjustmentInc; |
756 bitrateAccumulator = bitrateAccumulatorMax; | 758 bitrateAccumulator = bitrateAccumulatorMax; |
757 bitrateAdjustmentScaleExp--; | |
758 bitrateAdjustmentScaleChanged = true; | 759 bitrateAdjustmentScaleChanged = true; |
759 } else if (bitrateAccumulator < -bitrateAccumulatorMax) { | 760 } else if (bitrateAccumulator < -bitrateAccumulatorMax) { |
760 // Encoder generates too low bitrate - need to increase the scale. | 761 // Encoder generates too low bitrate - need to increase the scale. |
761 bitrateAdjustmentScaleExp++; | 762 int bitrateAdjustmentInc = (int) (-bitrateAccumulator / bitrateAccumulat
orMax + 0.5); |
| 763 bitrateAdjustmentScaleExp += bitrateAdjustmentInc; |
762 bitrateAccumulator = -bitrateAccumulatorMax; | 764 bitrateAccumulator = -bitrateAccumulatorMax; |
763 bitrateAdjustmentScaleChanged = true; | 765 bitrateAdjustmentScaleChanged = true; |
764 } | 766 } |
765 if (bitrateAdjustmentScaleChanged) { | 767 if (bitrateAdjustmentScaleChanged) { |
766 bitrateAdjustmentScaleExp = Math.min(bitrateAdjustmentScaleExp, BITRATE_
CORRECTION_STEPS); | 768 bitrateAdjustmentScaleExp = Math.min(bitrateAdjustmentScaleExp, BITRATE_
CORRECTION_STEPS); |
767 bitrateAdjustmentScaleExp = Math.max(bitrateAdjustmentScaleExp, -BITRATE
_CORRECTION_STEPS); | 769 bitrateAdjustmentScaleExp = Math.max(bitrateAdjustmentScaleExp, -BITRATE
_CORRECTION_STEPS); |
768 Logging.d(TAG, "Adjusting bitrate scale to " + bitrateAdjustmentScaleExp
+ ". Value: " | 770 Logging.d(TAG, "Adjusting bitrate scale to " + bitrateAdjustmentScaleExp
+ ". Value: " |
769 + getBitrateScale(bitrateAdjustmentScaleExp)); | 771 + getBitrateScale(bitrateAdjustmentScaleExp)); |
770 setRates(targetBitrateBps / 1000, targetFps); | 772 setRates(targetBitrateBps / 1000, targetFps); |
771 } | 773 } |
772 bitrateObservationTimeMs = 0; | 774 bitrateObservationTimeMs = 0; |
773 } | 775 } |
774 } | 776 } |
775 | 777 |
776 // Release a dequeued output buffer back to the codec for re-use. Return | 778 // Release a dequeued output buffer back to the codec for re-use. Return |
777 // false if the codec is no longer operable. | 779 // false if the codec is no longer operable. |
778 boolean releaseOutputBuffer(int index) { | 780 boolean releaseOutputBuffer(int index) { |
779 checkOnMediaCodecThread(); | 781 checkOnMediaCodecThread(); |
780 try { | 782 try { |
781 mediaCodec.releaseOutputBuffer(index, false); | 783 mediaCodec.releaseOutputBuffer(index, false); |
782 return true; | 784 return true; |
783 } catch (IllegalStateException e) { | 785 } catch (IllegalStateException e) { |
784 Logging.e(TAG, "releaseOutputBuffer failed", e); | 786 Logging.e(TAG, "releaseOutputBuffer failed", e); |
785 return false; | 787 return false; |
786 } | 788 } |
787 } | 789 } |
788 } | 790 } |
OLD | NEW |