Video.php
9.61 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
<?php
namespace common\models;
use Yii;
use common\lib\QcloudApi\Client as QcloudApiClient;
use common\lib\QcloudApi\QcloudApi as Qapi;
use common\lib\YohonowApi\Request as YHNowApiReq;
/**
* This is the model class for table "{{%video}}".
*
* @property string $id
* @property string $title
* @property string $pic
* @property string $url
* @property integer $app
* @property integer $task_id
* @property string $live_title
* @property string $live_start_time
* @property string $live_end_time
* @property string $master_id
* @property string $room_id
* @property integer $status
* @property integer $create_time
* @property string $update_time
*/
class Video extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%video}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['task_id', 'live_start_time', 'live_end_time', 'master_id', 'room_id', 'status', 'like_num', 'audience_num', 'replay_num', 'create_time', 'update_time'], 'integer'],
[['title', 'live_title'], 'string', 'max' => 100],
[['pic', 'url'], 'string', 'max' => 255],
[['app'], 'string', 'max' => 50],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Title',
'pic' => 'Pic',
'url' => 'Url',
'app' => 'App',
'task_id' => 'Task ID',
'live_title' => 'Live Title',
'live_start_time' => 'Live Start Time',
'live_end_time' => 'Live End Time',
'master_id' => 'Master ID',
'room_id' => 'Room ID',
'status' => 'Status',
'like_num' => 'Like Num',
'audience_num' => 'Audience Num',
'replay_num' => 'Replay Num',
'create_time' => 'Create Time',
'update_time' => 'Update Time',
];
}
public function behaviors() {
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
]
];
}
/**
* 关联表-主播
* @return type
*/
public function getMaster()
{
return $this->hasOne(Master::className(), ['master_id' => 'master_id']);
}
/**
* 关联表-主播
* @return type
*/
public function getRoom()
{
return $this->hasOne(Room::className(), ['room_id' => 'room_id']);
}
/**
* 关联表-计数
* @return type
*/
public function getRoomNums()
{
return $this->hasOne(RoomNums::className(), ['room_id' => 'room_id']);
}
public function setVideoStart($room_id)
{
$transaction = $this->getDb()->beginTransaction();
try {
/*$qchannel = (new \yii\db\Query())
->select('channel_id')
->from('tbl_room_qchannel')
->where(['room_id' => $room_id])
->one();*/
$room = (new \yii\db\Query())
->select('app,title,master_id')
->from('tbl_room')
->where(['room_id' => $room_id])
->one();
$time = time();
//此处有bug,channel_id必须要转成字符串,不然底层会转换成科学计数法的格式
//$ret = QcloudApiClient::self()->CreateRecord(strval($qchannel['channel_id']), date('Y-m-d H:i:s',time()+120), '');
//file_put_contents("/tmp/live.log",date('Y-m-d H:i:s')."|start ".var_export($ret,true),FILE_APPEND)."\n";
if (1) {//$ret['code'] == 0
$this->setAttributes([
'app' => $room['app'],
'task_id' => 0,//$ret['task_id']
'live_title' => $room['title'],
'live_start_time' => $time,
'live_end_time' => 0,
'master_id' => $room['master_id'],
'room_id' => $room_id,
]);
//插入视频表
if (!$this->insert()) {
throw new \Exception(current($this->getFirstErrors()));
}
//更新房间主表直播状态
$result = Yii::$app->db->createCommand()
->update('{{%room}}', ['living' => 1,'update_time'=>time()], ['room_id' => $room_id])
->execute();
if (!$result) {
throw new \Exception('tbl_room update error');
}
//该房间如果包含资讯业务线,那么要更新资讯那边状态
if(in_array(\common\config\Params::YOHONOW,explode(',',$room['app']))){
$news_ret = YHNowApiReq::getInstance()->setLiveStat($room_id,1);
if(!$news_ret){
throw new \Exception(YHNowApiReq::getInstance()->errors);
}
}
/*------------清除弹幕相关redis--------------*/
$cache_prefix = Yii::$app->params['cache_prefix'];
//##主播app 向弹幕服务器发送开始命令时也清除,这里也清除是为了防止弹幕服务器连不上没有清掉
//清掉在线马甲数
Yii::$app->redisIm->del(sprintf($cache_prefix . "vestnum_%s", $room_id));
//清在线观看数
Yii::$app->redisIm->del(sprintf($cache_prefix . "audience_nums_room_%s", $room_id));
//清点赞数
Yii::$app->redisIm->del(sprintf($cache_prefix . "like_numbers_room_%s", $room_id));
//清直播状态
Yii::$app->redisIm->del(sprintf($cache_prefix . "play_stat_room_%s", $room_id));
//清房间观看虚假累加值
Yii::$app->redisIm->del(sprintf($cache_prefix . "addup_num_%s", $room_id));
/*------------清除弹幕相关redis--------------*/
$transaction->commit();
return true;
} else {
throw new \Exception(QcloudApiClient::self()->error());
}
} catch (\Exception $e) {
$transaction->rollBack();
throw new \Exception($e->getMessage());
return false;
}
}
public function setVideoStop($room_id)
{
$transaction = $this->getDb()->beginTransaction();
try {
/*$qchannel = (new \yii\db\Query())
->select('channel_id')
->from('tbl_room_qchannel')
->where(['room_id' => $room_id])
->one();*/
$video = $this->find()
->where(['room_id' => $room_id, 'live_end_time' => 0])
->orderBy(['live_start_time' => SORT_DESC])
->one();
if ($video) {
//此处有bug,channel_id必须要转成字符串,不然底层会转换成科学计数法的格式
//$ret = QcloudApiClient::self()->StopRecord(strval($qchannel['channel_id']), $video['task_id']);
//file_put_contents("/tmp/live.log",date('Y-m-d H:i:s')."|stop ".var_export($ret,true),FILE_APPEND)."\n";
$room = (new \yii\db\Query())
->select('app,title,master_id')
->from('tbl_room')
->where(['room_id' => $room_id])
->one();
if (1) {//$ret['code'] == 0
$video->setAttributes([
'live_end_time' => time(),
]);
if (!$video->update()) {
throw new \Exception(current($this->getFirstErrors()));
}
$living_status = 2;
//更新房间主表直播状态
$result = Yii::$app->db->createCommand()
->update('{{%room}}', ['living' => $living_status,'update_time'=>time()], ['room_id' => $room_id])
->execute();
if (!$result) {
throw new \Exception('tbl_room update error');
}
//该房间如果包含资讯业务线,那么要更新资讯那边状态
if(in_array(\common\config\Params::YOHONOW,explode(',',$room['app']))){
$news_ret = YHNowApiReq::getInstance()->setLiveStat($room_id,0);
if(!$news_ret){
throw new \Exception(YHNowApiReq::getInstance()->errors);
}
}
$transaction->commit();
return true;
} else {
throw new \Exception(QcloudApiClient::self()->error());
return false;
}
}else{
throw new \Exception("没有找到相应视频信息");
return false;
}
} catch (\Exception $e) {
$transaction->rollBack();
throw new \Exception($e->getMessage());
return false;
}
}
/**
* 获取当前正在直播的视频
* @param $room_id
* @return array|null|\yii\db\ActiveRecord
*/
public function getCurrentLivingVideo($room_id)
{
$video = $this->find()
->where(['room_id' => $room_id, 'live_end_time' => 0])
->orderBy(['live_start_time' => SORT_DESC])
->one();
return $video;
}
}