1/*
2 *  Copyright 2016 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#import "RTCRtpReceiver+Private.h"
12
13#import "RTCMediaStreamTrack+Private.h"
14#import "RTCRtpParameters+Private.h"
15#import "RTCRtpReceiver+Native.h"
16#import "base/RTCLogging.h"
17#import "helpers/NSString+StdString.h"
18
19#include "api/media_stream_interface.h"
20
21namespace webrtc {
22
23RtpReceiverDelegateAdapter::RtpReceiverDelegateAdapter(RTC_OBJC_TYPE(RTCRtpReceiver) * receiver) {
24  RTC_CHECK(receiver);
25  receiver_ = receiver;
26}
27
28void RtpReceiverDelegateAdapter::OnFirstPacketReceived(
29    cricket::MediaType media_type) {
30  RTCRtpMediaType packet_media_type =
31      [RTC_OBJC_TYPE(RTCRtpReceiver) mediaTypeForNativeMediaType:media_type];
32  RTC_OBJC_TYPE(RTCRtpReceiver) *receiver = receiver_;
33  [receiver.delegate rtpReceiver:receiver didReceiveFirstPacketForMediaType:packet_media_type];
34}
35
36}  // namespace webrtc
37
38@implementation RTC_OBJC_TYPE (RTCRtpReceiver) {
39  RTC_OBJC_TYPE(RTCPeerConnectionFactory) * _factory;
40  rtc::scoped_refptr<webrtc::RtpReceiverInterface> _nativeRtpReceiver;
41  std::unique_ptr<webrtc::RtpReceiverDelegateAdapter> _observer;
42}
43
44@synthesize delegate = _delegate;
45
46- (NSString *)receiverId {
47  return [NSString stringForStdString:_nativeRtpReceiver->id()];
48}
49
50- (RTC_OBJC_TYPE(RTCRtpParameters) *)parameters {
51  return [[RTC_OBJC_TYPE(RTCRtpParameters) alloc]
52      initWithNativeParameters:_nativeRtpReceiver->GetParameters()];
53}
54
55- (nullable RTC_OBJC_TYPE(RTCMediaStreamTrack) *)track {
56  rtc::scoped_refptr<webrtc::MediaStreamTrackInterface> nativeTrack(
57    _nativeRtpReceiver->track());
58  if (nativeTrack) {
59    return [RTC_OBJC_TYPE(RTCMediaStreamTrack) mediaTrackForNativeTrack:nativeTrack
60                                                                factory:_factory];
61  }
62  return nil;
63}
64
65- (NSString *)description {
66  return [NSString
67      stringWithFormat:@"RTC_OBJC_TYPE(RTCRtpReceiver) {\n  receiverId: %@\n}", self.receiverId];
68}
69
70- (void)dealloc {
71  if (_nativeRtpReceiver) {
72    _nativeRtpReceiver->SetObserver(nullptr);
73  }
74}
75
76- (BOOL)isEqual:(id)object {
77  if (self == object) {
78    return YES;
79  }
80  if (object == nil) {
81    return NO;
82  }
83  if (![object isMemberOfClass:[self class]]) {
84    return NO;
85  }
86  RTC_OBJC_TYPE(RTCRtpReceiver) *receiver = (RTC_OBJC_TYPE(RTCRtpReceiver) *)object;
87  return _nativeRtpReceiver == receiver.nativeRtpReceiver;
88}
89
90- (NSUInteger)hash {
91  return (NSUInteger)_nativeRtpReceiver.get();
92}
93
94#pragma mark - Native
95
96- (void)setFrameDecryptor:(rtc::scoped_refptr<webrtc::FrameDecryptorInterface>)frameDecryptor {
97  _nativeRtpReceiver->SetFrameDecryptor(frameDecryptor);
98}
99
100#pragma mark - Private
101
102- (rtc::scoped_refptr<webrtc::RtpReceiverInterface>)nativeRtpReceiver {
103  return _nativeRtpReceiver;
104}
105
106- (instancetype)initWithFactory:(RTC_OBJC_TYPE(RTCPeerConnectionFactory) *)factory
107              nativeRtpReceiver:
108                  (rtc::scoped_refptr<webrtc::RtpReceiverInterface>)nativeRtpReceiver {
109  if (self = [super init]) {
110    _factory = factory;
111    _nativeRtpReceiver = nativeRtpReceiver;
112    RTCLogInfo(@"RTC_OBJC_TYPE(RTCRtpReceiver)(%p): created receiver: %@", self, self.description);
113    _observer.reset(new webrtc::RtpReceiverDelegateAdapter(self));
114    _nativeRtpReceiver->SetObserver(_observer.get());
115  }
116  return self;
117}
118
119+ (RTCRtpMediaType)mediaTypeForNativeMediaType:
120    (cricket::MediaType)nativeMediaType {
121  switch (nativeMediaType) {
122    case cricket::MEDIA_TYPE_AUDIO:
123      return RTCRtpMediaTypeAudio;
124    case cricket::MEDIA_TYPE_VIDEO:
125      return RTCRtpMediaTypeVideo;
126    case cricket::MEDIA_TYPE_DATA:
127      return RTCRtpMediaTypeData;
128  }
129}
130
131+ (cricket::MediaType)nativeMediaTypeForMediaType:(RTCRtpMediaType)mediaType {
132  switch (mediaType) {
133    case RTCRtpMediaTypeAudio:
134      return cricket::MEDIA_TYPE_AUDIO;
135    case RTCRtpMediaTypeVideo:
136      return cricket::MEDIA_TYPE_VIDEO;
137    case RTCRtpMediaTypeData:
138      return cricket::MEDIA_TYPE_DATA;
139  }
140}
141
142+ (NSString *)stringForMediaType:(RTCRtpMediaType)mediaType {
143  switch (mediaType) {
144    case RTCRtpMediaTypeAudio:
145      return @"AUDIO";
146    case RTCRtpMediaTypeVideo:
147      return @"VIDEO";
148    case RTCRtpMediaTypeData:
149      return @"DATA";
150  }
151}
152
153@end
154