AndroidVideoCapturer getSupportedFormats(): Change interface from JSON string to List/vector

This CL simplifies the VideoCapturer interface from 'String getSupportedFormatsAsJson() throws JSONException' to 'List<CaptureFormat> getSupportedFormats()'. The intermediate conversion to/from a JSON string is removed, and AndroidVideoCapturerJni converts the Java list to a C++ vector directly instead.

BUG=webrtc:5519
R=perkj@webrtc.org

Review URL: https://codereview.webrtc.org/1702603002 .

Cr-Commit-Position: refs/heads/master@{#11669}
This commit is contained in:
Magnus Jedvert 2016-02-18 13:09:54 +01:00
parent 347c0bb5b5
commit 5199c74d25
8 changed files with 43 additions and 73 deletions

View File

@ -12,7 +12,6 @@
#include "webrtc/api/java/jni/native_handle_impl.h"
#include "webrtc/base/common.h"
#include "webrtc/base/json.h"
#include "webrtc/base/timeutils.h"
#include "webrtc/media/engine/webrtcvideoframe.h"
@ -119,29 +118,7 @@ AndroidVideoCapturer::AndroidVideoCapturer(
frame_factory_(NULL),
current_state_(cricket::CS_STOPPED) {
thread_checker_.DetachFromThread();
std::string json_string = delegate_->GetSupportedFormats();
LOG(LS_INFO) << json_string;
Json::Value json_values;
Json::Reader reader(Json::Features::strictMode());
if (!reader.parse(json_string, json_values)) {
LOG(LS_ERROR) << "Failed to parse formats.";
}
std::vector<cricket::VideoFormat> formats;
for (Json::ArrayIndex i = 0; i < json_values.size(); ++i) {
const Json::Value& json_value = json_values[i];
RTC_CHECK(!json_value["width"].isNull() &&
!json_value["height"].isNull() &&
!json_value["framerate"].isNull());
cricket::VideoFormat format(
json_value["width"].asInt(),
json_value["height"].asInt(),
cricket::VideoFormat::FpsToInterval(json_value["framerate"].asInt()),
cricket::FOURCC_YV12);
formats.push_back(format);
}
SetSupportedFormats(formats);
SetSupportedFormats(delegate_->GetSupportedFormats());
}
AndroidVideoCapturer::~AndroidVideoCapturer() {

View File

@ -34,8 +34,7 @@ class AndroidVideoCapturerDelegate : public rtc::RefCountInterface {
// The delegate may not call into AndroidVideoCapturer after this call.
virtual void Stop() = 0;
// Must returns a JSON string "{{width=xxx, height=xxx, framerate = xxx}}"
virtual std::string GetSupportedFormats() = 0;
virtual std::vector<cricket::VideoFormat> GetSupportedFormats() = 0;
};
// Android implementation of cricket::VideoCapturer for use with WebRtc

View File

@ -74,16 +74,6 @@
4267, # conversion from 'size_t' to 'int', possible loss of data.
4389, # signed/unsigned mismatch.
],
'conditions': [
['build_json==1', {
'dependencies': [
'<(DEPTH)/third_party/jsoncpp/jsoncpp.gyp:jsoncpp',
],
'export_dependent_settings': [
'<(DEPTH)/third_party/jsoncpp/jsoncpp.gyp:jsoncpp',
],
}],
],
},
{
'target_name': 'libjingle_peerconnection_so',

View File

@ -14,10 +14,6 @@ import static java.lang.Math.abs;
import static java.lang.Math.ceil;
import android.graphics.ImageFormat;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.webrtc.Logging;
import java.util.Collections;
@ -42,7 +38,9 @@ public class CameraEnumerationAndroid {
}
public static synchronized List<CaptureFormat> getSupportedFormats(int cameraId) {
return enumerator.getSupportedFormats(cameraId);
final List<CaptureFormat> formats = enumerator.getSupportedFormats(cameraId);
Logging.d(TAG, "Supported formats for camera " + cameraId + ": " + formats);
return formats;
}
public static class CaptureFormat {
@ -137,21 +135,6 @@ public class CameraEnumerationAndroid {
return getNameOfDevice(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK);
}
public static String getSupportedFormatsAsJson(int id) throws JSONException {
List<CaptureFormat> formats = getSupportedFormats(id);
JSONArray json_formats = new JSONArray();
for (CaptureFormat format : formats) {
JSONObject json_format = new JSONObject();
json_format.put("width", format.width);
json_format.put("height", format.height);
json_format.put("framerate", (format.maxFramerate + 999) / 1000);
json_formats.put(json_format);
}
Logging.d(TAG, "Supported formats for camera " + id + ": "
+ json_formats.toString(2));
return json_formats.toString();
}
// Helper class for finding the closest supported format for the two functions below.
private static abstract class ClosestComparator<T> implements Comparator<T> {
// Difference between supported and requested parameter.

View File

@ -17,7 +17,6 @@ import android.os.SystemClock;
import android.view.Surface;
import android.view.WindowManager;
import org.json.JSONException;
import org.webrtc.CameraEnumerationAndroid.CaptureFormat;
import org.webrtc.Logging;
@ -284,6 +283,7 @@ public class VideoCapturerAndroid implements
}
}
@Override
public List<CaptureFormat> getSupportedFormats() {
return CameraEnumerationAndroid.getSupportedFormats(getCurrentCameraId());
}
@ -293,11 +293,6 @@ public class VideoCapturerAndroid implements
return isCapturingToTexture;
}
@Override
public String getSupportedFormatsAsJson() throws JSONException {
return CameraEnumerationAndroid.getSupportedFormatsAsJson(getCurrentCameraId());
}
@Override
public SurfaceTextureHelper getSurfaceTextureHelper() {
return surfaceHelper;

View File

@ -114,14 +114,40 @@ void AndroidVideoCapturerJni::AsyncCapturerInvoke(
invoker_->AsyncInvoke<void>(rtc::Bind(method, capturer_, args...));
}
std::string AndroidVideoCapturerJni::GetSupportedFormats() {
jmethodID m =
GetMethodID(jni(), *j_video_capturer_class_,
"getSupportedFormatsAsJson", "()Ljava/lang/String;");
jstring j_json_caps =
(jstring) jni()->CallObjectMethod(*j_video_capturer_, m);
CHECK_EXCEPTION(jni()) << "error during supportedFormatsAsJson";
return JavaToStdString(jni(), j_json_caps);
std::vector<cricket::VideoFormat>
AndroidVideoCapturerJni::GetSupportedFormats() {
JNIEnv* jni = AttachCurrentThreadIfNeeded();
jobject j_list_of_formats = jni->CallObjectMethod(
*j_video_capturer_,
GetMethodID(jni, *j_video_capturer_class_, "getSupportedFormats",
"()Ljava/util/List;"));
CHECK_EXCEPTION(jni) << "error during getSupportedFormats";
// Extract Java List<CaptureFormat> to std::vector<cricket::VideoFormat>.
jclass j_list_class = jni->FindClass("java/util/List");
jclass j_format_class =
jni->FindClass("org/webrtc/CameraEnumerationAndroid$CaptureFormat");
const int size = jni->CallIntMethod(
j_list_of_formats, GetMethodID(jni, j_list_class, "size", "()I"));
jmethodID j_get =
GetMethodID(jni, j_list_class, "get", "(I)Ljava/lang/Object;");
jfieldID j_width_field = GetFieldID(jni, j_format_class, "width", "I");
jfieldID j_height_field = GetFieldID(jni, j_format_class, "height", "I");
jfieldID j_max_framerate_field =
GetFieldID(jni, j_format_class, "maxFramerate", "I");
std::vector<cricket::VideoFormat> formats;
formats.reserve(size);
for (int i = 0; i < size; ++i) {
jobject j_format = jni->CallObjectMethod(j_list_of_formats, j_get, i);
const int frame_interval = cricket::VideoFormat::FpsToInterval(
(GetIntField(jni, j_format, j_max_framerate_field) + 999) / 1000);
formats.emplace_back(GetIntField(jni, j_format, j_width_field),
GetIntField(jni, j_format, j_height_field),
frame_interval, cricket::FOURCC_NV21);
}
CHECK_EXCEPTION(jni) << "error while extracting formats";
return formats;
}
void AndroidVideoCapturerJni::OnCapturerStarted(bool success) {

View File

@ -40,7 +40,7 @@ class AndroidVideoCapturerJni : public webrtc::AndroidVideoCapturerDelegate {
webrtc::AndroidVideoCapturer* capturer) override;
void Stop() override;
std::string GetSupportedFormats() override;
std::vector<cricket::VideoFormat> GetSupportedFormats() override;
// Called from VideoCapturer::NativeObserver on a Java thread.
void OnCapturerStarted(bool success);

View File

@ -12,7 +12,7 @@ package org.webrtc;
import android.content.Context;
import org.json.JSONException;
import java.util.List;
// Base interface for all VideoCapturers to implement.
// TODO(magjed): Simplify and improve this interface.
@ -83,7 +83,7 @@ public interface VideoCapturer {
int width, int height, int framerate);
}
String getSupportedFormatsAsJson() throws JSONException;
List<CameraEnumerationAndroid.CaptureFormat> getSupportedFormats();
SurfaceTextureHelper getSurfaceTextureHelper();