Shaka Player Embedded
net_objc.mm
Go to the documentation of this file.
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "shaka/net_objc.h"
16 
18 #include "src/util/objc_utils.h"
19 
21 
22 @implementation ShakaPlayerRequest
23 
24 @synthesize uris;
25 @synthesize method;
26 @synthesize headers;
27 @synthesize body;
28 
29 - (instancetype)initWithRequest:(const shaka::Request &)request {
30  if ((self = [super init])) {
31  uris = ObjcConverter<std::vector<std::string>>::ToObjc(request.uris);
34  if (request.body_size() != 0)
35  body = [[NSData alloc] initWithBytes:request.body() length:request.body_size()];
36  else
37  body = nil;
38  }
39  return self;
40 }
41 
42 - (void)finalize:(shaka::Request *)request {
43  request->uris = ObjcConverter<std::vector<std::string>>::FromObjc(uris);
46  if (body)
47  request->SetBodyCopy(reinterpret_cast<const uint8_t *>([body bytes]), [body length]);
48  else
49  request->SetBodyCopy(nullptr, 0);
50 }
51 
52 @end
53 
54 
55 @implementation ShakaPlayerResponse
56 
57 @synthesize uri;
58 @synthesize originalUri;
59 @synthesize headers;
60 @synthesize fromCache;
61 @synthesize timeMs;
62 @synthesize data;
63 
64 - (instancetype)initWithResponse:(const shaka::Response &)response {
65  if ((self = [super init])) {
67  originalUri = ObjcConverter<std::string>::ToObjc(response.originalUri);
69  fromCache = response.fromCache.value_or(false);
70  if (response.timeMs.has_value())
71  timeMs = [[NSNumber alloc] initWithDouble:*response.timeMs];
72  else
73  timeMs = nil;
74  if (response.data_size() != 0)
75  data = [[NSData alloc] initWithBytes:response.data() length:response.data_size()];
76  else
77  data = nil;
78  }
79  return self;
80 }
81 
82 - (void)finalize:(shaka::Response *)response {
84  response->originalUri = ObjcConverter<std::string>::FromObjc(originalUri);
85  response->headers =
87  response->fromCache = fromCache;
88  if (timeMs)
89  response->timeMs = [timeMs doubleValue];
90  else
91  response->timeMs = {};
92  if (data)
93  response->SetDataCopy(reinterpret_cast<const uint8_t *>([data bytes]), [data length]);
94  else
95  response->SetDataCopy(nullptr, 0);
96 }
97 
98 @end
NSString * uri
Definition: net_objc.h:81
NSString * originalUri
Definition: net_objc.h:87
NSString * method
Definition: net_objc.h:54
NSData * body
Definition: net_objc.h:60
NSMutableDictionary< NSString *, NSString * > * headers
Definition: net_objc.h:57
NSMutableArray< NSString * > * uris
Definition: net_objc.h:51
NSNumber * timeMs
Definition: net_objc.h:106