Skip to content

Commit 814604c

Browse files
authored
fix: cover the remaining native failure paths of the media module (#442)
A recorder whose writer cannot start stays idle, a frame that cannot be queued on a WebRTC thread is dropped instead of thrown into it, a player that fails to start loses its observer first, exception text reaches Java as ASCII, and MediaFileSource cleans up on errors too.
1 parent eca1493 commit 814604c

15 files changed

Lines changed: 466 additions & 24 deletions

File tree

‎webrtc-media/src/main/cpp/include/JNI_AllocationFault.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,38 @@ extern "C" {
3030
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_failNextStringCopy
3131
(JNIEnv *, jclass);
3232

33+
/*
34+
* Class: dev_onvoid_webrtc_media_player_AllocationFault
35+
* Method: failNextAllocation
36+
* Signature: ()V
37+
*/
38+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_failNextAllocation
39+
(JNIEnv *, jclass);
40+
41+
/*
42+
* Class: dev_onvoid_webrtc_media_player_AllocationFault
43+
* Method: failNextThreadStart
44+
* Signature: ()V
45+
*/
46+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_failNextThreadStart
47+
(JNIEnv *, jclass);
48+
49+
/*
50+
* Class: dev_onvoid_webrtc_media_player_AllocationFault
51+
* Method: isArmed
52+
* Signature: ()Z
53+
*/
54+
JNIEXPORT jboolean JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_isArmed
55+
(JNIEnv *, jclass);
56+
57+
/*
58+
* Class: dev_onvoid_webrtc_media_player_AllocationFault
59+
* Method: throwInNative
60+
* Signature: (ILjava/lang/String;)V
61+
*/
62+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_throwInNative
63+
(JNIEnv *, jclass, jint, jstring);
64+
3365
/*
3466
* Class: dev_onvoid_webrtc_media_player_AllocationFault
3567
* Method: disarm

‎webrtc-media/src/main/cpp/include/media/AllocationFault.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,21 @@ namespace ffmpeg
3030
public:
3131
// Fails the next copy of a Java string, once.
3232
static void FailNextStringCopy();
33+
// Fails the next other allocation that is checked, once.
34+
static void FailNextAllocation();
35+
// Fails the next thread start that is checked, once, as one does
36+
// that the system has no memory for.
37+
static void FailNextThreadStart();
3338
// Forgets a fault that has not fired.
3439
static void Disarm();
40+
// Whether a fault is armed that has not fired.
41+
static bool IsArmed();
3542
// Throws std::bad_alloc if a fault is armed, and disarms it.
3643
static void CheckStringCopy();
44+
// The same for the other allocations.
45+
static void CheckAllocation();
46+
// Throws std::system_error if a fault is armed, and disarms it.
47+
static void CheckThreadStart();
3748
};
3849
}
3950

‎webrtc-media/src/main/cpp/include/media/MediaRecorder.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ namespace ffmpeg
7575
// Start().
7676
int AddTrack(void * frames);
7777

78-
// Attaches to every track and starts writing.
78+
// Attaches to every track and starts writing. A writer thread that
79+
// cannot be started throws, and leaves the recorder unstarted.
7980
void Start();
8081

8182
// Detaches from every track, writes out what is queued, finishes
@@ -113,6 +114,10 @@ namespace ffmpeg
113114
static void OnEncodedFrame(void * opaque, const wj_encoded_frame * frame);
114115

115116
void Enqueue(Input * input, const wj_encoded_frame * frame);
117+
118+
// Makes up for a frame that could not be queued: the video after
119+
// it is useless until the next key frame.
120+
void Dropped(Input * input, bool video);
116121
void WantKeyFrameLocked(Input * input);
117122

118123
// Makes a video track wait for its next key frame, and asks for

‎webrtc-media/src/main/cpp/src/JNI_AllocationFault.cpp‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#include "media/AllocationFault.h"
1919
#include "media/JniBoundary.h"
2020

21+
#include <new>
22+
#include <stdexcept>
23+
2124
// The Java class this belongs to exists in the tests only, so that nothing but
2225
// them can make an allocation fail.
2326

@@ -27,6 +30,51 @@ JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_failN
2730
ffmpeg::AllocationFault::FailNextStringCopy();
2831
}
2932

33+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_failNextAllocation
34+
(JNIEnv * env, jclass caller)
35+
{
36+
ffmpeg::AllocationFault::FailNextAllocation();
37+
}
38+
39+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_failNextThreadStart
40+
(JNIEnv * env, jclass caller)
41+
{
42+
ffmpeg::AllocationFault::FailNextThreadStart();
43+
}
44+
45+
JNIEXPORT jboolean JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_isArmed
46+
(JNIEnv * env, jclass caller)
47+
{
48+
return ffmpeg::AllocationFault::IsArmed() ? JNI_TRUE : JNI_FALSE;
49+
}
50+
51+
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_throwInNative
52+
(JNIEnv * env, jclass caller, jint kind, jstring message)
53+
{
54+
ffmpeg::CallFromJava(env, [&] {
55+
// Held while throwing, as an entry point holds its arguments.
56+
ffmpeg::JavaStringChars text(env, message);
57+
58+
if (text.get() == nullptr) {
59+
return;
60+
}
61+
62+
switch (kind) {
63+
case 0:
64+
throw std::bad_alloc();
65+
case 1:
66+
throw std::runtime_error(text.get());
67+
case 2:
68+
// A JNI call that failed, and a C++ exception after it.
69+
env->FindClass("dev/onvoid/webrtc/media/player/NoSuchClass");
70+
71+
throw std::runtime_error(text.get());
72+
default:
73+
throw kind;
74+
}
75+
});
76+
}
77+
3078
JNIEXPORT void JNICALL Java_dev_onvoid_webrtc_media_player_AllocationFault_disarm
3179
(JNIEnv * env, jclass caller)
3280
{

‎webrtc-media/src/main/cpp/src/JNI_FFmpeg.cpp‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "JNI_FFmpeg.h"
18+
#include "media/JniBoundary.h"
1819

1920
extern "C" {
2021
#include <libavformat/avformat.h>
@@ -24,14 +25,18 @@ extern "C" {
2425
JNIEXPORT jstring JNICALL Java_dev_onvoid_webrtc_media_player_FFmpeg_version
2526
(JNIEnv * env, jclass caller)
2627
{
27-
// The version of the libraries that were actually loaded, which is not
28-
// necessarily the one this module was built against: the LGPL lets an
29-
// application replace them.
30-
return env->NewStringUTF(av_version_info());
28+
return ffmpeg::CallFromJava<jstring>(env, nullptr, [&]() -> jstring {
29+
// The version of the libraries that were actually loaded, which is not
30+
// necessarily the one this module was built against: the LGPL lets an
31+
// application replace them.
32+
return env->NewStringUTF(av_version_info());
33+
});
3134
}
3235

3336
JNIEXPORT jstring JNICALL Java_dev_onvoid_webrtc_media_player_FFmpeg_license
3437
(JNIEnv * env, jclass caller)
3538
{
36-
return env->NewStringUTF(avformat_license());
39+
return ffmpeg::CallFromJava<jstring>(env, nullptr, [&]() -> jstring {
40+
return env->NewStringUTF(avformat_license());
41+
});
3742
}

‎webrtc-media/src/main/cpp/src/JNI_MediaPlayer.cpp‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ namespace
3737
{
3838
return reinterpret_cast<ffmpeg::MediaPlayer *>(handle);
3939
}
40+
41+
// Releases a player that never made it to Java. Its observer goes first:
42+
// closing reports the closed state, which would be a call into Java for a
43+
// player nobody listens to yet, and with an exception pending such a call
44+
// is not allowed, and would also clear it.
45+
struct DiscardPlayer
46+
{
47+
void operator()(ffmpeg::MediaPlayer * player) const
48+
{
49+
player->SetObserver(nullptr);
50+
51+
delete player;
52+
}
53+
};
4054
}
4155

4256
JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_player_MediaPlayer_create
@@ -66,21 +80,19 @@ JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_player_MediaPlayer_create
6680
return 0;
6781
}
6882

69-
auto player = std::make_unique<ffmpeg::MediaPlayer>(std::move(reader), api,
70-
reinterpret_cast<void *>(videoSourceHandle),
71-
reinterpret_cast<void *>(audioSourceHandle));
83+
// Also released that way when a C++ exception unwinds through here.
84+
std::unique_ptr<ffmpeg::MediaPlayer, DiscardPlayer> player(
85+
new ffmpeg::MediaPlayer(std::move(reader), api,
86+
reinterpret_cast<void *>(videoSourceHandle),
87+
reinterpret_cast<void *>(audioSourceHandle)));
7288

7389
player->SetObserver(std::make_unique<ffmpeg::JavaPlayerObserver>(env, caller));
7490
player->SetHardwareDecoding(hardwareDecoding == JNI_TRUE);
7591

7692
int result = player->Initialize();
7793

7894
if (result < 0) {
79-
// Released before throwing: closing reports the closed state to the
80-
// observer, which calls into Java, and a call into Java with an
81-
// exception pending is not allowed, and would also clear it. Nothing
82-
// is listening yet anyway, so the observer goes first.
83-
player->SetObserver(nullptr);
95+
// Released before throwing, see DiscardPlayer.
8496
player.reset();
8597

8698
ffmpeg::ThrowIOException(env, "Opening the decoders failed: " + ffmpeg::ErrorText(result));

‎webrtc-media/src/main/cpp/src/media/AllocationFault.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818

1919
#include <atomic>
2020
#include <new>
21+
#include <system_error>
2122

2223
namespace
2324
{
2425
std::atomic<bool> fail_string_copy{ false };
26+
std::atomic<bool> fail_allocation{ false };
27+
std::atomic<bool> fail_thread_start{ false };
2528
}
2629

2730
namespace ffmpeg
@@ -31,9 +34,26 @@ namespace ffmpeg
3134
fail_string_copy.store(true);
3235
}
3336

37+
void AllocationFault::FailNextAllocation()
38+
{
39+
fail_allocation.store(true);
40+
}
41+
42+
void AllocationFault::FailNextThreadStart()
43+
{
44+
fail_thread_start.store(true);
45+
}
46+
3447
void AllocationFault::Disarm()
3548
{
3649
fail_string_copy.store(false);
50+
fail_allocation.store(false);
51+
fail_thread_start.store(false);
52+
}
53+
54+
bool AllocationFault::IsArmed()
55+
{
56+
return fail_string_copy.load() || fail_allocation.load() || fail_thread_start.load();
3757
}
3858

3959
void AllocationFault::CheckStringCopy()
@@ -42,4 +62,19 @@ namespace ffmpeg
4262
throw std::bad_alloc();
4363
}
4464
}
65+
66+
void AllocationFault::CheckAllocation()
67+
{
68+
if (fail_allocation.load() && fail_allocation.exchange(false)) {
69+
throw std::bad_alloc();
70+
}
71+
}
72+
73+
void AllocationFault::CheckThreadStart()
74+
{
75+
if (fail_thread_start.load() && fail_thread_start.exchange(false)) {
76+
throw std::system_error(std::make_error_code(std::errc::resource_unavailable_try_again),
77+
"Starting a thread failed");
78+
}
79+
}
4580
}

‎webrtc-media/src/main/cpp/src/media/JniBoundary.cpp‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,31 @@
1818
#include "media/AllocationFault.h"
1919

2020
#include <atomic>
21+
#include <cstddef>
2122
#include <exception>
2223
#include <new>
2324

2425
namespace
2526
{
2627
std::atomic<int> held_strings{ 0 };
28+
29+
// The text of a standard exception is in no defined encoding, and may be
30+
// in the locale's, while ThrowNew reads modified UTF-8. Printable ASCII is
31+
// both, so everything else is replaced. It goes into a buffer of fixed
32+
// size, because this may run when memory has run out.
33+
void CopyAscii(const char * text, char * buffer, size_t size) noexcept
34+
{
35+
size_t length = 0;
36+
37+
while (text != nullptr && text[length] != '\0' && length + 1 < size) {
38+
const unsigned char c = static_cast<unsigned char>(text[length]);
39+
40+
buffer[length] = (c >= 0x20 && c < 0x7F) ? static_cast<char>(c) : '?';
41+
length++;
42+
}
43+
44+
buffer[length] = '\0';
45+
}
2746
}
2847

2948
namespace ffmpeg
@@ -57,7 +76,11 @@ namespace ffmpeg
5776
ThrowJavaException(env, "java/lang/OutOfMemoryError", "Native memory allocation failed");
5877
}
5978
catch (const std::exception & e) {
60-
ThrowJavaException(env, "java/lang/RuntimeException", e.what());
79+
char message[256];
80+
81+
CopyAscii(e.what(), message, sizeof(message));
82+
83+
ThrowJavaException(env, "java/lang/RuntimeException", message);
6184
}
6285
catch (...) {
6386
ThrowJavaException(env, "java/lang/RuntimeException", "Unknown native exception");

‎webrtc-media/src/main/cpp/src/media/MediaPlayer.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "media/MediaPlayer.h"
18+
#include "media/AllocationFault.h"
1819
#include "media/ErrorText.h"
1920

2021
#include <chrono>
@@ -91,6 +92,8 @@ namespace ffmpeg
9192
{
9293
std::lock_guard<std::mutex> lock(mutex_);
9394

95+
AllocationFault::CheckThreadStart();
96+
9497
running_ = true;
9598
thread_ = std::thread(&MediaPlayer::Run, this);
9699
}

0 commit comments

Comments
 (0)