11
11
#import " PLStreamingSessionConstructor.h"
12
12
#import " PLPermissionRequestor.h"
13
13
#import " PLPanelDelegateGenerator.h"
14
+
15
+ #import " NSString+Random.h"
16
+
14
17
#import < PLMediaStreamingKit/PLMediaStreamingKit.h>
18
+
15
19
#import < Masonry/Masonry.h>
16
20
#import < BlocksKit/BlocksKit.h>
17
21
#import < BlocksKit/BlocksKit+UIKit.h>
24
28
#define kWeiboAppSecret @" Your weibo app secret"
25
29
#define kWeiXinAppID @" Your weixin app ID"
26
30
27
- @interface PLMainViewController () <PLMediaStreamingSessionDelegate, PLPanelDelegateGeneratorDelegate, PLStreamingSessionConstructorDelegate>
28
-
31
+ @interface PLMainViewController ()
32
+ <
33
+ PLMediaStreamingSessionDelegate,
34
+ PLPanelDelegateGeneratorDelegate,
35
+ UIAlertViewDelegate
36
+ >
29
37
@end
30
38
31
39
@implementation PLMainViewController
@@ -34,13 +42,21 @@ @implementation PLMainViewController
34
42
PLModelPanelGenerator *_modelPanelGenerator;
35
43
PLPanelDelegateGenerator *_panelDelegateGenerator;
36
44
PLStreamingSessionConstructor *_sessionConstructor;
45
+ PLAudioCaptureConfiguration *_audioCaptureConfiguration;
37
46
UIButton *_startButton;
47
+ UIButton *_liveButton;
38
48
UISlider *_zoomSlider;
49
+ NSURL *_streamCloudURL;
39
50
NSURL *_streamURL;
40
51
UIView *_inputURLView;
41
52
UITextView *_inputURLTextView;
42
53
}
43
54
55
+ - (void )dealloc
56
+ {
57
+
58
+ }
59
+
44
60
- (void )viewDidLoad
45
61
{
46
62
[super viewDidLoad ];
@@ -49,6 +65,31 @@ - (void)viewDidLoad
49
65
50
66
[WXApi registerApp: kWeiXinAppID withDescription: @" PLMediaStreamingKitDemo" ];
51
67
68
+ UIAlertView *alertView = [[UIAlertView alloc ] initWithTitle: @" 提示" message: @" 需要开启回声消除么?" delegate: self cancelButtonTitle: @" 不要" otherButtonTitles: @" 开开开" , nil ];
69
+ [alertView show ];
70
+ }
71
+
72
+ - (void )alertView : (UIAlertView *)alertView clickedButtonAtIndex : (NSInteger )buttonIndex {
73
+ UITextField *textFiled = [alertView textFieldAtIndex: 0 ];
74
+ if (textFiled) {
75
+ if (buttonIndex == 0 ) {
76
+ return ;
77
+ }
78
+
79
+ NSString *deviceID = textFiled.text ;
80
+ [PLStreamingEnv setDeviceID: deviceID];
81
+ return ;
82
+ }
83
+
84
+ if (!_audioCaptureConfiguration) {
85
+ _audioCaptureConfiguration = [PLAudioCaptureConfiguration defaultConfiguration ];
86
+ }
87
+ _audioCaptureConfiguration.acousticEchoCancellationEnable = (buttonIndex == 1 );
88
+
89
+ // 获取推流地址,该地址要拿去鉴权获取 token,最终使用的推流 URL 为 _streamURL
90
+ [self _getStreamCloudURL ];
91
+ [self _generateStreamURLFromServerWithURL: _streamCloudURL];
92
+
52
93
[self _prepareForCameraSetting ];
53
94
[self _prepareButtons ];
54
95
@@ -66,12 +107,19 @@ - (void)viewDidDisappear:(BOOL)animated
66
107
[_streamingSession destroy ];
67
108
}
68
109
110
+ - (void )_getStreamCloudURL {
111
+ #warning 在这里填写获取推流地址的业务服务器 url
112
+ NSString *streamServer = @" your app server url" ;
113
+ NSString *streamID = [NSString randomizedString ];
114
+
115
+ NSString *streamURLString = [streamServer stringByAppendingPathComponent: streamID];
116
+
117
+ _streamCloudURL = [NSURL URLWithString: streamURLString];
118
+ }
119
+
69
120
- (void )_prepareForCameraSetting
70
121
{
71
- #warning 在这里填写获取推流地址的业务服务器 url
72
- NSURL *streamCloudURL = [NSURL URLWithString: @" your app server url" ];
73
- _sessionConstructor = [[PLStreamingSessionConstructor alloc ] initWithStreamCloudURL: streamCloudURL];
74
- _sessionConstructor.delegate = self;
122
+ _sessionConstructor = [[PLStreamingSessionConstructor alloc ] initWithAudioCaptureConfiguration: _audioCaptureConfiguration];
75
123
_streamingSession = [_sessionConstructor streamingSession ];
76
124
77
125
_streamingSession.delegate = self;
@@ -89,6 +137,41 @@ - (void)_prepareForCameraSetting
89
137
[permission checkAndRequestPermission ];
90
138
}
91
139
140
+ - (void )_generateStreamURLFromServerWithURL : (NSURL *)url
141
+ {
142
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc ] initWithURL: url];
143
+ request.HTTPMethod = @" GET" ;
144
+ request.timeoutInterval = 10 ;
145
+
146
+ NSURLSessionDataTask *task = [[NSURLSession sharedSession ] dataTaskWithRequest: request completionHandler: ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
147
+ if (error != nil || response == nil || data == nil ) {
148
+ NSLog (@" get play json faild, %@ , %@ , %@ " , error, response, data);
149
+ dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(0.4 * NSEC_PER_SEC)), dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
150
+ [self _generateStreamURLFromServerWithURL: url];
151
+ });
152
+ return ;
153
+ }
154
+
155
+ NSURL *streamURL = [NSURL URLWithString: [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding]];
156
+
157
+ dispatch_async (dispatch_get_main_queue (), ^{
158
+ NSString *streamURLString = streamURL.absoluteString ;
159
+
160
+ // 将推流地址复制到剪切板
161
+ UIPasteboard *pasteboard = [UIPasteboard generalPasteboard ];
162
+ pasteboard.string = streamURLString;
163
+
164
+ // 弹出提示框,显示推流地址,当点击 ok 确认键后,推流被复制到了剪切板,方便将推流地址粘贴用于其它地方
165
+ UIAlertView *alert = [[UIAlertView alloc ] initWithTitle: @" streamURL" message: streamURLString delegate: nil cancelButtonTitle: @" ok" otherButtonTitles: nil ];
166
+ [alert show ];
167
+ });
168
+
169
+ // 更新推流的 URL
170
+ _streamURL = streamURL;
171
+ }];
172
+ [task resume ];
173
+ }
174
+
92
175
- (void )_prepareButtons
93
176
{
94
177
_startButton = ({
@@ -250,7 +333,10 @@ - (void)_pressedStartButton:(UIButton *)button
250
333
if (PLStreamStartStateSuccess == feedback) {
251
334
[button setTitle: @" stop" forState: UIControlStateNormal];
252
335
} else {
253
- [[[UIAlertView alloc ] initWithTitle: @" 错误" message: @" 推流失败了" delegate: nil cancelButtonTitle: @" 知道啦" otherButtonTitles: nil ] show ];
336
+ [[[UIAlertView alloc ] initWithTitle: @" 错误" message: @" 推流失败了,将重新请求有效的URL" delegate: nil cancelButtonTitle: @" 知道啦" otherButtonTitles: nil ] show ];
337
+
338
+ // 重新获取有效的URL,即更换 token,播放端的地址不会变
339
+ [self _generateStreamURLFromServerWithURL: _streamCloudURL];
254
340
}
255
341
});
256
342
}];
@@ -281,9 +367,8 @@ - (void)_pressedQRButton:(UIButton *)button
281
367
if (!_streamURL) {
282
368
[[[UIAlertView alloc ] initWithTitle: @" 错误" message: @" 还没有获取到 streamJson 没有可供播放的二维码哦" delegate: nil cancelButtonTitle: @" 知道啦" otherButtonTitles: nil ] show ];
283
369
} else {
284
- #warning 在这里填写相关的 host,hub 即可使用播放器直接扫码播放
285
- NSString *host = @" your play host" ;
286
- NSString *hub = @" your hub" ;
370
+ NSString *host = @" rtmp://pili-live-rtmp.pili2test.qbox.net" ;
371
+ NSString *hub = @" pili2test" ;
287
372
NSString *streamID = [[[[_streamURL.absoluteString componentsSeparatedByString: @" /" ] objectAtIndex: 4 ] componentsSeparatedByString: @" ?" ] objectAtIndex: 0 ];
288
373
NSString *url = [NSString stringWithFormat: @" %@ /%@ /%@ " ,host, hub, streamID];
289
374
UIImage *image = [self createQRForString: url];
@@ -398,11 +483,9 @@ - (void)panelDelegateGenerator:(PLPanelDelegateGenerator *)panelDelegateGenerato
398
483
- (void )panelDelegateGenerator : (PLPanelDelegateGenerator *)panelDelegateGenerator streamStateDidChange : (PLStreamState)state {
399
484
if (PLStreamStateDisconnected == state) {
400
485
[_startButton setTitle: @" start" forState: UIControlStateNormal];
486
+ } else if (PLStreamStateConnected == state) {
487
+ [_startButton setTitle: @" stop" forState: UIControlStateNormal];
401
488
}
402
489
}
403
490
404
- - (void )PLStreamingSessionConstructor : (PLStreamingSessionConstructor *)constructor didGetStreamURL : (NSURL *)streamURL {
405
- _streamURL = streamURL;
406
- }
407
-
408
491
@end
0 commit comments