-
Notifications
You must be signed in to change notification settings - Fork 9
/
actioncam.go
387 lines (349 loc) · 10.9 KB
/
actioncam.go
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package main
import (
"bufio"
"context"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"github.com/jonas-koeritz/actioncam/libipcamera"
"github.com/jonas-koeritz/actioncam/rtsp"
"github.com/spf13/cobra"
)
func connectAndLogin(ip net.IP, port int, username, password string, verbose bool) *libipcamera.Camera {
camera, err := libipcamera.CreateCamera(ip, port, username, password)
if err != nil {
log.Printf("ERROR instantiating camera: %s\n", err)
os.Exit(1)
}
camera.SetVerbose(verbose)
camera.Connect()
camera.Login()
return camera
}
func main() {
var username string
var password string
var port int16
var verbose bool
var cpuprofile string
var memoryprofile string
var cpuprofileFile *os.File
var camera *libipcamera.Camera
var applicationContext context.Context
var rootCmd = &cobra.Command{
Use: "actioncam [Cameras IP Address]",
Short: "actioncam is a tool to stream the video preview of cheap action cameras without the mobile application",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
defer camera.Disconnect()
relay := libipcamera.CreateRTPRelay(applicationContext, net.ParseIP("127.0.0.1"), 5220)
defer relay.Stop()
camera.StartPreviewStream()
bufio.NewReader(os.Stdin).ReadBytes('\n')
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
signalChannel := make(chan os.Signal)
signal.Notify(signalChannel, os.Interrupt)
var cancel context.CancelFunc
applicationContext, cancel = context.WithCancel(context.Background())
go func(cancel context.CancelFunc) {
select {
case sig := <-signalChannel:
log.Printf("Got signal %s, exiting...\n", sig)
cancel()
os.Exit(0)
}
}(cancel)
if cpuprofile != "" {
cpuprofileFile, err := os.Create(cpuprofile)
if err != nil {
log.Printf("Could not create CPU profiling file: %s\n", err)
return
}
err = pprof.StartCPUProfile(cpuprofileFile)
if err != nil {
log.Printf("Could not start CPU profiling: %s\n", err)
}
}
},
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
camera = connectAndLogin(discoverCamera(verbose), int(port), username, password, verbose)
} else {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
}
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
pprof.StopCPUProfile()
cpuprofileFile.Close()
runtime.GC()
if memoryprofile != "" {
f, err := os.Create(memoryprofile)
if err != nil {
log.Printf("Could not create Memory profiling file: %s\n", err)
return
}
err = pprof.WriteHeapProfile(f)
if err != nil {
log.Printf("Could not start Memory profiling: %s\n", err)
}
}
},
Version: "0.2.2",
}
rootCmd.PersistentFlags().Int16VarP(&port, "port", "P", 6666, "Specify an alternative camera port to connect to")
rootCmd.PersistentFlags().StringVarP(&username, "username", "u", "admin", "Specify the camera username")
rootCmd.PersistentFlags().StringVarP(&password, "password", "p", "12345", "Specify the camera password")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Print verbose output")
rootCmd.PersistentFlags().StringVarP(&cpuprofile, "cpuprofile", "c", "", "Profile CPU usage")
rootCmd.PersistentFlags().StringVarP(&memoryprofile, "memoryprofile", "m", "", "Profile memory usage")
rootCmd.PersistentFlags().MarkHidden("cpuprofile")
rootCmd.PersistentFlags().MarkHidden("memoryprofile")
var ls = &cobra.Command{
Use: "ls [Cameras IP Address]",
Short: "List files stored on the cameras SD-Card",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
files, err := camera.GetFileList()
if err != nil {
log.Printf("ERROR Receiving File List: %s\n", err)
return
}
for _, file := range files {
fmt.Printf("%s\t%d\n", file.Path, file.Size)
}
},
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
camera = connectAndLogin(discoverCamera(verbose), int(port), username, password, verbose)
} else {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
}
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
}
var discover = &cobra.Command{
Use: "discover",
Short: "Try to discover a camera by sending UDP broadcasts",
Args: cobra.MaximumNArgs(0),
Run: func(cmd *cobra.Command, args []string) {
cameraIP, err := libipcamera.AutodiscoverCamera(verbose)
if err != nil {
log.Printf("ERROR Discovering Camera: %s\n", err)
}
log.Printf("Found Camera: %+v\n", cameraIP)
},
}
var still = &cobra.Command{
Use: "still [Cameras IP Address]",
Short: "Take a still image and save to SD-Card",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
camera.TakePicture()
},
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
camera = connectAndLogin(discoverCamera(verbose), int(port), username, password, verbose)
} else {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
}
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
}
var record = &cobra.Command{
Use: "record [Cameras IP Address]",
Short: "Start recording video to SD-Card",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
camera.StartRecording()
},
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
camera = connectAndLogin(discoverCamera(verbose), int(port), username, password, verbose)
} else {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
}
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
}
var stop = &cobra.Command{
Use: "stop [Cameras IP Address]",
Short: "Stop recording video to SD-Card",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
camera.StopRecording()
},
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
camera = connectAndLogin(discoverCamera(verbose), int(port), username, password, verbose)
} else {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
}
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
}
var firmware = &cobra.Command{
Use: "firmware [Cameras IP Address]",
Short: "Retrieve firmware version information from the camera",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
firmware, err := camera.GetFirmwareInfo()
if err != nil {
log.Printf("ERROR retrieving version info: %s\n", err)
return
}
log.Printf("Firmware Version: %s\n", firmware)
},
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
camera = connectAndLogin(discoverCamera(verbose), int(port), username, password, verbose)
} else {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
}
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
}
var rtsp = &cobra.Command{
Use: "rtsp [Cameras IP Address]",
Short: "Start an RTSP-Server serving the cameras preview.",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
rtspServer := rtsp.CreateServer(applicationContext, "127.0.0.1", 8554, camera)
defer rtspServer.Stop()
log.Printf("Created RTSP Server\n")
err := rtspServer.ListenAndServe()
if err != nil {
log.Printf("ERROR starting RTSP Server: %s\n", err)
}
},
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
camera = connectAndLogin(discoverCamera(verbose), int(port), username, password, verbose)
} else {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
}
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
}
var cmd = &cobra.Command{
Use: "cmd [RAW Command] [Cameras IP Address]",
Short: "Send a raw command to the camera",
Args: cobra.RangeArgs(1, 2),
Run: func(cmd *cobra.Command, args []string) {
command, err := hex.DecodeString(args[0])
if err != nil {
log.Printf("ERROR: %s\n", err)
return
}
if len(command) >= 2 {
header := libipcamera.CreateCommandHeader(uint32(binary.BigEndian.Uint16(command[:2])))
payload := command[2:]
packet := libipcamera.CreatePacket(header, payload)
log.Printf("Sending Command: %X\n", packet)
camera.SendPacket(packet)
}
log.Printf("Waiting for Data, press ENTER to quit")
bufio.NewReader(os.Stdin).ReadBytes('\n')
},
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
camera = connectAndLogin(discoverCamera(verbose), int(port), username, password, verbose)
} else {
camera = connectAndLogin(net.ParseIP(args[1]), int(port), username, password, verbose)
}
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
}
var fetch = &cobra.Command{
Use: "fetch [Cameras IP Address]",
Short: "Download files from the cameras SD-Card",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
files, err := camera.GetFileList()
if err != nil {
log.Printf("ERROR Receiving File List: %s\n", err)
return
}
newestFile := files[len(files)-1].Path
url := "http://" + args[0] + newestFile
log.Printf("Downloading latest File: %s\n", url)
downloadFile(filepath.Base(newestFile), url)
},
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
camera = connectAndLogin(discoverCamera(verbose), int(port), username, password, verbose)
} else {
camera = connectAndLogin(net.ParseIP(args[0]), int(port), username, password, verbose)
}
},
PostRun: func(cmd *cobra.Command, args []string) {
camera.Disconnect()
},
}
rootCmd.AddCommand(ls)
rootCmd.AddCommand(cmd)
rootCmd.AddCommand(still)
rootCmd.AddCommand(stop)
rootCmd.AddCommand(fetch)
rootCmd.AddCommand(record)
rootCmd.AddCommand(firmware)
rootCmd.AddCommand(rtsp)
rootCmd.AddCommand(discover)
if err := rootCmd.Execute(); err != nil {
log.Println(err)
os.Exit(1)
}
}
func downloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
func discoverCamera(verbose bool) net.IP {
cameraIP, err := libipcamera.AutodiscoverCamera(verbose)
if err != nil {
log.Printf("ERROR during Autodiscover: %s\n", err)
}
if verbose {
log.Printf("Found Camera: %s\n", cameraIP)
}
return cameraIP
}