nonocast / me

记录和分享技术的博客
http://nonocast.cn
MIT License
20 stars 0 forks source link

Swift & C (Part 4: makefile with swift and c) #291

Open nonocast opened 2 years ago

nonocast commented 2 years ago

Version: Swift 5.6, macOS 12.3, Xcode 11

重新整理了swift调用librtmp

目录结构:

rtmpr/Makefile

all: librtmpr/librtmpr.a app

app: main.swift Makefile
    swiftc -target arm64-apple-macos12.3 -import-objc-header bridge.h -I. -lrtmpr -Llibrtmpr -o $@ $<

librtmpr/librtmpr.a: FORCE
    @cd librtmpr; make all

clean:
    rm -f app
    @cd librtmpr; make clean

run: app
    @./app

test: FORCE
    @cd librtmpr; make run

FORCE:

rtmpr/librtmpr/Makefile

VERSION=v2.4

# change default make data-base
CC=clang
CXX=clang++
COMPILE.c = $(CC) $(CFLAGS) $(TARGET_ARCH) -c 

DEF_=-DNO_CRYPTO
CRYPTO_DEF=$(DEF_$(CRYPTO))
DEF=-DRTMPDUMP_VERSION=\"$(VERSION)\" $(CRYPTO_DEF) $(XDEF)
OPT=-O2
CPPFLAGS=-std=c++11 -g
CFLAGS=-g -Wall -Wno-unused -arch arm64 -arch x86_64 $(DEF) $(OPT)
OBJECTS=rtmp.o log.o amf.o hashswf.o parseurl.o rtmpwrapper.o
TEST_OBJECTS=rtmpwrapper_test.o

all: librtmpr.a

run: test
    @./test

test: librtmpr.a $(TEST_OBJECTS)
    clang++ `pkg-config --cflags --libs gtest` -lrtmpr -L. -lgtest_main -o $@ $(TEST_OBJECTS)

librtmpr.a: $(OBJECTS)
    libtool -static -o $@ $^

# make -p show implicit rules
log.o: log.c log.h Makefile
rtmp.o: rtmp.c rtmp.h rtmp_sys.h handshake.h dh.h log.h amf.h Makefile
amf.o: amf.c amf.h bytes.h log.h Makefile
hashswf.o: hashswf.c http.h rtmp.h rtmp_sys.h Makefile
parseurl.o: parseurl.c rtmp.h rtmp_sys.h log.h Makefile
rtmpwrapper.o: rtmpwrapper.c rtmpwrapper.h Makefile
rtmpwrapper_test.o: rtmpwrapper_test.cc Makefile

clean:
    rm -f *.o *.out librtmpr.a test

.PHONY: run

bridge.h

#include "librtmpr/log.h"
#include "librtmpr/rtmp.h"
#include "librtmpr/rtmpwrapper.h"

app.swift

import Foundation

class App {
  var url: String?
  var rtmp = RTMP()

  convenience init(url: String) {
    self.init()
    self.url = url
  }

  init() {}

  func run() {
    RTMP_Init(&rtmp)
    RTMP_SetupURLEx(&rtmp, url!)
    RTMP_EnableWrite(&rtmp)
    RTMP_Connect(&rtmp, nil)
    RTMP_ConnectStream(&rtmp, 0)
    RTMP_CloseEx(&rtmp)
  }

  static func main() {
    RTMP_LogSetLevel(RTMP_LOGALL)
    let version = RTMP_LibVersion()
    print(String(format: "0x%08x", version))

    App(url: "rtmp://live.nonocast.cn/live/1").run()
  }
}

App.main()

source code: rtmpr.zip