20
20
#include < unordered_map>
21
21
22
22
#include " api/frame_transformer_interface.h"
23
+ #include " api/task_queue/pending_task_safety_flag.h"
24
+ #include " api/task_queue/task_queue_base.h"
23
25
#include " rtc_base/buffer.h"
24
26
#include " rtc_base/synchronization/mutex.h"
25
27
#include " rtc_base/system/rtc_export.h"
@@ -56,7 +58,7 @@ class KeyProvider : public rtc::RefCountInterface {
56
58
57
59
virtual bool SetSharedKey (int key_index, std::vector<uint8_t > key) = 0;
58
60
59
- virtual const std::shared_ptr <ParticipantKeyHandler> GetSharedKey (const std::string participant_id) = 0;
61
+ virtual const rtc::scoped_refptr <ParticipantKeyHandler> GetSharedKey (const std::string participant_id) = 0;
60
62
61
63
virtual const std::vector<uint8_t > RatchetSharedKey (int key_index) = 0;
62
64
@@ -66,7 +68,7 @@ class KeyProvider : public rtc::RefCountInterface {
66
68
int key_index,
67
69
std::vector<uint8_t > key) = 0;
68
70
69
- virtual const std::shared_ptr <ParticipantKeyHandler> GetKey (
71
+ virtual const rtc::scoped_refptr <ParticipantKeyHandler> GetKey (
70
72
const std::string participant_id) const = 0;
71
73
72
74
virtual const std::vector<uint8_t > RatchetKey (
@@ -84,9 +86,9 @@ class KeyProvider : public rtc::RefCountInterface {
84
86
virtual ~KeyProvider () {}
85
87
};
86
88
87
- class ParticipantKeyHandler {
89
+ class ParticipantKeyHandler : public rtc ::RefCountInterface {
88
90
public:
89
- struct KeySet {
91
+ struct KeySet : public rtc ::RefCountInterface {
90
92
std::vector<uint8_t > material;
91
93
std::vector<uint8_t > encryption_key;
92
94
KeySet (std::vector<uint8_t > material, std::vector<uint8_t > encryptionKey)
@@ -99,8 +101,8 @@ class ParticipantKeyHandler {
99
101
100
102
virtual ~ParticipantKeyHandler () = default ;
101
103
102
- std::shared_ptr <ParticipantKeyHandler> Clone () {
103
- auto clone = std::make_shared <ParticipantKeyHandler>(key_provider_);
104
+ rtc::scoped_refptr <ParticipantKeyHandler> Clone () {
105
+ auto clone = rtc::make_ref_counted <ParticipantKeyHandler>(key_provider_);
104
106
clone->crypto_key_ring_ = crypto_key_ring_;
105
107
clone->current_key_index_ = current_key_index_;
106
108
clone->has_valid_key_ = has_valid_key_;
@@ -124,7 +126,7 @@ class ParticipantKeyHandler {
124
126
return new_material;
125
127
}
126
128
127
- virtual std::shared_ptr <KeySet> GetKeySet (int key_index) {
129
+ virtual rtc::scoped_refptr <KeySet> GetKeySet (int key_index) {
128
130
webrtc::MutexLock lock (&mutex_);
129
131
return crypto_key_ring_[key_index != -1 ? key_index : current_key_index_];
130
132
}
@@ -144,13 +146,13 @@ class ParticipantKeyHandler {
144
146
return new_material;
145
147
}
146
148
147
- std::shared_ptr <KeySet> DeriveKeys (std::vector<uint8_t > password,
149
+ rtc::scoped_refptr <KeySet> DeriveKeys (std::vector<uint8_t > password,
148
150
std::vector<uint8_t > ratchet_salt,
149
151
unsigned int optional_length_bits) {
150
152
std::vector<uint8_t > derived_key;
151
153
if (DerivePBKDF2KeyFromRawKey (password, ratchet_salt, optional_length_bits,
152
154
&derived_key) == 0 ) {
153
- return std::make_shared <KeySet>(password, derived_key);
155
+ return rtc::make_ref_counted <KeySet>(password, derived_key);
154
156
}
155
157
return nullptr ;
156
158
}
@@ -193,7 +195,7 @@ class ParticipantKeyHandler {
193
195
mutable webrtc::Mutex mutex_;
194
196
int current_key_index_ = 0 ;
195
197
KeyProvider* key_provider_;
196
- std::vector<std::shared_ptr <KeySet>> crypto_key_ring_;
198
+ std::vector<rtc::scoped_refptr <KeySet>> crypto_key_ring_;
197
199
};
198
200
199
201
class DefaultKeyProviderImpl : public KeyProvider {
@@ -206,7 +208,7 @@ class DefaultKeyProviderImpl : public KeyProvider {
206
208
webrtc::MutexLock lock (&mutex_);
207
209
if (options_.shared_key ) {
208
210
if (keys_.find (" shared" ) == keys_.end ()) {
209
- keys_[" shared" ] = std::make_shared <ParticipantKeyHandler>(this );
211
+ keys_[" shared" ] = rtc::make_ref_counted <ParticipantKeyHandler>(this );
210
212
}
211
213
212
214
auto key_handler = keys_[" shared" ];
@@ -252,7 +254,7 @@ class DefaultKeyProviderImpl : public KeyProvider {
252
254
return std::vector<uint8_t >();
253
255
}
254
256
255
- const std::shared_ptr <ParticipantKeyHandler> GetSharedKey (const std::string participant_id) override {
257
+ const rtc::scoped_refptr <ParticipantKeyHandler> GetSharedKey (const std::string participant_id) override {
256
258
webrtc::MutexLock lock (&mutex_);
257
259
if (options_.shared_key && keys_.find (" shared" ) != keys_.end ()) {
258
260
auto shared_key_handler = keys_[" shared" ];
@@ -274,15 +276,15 @@ class DefaultKeyProviderImpl : public KeyProvider {
274
276
webrtc::MutexLock lock (&mutex_);
275
277
276
278
if (keys_.find (participant_id) == keys_.end ()) {
277
- keys_[participant_id] = std::make_shared <ParticipantKeyHandler>(this );
279
+ keys_[participant_id] = rtc::make_ref_counted <ParticipantKeyHandler>(this );
278
280
}
279
281
280
282
auto key_handler = keys_[participant_id];
281
283
key_handler->SetKey (key, index);
282
284
return true ;
283
285
}
284
286
285
- const std::shared_ptr <ParticipantKeyHandler> GetKey (
287
+ const rtc::scoped_refptr <ParticipantKeyHandler> GetKey (
286
288
const std::string participant_id) const override {
287
289
webrtc::MutexLock lock (&mutex_);
288
290
@@ -324,7 +326,7 @@ class DefaultKeyProviderImpl : public KeyProvider {
324
326
private:
325
327
mutable webrtc::Mutex mutex_;
326
328
KeyProviderOptions options_;
327
- std::unordered_map<std::string, std::shared_ptr <ParticipantKeyHandler>> keys_;
329
+ std::unordered_map<std::string, rtc::scoped_refptr <ParticipantKeyHandler>> keys_;
328
330
};
329
331
330
332
enum FrameCryptionState {
@@ -337,7 +339,7 @@ enum FrameCryptionState {
337
339
kInternalError ,
338
340
};
339
341
340
- class FrameCryptorTransformerObserver {
342
+ class FrameCryptorTransformerObserver : public rtc ::RefCountInterface {
341
343
public:
342
344
virtual void OnFrameCryptionStateChanged (const std::string participant_id,
343
345
FrameCryptionState error) = 0;
@@ -359,17 +361,23 @@ class RTC_EXPORT FrameCryptorTransformer
359
361
kAesCbc ,
360
362
};
361
363
362
- explicit FrameCryptorTransformer (const std::string participant_id,
364
+ explicit FrameCryptorTransformer (rtc::Thread* signaling_thread,
365
+ const std::string participant_id,
363
366
MediaType type,
364
367
Algorithm algorithm,
365
368
rtc::scoped_refptr<KeyProvider> key_provider);
366
369
367
- virtual void SetFrameCryptorTransformerObserver (
368
- FrameCryptorTransformerObserver* observer) {
370
+ virtual void RegisterFrameCryptorTransformerObserver (
371
+ rtc::scoped_refptr< FrameCryptorTransformerObserver> observer) {
369
372
webrtc::MutexLock lock (&mutex_);
370
373
observer_ = observer;
371
374
}
372
375
376
+ virtual void UnRegisterFrameCryptorTransformerObserver () {
377
+ webrtc::MutexLock lock (&mutex_);
378
+ observer_ = nullptr ;
379
+ }
380
+
373
381
virtual void SetKeyIndex (int index) {
374
382
webrtc::MutexLock lock (&mutex_);
375
383
key_index_ = index;
@@ -417,10 +425,12 @@ class RTC_EXPORT FrameCryptorTransformer
417
425
private:
418
426
void encryptFrame (std::unique_ptr<webrtc::TransformableFrameInterface> frame);
419
427
void decryptFrame (std::unique_ptr<webrtc::TransformableFrameInterface> frame);
428
+ void onFrameCryptionStateChanged (FrameCryptionState error);
420
429
rtc::Buffer makeIv (uint32_t ssrc, uint32_t timestamp);
421
430
uint8_t getIvSize ();
422
431
423
432
private:
433
+ TaskQueueBase* const signaling_thread_;
424
434
std::string participant_id_;
425
435
mutable webrtc::Mutex mutex_;
426
436
mutable webrtc::Mutex sink_mutex_;
@@ -433,10 +443,9 @@ class RTC_EXPORT FrameCryptorTransformer
433
443
int key_index_ = 0 ;
434
444
std::map<uint32_t , uint32_t > send_counts_;
435
445
rtc::scoped_refptr<KeyProvider> key_provider_;
436
- FrameCryptorTransformerObserver* observer_ = nullptr ;
437
- std::unique_ptr<rtc::Thread> thread_;
446
+ rtc::scoped_refptr<FrameCryptorTransformerObserver> observer_;
438
447
FrameCryptionState last_enc_error_ = FrameCryptionState::kNew ;
439
- FrameCryptionState last_dec_error_ = FrameCryptionState::kNew ;
448
+ FrameCryptionState last_dec_error_ = FrameCryptionState::kNew ;
440
449
};
441
450
442
451
} // namespace webrtc
0 commit comments