This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 145
/
StreamerService.java
138 lines (116 loc) · 4.42 KB
/
StreamerService.java
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
/*
* Copyright (c) 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.watchme;
import android.Manifest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Binder;
import android.os.IBinder;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import com.google.android.apps.watchme.util.Utils;
import java.util.ArrayList;
import java.util.List;
/**
* @author Ibrahim Ulukaya <ulukaya@google.com>
* <p/>
* StreamerService class which streams the video from camera.
*/
public class StreamerService extends Service {
private static int STREAMER_NOTIFICATION_ID = 1001;
private final IBinder binder = new LocalBinder();
// Member variables.
private VideoStreamingConnection connection;
private Camera camera;
@Override
public void onCreate() {
Log.d(MainActivity.APP_NAME, "onCreate");
}
@Override
public void onDestroy() {
Log.d(MainActivity.APP_NAME, "onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
camera = Utils.getCamera(CameraInfo.CAMERA_FACING_FRONT);
Log.d(MainActivity.APP_NAME, "onBind");
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(MainActivity.APP_NAME, "onUnbind");
return false;
}
public void startStreaming(String streamUrl) {
Log.d(MainActivity.APP_NAME, "startStreaming");
showForegroundNotification();
connection = new VideoStreamingConnection();
// TODO Pass an actual preview surface.
connection.open(streamUrl, camera, null);
}
public void stopStreaming() {
Log.d(MainActivity.APP_NAME, "stopStreaming");
if (connection != null) {
connection.close();
}
connection = null;
stopForeground(true);
}
public boolean isStreaming() {
return (connection != null);
}
public void releaseCamera() {
Log.d(MainActivity.APP_NAME, "releaseCamera");
if (!isStreaming() && camera != null) {
Utils.releaseCamera();
Log.d(MainActivity.APP_NAME, "Camera was released.");
camera = null;
} else {
Log.d(MainActivity.APP_NAME, "Camera was not released.");
}
}
private void showForegroundNotification() {
final NotificationManager notifyManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Intent to call our activity from background.
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// The PendingIntent to launch our activity if the user selects this notification.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle(getText(R.string.activeStreamingLabel))
.setContentText(getText(R.string.activeStreamingStatus))
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setWhen(System.currentTimeMillis())
.build();
notifyManager.notify(STREAMER_NOTIFICATION_ID, notification);
}
public class LocalBinder extends Binder {
StreamerService getService() {
return StreamerService.this;
}
}
}