nativeVideo.js
7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
var client; //客户端
var pubResultObj; // 存储底层给我的推流结果
var playResultObj;//存储底层给我的播流结果
var playVideoId; //播放的流的videoId
var pushVideoId; //推流的videoId
var playCallbackDate; //视频更新的callBack
var nativeVideo={};
window.nativeVideo=nativeVideo;
nativeVideo.nativeInit=function (_client){
client=_client;
};
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
function mobileCallback(_param) {
console.log(_param);
if(isiOS){
console.log(isiOS);
_js2native(_param);
}else if (isAndroid){
console.log(isAndroid);
window.xdyAndroid._js2native(_param);
}
}
function teaTimeStamp() {
var _teatimestamp= Date.parse(new Date());
return _teatimestamp;
}
$(function () {
//学生视频按钮点击
$(".stuVideo-video").click(function (e) {
console.log("stuVideo-video点击");
console.log( $(".studentVideo").offset().left);
$("#stuVideo-video").html(" ");
$("#stuVideo-video").removeClass("stuVideo-video");
$("#stuVideo-video").addClass("stuVideo-videoBg");
$("#stuVideo-voice").html("");
$("#stuVideo-voice").addClass("stuVideo-voice");
$("#stuVideo-voice").removeClass("stuVideo-voiceBg");
$(".studentVideo").css("background", "#CCCCCC");
var pcVideoTop=$(".studentVideo").offset().top;
var pcVideoLeft=$(".studentVideo").offset().left;
var pcVideoW=$(".studentVideo").width();
var pcVideoH=pcVideoW*3/4;
//获取位置
var scrollW=document.body.scrollWidth||document.documentElement.scrollWidth;
var videoStuXFloat=pcVideoLeft/scrollW*100;
var videoStuX=Math.round(videoStuXFloat*100)/100;
var scrollH=document.body.scrollHeight||document.documentElement.scrollHeight;
var videoStuYFloat=pcVideoTop/scrollH*100;
var videoStuY=Math.round(videoStuYFloat*100)/100;
//宽高
var videoStuWFloat=pcVideoW/scrollW*100;
var videoStuW=Math.round(videoStuWFloat*100)/100;
var videoStuHFloat=pcVideoH/scrollH*100;
var videoStuH=Math.round(videoStuHFloat*100)/100;
console.log(videoStuX,videoStuY,videoStuW,videoStuH);
//创建显示nativeVideo学生窗口,进行推流
var creatTeaNativeVideoObj={
"type":0,
"videoId":teaTimeStamp(),
"data":{
"wintype":'rtmp',
"x": videoStuX,
"y": videoStuY,
"width": videoStuW,
"height": videoStuH
}
};
console.log(creatTeaNativeVideoObj);
var creatTeaNativeVideoJson=JSON.stringify(creatTeaNativeVideoObj);
console.log(creatTeaNativeVideoJson);
mobileCallback(creatTeaNativeVideoJson);
//获取学生推流地址
achieveStuPushStream();
});
//学生挂断
$(".stuVideo-videoBg").click(function (e) {
})
//学生音频按钮点击
$("#stuVideo-voice").click(function (e) {
console.log("stuVideo-voice点击");
$("#stuVideo-video").html("");
$("#stuVideo-video").removeClass("stuVideo-videoBg");
$("#stuVideo-video").addClass("stuVideo-video");
$("#stuVideo-voice").html("");
$("#stuVideo-voice").removeClass("stuVideo-voice");
$("#stuVideo-voice").addClass("stuVideo-voiceBg");
});
//根据传进来的消息,创建老师NativeVideo窗口,进行播流
client.on(MessageTypes.VIDEO_UPDATE,videoUpdate);
});
function videoUpdate(callbackDate){
playCallbackDate=callbackDate;
if ((callbackDate.status==1)&&(callbackDate.channelId!=='')){
var scrollW=document.body.scrollWidth||document.documentElement.scrollWidth;
var scrollH=document.body.scrollHeight||document.documentElement.scrollHeight;
var videoTeaXFloat=(scrollW-246)/scrollW*100;
var videoTeaX=Math.round(videoTeaXFloat*100)/100;
var videoTeaYFloat=241/scrollH*100;
var videoTeaY=Math.round(videoTeaYFloat*100)/100;
var creatStuNativeVideoObj={
"type":0,
"videoId":teaTimeStamp(),
"data":{
"x": videoTeaX,
"y": videoTeaY,
"width": 30,
"height": 30
}
};
var creatStuNativeVideoJson=JSON.stringify(creatStuNativeVideoObj);
mobileCallback(creatStuNativeVideoJson);
//获取播流地址
achievePlayStream();
}
}
//从底层获取播流地址,然后传给nativa进行播放
function achievePlayStream(){
var paramInfo={
"type": "rtmp"
};
var playResult=client.getVideoPlayPath(paramInfo);
console.log("执行getVideoPlayPath,返回的结果",playResult);
playResultObj=playResult;
if (playResult.code==0){
var _playMsg={
"type": 2,
"videoId": teaTimeStamp(),
"data":{
"video": true, // true/false
"audio": true, // true/false
"url":playResult.publishUrl
}
};
//播流的videoId
playVideoId=_playMsg.videoId;
//传给native
mobileCallback(_playMsg);
}
}
//从底层获取推流地址
function achievePushStream(){
var paramInfo={
"type": "live"
};
var pubResult=client.getVideoPublishPath(paramInfo);
console.log("执行getVideoPublishPath,返回的结果",pubResult)
pubResultObj=pubResult;
//code为0,代表成功
if (pubResult.code==0){
var _publishMsg={
"type": 10,
"videoId": teaTimeStamp(),
"data":{
"video": true, // true/false
"audio": true, // true/false
"url":pubResult.publishUrl
}
};
//推流时的VideoId
var pushVideoId=_publishMsg.videoId;
//传给native
mobileCallback(_publishMsg);
}
};
//native传给我
nativeVideo._native2js=function (_fromNative){
var fromNative=JSON.parse(_fromNative);
var type=fromNative.type;
switch (type){
case 0: SucessError(_fromNative);break;
default:break;
}
};
function SucessError(_fromNative) {
var successErrorData=_fromNative.data;
//0成功 1失败
if (successErrorData.result==0){
//将之前底层取的推流结果传递给底层
var _successErrorResult= client.publishVideo(pubResultObj);
if(_successErrorResult.code==0){
console.log("底层广播推流成功");
} else if(_successErrorResult.code==1){
console.log("底层广播推流失败,关闭推流");
//应用层关闭流
var _closeStream={
"type": 5,
"videoId":pushVideoId
}
var paramInfo={
"nodeId": 0,//用户的nodeId,默认0,就是自己的
};
client.stopPublishVideo(paramInfo);
}
}
else if (successErrorData.result==1){
console.log("native失败了,错误信息是",successErrorData.errmsg);
var paramInfo={
"nodeId": 0,//用户的nodeId,默认0,就是自己的
};
client.stopPublishVideo(paramInfo);
//应用层出弹框提示
$(".pop").trigger('click');
}
}