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 #include "pc/rtp_receiver.h"
12 
13 #include <stddef.h>
14 
15 #include <utility>
16 #include <vector>
17 
18 #include "api/media_stream_proxy.h"
19 #include "pc/media_stream.h"
20 #include "rtc_base/location.h"
21 
22 namespace webrtc {
23 
24 // This function is only expected to be called on the signalling thread.
GenerateUniqueId()25 int RtpReceiverInternal::GenerateUniqueId() {
26   static int g_unique_id = 0;
27 
28   return ++g_unique_id;
29 }
30 
31 std::vector<rtc::scoped_refptr<MediaStreamInterface>>
CreateStreamsFromIds(std::vector<std::string> stream_ids)32 RtpReceiverInternal::CreateStreamsFromIds(std::vector<std::string> stream_ids) {
33   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams(
34       stream_ids.size());
35   for (size_t i = 0; i < stream_ids.size(); ++i) {
36     streams[i] = MediaStreamProxy::Create(
37         rtc::Thread::Current(), MediaStream::Create(std::move(stream_ids[i])));
38   }
39   return streams;
40 }
41 
42 // Attempt to attach the frame decryptor to the current media channel on the
43 // correct worker thread only if both the media channel exists and a ssrc has
44 // been allocated to the stream.
MaybeAttachFrameDecryptorToMediaChannel(const absl::optional<uint32_t> & ssrc,rtc::Thread * worker_thread,rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor,cricket::MediaChannel * media_channel,bool stopped)45 void RtpReceiverInternal::MaybeAttachFrameDecryptorToMediaChannel(
46     const absl::optional<uint32_t>& ssrc,
47     rtc::Thread* worker_thread,
48     rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor,
49     cricket::MediaChannel* media_channel,
50     bool stopped) {
51   if (media_channel && frame_decryptor && ssrc.has_value() && !stopped) {
52     worker_thread->Invoke<void>(RTC_FROM_HERE, [&] {
53       media_channel->SetFrameDecryptor(*ssrc, frame_decryptor);
54     });
55   }
56 }
57 
58 }  // namespace webrtc
59