Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(353)

Side by Side Diff: webrtc/examples/androidapp/src/org/appspot/apprtc/CaptureQualityController.java

Issue 1361083002: Android AppRTCDemo: Add slider for changing camera capture quality during call (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 *
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
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 package org.appspot.apprtc;
12
13 import android.widget.SeekBar;
14 import android.widget.TextView;
15
16 import org.webrtc.CameraEnumerationAndroid.CaptureFormat;
17
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.Comparator;
21 import java.util.List;
22
23 /**
24 * Control capture format based on a seekbar listener.
25 */
26 public class CaptureQualityController implements SeekBar.OnSeekBarChangeListener {
27 private final List<CaptureFormat> formats = Arrays.asList(
28 new CaptureFormat(1280, 720, 0, 30000),
29 new CaptureFormat(640, 480, 0, 30000),
30 new CaptureFormat(320, 240, 0, 30000),
31 new CaptureFormat(256, 144, 0, 30000));
32 // Prioritize framerate below this threshold and resolution above the threshol d.
33 private static final int FRAMERATE_THRESHOLD = 15;
34 private TextView captureFormatText;
35 private CallFragment.OnCallEvents callEvents;
36 private int width = 0;
37 private int height = 0;
38 private int framerate = 0;
39 private double targetBandwidth = 0;
40
41 public CaptureQualityController(
42 TextView captureFormatText, CallFragment.OnCallEvents callEvents) {
43 this.captureFormatText = captureFormatText;
44 this.callEvents = callEvents;
45 captureFormatText.setText("Slide to change capture format");
46 }
47
48 private final Comparator<CaptureFormat> compareFormats = new Comparator<Captur eFormat>() {
49 @Override
50 public int compare(CaptureFormat first, CaptureFormat second) {
51 int firstFps = calculateFramerate(targetBandwidth, first);
52 int secondFps = calculateFramerate(targetBandwidth, second);
53
54 if (firstFps >= FRAMERATE_THRESHOLD && secondFps >= FRAMERATE_THRESHOLD
55 || firstFps == secondFps) {
56 // Compare resolution.
57 return first.width * first.height - second.width * second.height;
58 } else {
59 // Compare fps.
60 return firstFps - secondFps;
61 }
62 }
63 };
64
65 @Override
66 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
67 if (progress == 0) {
68 width = 0;
69 height = 0;
70 framerate = 0;
71 captureFormatText.setText("Muted");
72 return;
73 }
74
75 // Extract max bandwidth (in millipixels / second).
76 long maxCaptureBandwidth = java.lang.Long.MIN_VALUE;
77 for (CaptureFormat format : formats) {
78 maxCaptureBandwidth = Math.max(maxCaptureBandwidth,
79 (long) format.width * format.height * format.maxFramerate);
80 }
81
82 // Fraction between 0 and 1.
83 double bandwidthFraction = (double) progress / 100.0;
84 // Make a log-scale transformation, still between 0 and 1.
85 final double kExpConstant = 3.0;
86 bandwidthFraction =
87 (Math.exp(kExpConstant * bandwidthFraction) - 1) / (Math.exp(kExpConstan t) - 1);
88 targetBandwidth = bandwidthFraction * maxCaptureBandwidth;
89
90 // Choose the best format given a target bandwidth.
91 final CaptureFormat bestFormat = Collections.max(formats, compareFormats);
92 width = bestFormat.width;
93 height = bestFormat.height;
94 framerate = calculateFramerate(targetBandwidth, bestFormat);
95 captureFormatText.setText(width + "x" + height + " @ " + framerate + "fps");
96 }
97
98 @Override
99 public void onStartTrackingTouch(SeekBar seekBar) {
100 }
101
102 @Override
103 public void onStopTrackingTouch(SeekBar seekBar) {
104 callEvents.onCaptureFormatChange(width, height, framerate);
105 }
106
107 // Return the highest frame rate possible based on bandwidth and format.
108 private int calculateFramerate(double bandwidth, CaptureFormat format) {
109 return (int) Math.round(Math.min(format.maxFramerate,
110 (int) Math.round(bandwidth / (format.width * format.height))) / 1000.0);
111 }
112 }
113
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698